📄 cls_rsvp.h
字号:
/* * net/sched/cls_rsvp.h Template file for RSVPv[46] classifiers. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> *//* Comparing to general packet classification problem, RSVP needs only sevaral relatively simple rules: * (dst, protocol) are always specified, so that we are able to hash them. * src may be exact, or may be wildcard, so that we can keep a hash table plus one wildcard entry. * source port (or flow label) is important only if src is given. IMPLEMENTATION. We use a two level hash table: The top level is keyed by destination address and protocol ID, every bucket contains a list of "rsvp sessions", identified by destination address, protocol and DPI(="Destination Port ID"): triple (key, mask, offset). Every bucket has a smaller hash table keyed by source address (cf. RSVP flowspec) and one wildcard entry for wildcard reservations. Every bucket is again a list of "RSVP flows", selected by source address and SPI(="Source Port ID" here rather than "security parameter index"): triple (key, mask, offset). NOTE 1. All the packets with IPv6 extension headers (but AH and ESP) and all fragmented packets go to the best-effort traffic class. NOTE 2. Two "port id"'s seems to be redundant, rfc2207 requires only one "Generalized Port Identifier". So that for classic ah, esp (and udp,tcp) both *pi should coincide or one of them should be wildcard. At first sight, this redundancy is just a waste of CPU resources. But DPI and SPI add the possibility to assign different priorities to GPIs. Look also at note 4 about tunnels below. NOTE 3. One complication is the case of tunneled packets. We implement it as following: if the first lookup matches a special session with "tunnelhdr" value not zero, flowid doesn't contain the true flow ID, but the tunnel ID (1...255). In this case, we pull tunnelhdr bytes and restart lookup with tunnel ID added to the list of keys. Simple and stupid 8)8) It's enough for PIMREG and IPIP. NOTE 4. Two GPIs make it possible to parse even GRE packets. F.e. DPI can select ETH_P_IP (and necessary flags to make tunnelhdr correct) in GRE protocol field and SPI matches GRE key. Is it not nice? 8)8) Well, as result, despite its simplicity, we get a pretty powerful classification engine. */#include <linux/config.h>struct rsvp_head{ u32 tmap[256/32]; u32 hgenerator; u8 tgenerator; struct rsvp_session *ht[256];};struct rsvp_session{ struct rsvp_session *next; u32 dst[RSVP_DST_LEN]; struct tc_rsvp_gpi dpi; u8 protocol; u8 tunnelid; /* 16 (src,sport) hash slots, and one wildcard source slot */ struct rsvp_filter *ht[16+1];};struct rsvp_filter{ struct rsvp_filter *next; u32 src[RSVP_DST_LEN]; struct tc_rsvp_gpi spi; u8 tunnelhdr; struct tcf_result res;#ifdef CONFIG_NET_CLS_POLICE struct tcf_police *police;#endif u32 handle; struct rsvp_session *sess;};static __inline__ unsigned hash_dst(u32 *dst, u8 protocol, u8 tunnelid){ unsigned h = dst[RSVP_DST_LEN-1]; h ^= h>>16; h ^= h>>8; return (h ^ protocol ^ tunnelid) & 0xFF;}static __inline__ unsigned hash_src(u32 *src){ unsigned h = src[RSVP_DST_LEN-1]; h ^= h>>16; h ^= h>>8; h ^= h>>4; return h & 0xF;}#ifdef CONFIG_NET_CLS_POLICE#define RSVP_POLICE() \if (f->police) { \ int pol_res = tcf_police(skb, f->police); \ if (pol_res < 0) continue; \ if (pol_res) return pol_res; \}#else#define RSVP_POLICE()#endifstatic int rsvp_classify(struct sk_buff *skb, struct tcf_proto *tp, struct tcf_result *res){ struct rsvp_session **sht = ((struct rsvp_head*)tp->root)->ht; struct rsvp_session *s; struct rsvp_filter *f; unsigned h1, h2; u32 *dst, *src; u8 protocol; u8 tunnelid = 0; u8 *xprt;#if RSVP_DST_LEN == 4 struct ipv6hdr *nhptr = skb->nh.ipv6h;#else struct iphdr *nhptr = skb->nh.iph;#endif#if !defined( __i386__) && !defined(__mc68000__) if ((unsigned long)nhptr & 3) return -1;#endifrestart:#if RSVP_DST_LEN == 4 src = &nhptr->saddr.s6_addr32[0]; dst = &nhptr->daddr.s6_addr32[0]; protocol = nhptr->nexthdr; xprt = ((u8*)nhptr) + sizeof(struct ipv6hdr);#else src = &nhptr->saddr; dst = &nhptr->daddr; protocol = nhptr->protocol; xprt = ((u8*)nhptr) + (nhptr->ihl<<2); if (nhptr->frag_off&__constant_htons(IP_MF|IP_OFFSET)) return -1;#endif h1 = hash_dst(dst, protocol, tunnelid); h2 = hash_src(src); for (s = sht[h1]; s; s = s->next) { if (dst[RSVP_DST_LEN-1] == s->dst[RSVP_DST_LEN-1] && protocol == s->protocol && !(s->dpi.mask & (*(u32*)(xprt+s->dpi.offset)^s->dpi.key))#if RSVP_DST_LEN == 4 && dst[0] == s->dst[0] && dst[1] == s->dst[1] && dst[2] == s->dst[2]#endif && tunnelid == s->tunnelid) { for (f = s->ht[h2]; f; f = f->next) { if (src[RSVP_DST_LEN-1] == f->src[RSVP_DST_LEN-1] && !(f->spi.mask & (*(u32*)(xprt+f->spi.offset)^f->spi.key))#if RSVP_DST_LEN == 4 && src[0] == f->src[0] && src[1] == f->src[1] && src[2] == f->src[2]#endif ) { *res = f->res; RSVP_POLICE();matched: if (f->tunnelhdr == 0) return 0; tunnelid = f->res.classid; nhptr = (void*)(xprt + f->tunnelhdr - sizeof(*nhptr)); goto restart; } } /* And wildcard bucket... */ for (f = s->ht[16]; f; f = f->next) { *res = f->res; RSVP_POLICE(); goto matched; } return -1; } } return -1;}static unsigned long rsvp_get(struct tcf_proto *tp, u32 handle){ struct rsvp_session **sht = ((struct rsvp_head*)tp->root)->ht; struct rsvp_session *s; struct rsvp_filter *f; unsigned h1 = handle&0xFF; unsigned h2 = (handle>>8)&0xFF; if (h2 > 16) return 0; for (s = sht[h1]; s; s = s->next) { for (f = s->ht[h2]; f; f = f->next) { if (f->handle == handle) return (unsigned long)f; } } return 0;}static void rsvp_put(struct tcf_proto *tp, unsigned long f){}static int rsvp_init(struct tcf_proto *tp){ struct rsvp_head *data; MOD_INC_USE_COUNT; data = kmalloc(sizeof(struct rsvp_head), GFP_KERNEL); if (data) { memset(data, 0, sizeof(struct rsvp_head)); tp->root = data; return 0; } MOD_DEC_USE_COUNT; return -ENOBUFS;}static void rsvp_destroy(struct tcf_proto *tp){ struct rsvp_head *data = xchg(&tp->root, NULL); struct rsvp_session **sht; int h1, h2; if (data == NULL) return; sht = data->ht; for (h1=0; h1<256; h1++) { struct rsvp_session *s; while ((s = sht[h1]) != NULL) { sht[h1] = s->next; for (h2=0; h2<=16; h2++) { struct rsvp_filter *f; while ((f = s->ht[h2]) != NULL) { unsigned long cl; s->ht[h2] = f->next; if ((cl = __cls_set_class(&f->res.class, 0)) != 0) tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);#ifdef CONFIG_NET_CLS_POLICE tcf_police_release(f->police);#endif kfree(f); } } kfree(s); } } kfree(data); MOD_DEC_USE_COUNT;}static int rsvp_delete(struct tcf_proto *tp, unsigned long arg){ struct rsvp_filter **fp, *f = (struct rsvp_filter*)arg; unsigned h = f->handle; struct rsvp_session **sp; struct rsvp_session *s = f->sess; int i; for (fp = &s->ht[(h>>8)&0xFF]; *fp; fp = &(*fp)->next) { if (*fp == f) { unsigned long cl; tcf_tree_lock(tp); *fp = f->next; tcf_tree_unlock(tp); if ((cl = cls_set_class(tp, &f->res.class, 0)) != 0) tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);#ifdef CONFIG_NET_CLS_POLICE tcf_police_release(f->police);#endif kfree(f); /* Strip tree */ for (i=0; i<=16; i++) if (s->ht[i]) return 0; /* OK, session has no flows */ for (sp = &((struct rsvp_head*)tp->root)->ht[h&0xFF]; *sp; sp = &(*sp)->next) { if (*sp == s) { tcf_tree_lock(tp); *sp = s->next; tcf_tree_unlock(tp); kfree(s); return 0; } } return 0; } } return 0;}static unsigned gen_handle(struct tcf_proto *tp, unsigned salt){ struct rsvp_head *data = tp->root; int i = 0xFFFF;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -