linma / rpms / iproute

Forked from rpms/iproute 4 years ago
Clone

Blame SOURCES/0124-tc-introduce-simple-action.patch

049c96
From 1ce74bc27f9b766ed42fc23b3a3754bbef6a1a3c Mon Sep 17 00:00:00 2001
049c96
From: Phil Sutter <psutter@redhat.com>
049c96
Date: Fri, 11 Mar 2016 15:37:57 +0100
049c96
Subject: [PATCH] tc: introduce simple action
049c96
049c96
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1315930
049c96
Upstream Status: iproute2.git commit 087f46ee4ebd1
049c96
049c96
commit 087f46ee4ebd178a2a8562989fd9a4e02c93f406
049c96
Author: Jamal Hadi Salim <jhs@mojatatu.com>
049c96
Date:   Sun Sep 29 07:33:42 2013 -0400
049c96
049c96
    tc: introduce simple action
049c96
049c96
    Simple action is already in the kernel for years now as an
049c96
    example. This complements it with user space control.
049c96
049c96
    Signed-off-by: Jamal Hadi Salim <jhs@mojatatu.com>
049c96
---
049c96
 tc/Makefile   |   1 +
049c96
 tc/m_simple.c | 202 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
049c96
 2 files changed, 203 insertions(+)
049c96
 create mode 100644 tc/m_simple.c
049c96
049c96
diff --git a/tc/Makefile b/tc/Makefile
049c96
index 79116f3..59ee1dd 100644
049c96
--- a/tc/Makefile
049c96
+++ b/tc/Makefile
049c96
@@ -44,6 +44,7 @@ TCMODULES += m_nat.o
049c96
 TCMODULES += m_pedit.o
049c96
 TCMODULES += m_skbedit.o
049c96
 TCMODULES += m_csum.o
049c96
+TCMODULES += m_simple.o
049c96
 TCMODULES += p_ip.o
049c96
 TCMODULES += p_icmp.o
049c96
 TCMODULES += p_tcp.o
049c96
diff --git a/tc/m_simple.c b/tc/m_simple.c
049c96
new file mode 100644
049c96
index 0000000..0224440
049c96
--- /dev/null
049c96
+++ b/tc/m_simple.c
049c96
@@ -0,0 +1,202 @@
049c96
+/*
049c96
+ * m_simple.c	simple action
049c96
+ *
049c96
+ *		This program is free software; you can distribute it and/or
049c96
+ *		modify it under the terms of the GNU General Public License
049c96
+ *		as published by the Free Software Foundation; either version
049c96
+ *		2 of the License, or (at your option) any later version.
049c96
+ *
049c96
+ * Authors:	J Hadi Salim <jhs@mojatatu.com>
049c96
+ *
049c96
+ * Pedagogical example. Adds a string that will be printed everytime
049c96
+ * the simple instance is hit. 
049c96
+ * Use this as a skeleton action and keep modifying it to meet your needs.
049c96
+ * Look at linux/tc_act/tc_defact.h for the different components ids and
049c96
+ * definitions used in  this actions
049c96
+ *
049c96
+ * example use, yell "Incoming ICMP!" every time you see an incoming ICMP on
049c96
+ * eth0. Steps are:
049c96
+ * 1) Add an ingress qdisc point to eth0
049c96
+ * 2) Start a chain on ingress of eth0 that first matches ICMP then invokes
049c96
+ *    the simple action to shout.
049c96
+ * 3) display stats and show that no packet has been seen by the action
049c96
+ * 4) Send one ping packet to google (expect to receive a response back)
049c96
+ * 5) grep the logs to see the logged message
049c96
+ * 6) display stats again and observe increment by 1
049c96
+ *
049c96
+  hadi@noma1:$ tc qdisc add dev eth0 ingress
049c96
+  hadi@noma1:$tc filter add dev eth0 parent ffff: protocol ip prio 5 \
049c96
+   	 u32 match ip protocol 1 0xff flowid 1:1 action simple "Incoming ICMP"
049c96
+  
049c96
+  hadi@noma1:$ sudo tc -s filter ls  dev eth0 parent ffff:
049c96
+   filter protocol ip pref 5 u32 
049c96
+   filter protocol ip pref 5 u32 fh 800: ht divisor 1 
049c96
+   filter protocol ip pref 5 u32 fh 800::800 order 2048 key ht 800 bkt 0 flowid 1:1 
049c96
+     match 00010000/00ff0000 at 8
049c96
+     	action order 1: Simple <Incoming ICMP>
049c96
+     	 index 4 ref 1 bind 1 installed 29 sec used 29 sec
049c96
+     	 Action statistics:
049c96
+     		Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0) 
049c96
+     	 	backlog 0b 0p requeues 0 
049c96
+  
049c96
+  
049c96
+  hadi@noma1$ ping -c 1 www.google.ca
049c96
+  PING www.google.ca (74.125.225.120) 56(84) bytes of data.
049c96
+  64 bytes from ord08s08-in-f24.1e100.net (74.125.225.120): icmp_req=1 ttl=53 time=31.3 ms
049c96
+
049c96
+  --- www.google.ca ping statistics ---
049c96
+  1 packets transmitted, 1 received, 0% packet loss, time 0ms
049c96
+  rtt min/avg/max/mdev = 31.316/31.316/31.316/0.000 ms
049c96
+
049c96
+  hadi@noma1$ dmesg | grep simple
049c96
+  [135354.473951] simple: Incoming ICMP_1
049c96
+
049c96
+  hadi@noma1$ sudo tc/tc -s filter ls  dev eth0 parent ffff:
049c96
+  filter protocol ip pref 5 u32 
049c96
+  filter protocol ip pref 5 u32 fh 800: ht divisor 1 
049c96
+  filter protocol ip pref 5 u32 fh 800::800 order 2048 key ht 800 bkt 0 flowid 1:1 
049c96
+    match 00010000/00ff0000 at 8
049c96
+	action order 1: Simple <Incoming ICMP>
049c96
+	 index 4 ref 1 bind 1 installed 206 sec used 67 sec
049c96
+	Action statistics:
049c96
+	Sent 84 bytes 1 pkt (dropped 0, overlimits 0 requeues 0) 
049c96
+	backlog 0b 0p requeues 0 
049c96
+*/
049c96
+
049c96
+#include <stdio.h>
049c96
+#include <stdlib.h>
049c96
+#include <unistd.h>
049c96
+#include <syslog.h>
049c96
+#include <fcntl.h>
049c96
+#include <sys/socket.h>
049c96
+#include <netinet/in.h>
049c96
+#include <arpa/inet.h>
049c96
+#include <string.h>
049c96
+#include "utils.h"
049c96
+#include "tc_util.h"
049c96
+#include <linux/tc_act/tc_defact.h>
049c96
+
049c96
+#ifndef SIMP_MAX_DATA
049c96
+#define SIMP_MAX_DATA   32
049c96
+#endif
049c96
+static void explain(void)
049c96
+{
049c96
+	fprintf(stderr, "Usage: ... simple STRING\n"
049c96
+		"STRING being an arbitrary string\n" 
049c96
+		"example: \"simple blah\"\n");
049c96
+}
049c96
+
049c96
+static void usage(void)
049c96
+{
049c96
+	explain();
049c96
+	exit(-1);
049c96
+}
049c96
+
049c96
+static int
049c96
+parse_simple(struct action_util *a, int *argc_p, char ***argv_p, int tca_id,
049c96
+	     struct nlmsghdr *n)
049c96
+{
049c96
+	struct tc_defact sel = {};
049c96
+	int argc = *argc_p;
049c96
+	char **argv = *argv_p;
049c96
+	int ok = 0;
049c96
+	struct rtattr *tail;
049c96
+	char *simpdata = NULL;
049c96
+
049c96
+
049c96
+	while (argc > 0) {
049c96
+		if (matches(*argv, "simple") == 0) {
049c96
+			NEXT_ARG();
049c96
+			simpdata = *argv;
049c96
+			ok = 1;
049c96
+			argc--;
049c96
+			argv++;
049c96
+			break;
049c96
+		} else if (matches(*argv, "help") == 0) {
049c96
+			usage();
049c96
+		} else {
049c96
+			break;
049c96
+		}
049c96
+
049c96
+	}
049c96
+
049c96
+	if (!ok) {
049c96
+		explain();
049c96
+		return -1;
049c96
+	}
049c96
+
049c96
+	if (argc) {
049c96
+		if (matches(*argv, "index") == 0) {
049c96
+			NEXT_ARG();
049c96
+			if (get_u32(&sel.index, *argv, 10)) {
049c96
+				fprintf(stderr, "simple: Illegal \"index\"\n");
049c96
+				return -1;
049c96
+			}
049c96
+			argc--;
049c96
+			argv++;
049c96
+		}
049c96
+	}
049c96
+
049c96
+	if (strlen(simpdata) > (SIMP_MAX_DATA - 1)) {
049c96
+		fprintf(stderr, "simple: Illegal string len %ld <%s> \n",
049c96
+			strlen(simpdata), simpdata);
049c96
+		return -1;
049c96
+	}
049c96
+
049c96
+	sel.action = TC_ACT_PIPE;
049c96
+
049c96
+	tail = NLMSG_TAIL(n);
049c96
+	addattr_l(n, MAX_MSG, tca_id, NULL, 0);
049c96
+	addattr_l(n, MAX_MSG, TCA_DEF_PARMS, &sel, sizeof(sel));
049c96
+	addattr_l(n, MAX_MSG, TCA_DEF_DATA, simpdata, SIMP_MAX_DATA);
049c96
+	tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail;
049c96
+
049c96
+	*argc_p = argc;
049c96
+	*argv_p = argv;
049c96
+	return 0;
049c96
+}
049c96
+
049c96
+static int print_simple(struct action_util *au, FILE * f, struct rtattr *arg)
049c96
+{
049c96
+	struct tc_defact *sel;
049c96
+	struct rtattr *tb[TCA_DEF_MAX + 1];
049c96
+	char *simpdata;
049c96
+
049c96
+	if (arg == NULL)
049c96
+		return -1;
049c96
+
049c96
+	parse_rtattr_nested(tb, TCA_DEF_MAX, arg);
049c96
+
049c96
+	if (tb[TCA_DEF_PARMS] == NULL) {
049c96
+		fprintf(f, "[NULL simple parameters]");
049c96
+		return -1;
049c96
+	}
049c96
+	sel = RTA_DATA(tb[TCA_DEF_PARMS]);
049c96
+
049c96
+	if (tb[TCA_DEF_DATA] == NULL) {
049c96
+		fprintf(f, "[missing simple string]");
049c96
+		return -1;
049c96
+	}
049c96
+
049c96
+	simpdata = RTA_DATA(tb[TCA_DEF_DATA]);
049c96
+
049c96
+	fprintf(f, "Simple <%s>\n", simpdata);
049c96
+	fprintf(f, "\t index %d ref %d bind %d", sel->index,
049c96
+		sel->refcnt, sel->bindcnt);
049c96
+
049c96
+	if (show_stats) {
049c96
+		if (tb[TCA_DEF_TM]) {
049c96
+			struct tcf_t *tm = RTA_DATA(tb[TCA_DEF_TM]);
049c96
+			print_tm(f, tm);
049c96
+			fprintf(f, "\n");
049c96
+		}
049c96
+	}
049c96
+
049c96
+	return 0;
049c96
+}
049c96
+
049c96
+struct action_util simple_action_util = {
049c96
+	.id = "simple",
049c96
+	.parse_aopt = parse_simple,
049c96
+	.print_aopt = print_simple,
049c96
+};
049c96
-- 
049c96
1.8.3.1
049c96