From 59880cb01e0609f64bf004f8226541646b652cec Mon Sep 17 00:00:00 2001 From: Thomas GraF Date: Thu, 4 Nov 2010 20:01:36 +0100 Subject: [PATCH] pktloc: support to specify a shift operator for packet locations no users yet though. --- etc/pktloc | 44 ++++++++++++++++++++++++---------- include/netlink/route/pktloc.h | 1 + lib/route/pktloc.c | 5 ++-- lib/route/pktloc_syntax.y | 13 ++++++++-- src/nl-pktloc-lookup.c | 14 +++++++---- 5 files changed, 56 insertions(+), 21 deletions(-) diff --git a/etc/pktloc b/etc/pktloc index 96f5a41..505c44e 100644 --- a/etc/pktloc +++ b/etc/pktloc @@ -2,14 +2,15 @@ # Location definitions for packet matching # -# name alignment offset mask -ip.version u8 net+0 0xF0 +# name alignment offset mask shift +ip.version u8 net+0 0xF0 4 ip.hdrlen u8 net+0 0x0F ip.diffserv u8 net+1 ip.length u16 net+2 ip.id u16 net+4 -ip.df u8 net+6 0x40 -ip.mf u8 net+6 0x20 +ip.flag.res u8 net+6 0xff 7 +ip.df u8 net+6 0x40 6 +ip.mf u8 net+6 0x20 5 ip.offset u16 net+6 0x1FFF ip.ttl u8 net+8 ip.proto u8 net+9 @@ -17,12 +18,16 @@ ip.chksum u16 net+10 ip.src u32 net+12 ip.dst u32 net+16 +# if ip.ihl > 5 +ip.opts u32 net+20 + + # # IP version 6 # -# name alignment offset mask -ip6.version u8 net+0 0xF0 -ip6.tc u16 net+0 0xFF0 +# name alignment offset mask shift +ip6.version u8 net+0 0xF0 4 +ip6.tc u16 net+0 0xFF0 4 ip6.flowlabel u32 net+0 0xFFFFF ip6.length u16 net+4 ip6.nexthdr u8 net+6 @@ -33,14 +38,29 @@ ip6.dst 16 net+24 # # Transmission Control Protocol (TCP) # -# name alignment offset mask +# name alignment offset mask shift tcp.sport u16 tcp+0 tcp.dport u16 tcp+2 tcp.seq u32 tcp+4 tcp.ack u32 tcp+8 -tcp.off u8 tcp+12 0xF0 -tcp.reserved u8 tcp+12 0x0F -# FLAGS + +# Data offset (4 bits) +tcp.off u8 tcp+12 0xF0 4 + +# Reserved [0 0 0] (3 bits) +tcp.reserved u8 tcp+12 0x04 1 + +# ECN [N C E] (3 bits) +tcp.ecn u16 tcp+12 0x01C00 6 + +# Individual TCP flags (0|1) (6 bits in total) +tcp.flag.urg u8 tcp+13 0x20 5 +tcp.flag.ack u8 tcp+13 0x10 4 +tcp.flag.psh u8 tcp+13 0x08 3 +tcp.flag.rst u8 tcp+13 0x04 2 +tpc.flag.syn u8 tcp+13 0x02 1 +tcp.flag.fin u8 tcp+13 0x01 + tcp.win u16 tcp+14 tcp.csum u16 tcp+16 tcp.urg u16 tcp+18 @@ -49,7 +69,7 @@ tcp.opts u32 tcp+20 # # User Datagram Protocol (UDP) # -# name alignment offset mask +# name alignment offset mask shift udp.sport u16 tcp+0 udp.dport u16 tcp+2 udp.length u16 tcp+4 diff --git a/include/netlink/route/pktloc.h b/include/netlink/route/pktloc.h index ad8c66c..c3768ce 100644 --- a/include/netlink/route/pktloc.h +++ b/include/netlink/route/pktloc.h @@ -26,6 +26,7 @@ struct rtnl_pktloc { char * name; uint8_t layer; + uint8_t shift; uint16_t offset; uint16_t align; uint32_t mask; diff --git a/lib/route/pktloc.c b/lib/route/pktloc.c index b5e5b77..823a3c7 100644 --- a/lib/route/pktloc.c +++ b/lib/route/pktloc.c @@ -222,8 +222,9 @@ int rtnl_pktloc_add(struct rtnl_pktloc *loc) } NL_DBG(2, "New packet location entry \"%s\" align=%u layer=%u " - "offset=%u mask=%#x refnt=%u\n", loc->name, loc->align, - loc->layer, loc->offset, loc->mask, loc->refcnt); + "offset=%u mask=%#x shift=%u refnt=%u\n", + loc->name, loc->align, loc->layer, loc->offset, + loc->mask, loc->shift, loc->refcnt); nl_list_add_tail(&loc->list, &pktloc_name_ht[pktloc_hash(loc->name)]); diff --git a/lib/route/pktloc_syntax.y b/lib/route/pktloc_syntax.y index 95cd6f4..4a2ce48 100644 --- a/lib/route/pktloc_syntax.y +++ b/lib/route/pktloc_syntax.y @@ -13,6 +13,7 @@ %parse-param {void *scanner} %lex-param {void *scanner} +%expect 1 %union { struct rtnl_pktloc *l; @@ -32,7 +33,7 @@ static void yyerror(YYLTYPE *locp, void *scanner, const char *msg) %token ERROR NUMBER LAYER ALIGN %token NAME -%type mask layer align +%type mask layer align shift %type location %destructor { free($$); } NAME @@ -47,7 +48,7 @@ input: ; location: - NAME align layer NUMBER mask + NAME align layer NUMBER mask shift { struct rtnl_pktloc *loc; @@ -62,6 +63,7 @@ location: loc->layer = $3; loc->offset = $4; loc->mask = $5; + loc->shift = $6; if (rtnl_pktloc_add(loc) < 0) { NL_DBG(1, "Duplicate packet location entry " @@ -92,3 +94,10 @@ mask: | NUMBER { $$ = $1; } ; + +shift: + /* empty */ + { $$ = 0; } + | NUMBER + { $$ = $1; } + ; diff --git a/src/nl-pktloc-lookup.c b/src/nl-pktloc-lookup.c index 0f9831a..17c867b 100644 --- a/src/nl-pktloc-lookup.c +++ b/src/nl-pktloc-lookup.c @@ -59,6 +59,9 @@ static void dump_u32_style(struct rtnl_pktloc *loc, uint32_t value) nl_cli_fatal(EINVAL, "u32 does not support link " "layer locations."); + if (loc->shift > 0) + nl_cli_fatal(EINVAL, "u32 does not support shifting."); + printf("%s %x %x at %s%u\n", align_txt[loc->align], value, loc->mask ? loc->mask : align_mask[loc->align], @@ -80,21 +83,22 @@ static char *get_align_txt(struct rtnl_pktloc *loc) static void dump_loc(struct rtnl_pktloc *loc) { - printf("%s = %s at %s+%u %#x\n", + printf("%s = %s at %s+%u & %#x >> %u\n", loc->name, get_align_txt(loc), layer_txt[loc->layer], - loc->offset, loc->mask); + loc->offset, loc->mask, loc->shift); } static void list_cb(struct rtnl_pktloc *loc, void *arg) { - printf("%-26s %-5s %3s+%-4u %#-10x %u\n", + printf("%-26s %-5s %3s+%-4u %#-10x %-8u %u\n", loc->name, get_align_txt(loc), layer_txt[loc->layer], - loc->offset, loc->mask, loc->refcnt); + loc->offset, loc->mask, loc->shift, loc->refcnt); } static void do_list(void) { - printf("name align offset mask refcnt\n"); + printf( +"name align offset mask shift refcnt\n"); printf("---------------------------------------------------------\n"); rtnl_pktloc_foreach(&list_cb, NULL);