]> git.proxmox.com Git - mirror_ovs.git/blob - datapath/actions.c
datapath: Support for Linux kernel 3.10
[mirror_ovs.git] / datapath / actions.c
1 /*
2 * Copyright (c) 2007-2013 Nicira, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include <linux/skbuff.h>
22 #include <linux/in.h>
23 #include <linux/ip.h>
24 #include <linux/openvswitch.h>
25 #include <linux/tcp.h>
26 #include <linux/udp.h>
27 #include <linux/in6.h>
28 #include <linux/if_arp.h>
29 #include <linux/if_vlan.h>
30 #include <net/ip.h>
31 #include <net/ipv6.h>
32 #include <net/checksum.h>
33 #include <net/dsfield.h>
34
35 #include "checksum.h"
36 #include "datapath.h"
37 #include "vlan.h"
38 #include "vport.h"
39
40 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
41 const struct nlattr *attr, int len, bool keep_skb);
42
43 static int make_writable(struct sk_buff *skb, int write_len)
44 {
45 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
46 return 0;
47
48 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
49 }
50
51 /* remove VLAN header from packet and update csum accordingly. */
52 static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
53 {
54 struct vlan_hdr *vhdr;
55 int err;
56
57 err = make_writable(skb, VLAN_ETH_HLEN);
58 if (unlikely(err))
59 return err;
60
61 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
62 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
63 + (2 * ETH_ALEN), VLAN_HLEN, 0));
64
65 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
66 *current_tci = vhdr->h_vlan_TCI;
67
68 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
69 __skb_pull(skb, VLAN_HLEN);
70
71 vlan_set_encap_proto(skb, vhdr);
72 skb->mac_header += VLAN_HLEN;
73 skb_reset_mac_len(skb);
74
75 return 0;
76 }
77
78 static int pop_vlan(struct sk_buff *skb)
79 {
80 __be16 tci;
81 int err;
82
83 if (likely(vlan_tx_tag_present(skb))) {
84 vlan_set_tci(skb, 0);
85 } else {
86 if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
87 skb->len < VLAN_ETH_HLEN))
88 return 0;
89
90 err = __pop_vlan_tci(skb, &tci);
91 if (err)
92 return err;
93 }
94 /* move next vlan tag to hw accel tag */
95 if (likely(skb->protocol != htons(ETH_P_8021Q) ||
96 skb->len < VLAN_ETH_HLEN))
97 return 0;
98
99 err = __pop_vlan_tci(skb, &tci);
100 if (unlikely(err))
101 return err;
102
103 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(tci));
104 return 0;
105 }
106
107 static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan)
108 {
109 if (unlikely(vlan_tx_tag_present(skb))) {
110 u16 current_tag;
111
112 /* push down current VLAN tag */
113 current_tag = vlan_tx_tag_get(skb);
114
115 if (!__vlan_put_tag(skb, skb->vlan_proto, current_tag))
116 return -ENOMEM;
117
118 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
119 skb->csum = csum_add(skb->csum, csum_partial(skb->data
120 + (2 * ETH_ALEN), VLAN_HLEN, 0));
121
122 }
123 __vlan_hwaccel_put_tag(skb, vlan->vlan_tpid, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
124 return 0;
125 }
126
127 static int set_eth_addr(struct sk_buff *skb,
128 const struct ovs_key_ethernet *eth_key)
129 {
130 int err;
131 err = make_writable(skb, ETH_HLEN);
132 if (unlikely(err))
133 return err;
134
135 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
136 skb->csum = csum_sub(skb->csum, csum_partial(eth_hdr(skb),
137 ETH_ALEN * 2, 0));
138
139 memcpy(eth_hdr(skb)->h_source, eth_key->eth_src, ETH_ALEN);
140 memcpy(eth_hdr(skb)->h_dest, eth_key->eth_dst, ETH_ALEN);
141
142 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
143 skb->csum = csum_add(skb->csum, csum_partial(eth_hdr(skb),
144 ETH_ALEN * 2, 0));
145
146 return 0;
147 }
148
149 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
150 __be32 *addr, __be32 new_addr)
151 {
152 int transport_len = skb->len - skb_transport_offset(skb);
153
154 if (nh->protocol == IPPROTO_TCP) {
155 if (likely(transport_len >= sizeof(struct tcphdr)))
156 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
157 *addr, new_addr, 1);
158 } else if (nh->protocol == IPPROTO_UDP) {
159 if (likely(transport_len >= sizeof(struct udphdr))) {
160 struct udphdr *uh = udp_hdr(skb);
161
162 if (uh->check ||
163 get_ip_summed(skb) == OVS_CSUM_PARTIAL) {
164 inet_proto_csum_replace4(&uh->check, skb,
165 *addr, new_addr, 1);
166 if (!uh->check)
167 uh->check = CSUM_MANGLED_0;
168 }
169 }
170 }
171
172 csum_replace4(&nh->check, *addr, new_addr);
173 skb_clear_rxhash(skb);
174 *addr = new_addr;
175 }
176
177 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
178 __be32 addr[4], const __be32 new_addr[4])
179 {
180 int transport_len = skb->len - skb_transport_offset(skb);
181
182 if (l4_proto == IPPROTO_TCP) {
183 if (likely(transport_len >= sizeof(struct tcphdr)))
184 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
185 addr, new_addr, 1);
186 } else if (l4_proto == IPPROTO_UDP) {
187 if (likely(transport_len >= sizeof(struct udphdr))) {
188 struct udphdr *uh = udp_hdr(skb);
189
190 if (uh->check ||
191 get_ip_summed(skb) == OVS_CSUM_PARTIAL) {
192 inet_proto_csum_replace16(&uh->check, skb,
193 addr, new_addr, 1);
194 if (!uh->check)
195 uh->check = CSUM_MANGLED_0;
196 }
197 }
198 }
199 }
200
201 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
202 __be32 addr[4], const __be32 new_addr[4],
203 bool recalculate_csum)
204 {
205 if (recalculate_csum)
206 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
207
208 skb_clear_rxhash(skb);
209 memcpy(addr, new_addr, sizeof(__be32[4]));
210 }
211
212 static void set_ipv6_tc(struct ipv6hdr *nh, u8 tc)
213 {
214 nh->priority = tc >> 4;
215 nh->flow_lbl[0] = (nh->flow_lbl[0] & 0x0F) | ((tc & 0x0F) << 4);
216 }
217
218 static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl)
219 {
220 nh->flow_lbl[0] = (nh->flow_lbl[0] & 0xF0) | (fl & 0x000F0000) >> 16;
221 nh->flow_lbl[1] = (fl & 0x0000FF00) >> 8;
222 nh->flow_lbl[2] = fl & 0x000000FF;
223 }
224
225 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
226 {
227 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
228 nh->ttl = new_ttl;
229 }
230
231 static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
232 {
233 struct iphdr *nh;
234 int err;
235
236 err = make_writable(skb, skb_network_offset(skb) +
237 sizeof(struct iphdr));
238 if (unlikely(err))
239 return err;
240
241 nh = ip_hdr(skb);
242
243 if (ipv4_key->ipv4_src != nh->saddr)
244 set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
245
246 if (ipv4_key->ipv4_dst != nh->daddr)
247 set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
248
249 if (ipv4_key->ipv4_tos != nh->tos)
250 ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
251
252 if (ipv4_key->ipv4_ttl != nh->ttl)
253 set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
254
255 return 0;
256 }
257
258 static int set_ipv6(struct sk_buff *skb, const struct ovs_key_ipv6 *ipv6_key)
259 {
260 struct ipv6hdr *nh;
261 int err;
262 __be32 *saddr;
263 __be32 *daddr;
264
265 err = make_writable(skb, skb_network_offset(skb) +
266 sizeof(struct ipv6hdr));
267 if (unlikely(err))
268 return err;
269
270 nh = ipv6_hdr(skb);
271 saddr = (__be32 *)&nh->saddr;
272 daddr = (__be32 *)&nh->daddr;
273
274 if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src)))
275 set_ipv6_addr(skb, ipv6_key->ipv6_proto, saddr,
276 ipv6_key->ipv6_src, true);
277
278 if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) {
279 unsigned int offset = 0;
280 int flags = OVS_IP6T_FH_F_SKIP_RH;
281 bool recalc_csum = true;
282
283 if (ipv6_ext_hdr(nh->nexthdr))
284 recalc_csum = ipv6_find_hdr(skb, &offset,
285 NEXTHDR_ROUTING, NULL,
286 &flags) != NEXTHDR_ROUTING;
287
288 set_ipv6_addr(skb, ipv6_key->ipv6_proto, daddr,
289 ipv6_key->ipv6_dst, recalc_csum);
290 }
291
292 set_ipv6_tc(nh, ipv6_key->ipv6_tclass);
293 set_ipv6_fl(nh, ntohl(ipv6_key->ipv6_label));
294 nh->hop_limit = ipv6_key->ipv6_hlimit;
295
296 return 0;
297 }
298
299 /* Must follow make_writable() since that can move the skb data. */
300 static void set_tp_port(struct sk_buff *skb, __be16 *port,
301 __be16 new_port, __sum16 *check)
302 {
303 inet_proto_csum_replace2(check, skb, *port, new_port, 0);
304 *port = new_port;
305 skb_clear_rxhash(skb);
306 }
307
308 static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
309 {
310 struct udphdr *uh = udp_hdr(skb);
311
312 if (uh->check && get_ip_summed(skb) != OVS_CSUM_PARTIAL) {
313 set_tp_port(skb, port, new_port, &uh->check);
314
315 if (!uh->check)
316 uh->check = CSUM_MANGLED_0;
317 } else {
318 *port = new_port;
319 skb_clear_rxhash(skb);
320 }
321 }
322
323 static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key)
324 {
325 struct udphdr *uh;
326 int err;
327
328 err = make_writable(skb, skb_transport_offset(skb) +
329 sizeof(struct udphdr));
330 if (unlikely(err))
331 return err;
332
333 uh = udp_hdr(skb);
334 if (udp_port_key->udp_src != uh->source)
335 set_udp_port(skb, &uh->source, udp_port_key->udp_src);
336
337 if (udp_port_key->udp_dst != uh->dest)
338 set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
339
340 return 0;
341 }
342
343 static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key)
344 {
345 struct tcphdr *th;
346 int err;
347
348 err = make_writable(skb, skb_transport_offset(skb) +
349 sizeof(struct tcphdr));
350 if (unlikely(err))
351 return err;
352
353 th = tcp_hdr(skb);
354 if (tcp_port_key->tcp_src != th->source)
355 set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
356
357 if (tcp_port_key->tcp_dst != th->dest)
358 set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
359
360 return 0;
361 }
362
363 static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
364 {
365 struct vport *vport;
366
367 if (unlikely(!skb))
368 return -ENOMEM;
369
370 vport = ovs_vport_rcu(dp, out_port);
371 if (unlikely(!vport)) {
372 kfree_skb(skb);
373 return -ENODEV;
374 }
375
376 ovs_vport_send(vport, skb);
377 return 0;
378 }
379
380 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
381 const struct nlattr *attr)
382 {
383 struct dp_upcall_info upcall;
384 const struct nlattr *a;
385 int rem;
386
387 BUG_ON(!OVS_CB(skb)->pkt_key);
388
389 upcall.cmd = OVS_PACKET_CMD_ACTION;
390 upcall.key = OVS_CB(skb)->pkt_key;
391 upcall.userdata = NULL;
392 upcall.portid = 0;
393
394 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
395 a = nla_next(a, &rem)) {
396 switch (nla_type(a)) {
397 case OVS_USERSPACE_ATTR_USERDATA:
398 upcall.userdata = a;
399 break;
400
401 case OVS_USERSPACE_ATTR_PID:
402 upcall.portid = nla_get_u32(a);
403 break;
404 }
405 }
406
407 return ovs_dp_upcall(dp, skb, &upcall);
408 }
409
410 static int sample(struct datapath *dp, struct sk_buff *skb,
411 const struct nlattr *attr)
412 {
413 const struct nlattr *acts_list = NULL;
414 const struct nlattr *a;
415 int rem;
416
417 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
418 a = nla_next(a, &rem)) {
419 switch (nla_type(a)) {
420 case OVS_SAMPLE_ATTR_PROBABILITY:
421 if (net_random() >= nla_get_u32(a))
422 return 0;
423 break;
424
425 case OVS_SAMPLE_ATTR_ACTIONS:
426 acts_list = a;
427 break;
428 }
429 }
430
431 return do_execute_actions(dp, skb, nla_data(acts_list),
432 nla_len(acts_list), true);
433 }
434
435 static int execute_set_action(struct sk_buff *skb,
436 const struct nlattr *nested_attr)
437 {
438 int err = 0;
439
440 switch (nla_type(nested_attr)) {
441 case OVS_KEY_ATTR_PRIORITY:
442 skb->priority = nla_get_u32(nested_attr);
443 break;
444
445 case OVS_KEY_ATTR_SKB_MARK:
446 skb_set_mark(skb, nla_get_u32(nested_attr));
447 break;
448
449 case OVS_KEY_ATTR_IPV4_TUNNEL:
450 OVS_CB(skb)->tun_key = nla_data(nested_attr);
451 break;
452
453 case OVS_KEY_ATTR_ETHERNET:
454 err = set_eth_addr(skb, nla_data(nested_attr));
455 break;
456
457 case OVS_KEY_ATTR_IPV4:
458 err = set_ipv4(skb, nla_data(nested_attr));
459 break;
460
461 case OVS_KEY_ATTR_IPV6:
462 err = set_ipv6(skb, nla_data(nested_attr));
463 break;
464
465 case OVS_KEY_ATTR_TCP:
466 err = set_tcp(skb, nla_data(nested_attr));
467 break;
468
469 case OVS_KEY_ATTR_UDP:
470 err = set_udp(skb, nla_data(nested_attr));
471 break;
472 }
473
474 return err;
475 }
476
477 /* Execute a list of actions against 'skb'. */
478 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
479 const struct nlattr *attr, int len, bool keep_skb)
480 {
481 /* Every output action needs a separate clone of 'skb', but the common
482 * case is just a single output action, so that doing a clone and
483 * then freeing the original skbuff is wasteful. So the following code
484 * is slightly obscure just to avoid that. */
485 int prev_port = -1;
486 const struct nlattr *a;
487 int rem;
488
489 for (a = attr, rem = len; rem > 0;
490 a = nla_next(a, &rem)) {
491 int err = 0;
492
493 if (prev_port != -1) {
494 do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
495 prev_port = -1;
496 }
497
498 switch (nla_type(a)) {
499 case OVS_ACTION_ATTR_OUTPUT:
500 prev_port = nla_get_u32(a);
501 break;
502
503 case OVS_ACTION_ATTR_USERSPACE:
504 output_userspace(dp, skb, a);
505 break;
506
507 case OVS_ACTION_ATTR_PUSH_VLAN:
508 err = push_vlan(skb, nla_data(a));
509 if (unlikely(err)) /* skb already freed. */
510 return err;
511 break;
512
513 case OVS_ACTION_ATTR_POP_VLAN:
514 err = pop_vlan(skb);
515 break;
516
517 case OVS_ACTION_ATTR_SET:
518 err = execute_set_action(skb, nla_data(a));
519 break;
520
521 case OVS_ACTION_ATTR_SAMPLE:
522 err = sample(dp, skb, a);
523 break;
524 }
525
526 if (unlikely(err)) {
527 kfree_skb(skb);
528 return err;
529 }
530 }
531
532 if (prev_port != -1) {
533 if (keep_skb)
534 skb = skb_clone(skb, GFP_ATOMIC);
535
536 do_output(dp, skb, prev_port);
537 } else if (!keep_skb)
538 consume_skb(skb);
539
540 return 0;
541 }
542
543 /* We limit the number of times that we pass into execute_actions()
544 * to avoid blowing out the stack in the event that we have a loop. */
545 #define MAX_LOOPS 4
546
547 struct loop_counter {
548 u8 count; /* Count. */
549 bool looping; /* Loop detected? */
550 };
551
552 static DEFINE_PER_CPU(struct loop_counter, loop_counters);
553
554 static int loop_suppress(struct datapath *dp, struct sw_flow_actions *actions)
555 {
556 if (net_ratelimit())
557 pr_warn("%s: flow looped %d times, dropping\n",
558 ovs_dp_name(dp), MAX_LOOPS);
559 actions->actions_len = 0;
560 return -ELOOP;
561 }
562
563 /* Execute a list of actions against 'skb'. */
564 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb)
565 {
566 struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
567 struct loop_counter *loop;
568 int error;
569
570 /* Check whether we've looped too much. */
571 loop = &__get_cpu_var(loop_counters);
572 if (unlikely(++loop->count > MAX_LOOPS))
573 loop->looping = true;
574 if (unlikely(loop->looping)) {
575 error = loop_suppress(dp, acts);
576 kfree_skb(skb);
577 goto out_loop;
578 }
579
580 OVS_CB(skb)->tun_key = NULL;
581 error = do_execute_actions(dp, skb, acts->actions,
582 acts->actions_len, false);
583
584 /* Check whether sub-actions looped too much. */
585 if (unlikely(loop->looping))
586 error = loop_suppress(dp, acts);
587
588 out_loop:
589 /* Decrement loop counter. */
590 if (!--loop->count)
591 loop->looping = false;
592
593 return error;
594 }