2 * Distributed under the terms of the GNU GPL version 2.
3 * Copyright (c) 2007, 2008 The Board of Trustees of The Leland
4 * Stanford Junior University
8 #include <linux/netdevice.h>
9 #include <linux/etherdevice.h>
10 #include <linux/if_ether.h>
11 #include <linux/if_vlan.h>
12 #include <net/llc_pdu.h>
14 #include <linux/jiffies.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/tcp.h>
18 #include <linux/udp.h>
20 #include <linux/rcupdate.h>
27 struct kmem_cache *flow_cache;
29 /* Internal function used to compare fields in flow. */
31 int flow_fields_match(const struct sw_flow_key *a, const struct sw_flow_key *b,
34 return ((w & OFPFW_IN_PORT || a->in_port == b->in_port)
35 && (w & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
36 && (w & OFPFW_DL_SRC || !memcmp(a->dl_src, b->dl_src, ETH_ALEN))
37 && (w & OFPFW_DL_DST || !memcmp(a->dl_dst, b->dl_dst, ETH_ALEN))
38 && (w & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
39 && (w & OFPFW_NW_SRC || a->nw_src == b->nw_src)
40 && (w & OFPFW_NW_DST || a->nw_dst == b->nw_dst)
41 && (w & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
42 && (w & OFPFW_TP_SRC || a->tp_src == b->tp_src)
43 && (w & OFPFW_TP_DST || a->tp_dst == b->tp_dst));
46 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
47 * modulo wildcards, zero otherwise. */
48 int flow_matches(const struct sw_flow_key *a, const struct sw_flow_key *b)
50 return flow_fields_match(a, b, (a->wildcards | b->wildcards));
52 EXPORT_SYMBOL(flow_matches);
54 /* Returns nonzero if 't' (the table entry's key) and 'd' (the key
55 * describing the deletion) match, that is, if their fields are
56 * equal modulo wildcards, zero otherwise. If 'strict' is nonzero, the
57 * wildcards must match in both 't_key' and 'd_key'. Note that the
58 * table's wildcards are ignored unless 'strict' is set. */
59 int flow_del_matches(const struct sw_flow_key *t, const struct sw_flow_key *d, int strict)
61 if (strict && (t->wildcards != d->wildcards))
64 return flow_fields_match(t, d, d->wildcards);
66 EXPORT_SYMBOL(flow_del_matches);
68 void flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from)
70 to->wildcards = ntohs(from->wildcards) & OFPFW_ALL;
71 memset(to->pad, '\0', sizeof(to->pad));
72 to->in_port = from->in_port;
73 to->dl_vlan = from->dl_vlan;
74 memcpy(to->dl_src, from->dl_src, ETH_ALEN);
75 memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
76 to->dl_type = from->dl_type;
78 to->nw_src = to->nw_dst = to->nw_proto = 0;
79 to->tp_src = to->tp_dst = 0;
81 #define OFPFW_TP (OFPFW_TP_SRC | OFPFW_TP_DST)
82 #define OFPFW_NW (OFPFW_NW_SRC | OFPFW_NW_DST | OFPFW_NW_PROTO)
83 if (to->wildcards & OFPFW_DL_TYPE) {
84 /* Can't sensibly match on network or transport headers if the
85 * data link type is unknown. */
86 to->wildcards |= OFPFW_NW | OFPFW_TP;
87 } else if (from->dl_type == htons(ETH_P_IP)) {
88 to->nw_src = from->nw_src;
89 to->nw_dst = from->nw_dst;
90 to->nw_proto = from->nw_proto;
92 if (to->wildcards & OFPFW_NW_PROTO) {
93 /* Can't sensibly match on transport headers if the
94 * network protocol is unknown. */
95 to->wildcards |= OFPFW_TP;
96 } else if (from->nw_proto == IPPROTO_TCP
97 || from->nw_proto == IPPROTO_UDP) {
98 to->tp_src = from->tp_src;
99 to->tp_dst = from->tp_dst;
101 /* Transport layer fields are undefined. Mark them as
102 * exact-match to allow such flows to reside in
103 * table-hash, instead of falling into table-linear. */
104 to->wildcards &= ~OFPFW_TP;
107 /* Network and transport layer fields are undefined. Mark them
108 * as exact-match to allow such flows to reside in table-hash,
109 * instead of falling into table-linear. */
110 to->wildcards &= ~(OFPFW_NW | OFPFW_TP);
114 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
116 to->wildcards = htons(from->wildcards);
117 to->in_port = from->in_port;
118 to->dl_vlan = from->dl_vlan;
119 memcpy(to->dl_src, from->dl_src, ETH_ALEN);
120 memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
121 to->dl_type = from->dl_type;
122 to->nw_src = from->nw_src;
123 to->nw_dst = from->nw_dst;
124 to->nw_proto = from->nw_proto;
125 to->tp_src = from->tp_src;
126 to->tp_dst = from->tp_dst;
127 memset(to->pad, '\0', sizeof(to->pad));
130 int flow_timeout(struct sw_flow *flow)
132 if (flow->idle_timeout != OFP_FLOW_PERMANENT
133 && time_after(jiffies, flow->used + flow->idle_timeout * HZ))
134 return OFPER_IDLE_TIMEOUT;
135 else if (flow->hard_timeout != OFP_FLOW_PERMANENT
136 && time_after(jiffies,
137 flow->init_time + flow->hard_timeout * HZ))
138 return OFPER_HARD_TIMEOUT;
142 EXPORT_SYMBOL(flow_timeout);
144 /* Allocates and returns a new flow with 'n_actions' action, using allocation
145 * flags 'flags'. Returns the new flow or a null pointer on failure. */
146 struct sw_flow *flow_alloc(int n_actions, gfp_t flags)
148 struct sw_flow *flow = kmem_cache_alloc(flow_cache, flags);
152 flow->n_actions = n_actions;
153 flow->actions = kmalloc(n_actions * sizeof *flow->actions,
155 if (unlikely(!flow->actions) && n_actions > 0) {
156 kmem_cache_free(flow_cache, flow);
162 /* Frees 'flow' immediately. */
163 void flow_free(struct sw_flow *flow)
167 kfree(flow->actions);
168 kmem_cache_free(flow_cache, flow);
170 EXPORT_SYMBOL(flow_free);
172 /* RCU callback used by flow_deferred_free. */
173 static void rcu_callback(struct rcu_head *rcu)
175 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
179 /* Schedules 'flow' to be freed after the next RCU grace period.
180 * The caller must hold rcu_read_lock for this to be sensible. */
181 void flow_deferred_free(struct sw_flow *flow)
183 call_rcu(&flow->rcu, rcu_callback);
185 EXPORT_SYMBOL(flow_deferred_free);
187 /* Prints a representation of 'key' to the kernel log. */
188 void print_flow(const struct sw_flow_key *key)
190 printk("wild%04x port%04x:vlan%04x mac%02x:%02x:%02x:%02x:%02x:%02x"
191 "->%02x:%02x:%02x:%02x:%02x:%02x "
192 "proto%04x ip%u.%u.%u.%u->%u.%u.%u.%u port%d->%d\n",
193 key->wildcards, ntohs(key->in_port), ntohs(key->dl_vlan),
194 key->dl_src[0], key->dl_src[1], key->dl_src[2],
195 key->dl_src[3], key->dl_src[4], key->dl_src[5],
196 key->dl_dst[0], key->dl_dst[1], key->dl_dst[2],
197 key->dl_dst[3], key->dl_dst[4], key->dl_dst[5],
199 ((unsigned char *)&key->nw_src)[0],
200 ((unsigned char *)&key->nw_src)[1],
201 ((unsigned char *)&key->nw_src)[2],
202 ((unsigned char *)&key->nw_src)[3],
203 ((unsigned char *)&key->nw_dst)[0],
204 ((unsigned char *)&key->nw_dst)[1],
205 ((unsigned char *)&key->nw_dst)[2],
206 ((unsigned char *)&key->nw_dst)[3],
207 ntohs(key->tp_src), ntohs(key->tp_dst));
209 EXPORT_SYMBOL(print_flow);
211 static int tcphdr_ok(struct sk_buff *skb)
213 int th_ofs = skb_transport_offset(skb);
214 if (skb->len >= th_ofs + sizeof(struct tcphdr)) {
215 int tcp_len = tcp_hdrlen(skb);
216 return (tcp_len >= sizeof(struct tcphdr)
217 && skb->len >= th_ofs + tcp_len);
222 static int udphdr_ok(struct sk_buff *skb)
224 int th_ofs = skb_transport_offset(skb);
225 return skb->len >= th_ofs + sizeof(struct udphdr);
228 /* Parses the Ethernet frame in 'skb', which was received on 'in_port',
229 * and initializes 'key' to match. Returns 1 if 'skb' contains an IP
230 * fragment, 0 otherwise. */
231 int flow_extract(struct sk_buff *skb, uint16_t in_port,
232 struct sw_flow_key *key)
238 key->in_port = htons(in_port);
240 memset(key->pad, '\0', sizeof(key->pad));
242 /* This code doesn't check that skb->len is long enough to contain the
243 * MAC or network header. With a 46-byte minimum length frame this
244 * assumption is always correct. */
246 /* Doesn't verify checksums. Should it? */
248 /* Data link layer. We only support Ethernet. */
250 nh_ofs = sizeof(struct ethhdr);
251 if (likely(ntohs(mac->h_proto) >= OFP_DL_TYPE_ETH2_CUTOFF)) {
252 /* This is an Ethernet II frame */
253 key->dl_type = mac->h_proto;
255 /* This is an 802.2 frame */
256 if (snap_get_ethertype(skb, &key->dl_type) != -EINVAL) {
257 nh_ofs += sizeof(struct snap_hdr);
259 key->dl_type = OFP_DL_TYPE_NOT_ETH_TYPE;
260 nh_ofs += sizeof(struct llc_pdu_un);
264 /* Check for a VLAN tag */
265 if (likely(key->dl_type != htons(ETH_P_8021Q))) {
266 key->dl_vlan = htons(OFP_VLAN_NONE);
268 struct vlan_hdr *vh = (struct vlan_hdr *)(skb_mac_header(skb) + nh_ofs);
269 key->dl_type = vh->h_vlan_encapsulated_proto;
270 key->dl_vlan = vh->h_vlan_TCI & htons(VLAN_VID_MASK);
271 nh_ofs += sizeof(*vh);
273 memcpy(key->dl_src, mac->h_source, ETH_ALEN);
274 memcpy(key->dl_dst, mac->h_dest, ETH_ALEN);
275 skb_set_network_header(skb, nh_ofs);
278 if (likely(key->dl_type == htons(ETH_P_IP))) {
279 struct iphdr *nh = ip_hdr(skb);
280 key->nw_src = nh->saddr;
281 key->nw_dst = nh->daddr;
282 key->nw_proto = nh->protocol;
283 th_ofs = nh_ofs + nh->ihl * 4;
284 skb_set_transport_header(skb, th_ofs);
286 /* Transport layer. */
287 if (!(nh->frag_off & htons(IP_MF | IP_OFFSET))) {
288 if (key->nw_proto == IPPROTO_TCP) {
289 if (tcphdr_ok(skb)) {
290 struct tcphdr *tcp = tcp_hdr(skb);
291 key->tp_src = tcp->source;
292 key->tp_dst = tcp->dest;
294 /* Avoid tricking other code into
295 * thinking that this packet has an L4
299 } else if (key->nw_proto == IPPROTO_UDP) {
300 if (udphdr_ok(skb)) {
301 struct udphdr *udp = udp_hdr(skb);
302 key->tp_src = udp->source;
303 key->tp_dst = udp->dest;
305 /* Avoid tricking other code into
306 * thinking that this packet has an L4
333 /* Initializes the flow module.
334 * Returns zero if successful or a negative error code. */
337 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
339 if (flow_cache == NULL)
345 /* Uninitializes the flow module. */
348 kmem_cache_destroy(flow_cache);