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 <linux/if_arp.h>
13 #include <net/llc_pdu.h>
15 #include <linux/ipv6.h>
16 #include <linux/kernel.h>
17 #include <linux/tcp.h>
18 #include <linux/udp.h>
20 #include <linux/rcupdate.h>
26 struct kmem_cache *flow_cache;
28 /* Internal function used to compare fields in flow. */
30 int flow_fields_match(const struct sw_flow_key *a, const struct sw_flow_key *b,
33 return ((w & OFPFW_IN_PORT || a->in_port == b->in_port)
34 && (w & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
35 && (w & OFPFW_DL_SRC || !memcmp(a->dl_src, b->dl_src, ETH_ALEN))
36 && (w & OFPFW_DL_DST || !memcmp(a->dl_dst, b->dl_dst, ETH_ALEN))
37 && (w & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
38 && (w & OFPFW_NW_SRC || a->nw_src == b->nw_src)
39 && (w & OFPFW_NW_DST || a->nw_dst == b->nw_dst)
40 && (w & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
41 && (w & OFPFW_TP_SRC || a->tp_src == b->tp_src)
42 && (w & OFPFW_TP_DST || a->tp_dst == b->tp_dst));
45 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
46 * 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));
53 /* Returns nonzero if 't' (the table entry's key) and 'd' (the key
54 * describing the deletion) match, that is, if their fields are
55 * equal modulo wildcards, zero otherwise. If 'strict' is nonzero, the
56 * wildcards must match in both 't_key' and 'd_key'. Note that the
57 * 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);
67 void flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from)
69 to->wildcards = ntohs(from->wildcards) & OFPFW_ALL;
70 memset(to->pad, '\0', sizeof(to->pad));
71 to->in_port = from->in_port;
72 to->dl_vlan = from->dl_vlan;
73 memcpy(to->dl_src, from->dl_src, ETH_ALEN);
74 memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
75 to->dl_type = from->dl_type;
77 if (likely(from->dl_type == htons(ETH_P_IP))) {
78 to->nw_src = from->nw_src;
79 to->nw_dst = from->nw_dst;
80 to->nw_proto = from->nw_proto;
82 if ((from->nw_proto != IPPROTO_TCP && from->nw_proto != IPPROTO_UDP)) {
85 to->tp_src = from->tp_src;
86 to->tp_dst = from->tp_dst;
88 } else if (from->dl_type == htons(ETH_P_ARP)) {
89 to->nw_src = from->nw_src;
90 to->nw_dst = from->nw_dst;
104 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
106 to->wildcards = htons(from->wildcards);
107 to->in_port = from->in_port;
108 to->dl_vlan = from->dl_vlan;
109 memcpy(to->dl_src, from->dl_src, ETH_ALEN);
110 memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
111 to->dl_type = from->dl_type;
112 to->nw_src = from->nw_src;
113 to->nw_dst = from->nw_dst;
114 to->nw_proto = from->nw_proto;
115 to->tp_src = from->tp_src;
116 to->tp_dst = from->tp_dst;
117 memset(to->pad, '\0', sizeof(to->pad));
120 /* Returns true if 'flow' can be deleted and set up for a deferred free, false
121 * if deletion has already been scheduled (by another thread).
123 * Caller must hold rcu_read_lock. */
124 int flow_del(struct sw_flow *flow)
126 return !atomic_cmpxchg(&flow->deleted, 0, 1);
129 /* Allocates and returns a new flow with 'n_actions' action, using allocation
130 * flags 'flags'. Returns the new flow or a null pointer on failure. */
131 struct sw_flow *flow_alloc(int n_actions, gfp_t flags)
133 struct sw_flow *flow = kmem_cache_alloc(flow_cache, flags);
137 flow->n_actions = n_actions;
138 flow->actions = kmalloc(n_actions * sizeof *flow->actions,
140 if (unlikely(!flow->actions) && n_actions > 0) {
141 kmem_cache_free(flow_cache, flow);
147 /* Frees 'flow' immediately. */
148 void flow_free(struct sw_flow *flow)
152 kfree(flow->actions);
153 kmem_cache_free(flow_cache, flow);
156 /* RCU callback used by flow_deferred_free. */
157 static void rcu_callback(struct rcu_head *rcu)
159 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
163 /* Schedules 'flow' to be freed after the next RCU grace period.
164 * The caller must hold rcu_read_lock for this to be sensible. */
165 void flow_deferred_free(struct sw_flow *flow)
167 call_rcu(&flow->rcu, rcu_callback);
170 /* Prints a representation of 'key' to the kernel log. */
171 void print_flow(const struct sw_flow_key *key)
173 printk("wild%04x port%04x:vlan%04x mac%02x:%02x:%02x:%02x:%02x:%02x"
174 "->%02x:%02x:%02x:%02x:%02x:%02x "
175 "proto%04x ip%u.%u.%u.%u->%u.%u.%u.%u port%d->%d\n",
176 key->wildcards, ntohs(key->in_port), ntohs(key->dl_vlan),
177 key->dl_src[0], key->dl_src[1], key->dl_src[2],
178 key->dl_src[3], key->dl_src[4], key->dl_src[5],
179 key->dl_dst[0], key->dl_dst[1], key->dl_dst[2],
180 key->dl_dst[3], key->dl_dst[4], key->dl_dst[5],
182 ((unsigned char *)&key->nw_src)[0],
183 ((unsigned char *)&key->nw_src)[1],
184 ((unsigned char *)&key->nw_src)[2],
185 ((unsigned char *)&key->nw_src)[3],
186 ((unsigned char *)&key->nw_dst)[0],
187 ((unsigned char *)&key->nw_dst)[1],
188 ((unsigned char *)&key->nw_dst)[2],
189 ((unsigned char *)&key->nw_dst)[3],
190 ntohs(key->tp_src), ntohs(key->tp_dst));
193 uint32_t hash_in6(const struct in6_addr *in)
195 return (in->s6_addr32[0] ^ in->s6_addr32[1]
196 ^ in->s6_addr32[2] ^ in->s6_addr32[3]);
199 // with inspiration from linux/if_arp.h
201 uint16_t ar_hrd; /* format of hardware address */
202 uint16_t ar_pro; /* format of protocol address */
203 uint8_t ar_hln; /* length of hardware address */
204 uint8_t ar_pln; /* length of protocol address */
205 uint16_t ar_op; /* ARP opcode (command) */
207 uint8_t ar_sha[ETH_ALEN]; /* source hardware addr */
208 uint32_t ar_sip; /* source protocol addr */
209 uint8_t ar_tha[ETH_ALEN]; /* dest hardware addr */
210 uint32_t ar_tip; /* dest protocol addr */
211 } __attribute__((packed));
213 /* Parses the Ethernet frame in 'skb', which was received on 'in_port',
214 * and initializes 'key' to match. */
215 void flow_extract(struct sk_buff *skb, uint16_t in_port,
216 struct sw_flow_key *key)
222 key->in_port = htons(in_port);
224 memset(key->pad, '\0', sizeof(key->pad));
226 /* This code doesn't check that skb->len is long enough to contain the
227 * MAC or network header. With a 46-byte minimum length frame this
228 * assumption is always correct. */
230 /* Doesn't verify checksums. Should it? */
232 /* Data link layer. We only support Ethernet. */
234 nh_ofs = sizeof(struct ethhdr);
235 if (likely(ntohs(mac->h_proto) >= OFP_DL_TYPE_ETH2_CUTOFF)) {
236 /* This is an Ethernet II frame */
237 key->dl_type = mac->h_proto;
239 /* This is an 802.2 frame */
240 if (snap_get_ethertype(skb, &key->dl_type) != -EINVAL) {
241 nh_ofs += sizeof(struct snap_hdr);
243 key->dl_type = OFP_DL_TYPE_NOT_ETH_TYPE;
244 nh_ofs += sizeof(struct llc_pdu_un);
248 /* Check for a VLAN tag */
249 if (likely(key->dl_type != htons(ETH_P_8021Q))) {
250 key->dl_vlan = htons(OFP_VLAN_NONE);
252 struct vlan_hdr *vh = (struct vlan_hdr *)(skb_mac_header(skb) + nh_ofs);
253 key->dl_type = vh->h_vlan_encapsulated_proto;
254 key->dl_vlan = vh->h_vlan_TCI & htons(VLAN_VID_MASK);
255 nh_ofs += sizeof(*vh);
257 memcpy(key->dl_src, mac->h_source, ETH_ALEN);
258 memcpy(key->dl_dst, mac->h_dest, ETH_ALEN);
259 skb_set_network_header(skb, nh_ofs);
262 if (likely(key->dl_type == htons(ETH_P_IP))) {
263 struct iphdr *nh = ip_hdr(skb);
264 key->nw_src = nh->saddr;
265 key->nw_dst = nh->daddr;
266 key->nw_proto = nh->protocol;
267 th_ofs = nh_ofs + nh->ihl * 4;
268 skb_set_transport_header(skb, th_ofs);
270 /* Transport layer. */
271 if ((key->nw_proto != IPPROTO_TCP && key->nw_proto != IPPROTO_UDP)
272 || skb->len < th_ofs + sizeof(struct udphdr)) {
276 key->tp_src = th->source;
277 key->tp_dst = th->dest;
280 } else if (key->dl_type == htons(ETH_P_IPV6)) {
281 struct ipv6hdr *nh = ipv6_hdr(skb);
282 key->nw_src = hash_in6(&nh->saddr);
283 key->nw_dst = hash_in6(&nh->daddr);
284 /* FIXME: Need to traverse next-headers until we find the
285 * upper-layer header. */
288 } else if (key->dl_type == htons(ETH_P_ARP)) {
289 /* just barely within 46-byte minimum packet */
290 struct arp_eth_hdr *ah = (struct arp_eth_hdr *)skb_network_header(skb);
291 if (ah->ar_hrd == htons(ARPHRD_ETHER)
292 && ah->ar_pro == htons(ETH_P_IP)
293 && ah->ar_hln == ETH_ALEN
294 && ah->ar_pln == sizeof(key->nw_src))
296 /* check if sha/tha match dl_src/dl_dst? */
297 key->nw_src = ah->ar_sip;
298 key->nw_dst = ah->ar_tip;
315 /* Initializes the flow module.
316 * Returns zero if successful or a negative error code. */
319 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
321 if (flow_cache == NULL)
327 /* Uninitializes the flow module. */
330 kmem_cache_destroy(flow_cache);