2 * Distributed under the terms of the GNU GPL version 2.
3 * Copyright (c) 2007, 2008, 2009, 2010 Nicira Networks.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
11 #include <linux/netdevice.h>
12 #include <linux/etherdevice.h>
13 #include <linux/if_ether.h>
14 #include <linux/if_vlan.h>
15 #include <net/llc_pdu.h>
16 #include <linux/kernel.h>
17 #include <linux/jhash.h>
18 #include <linux/jiffies.h>
19 #include <linux/llc.h>
20 #include <linux/module.h>
22 #include <linux/rcupdate.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_ether.h>
26 #include <linux/tcp.h>
27 #include <linux/udp.h>
28 #include <linux/icmp.h>
29 #include <net/inet_ecn.h>
34 struct kmem_cache *flow_cache;
35 static unsigned int hash_seed;
37 static inline bool arphdr_ok(struct sk_buff *skb)
39 int nh_ofs = skb_network_offset(skb);
40 return pskb_may_pull(skb, nh_ofs + sizeof(struct arp_eth_header));
43 static inline bool iphdr_ok(struct sk_buff *skb)
45 int nh_ofs = skb_network_offset(skb);
46 if (skb->len >= nh_ofs + sizeof(struct iphdr)) {
47 int ip_len = ip_hdrlen(skb);
48 return (ip_len >= sizeof(struct iphdr)
49 && pskb_may_pull(skb, nh_ofs + ip_len));
54 static inline bool tcphdr_ok(struct sk_buff *skb)
56 int th_ofs = skb_transport_offset(skb);
57 if (pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))) {
58 int tcp_len = tcp_hdrlen(skb);
59 return (tcp_len >= sizeof(struct tcphdr)
60 && skb->len >= th_ofs + tcp_len);
65 static inline bool udphdr_ok(struct sk_buff *skb)
67 int th_ofs = skb_transport_offset(skb);
68 return pskb_may_pull(skb, th_ofs + sizeof(struct udphdr));
71 static inline bool icmphdr_ok(struct sk_buff *skb)
73 int th_ofs = skb_transport_offset(skb);
74 return pskb_may_pull(skb, th_ofs + sizeof(struct icmphdr));
77 #define TCP_FLAGS_OFFSET 13
78 #define TCP_FLAG_MASK 0x3f
80 void flow_used(struct sw_flow *flow, struct sk_buff *skb)
84 if (flow->key.dl_type == htons(ETH_P_IP) &&
85 flow->key.nw_proto == IPPROTO_TCP) {
86 u8 *tcp = (u8 *)tcp_hdr(skb);
87 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
90 spin_lock_bh(&flow->lock);
93 flow->byte_count += skb->len;
94 flow->tcp_flags |= tcp_flags;
95 spin_unlock_bh(&flow->lock);
98 struct sw_flow_actions *flow_actions_alloc(size_t n_actions)
100 struct sw_flow_actions *sfa;
102 if (n_actions > (PAGE_SIZE - sizeof *sfa) / sizeof(union odp_action))
103 return ERR_PTR(-EINVAL);
105 sfa = kmalloc(sizeof *sfa + n_actions * sizeof(union odp_action),
108 return ERR_PTR(-ENOMEM);
110 sfa->n_actions = n_actions;
115 /* Frees 'flow' immediately. */
116 static void flow_free(struct sw_flow *flow)
120 kfree(flow->sf_acts);
121 kmem_cache_free(flow_cache, flow);
124 void flow_free_tbl(struct tbl_node *node)
126 struct sw_flow *flow = flow_cast(node);
130 /* RCU callback used by flow_deferred_free. */
131 static void rcu_free_flow_callback(struct rcu_head *rcu)
133 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
137 /* Schedules 'flow' to be freed after the next RCU grace period.
138 * The caller must hold rcu_read_lock for this to be sensible. */
139 void flow_deferred_free(struct sw_flow *flow)
141 call_rcu(&flow->rcu, rcu_free_flow_callback);
144 /* RCU callback used by flow_deferred_free_acts. */
145 static void rcu_free_acts_callback(struct rcu_head *rcu)
147 struct sw_flow_actions *sf_acts = container_of(rcu,
148 struct sw_flow_actions, rcu);
152 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
153 * The caller must hold rcu_read_lock for this to be sensible. */
154 void flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
156 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
159 static void parse_vlan(struct sk_buff *skb, struct odp_flow_key *key)
162 __be16 eth_type; /* ETH_P_8021Q */
165 struct qtag_prefix *qp;
167 if (skb->len < sizeof(struct qtag_prefix) + sizeof(__be16))
170 qp = (struct qtag_prefix *) skb->data;
171 key->dl_vlan = qp->tci & htons(VLAN_VID_MASK);
172 key->dl_vlan_pcp = (ntohs(qp->tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
173 __skb_pull(skb, sizeof(struct qtag_prefix));
176 static __be16 parse_ethertype(struct sk_buff *skb)
178 struct llc_snap_hdr {
179 u8 dsap; /* Always 0xAA */
180 u8 ssap; /* Always 0xAA */
185 struct llc_snap_hdr *llc;
188 proto = *(__be16 *) skb->data;
189 __skb_pull(skb, sizeof(__be16));
191 if (ntohs(proto) >= ODP_DL_TYPE_ETH2_CUTOFF)
194 if (unlikely(skb->len < sizeof(struct llc_snap_hdr)))
195 return htons(ODP_DL_TYPE_NOT_ETH_TYPE);
197 llc = (struct llc_snap_hdr *) skb->data;
198 if (llc->dsap != LLC_SAP_SNAP ||
199 llc->ssap != LLC_SAP_SNAP ||
200 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
201 return htons(ODP_DL_TYPE_NOT_ETH_TYPE);
203 __skb_pull(skb, sizeof(struct llc_snap_hdr));
204 return llc->ethertype;
208 * flow_extract - extracts a flow key from an Ethernet frame.
209 * @skb: sk_buff that contains the frame, with skb->data pointing to the
211 * @in_port: port number on which @skb was received.
212 * @key: output flow key
214 * The caller must ensure that skb->len >= ETH_HLEN.
216 * Sets OVS_CB(skb)->is_frag to %true if @skb is an IPv4 fragment, otherwise to
219 int flow_extract(struct sk_buff *skb, u16 in_port, struct odp_flow_key *key)
223 memset(key, 0, sizeof *key);
224 key->tun_id = OVS_CB(skb)->tun_id;
225 key->in_port = in_port;
226 key->dl_vlan = htons(ODP_VLAN_NONE);
227 OVS_CB(skb)->is_frag = false;
229 if (!pskb_may_pull(skb, min(skb->len, 64u)))
232 skb_reset_mac_header(skb);
236 memcpy(key->dl_src, eth->h_source, ETH_ALEN);
237 memcpy(key->dl_dst, eth->h_dest, ETH_ALEN);
239 /* dl_type, dl_vlan, dl_vlan_pcp. */
240 __skb_pull(skb, 2 * ETH_ALEN);
241 if (eth->h_proto == htons(ETH_P_8021Q))
242 parse_vlan(skb, key);
243 key->dl_type = parse_ethertype(skb);
244 skb_reset_network_header(skb);
245 __skb_push(skb, skb->data - (unsigned char *)eth);
248 if (key->dl_type == htons(ETH_P_IP) && iphdr_ok(skb)) {
249 struct iphdr *nh = ip_hdr(skb);
250 int th_ofs = skb_network_offset(skb) + nh->ihl * 4;
251 key->nw_src = nh->saddr;
252 key->nw_dst = nh->daddr;
253 key->nw_tos = nh->tos & ~INET_ECN_MASK;
254 key->nw_proto = nh->protocol;
255 skb_set_transport_header(skb, th_ofs);
257 /* Transport layer. */
258 if (!(nh->frag_off & htons(IP_MF | IP_OFFSET))) {
259 if (key->nw_proto == IPPROTO_TCP) {
260 if (tcphdr_ok(skb)) {
261 struct tcphdr *tcp = tcp_hdr(skb);
262 key->tp_src = tcp->source;
263 key->tp_dst = tcp->dest;
265 /* Avoid tricking other code into
266 * thinking that this packet has an L4
270 } else if (key->nw_proto == IPPROTO_UDP) {
271 if (udphdr_ok(skb)) {
272 struct udphdr *udp = udp_hdr(skb);
273 key->tp_src = udp->source;
274 key->tp_dst = udp->dest;
276 /* Avoid tricking other code into
277 * thinking that this packet has an L4
281 } else if (key->nw_proto == IPPROTO_ICMP) {
282 if (icmphdr_ok(skb)) {
283 struct icmphdr *icmp = icmp_hdr(skb);
284 /* The ICMP type and code fields use the 16-bit
285 * transport port fields, so we need to store them
286 * in 16-bit network byte order. */
287 key->tp_src = htons(icmp->type);
288 key->tp_dst = htons(icmp->code);
290 /* Avoid tricking other code into
291 * thinking that this packet has an L4
297 OVS_CB(skb)->is_frag = true;
299 } else if (key->dl_type == htons(ETH_P_ARP) && arphdr_ok(skb)) {
300 struct arp_eth_header *arp;
302 arp = (struct arp_eth_header *)skb_network_header(skb);
304 if (arp->ar_hrd == htons(ARPHRD_ETHER)
305 && arp->ar_pro == htons(ETH_P_IP)
306 && arp->ar_hln == ETH_ALEN
307 && arp->ar_pln == 4) {
309 /* We only match on the lower 8 bits of the opcode. */
310 if (ntohs(arp->ar_op) <= 0xff) {
311 key->nw_proto = ntohs(arp->ar_op);
314 if (key->nw_proto == ARPOP_REQUEST
315 || key->nw_proto == ARPOP_REPLY) {
316 memcpy(&key->nw_src, arp->ar_sip, sizeof(key->nw_src));
317 memcpy(&key->nw_dst, arp->ar_tip, sizeof(key->nw_dst));
321 skb_reset_transport_header(skb);
326 u32 flow_hash(const struct odp_flow_key *key)
328 return jhash2((u32*)key, sizeof *key / sizeof(u32), hash_seed);
331 int flow_cmp(const struct tbl_node *node, void *key2_)
333 const struct odp_flow_key *key1 = &flow_cast(node)->key;
334 const struct odp_flow_key *key2 = key2_;
336 return !memcmp(key1, key2, sizeof(struct odp_flow_key));
339 /* Initializes the flow module.
340 * Returns zero if successful or a negative error code. */
343 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
345 if (flow_cache == NULL)
348 get_random_bytes(&hash_seed, sizeof hash_seed);
353 /* Uninitializes the flow module. */
356 kmem_cache_destroy(flow_cache);