]> git.proxmox.com Git - mirror_ovs.git/blame - datapath/flow.c
datapath: Use NULL for null pointer in compat code.
[mirror_ovs.git] / datapath / flow.c
CommitLineData
064af421
BP
1/*
2 * Distributed under the terms of the GNU GPL version 2.
834377ea 3 * Copyright (c) 2007, 2008, 2009, 2010 Nicira Networks.
a14bc59f
BP
4 *
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
064af421
BP
7 */
8
9#include "flow.h"
f5e86186 10#include "datapath.h"
064af421
BP
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>
8d5ebd83 17#include <linux/jhash.h>
064af421
BP
18#include <linux/jiffies.h>
19#include <linux/llc.h>
20#include <linux/module.h>
21#include <linux/in.h>
22#include <linux/rcupdate.h>
a26ef517 23#include <linux/if_arp.h>
064af421
BP
24#include <linux/if_ether.h>
25#include <linux/ip.h>
26#include <linux/tcp.h>
27#include <linux/udp.h>
28#include <linux/icmp.h>
3c5f6de3 29#include <net/inet_ecn.h>
064af421
BP
30#include <net/ip.h>
31
32#include "compat.h"
33
33b38b63 34static struct kmem_cache *flow_cache;
83e3e75b 35static unsigned int hash_seed __read_mostly;
064af421 36
e819fb47 37static inline bool arphdr_ok(struct sk_buff *skb)
a26ef517 38{
7d0ab001 39 return skb->len >= skb_network_offset(skb) + sizeof(struct arp_eth_header);
a26ef517
JP
40}
41
4c1ad233 42static inline int check_iphdr(struct sk_buff *skb)
064af421 43{
4c1ad233
BP
44 unsigned int nh_ofs = skb_network_offset(skb);
45 unsigned int ip_len;
46
47 if (skb->len < nh_ofs + sizeof(struct iphdr))
48 return -EINVAL;
49
50 ip_len = ip_hdrlen(skb);
51 if (ip_len < sizeof(struct iphdr) || skb->len < nh_ofs + ip_len)
52 return -EINVAL;
53
54 /*
55 * Pull enough header bytes to account for the IP header plus the
56 * longest transport header that we parse, currently 20 bytes for TCP.
57 */
58 if (!pskb_may_pull(skb, min(nh_ofs + ip_len + 20, skb->len)))
59 return -ENOMEM;
60
61 skb_set_transport_header(skb, nh_ofs + ip_len);
62 return 0;
064af421
BP
63}
64
e819fb47 65static inline bool tcphdr_ok(struct sk_buff *skb)
064af421
BP
66{
67 int th_ofs = skb_transport_offset(skb);
7d0ab001 68 if (skb->len >= th_ofs + sizeof(struct tcphdr)) {
064af421
BP
69 int tcp_len = tcp_hdrlen(skb);
70 return (tcp_len >= sizeof(struct tcphdr)
71 && skb->len >= th_ofs + tcp_len);
72 }
e819fb47 73 return false;
064af421
BP
74}
75
e819fb47 76static inline bool udphdr_ok(struct sk_buff *skb)
064af421 77{
7d0ab001 78 return skb->len >= skb_transport_offset(skb) + sizeof(struct udphdr);
064af421
BP
79}
80
e819fb47 81static inline bool icmphdr_ok(struct sk_buff *skb)
064af421 82{
7d0ab001 83 return skb->len >= skb_transport_offset(skb) + sizeof(struct icmphdr);
064af421
BP
84}
85
86#define TCP_FLAGS_OFFSET 13
87#define TCP_FLAG_MASK 0x3f
88
064af421
BP
89void flow_used(struct sw_flow *flow, struct sk_buff *skb)
90{
064af421
BP
91 u8 tcp_flags = 0;
92
abfec865
BP
93 if (flow->key.dl_type == htons(ETH_P_IP) &&
94 flow->key.nw_proto == IPPROTO_TCP) {
95 u8 *tcp = (u8 *)tcp_hdr(skb);
96 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
064af421
BP
97 }
98
f2459fe7 99 spin_lock_bh(&flow->lock);
6bfafa55 100 flow->used = jiffies;
064af421
BP
101 flow->packet_count++;
102 flow->byte_count += skb->len;
103 flow->tcp_flags |= tcp_flags;
f2459fe7 104 spin_unlock_bh(&flow->lock);
064af421
BP
105}
106
cdee00fd 107struct sw_flow_actions *flow_actions_alloc(u32 actions_len)
064af421
BP
108{
109 struct sw_flow_actions *sfa;
110
cdee00fd
BP
111 if (actions_len % NLA_ALIGNTO)
112 return ERR_PTR(-EINVAL);
113
722d19c5
BP
114 /* At least DP_MAX_PORTS actions are required to be able to flood a
115 * packet to every port. Factor of 2 allows for setting VLAN tags,
116 * etc. */
cdee00fd 117 if (actions_len > 2 * DP_MAX_PORTS * nla_total_size(4))
064af421
BP
118 return ERR_PTR(-EINVAL);
119
cdee00fd 120 sfa = kmalloc(sizeof *sfa + actions_len, GFP_KERNEL);
064af421
BP
121 if (!sfa)
122 return ERR_PTR(-ENOMEM);
123
cdee00fd 124 sfa->actions_len = actions_len;
064af421
BP
125 return sfa;
126}
127
560e8022
JG
128struct sw_flow *flow_alloc(void)
129{
130 struct sw_flow *flow;
131
132 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
133 if (!flow)
134 return ERR_PTR(-ENOMEM);
135
136 spin_lock_init(&flow->lock);
fb8c9347
JG
137 atomic_set(&flow->refcnt, 1);
138 flow->dead = false;
064af421 139
560e8022
JG
140 return flow;
141}
142
8d5ebd83
JG
143void flow_free_tbl(struct tbl_node *node)
144{
145 struct sw_flow *flow = flow_cast(node);
fb8c9347
JG
146
147 flow->dead = true;
148 flow_put(flow);
8d5ebd83
JG
149}
150
064af421
BP
151/* RCU callback used by flow_deferred_free. */
152static void rcu_free_flow_callback(struct rcu_head *rcu)
153{
154 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
fb8c9347
JG
155
156 flow->dead = true;
157 flow_put(flow);
064af421
BP
158}
159
160/* Schedules 'flow' to be freed after the next RCU grace period.
161 * The caller must hold rcu_read_lock for this to be sensible. */
162void flow_deferred_free(struct sw_flow *flow)
163{
164 call_rcu(&flow->rcu, rcu_free_flow_callback);
165}
166
fb8c9347
JG
167void flow_hold(struct sw_flow *flow)
168{
169 atomic_inc(&flow->refcnt);
170}
171
172void flow_put(struct sw_flow *flow)
173{
174 if (unlikely(!flow))
175 return;
176
177 if (atomic_dec_and_test(&flow->refcnt)) {
39872c70 178 kfree((struct sf_flow_acts __force *)flow->sf_acts);
fb8c9347
JG
179 kmem_cache_free(flow_cache, flow);
180 }
181}
182
064af421
BP
183/* RCU callback used by flow_deferred_free_acts. */
184static void rcu_free_acts_callback(struct rcu_head *rcu)
185{
d295e8e9 186 struct sw_flow_actions *sf_acts = container_of(rcu,
064af421
BP
187 struct sw_flow_actions, rcu);
188 kfree(sf_acts);
189}
190
191/* Schedules 'sf_acts' to be freed after the next RCU grace period.
192 * The caller must hold rcu_read_lock for this to be sensible. */
193void flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
194{
195 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
196}
197
50f06e16 198static void parse_vlan(struct sk_buff *skb, struct odp_flow_key *key)
064af421 199{
50f06e16
BP
200 struct qtag_prefix {
201 __be16 eth_type; /* ETH_P_8021Q */
202 __be16 tci;
203 };
204 struct qtag_prefix *qp;
205
206 if (skb->len < sizeof(struct qtag_prefix) + sizeof(__be16))
207 return;
208
209 qp = (struct qtag_prefix *) skb->data;
26233bb4 210 key->dl_tci = qp->tci | htons(ODP_TCI_PRESENT);
50f06e16
BP
211 __skb_pull(skb, sizeof(struct qtag_prefix));
212}
213
214static __be16 parse_ethertype(struct sk_buff *skb)
064af421 215{
50f06e16
BP
216 struct llc_snap_hdr {
217 u8 dsap; /* Always 0xAA */
218 u8 ssap; /* Always 0xAA */
219 u8 ctrl;
220 u8 oui[3];
8dda8c9b 221 __be16 ethertype;
50f06e16
BP
222 };
223 struct llc_snap_hdr *llc;
224 __be16 proto;
225
226 proto = *(__be16 *) skb->data;
227 __skb_pull(skb, sizeof(__be16));
228
229 if (ntohs(proto) >= ODP_DL_TYPE_ETH2_CUTOFF)
230 return proto;
231
232 if (unlikely(skb->len < sizeof(struct llc_snap_hdr)))
233 return htons(ODP_DL_TYPE_NOT_ETH_TYPE);
234
235 llc = (struct llc_snap_hdr *) skb->data;
236 if (llc->dsap != LLC_SAP_SNAP ||
237 llc->ssap != LLC_SAP_SNAP ||
238 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
239 return htons(ODP_DL_TYPE_NOT_ETH_TYPE);
240
241 __skb_pull(skb, sizeof(struct llc_snap_hdr));
242 return llc->ethertype;
064af421
BP
243}
244
a31e0e31
BP
245/**
246 * flow_extract - extracts a flow key from an Ethernet frame.
247 * @skb: sk_buff that contains the frame, with skb->data pointing to the
248 * Ethernet header
249 * @in_port: port number on which @skb was received.
250 * @key: output flow key
26233bb4
BP
251 * @is_frag: set to 1 if @skb contains an IPv4 fragment, or to 0 if @skb does
252 * not contain an IPv4 packet or if it is not a fragment.
a31e0e31
BP
253 *
254 * The caller must ensure that skb->len >= ETH_HLEN.
255 *
4c1ad233
BP
256 * Returns 0 if successful, otherwise a negative errno value.
257 *
59a18f80
BP
258 * Initializes @skb header pointers as follows:
259 *
260 * - skb->mac_header: the Ethernet header.
261 *
262 * - skb->network_header: just past the Ethernet header, or just past the
263 * VLAN header, to the first byte of the Ethernet payload.
264 *
265 * - skb->transport_header: If key->dl_type is ETH_P_IP on output, then just
266 * past the IPv4 header, if one is present and of a correct length,
267 * otherwise the same as skb->network_header. For other key->dl_type
268 * values it is left untouched.
a31e0e31 269 */
b7a31ec1
JG
270int flow_extract(struct sk_buff *skb, u16 in_port, struct odp_flow_key *key,
271 bool *is_frag)
064af421
BP
272{
273 struct ethhdr *eth;
064af421
BP
274
275 memset(key, 0, sizeof *key);
659586ef 276 key->tun_id = OVS_CB(skb)->tun_id;
064af421 277 key->in_port = in_port;
b7a31ec1 278 *is_frag = false;
064af421 279
4c1ad233
BP
280 /*
281 * We would really like to pull as many bytes as we could possibly
282 * want to parse into the linear data area. Currently that is:
283 *
284 * 14 Ethernet header
285 * 4 VLAN header
286 * 60 max IP header with options
287 * 20 max TCP/UDP/ICMP header (don't care about options)
288 * --
289 * 98
290 *
291 * But Xen only allocates 64 or 72 bytes for the linear data area in
292 * netback, which means that we would reallocate and copy the skb's
293 * linear data on every packet if we did that. So instead just pull 64
294 * bytes, which is always sufficient without IP options, and then check
295 * whether we need to pull more later when we look at the IP header.
296 */
d9fce1ca 297 if (!pskb_may_pull(skb, min(skb->len, 64u)))
4c1ad233 298 return -ENOMEM;
064af421
BP
299
300 skb_reset_mac_header(skb);
064af421 301
50f06e16
BP
302 /* Link layer. */
303 eth = eth_hdr(skb);
064af421
BP
304 memcpy(key->dl_src, eth->h_source, ETH_ALEN);
305 memcpy(key->dl_dst, eth->h_dest, ETH_ALEN);
50f06e16
BP
306
307 /* dl_type, dl_vlan, dl_vlan_pcp. */
308 __skb_pull(skb, 2 * ETH_ALEN);
309 if (eth->h_proto == htons(ETH_P_8021Q))
310 parse_vlan(skb, key);
311 key->dl_type = parse_ethertype(skb);
312 skb_reset_network_header(skb);
313 __skb_push(skb, skb->data - (unsigned char *)eth);
064af421
BP
314
315 /* Network layer. */
4c1ad233
BP
316 if (key->dl_type == htons(ETH_P_IP)) {
317 struct iphdr *nh;
318 int error;
319
320 error = check_iphdr(skb);
321 if (unlikely(error)) {
322 if (error == -EINVAL) {
323 skb->transport_header = skb->network_header;
324 return 0;
325 }
326 return error;
327 }
328
329 nh = ip_hdr(skb);
064af421
BP
330 key->nw_src = nh->saddr;
331 key->nw_dst = nh->daddr;
f5e86186 332 key->nw_tos = nh->tos & ~INET_ECN_MASK;
064af421 333 key->nw_proto = nh->protocol;
064af421
BP
334
335 /* Transport layer. */
875244c7
SH
336 if (!(nh->frag_off & htons(IP_MF | IP_OFFSET)) &&
337 !(skb_shinfo(skb)->gso_type & SKB_GSO_UDP)) {
064af421
BP
338 if (key->nw_proto == IPPROTO_TCP) {
339 if (tcphdr_ok(skb)) {
340 struct tcphdr *tcp = tcp_hdr(skb);
341 key->tp_src = tcp->source;
342 key->tp_dst = tcp->dest;
064af421
BP
343 }
344 } else if (key->nw_proto == IPPROTO_UDP) {
345 if (udphdr_ok(skb)) {
346 struct udphdr *udp = udp_hdr(skb);
347 key->tp_src = udp->source;
348 key->tp_dst = udp->dest;
064af421
BP
349 }
350 } else if (key->nw_proto == IPPROTO_ICMP) {
351 if (icmphdr_ok(skb)) {
352 struct icmphdr *icmp = icmp_hdr(skb);
353 /* The ICMP type and code fields use the 16-bit
354 * transport port fields, so we need to store them
355 * in 16-bit network byte order. */
356 key->tp_src = htons(icmp->type);
357 key->tp_dst = htons(icmp->code);
064af421
BP
358 }
359 }
b7a31ec1
JG
360 } else
361 *is_frag = true;
362
a26ef517
JP
363 } else if (key->dl_type == htons(ETH_P_ARP) && arphdr_ok(skb)) {
364 struct arp_eth_header *arp;
365
366 arp = (struct arp_eth_header *)skb_network_header(skb);
367
f5e86186 368 if (arp->ar_hrd == htons(ARPHRD_ETHER)
de3f65ea
JP
369 && arp->ar_pro == htons(ETH_P_IP)
370 && arp->ar_hln == ETH_ALEN
371 && arp->ar_pln == 4) {
372
373 /* We only match on the lower 8 bits of the opcode. */
b7a31ec1 374 if (ntohs(arp->ar_op) <= 0xff)
de3f65ea 375 key->nw_proto = ntohs(arp->ar_op);
de3f65ea 376
d295e8e9 377 if (key->nw_proto == ARPOP_REQUEST
de3f65ea
JP
378 || key->nw_proto == ARPOP_REPLY) {
379 memcpy(&key->nw_src, arp->ar_sip, sizeof(key->nw_src));
380 memcpy(&key->nw_dst, arp->ar_tip, sizeof(key->nw_dst));
381 }
382 }
064af421 383 }
769f8ccd 384 return 0;
064af421
BP
385}
386
8d5ebd83
JG
387u32 flow_hash(const struct odp_flow_key *key)
388{
389 return jhash2((u32*)key, sizeof *key / sizeof(u32), hash_seed);
390}
391
392int flow_cmp(const struct tbl_node *node, void *key2_)
393{
394 const struct odp_flow_key *key1 = &flow_cast(node)->key;
395 const struct odp_flow_key *key2 = key2_;
396
397 return !memcmp(key1, key2, sizeof(struct odp_flow_key));
398}
399
064af421
BP
400/* Initializes the flow module.
401 * Returns zero if successful or a negative error code. */
402int flow_init(void)
403{
404 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
405 0, NULL);
406 if (flow_cache == NULL)
407 return -ENOMEM;
408
8d5ebd83
JG
409 get_random_bytes(&hash_seed, sizeof hash_seed);
410
064af421
BP
411 return 0;
412}
413
414/* Uninitializes the flow module. */
415void flow_exit(void)
416{
417 kmem_cache_destroy(flow_cache);
418}