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/kernel.h>
15 #include <linux/module.h>
16 #include <linux/tcp.h>
17 #include <linux/udp.h>
19 #include <linux/rcupdate.h>
25 struct kmem_cache *flow_cache;
27 /* Internal function used to compare fields in flow. */
29 int flow_fields_match(const struct sw_flow_key *a, const struct sw_flow_key *b,
32 return ((w & OFPFW_IN_PORT || a->in_port == b->in_port)
33 && (w & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
34 && (w & OFPFW_DL_SRC || !memcmp(a->dl_src, b->dl_src, ETH_ALEN))
35 && (w & OFPFW_DL_DST || !memcmp(a->dl_dst, b->dl_dst, ETH_ALEN))
36 && (w & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
37 && (w & OFPFW_NW_SRC || a->nw_src == b->nw_src)
38 && (w & OFPFW_NW_DST || a->nw_dst == b->nw_dst)
39 && (w & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
40 && (w & OFPFW_TP_SRC || a->tp_src == b->tp_src)
41 && (w & OFPFW_TP_DST || a->tp_dst == b->tp_dst));
44 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
45 * modulo wildcards, zero otherwise. */
46 int flow_matches(const struct sw_flow_key *a, const struct sw_flow_key *b)
48 return flow_fields_match(a, b, (a->wildcards | b->wildcards));
50 EXPORT_SYMBOL(flow_matches);
52 /* Returns nonzero if 't' (the table entry's key) and 'd' (the key
53 * describing the deletion) match, that is, if their fields are
54 * equal modulo wildcards, zero otherwise. If 'strict' is nonzero, the
55 * wildcards must match in both 't_key' and 'd_key'. Note that the
56 * table's wildcards are ignored unless 'strict' is set. */
57 int flow_del_matches(const struct sw_flow_key *t, const struct sw_flow_key *d, int strict)
59 if (strict && (t->wildcards != d->wildcards))
62 return flow_fields_match(t, d, d->wildcards);
64 EXPORT_SYMBOL(flow_del_matches);
66 void flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from)
68 to->wildcards = ntohs(from->wildcards) & OFPFW_ALL;
69 memset(to->pad, '\0', sizeof(to->pad));
70 to->in_port = from->in_port;
71 to->dl_vlan = from->dl_vlan;
72 memcpy(to->dl_src, from->dl_src, ETH_ALEN);
73 memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
74 to->dl_type = from->dl_type;
76 to->nw_src = to->nw_dst = to->nw_proto = 0;
77 to->tp_src = to->tp_dst = 0;
79 #define OFPFW_TP (OFPFW_TP_SRC | OFPFW_TP_DST)
80 #define OFPFW_NW (OFPFW_NW_SRC | OFPFW_NW_DST | OFPFW_NW_PROTO)
81 if (to->wildcards & OFPFW_DL_TYPE) {
82 /* Can't sensibly match on network or transport headers if the
83 * data link type is unknown. */
84 to->wildcards |= OFPFW_NW | OFPFW_TP;
85 } else if (from->dl_type == htons(ETH_P_IP)) {
86 to->nw_src = from->nw_src;
87 to->nw_dst = from->nw_dst;
88 to->nw_proto = from->nw_proto;
90 if (to->wildcards & OFPFW_NW_PROTO) {
91 /* Can't sensibly match on transport headers if the
92 * network protocol is unknown. */
93 to->wildcards |= OFPFW_TP;
94 } else if (from->nw_proto == IPPROTO_TCP
95 || from->nw_proto == IPPROTO_UDP) {
96 to->tp_src = from->tp_src;
97 to->tp_dst = from->tp_dst;
99 /* Transport layer fields are undefined. Mark them as
100 * exact-match to allow such flows to reside in
101 * table-hash, instead of falling into table-linear. */
102 to->wildcards &= ~OFPFW_TP;
105 /* Network and transport layer fields are undefined. Mark them
106 * as exact-match to allow such flows to reside in table-hash,
107 * instead of falling into table-linear. */
108 to->wildcards &= ~(OFPFW_NW | OFPFW_TP);
112 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
114 to->wildcards = htons(from->wildcards);
115 to->in_port = from->in_port;
116 to->dl_vlan = from->dl_vlan;
117 memcpy(to->dl_src, from->dl_src, ETH_ALEN);
118 memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
119 to->dl_type = from->dl_type;
120 to->nw_src = from->nw_src;
121 to->nw_dst = from->nw_dst;
122 to->nw_proto = from->nw_proto;
123 to->tp_src = from->tp_src;
124 to->tp_dst = from->tp_dst;
125 memset(to->pad, '\0', sizeof(to->pad));
128 /* Returns true if 'flow' can be deleted and set up for a deferred free, false
129 * if deletion has already been scheduled (by another thread).
131 * Caller must hold rcu_read_lock. */
132 int flow_del(struct sw_flow *flow)
134 return !atomic_cmpxchg(&flow->deleted, 0, 1);
136 EXPORT_SYMBOL(flow_del);
138 /* Allocates and returns a new flow with 'n_actions' action, using allocation
139 * flags 'flags'. Returns the new flow or a null pointer on failure. */
140 struct sw_flow *flow_alloc(int n_actions, gfp_t flags)
142 struct sw_flow *flow = kmem_cache_alloc(flow_cache, flags);
146 flow->n_actions = n_actions;
147 flow->actions = kmalloc(n_actions * sizeof *flow->actions,
149 if (unlikely(!flow->actions) && n_actions > 0) {
150 kmem_cache_free(flow_cache, flow);
156 /* Frees 'flow' immediately. */
157 void flow_free(struct sw_flow *flow)
161 kfree(flow->actions);
162 kmem_cache_free(flow_cache, flow);
164 EXPORT_SYMBOL(flow_free);
166 /* RCU callback used by flow_deferred_free. */
167 static void rcu_callback(struct rcu_head *rcu)
169 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
173 /* Schedules 'flow' to be freed after the next RCU grace period.
174 * The caller must hold rcu_read_lock for this to be sensible. */
175 void flow_deferred_free(struct sw_flow *flow)
177 call_rcu(&flow->rcu, rcu_callback);
179 EXPORT_SYMBOL(flow_deferred_free);
181 /* Prints a representation of 'key' to the kernel log. */
182 void print_flow(const struct sw_flow_key *key)
184 printk("wild%04x port%04x:vlan%04x mac%02x:%02x:%02x:%02x:%02x:%02x"
185 "->%02x:%02x:%02x:%02x:%02x:%02x "
186 "proto%04x ip%u.%u.%u.%u->%u.%u.%u.%u port%d->%d\n",
187 key->wildcards, ntohs(key->in_port), ntohs(key->dl_vlan),
188 key->dl_src[0], key->dl_src[1], key->dl_src[2],
189 key->dl_src[3], key->dl_src[4], key->dl_src[5],
190 key->dl_dst[0], key->dl_dst[1], key->dl_dst[2],
191 key->dl_dst[3], key->dl_dst[4], key->dl_dst[5],
193 ((unsigned char *)&key->nw_src)[0],
194 ((unsigned char *)&key->nw_src)[1],
195 ((unsigned char *)&key->nw_src)[2],
196 ((unsigned char *)&key->nw_src)[3],
197 ((unsigned char *)&key->nw_dst)[0],
198 ((unsigned char *)&key->nw_dst)[1],
199 ((unsigned char *)&key->nw_dst)[2],
200 ((unsigned char *)&key->nw_dst)[3],
201 ntohs(key->tp_src), ntohs(key->tp_dst));
203 EXPORT_SYMBOL(print_flow);
205 /* Parses the Ethernet frame in 'skb', which was received on 'in_port',
206 * and initializes 'key' to match. */
207 void flow_extract(struct sk_buff *skb, uint16_t in_port,
208 struct sw_flow_key *key)
214 key->in_port = htons(in_port);
216 memset(key->pad, '\0', sizeof(key->pad));
218 /* This code doesn't check that skb->len is long enough to contain the
219 * MAC or network header. With a 46-byte minimum length frame this
220 * assumption is always correct. */
222 /* Doesn't verify checksums. Should it? */
224 /* Data link layer. We only support Ethernet. */
226 nh_ofs = sizeof(struct ethhdr);
227 if (likely(ntohs(mac->h_proto) >= OFP_DL_TYPE_ETH2_CUTOFF)) {
228 /* This is an Ethernet II frame */
229 key->dl_type = mac->h_proto;
231 /* This is an 802.2 frame */
232 if (snap_get_ethertype(skb, &key->dl_type) != -EINVAL) {
233 nh_ofs += sizeof(struct snap_hdr);
235 key->dl_type = OFP_DL_TYPE_NOT_ETH_TYPE;
236 nh_ofs += sizeof(struct llc_pdu_un);
240 /* Check for a VLAN tag */
241 if (likely(key->dl_type != htons(ETH_P_8021Q))) {
242 key->dl_vlan = htons(OFP_VLAN_NONE);
244 struct vlan_hdr *vh = (struct vlan_hdr *)(skb_mac_header(skb) + nh_ofs);
245 key->dl_type = vh->h_vlan_encapsulated_proto;
246 key->dl_vlan = vh->h_vlan_TCI & htons(VLAN_VID_MASK);
247 nh_ofs += sizeof(*vh);
249 memcpy(key->dl_src, mac->h_source, ETH_ALEN);
250 memcpy(key->dl_dst, mac->h_dest, ETH_ALEN);
251 skb_set_network_header(skb, nh_ofs);
254 if (likely(key->dl_type == htons(ETH_P_IP))) {
255 struct iphdr *nh = ip_hdr(skb);
256 key->nw_src = nh->saddr;
257 key->nw_dst = nh->daddr;
258 key->nw_proto = nh->protocol;
259 th_ofs = nh_ofs + nh->ihl * 4;
260 skb_set_transport_header(skb, th_ofs);
262 /* Transport layer. */
263 if ((key->nw_proto != IPPROTO_TCP && key->nw_proto != IPPROTO_UDP)
264 || skb->len < th_ofs + sizeof(struct udphdr)) {
268 key->tp_src = th->source;
269 key->tp_dst = th->dest;
283 /* Initializes the flow module.
284 * Returns zero if successful or a negative error code. */
287 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
289 if (flow_cache == NULL)
295 /* Uninitializes the flow module. */
298 kmem_cache_destroy(flow_cache);