]> git.proxmox.com Git - mirror_ovs.git/blob - datapath/actions.c
datapath: add recirc action
[mirror_ovs.git] / datapath / actions.c
1 /*
2 * Copyright (c) 2007-2014 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/sctp.h>
26 #include <linux/tcp.h>
27 #include <linux/udp.h>
28 #include <linux/in6.h>
29 #include <linux/if_arp.h>
30 #include <linux/if_vlan.h>
31 #include <net/ip.h>
32 #include <net/ipv6.h>
33 #include <net/checksum.h>
34 #include <net/dsfield.h>
35 #include <net/sctp/checksum.h>
36
37 #include "datapath.h"
38 #include "vlan.h"
39 #include "vport.h"
40
41 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
42 const struct nlattr *attr, int len, bool keep_skb);
43
44 static int make_writable(struct sk_buff *skb, int write_len)
45 {
46 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
47 return 0;
48
49 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
50 }
51
52 /* remove VLAN header from packet and update csum accordingly. */
53 static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
54 {
55 struct vlan_hdr *vhdr;
56 int err;
57
58 err = make_writable(skb, VLAN_ETH_HLEN);
59 if (unlikely(err))
60 return err;
61
62 if (skb->ip_summed == CHECKSUM_COMPLETE)
63 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
64 + (2 * ETH_ALEN), VLAN_HLEN, 0));
65
66 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
67 *current_tci = vhdr->h_vlan_TCI;
68
69 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
70 __skb_pull(skb, VLAN_HLEN);
71
72 vlan_set_encap_proto(skb, vhdr);
73 skb->mac_header += VLAN_HLEN;
74 skb_reset_mac_len(skb);
75
76 return 0;
77 }
78
79 static int pop_vlan(struct sk_buff *skb)
80 {
81 __be16 tci;
82 int err;
83
84 if (likely(vlan_tx_tag_present(skb))) {
85 vlan_set_tci(skb, 0);
86 } else {
87 if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
88 skb->len < VLAN_ETH_HLEN))
89 return 0;
90
91 err = __pop_vlan_tci(skb, &tci);
92 if (err)
93 return err;
94 }
95 /* move next vlan tag to hw accel tag */
96 if (likely(skb->protocol != htons(ETH_P_8021Q) ||
97 skb->len < VLAN_ETH_HLEN))
98 return 0;
99
100 err = __pop_vlan_tci(skb, &tci);
101 if (unlikely(err))
102 return err;
103
104 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(tci));
105 return 0;
106 }
107
108 static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan)
109 {
110 if (unlikely(vlan_tx_tag_present(skb))) {
111 u16 current_tag;
112
113 /* push down current VLAN tag */
114 current_tag = vlan_tx_tag_get(skb);
115
116 if (!__vlan_put_tag(skb, skb->vlan_proto, current_tag))
117 return -ENOMEM;
118
119 if (skb->ip_summed == CHECKSUM_COMPLETE)
120 skb->csum = csum_add(skb->csum, csum_partial(skb->data
121 + (2 * ETH_ALEN), VLAN_HLEN, 0));
122
123 }
124 __vlan_hwaccel_put_tag(skb, vlan->vlan_tpid, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
125 return 0;
126 }
127
128 static int set_eth_addr(struct sk_buff *skb,
129 const struct ovs_key_ethernet *eth_key)
130 {
131 int err;
132 err = make_writable(skb, ETH_HLEN);
133 if (unlikely(err))
134 return err;
135
136 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
137
138 ether_addr_copy(eth_hdr(skb)->h_source, eth_key->eth_src);
139 ether_addr_copy(eth_hdr(skb)->h_dest, eth_key->eth_dst);
140
141 ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
142
143 return 0;
144 }
145
146 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
147 __be32 *addr, __be32 new_addr)
148 {
149 int transport_len = skb->len - skb_transport_offset(skb);
150
151 if (nh->protocol == IPPROTO_TCP) {
152 if (likely(transport_len >= sizeof(struct tcphdr)))
153 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
154 *addr, new_addr, 1);
155 } else if (nh->protocol == IPPROTO_UDP) {
156 if (likely(transport_len >= sizeof(struct udphdr))) {
157 struct udphdr *uh = udp_hdr(skb);
158
159 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
160 inet_proto_csum_replace4(&uh->check, skb,
161 *addr, new_addr, 1);
162 if (!uh->check)
163 uh->check = CSUM_MANGLED_0;
164 }
165 }
166 }
167
168 csum_replace4(&nh->check, *addr, new_addr);
169 skb_clear_rxhash(skb);
170 *addr = new_addr;
171 }
172
173 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
174 __be32 addr[4], const __be32 new_addr[4])
175 {
176 int transport_len = skb->len - skb_transport_offset(skb);
177
178 if (l4_proto == IPPROTO_TCP) {
179 if (likely(transport_len >= sizeof(struct tcphdr)))
180 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
181 addr, new_addr, 1);
182 } else if (l4_proto == IPPROTO_UDP) {
183 if (likely(transport_len >= sizeof(struct udphdr))) {
184 struct udphdr *uh = udp_hdr(skb);
185
186 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
187 inet_proto_csum_replace16(&uh->check, skb,
188 addr, new_addr, 1);
189 if (!uh->check)
190 uh->check = CSUM_MANGLED_0;
191 }
192 }
193 }
194 }
195
196 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
197 __be32 addr[4], const __be32 new_addr[4],
198 bool recalculate_csum)
199 {
200 if (recalculate_csum)
201 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
202
203 skb_clear_rxhash(skb);
204 memcpy(addr, new_addr, sizeof(__be32[4]));
205 }
206
207 static void set_ipv6_tc(struct ipv6hdr *nh, u8 tc)
208 {
209 nh->priority = tc >> 4;
210 nh->flow_lbl[0] = (nh->flow_lbl[0] & 0x0F) | ((tc & 0x0F) << 4);
211 }
212
213 static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl)
214 {
215 nh->flow_lbl[0] = (nh->flow_lbl[0] & 0xF0) | (fl & 0x000F0000) >> 16;
216 nh->flow_lbl[1] = (fl & 0x0000FF00) >> 8;
217 nh->flow_lbl[2] = fl & 0x000000FF;
218 }
219
220 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
221 {
222 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
223 nh->ttl = new_ttl;
224 }
225
226 static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
227 {
228 struct iphdr *nh;
229 int err;
230
231 err = make_writable(skb, skb_network_offset(skb) +
232 sizeof(struct iphdr));
233 if (unlikely(err))
234 return err;
235
236 nh = ip_hdr(skb);
237
238 if (ipv4_key->ipv4_src != nh->saddr)
239 set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
240
241 if (ipv4_key->ipv4_dst != nh->daddr)
242 set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
243
244 if (ipv4_key->ipv4_tos != nh->tos)
245 ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
246
247 if (ipv4_key->ipv4_ttl != nh->ttl)
248 set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
249
250 return 0;
251 }
252
253 static int set_ipv6(struct sk_buff *skb, const struct ovs_key_ipv6 *ipv6_key)
254 {
255 struct ipv6hdr *nh;
256 int err;
257 __be32 *saddr;
258 __be32 *daddr;
259
260 err = make_writable(skb, skb_network_offset(skb) +
261 sizeof(struct ipv6hdr));
262 if (unlikely(err))
263 return err;
264
265 nh = ipv6_hdr(skb);
266 saddr = (__be32 *)&nh->saddr;
267 daddr = (__be32 *)&nh->daddr;
268
269 if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src)))
270 set_ipv6_addr(skb, ipv6_key->ipv6_proto, saddr,
271 ipv6_key->ipv6_src, true);
272
273 if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) {
274 unsigned int offset = 0;
275 int flags = OVS_IP6T_FH_F_SKIP_RH;
276 bool recalc_csum = true;
277
278 if (ipv6_ext_hdr(nh->nexthdr))
279 recalc_csum = ipv6_find_hdr(skb, &offset,
280 NEXTHDR_ROUTING, NULL,
281 &flags) != NEXTHDR_ROUTING;
282
283 set_ipv6_addr(skb, ipv6_key->ipv6_proto, daddr,
284 ipv6_key->ipv6_dst, recalc_csum);
285 }
286
287 set_ipv6_tc(nh, ipv6_key->ipv6_tclass);
288 set_ipv6_fl(nh, ntohl(ipv6_key->ipv6_label));
289 nh->hop_limit = ipv6_key->ipv6_hlimit;
290
291 return 0;
292 }
293
294 /* Must follow make_writable() since that can move the skb data. */
295 static void set_tp_port(struct sk_buff *skb, __be16 *port,
296 __be16 new_port, __sum16 *check)
297 {
298 inet_proto_csum_replace2(check, skb, *port, new_port, 0);
299 *port = new_port;
300 skb_clear_rxhash(skb);
301 }
302
303 static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
304 {
305 struct udphdr *uh = udp_hdr(skb);
306
307 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
308 set_tp_port(skb, port, new_port, &uh->check);
309
310 if (!uh->check)
311 uh->check = CSUM_MANGLED_0;
312 } else {
313 *port = new_port;
314 skb_clear_rxhash(skb);
315 }
316 }
317
318 static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key)
319 {
320 struct udphdr *uh;
321 int err;
322
323 err = make_writable(skb, skb_transport_offset(skb) +
324 sizeof(struct udphdr));
325 if (unlikely(err))
326 return err;
327
328 uh = udp_hdr(skb);
329 if (udp_port_key->udp_src != uh->source)
330 set_udp_port(skb, &uh->source, udp_port_key->udp_src);
331
332 if (udp_port_key->udp_dst != uh->dest)
333 set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
334
335 return 0;
336 }
337
338 static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key)
339 {
340 struct tcphdr *th;
341 int err;
342
343 err = make_writable(skb, skb_transport_offset(skb) +
344 sizeof(struct tcphdr));
345 if (unlikely(err))
346 return err;
347
348 th = tcp_hdr(skb);
349 if (tcp_port_key->tcp_src != th->source)
350 set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
351
352 if (tcp_port_key->tcp_dst != th->dest)
353 set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
354
355 return 0;
356 }
357
358 static int set_sctp(struct sk_buff *skb,
359 const struct ovs_key_sctp *sctp_port_key)
360 {
361 struct sctphdr *sh;
362 int err;
363 unsigned int sctphoff = skb_transport_offset(skb);
364
365 err = make_writable(skb, sctphoff + sizeof(struct sctphdr));
366 if (unlikely(err))
367 return err;
368
369 sh = sctp_hdr(skb);
370 if (sctp_port_key->sctp_src != sh->source ||
371 sctp_port_key->sctp_dst != sh->dest) {
372 __le32 old_correct_csum, new_csum, old_csum;
373
374 old_csum = sh->checksum;
375 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
376
377 sh->source = sctp_port_key->sctp_src;
378 sh->dest = sctp_port_key->sctp_dst;
379
380 new_csum = sctp_compute_cksum(skb, sctphoff);
381
382 /* Carry any checksum errors through. */
383 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
384
385 skb_clear_rxhash(skb);
386 }
387
388 return 0;
389 }
390
391 static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
392 {
393 struct vport *vport;
394
395 if (unlikely(!skb))
396 return -ENOMEM;
397
398 vport = ovs_vport_rcu(dp, out_port);
399 if (unlikely(!vport)) {
400 kfree_skb(skb);
401 return -ENODEV;
402 }
403
404 ovs_vport_send(vport, skb);
405 return 0;
406 }
407
408 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
409 const struct nlattr *attr)
410 {
411 struct dp_upcall_info upcall;
412 const struct nlattr *a;
413 int rem;
414
415 BUG_ON(!OVS_CB(skb)->pkt_key);
416
417 upcall.cmd = OVS_PACKET_CMD_ACTION;
418 upcall.key = OVS_CB(skb)->pkt_key;
419 upcall.userdata = NULL;
420 upcall.portid = 0;
421
422 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
423 a = nla_next(a, &rem)) {
424 switch (nla_type(a)) {
425 case OVS_USERSPACE_ATTR_USERDATA:
426 upcall.userdata = a;
427 break;
428
429 case OVS_USERSPACE_ATTR_PID:
430 upcall.portid = nla_get_u32(a);
431 break;
432 }
433 }
434
435 return ovs_dp_upcall(dp, skb, &upcall);
436 }
437
438 static int sample(struct datapath *dp, struct sk_buff *skb,
439 const struct nlattr *attr)
440 {
441 const struct nlattr *acts_list = NULL;
442 const struct nlattr *a;
443 int rem;
444
445 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
446 a = nla_next(a, &rem)) {
447 switch (nla_type(a)) {
448 case OVS_SAMPLE_ATTR_PROBABILITY:
449 if (net_random() >= nla_get_u32(a))
450 return 0;
451 break;
452
453 case OVS_SAMPLE_ATTR_ACTIONS:
454 acts_list = a;
455 break;
456 }
457 }
458
459 return do_execute_actions(dp, skb, nla_data(acts_list),
460 nla_len(acts_list), true);
461 }
462
463 static void execute_hash(struct sk_buff *skb, const struct nlattr *attr)
464 {
465 struct sw_flow_key *key = OVS_CB(skb)->pkt_key;
466 struct ovs_action_hash *hash_act = nla_data(attr);
467 u32 hash = 0;
468
469 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
470 hash = skb_get_rxhash(skb);
471 hash = jhash_1word(hash, hash_act->hash_basis);
472 if (!hash)
473 hash = 0x1;
474
475 key->ovs_flow_hash = hash;
476 }
477
478 static int execute_set_action(struct sk_buff *skb,
479 const struct nlattr *nested_attr)
480 {
481 int err = 0;
482
483 switch (nla_type(nested_attr)) {
484 case OVS_KEY_ATTR_PRIORITY:
485 skb->priority = nla_get_u32(nested_attr);
486 break;
487
488 case OVS_KEY_ATTR_SKB_MARK:
489 skb->mark = nla_get_u32(nested_attr);
490 break;
491
492 case OVS_KEY_ATTR_IPV4_TUNNEL:
493 OVS_CB(skb)->tun_key = nla_data(nested_attr);
494 break;
495
496 case OVS_KEY_ATTR_ETHERNET:
497 err = set_eth_addr(skb, nla_data(nested_attr));
498 break;
499
500 case OVS_KEY_ATTR_IPV4:
501 err = set_ipv4(skb, nla_data(nested_attr));
502 break;
503
504 case OVS_KEY_ATTR_IPV6:
505 err = set_ipv6(skb, nla_data(nested_attr));
506 break;
507
508 case OVS_KEY_ATTR_TCP:
509 err = set_tcp(skb, nla_data(nested_attr));
510 break;
511
512 case OVS_KEY_ATTR_UDP:
513 err = set_udp(skb, nla_data(nested_attr));
514 break;
515
516 case OVS_KEY_ATTR_SCTP:
517 err = set_sctp(skb, nla_data(nested_attr));
518 break;
519 }
520
521 return err;
522 }
523
524 static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
525 const struct nlattr *a)
526 {
527 struct sw_flow_key recirc_key;
528 const struct vport *p = OVS_CB(skb)->input_vport;
529 uint32_t hash = OVS_CB(skb)->pkt_key->ovs_flow_hash;
530 int err;
531
532 err = ovs_flow_extract(skb, p->port_no, &recirc_key);
533 if (err)
534 return err;
535
536 recirc_key.ovs_flow_hash = hash;
537 recirc_key.recirc_id = nla_get_u32(a);
538
539 ovs_dp_process_packet_with_key(skb, &recirc_key);
540
541 return 0;
542 }
543
544 /* Execute a list of actions against 'skb'. */
545 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
546 const struct nlattr *attr, int len, bool keep_skb)
547 {
548 /* Every output action needs a separate clone of 'skb', but the common
549 * case is just a single output action, so that doing a clone and
550 * then freeing the original skbuff is wasteful. So the following code
551 * is slightly obscure just to avoid that. */
552 int prev_port = -1;
553 const struct nlattr *a;
554 int rem;
555
556 for (a = attr, rem = len; rem > 0;
557 a = nla_next(a, &rem)) {
558 int err = 0;
559
560 if (prev_port != -1) {
561 do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
562 prev_port = -1;
563 }
564
565 switch (nla_type(a)) {
566 case OVS_ACTION_ATTR_OUTPUT:
567 prev_port = nla_get_u32(a);
568 break;
569
570 case OVS_ACTION_ATTR_USERSPACE:
571 output_userspace(dp, skb, a);
572 break;
573
574 case OVS_ACTION_ATTR_HASH:
575 execute_hash(skb, a);
576 break;
577
578 case OVS_ACTION_ATTR_PUSH_VLAN:
579 err = push_vlan(skb, nla_data(a));
580 if (unlikely(err)) /* skb already freed. */
581 return err;
582 break;
583
584 case OVS_ACTION_ATTR_POP_VLAN:
585 err = pop_vlan(skb);
586 break;
587
588 case OVS_ACTION_ATTR_RECIRC: {
589 struct sk_buff *recirc_skb;
590 const bool last_action = (a->nla_len == rem);
591
592 if (!last_action || keep_skb)
593 recirc_skb = skb_clone(skb, GFP_ATOMIC);
594 else
595 recirc_skb = skb;
596
597 err = execute_recirc(dp, recirc_skb, a);
598
599 if (last_action || err)
600 return err;
601
602 break;
603 }
604
605 case OVS_ACTION_ATTR_SET:
606 err = execute_set_action(skb, nla_data(a));
607 break;
608
609 case OVS_ACTION_ATTR_SAMPLE:
610 err = sample(dp, skb, a);
611 if (unlikely(err)) /* skb already freed. */
612 return err;
613 break;
614 }
615
616 if (unlikely(err)) {
617 kfree_skb(skb);
618 return err;
619 }
620 }
621
622 if (prev_port != -1) {
623 if (keep_skb)
624 skb = skb_clone(skb, GFP_ATOMIC);
625
626 do_output(dp, skb, prev_port);
627 } else if (!keep_skb)
628 consume_skb(skb);
629
630 return 0;
631 }
632
633 /* We limit the number of times that we pass into execute_actions()
634 * to avoid blowing out the stack in the event that we have a loop. */
635 #define MAX_LOOPS 4
636
637 struct loop_counter {
638 u8 count; /* Count. */
639 bool looping; /* Loop detected? */
640 };
641
642 static DEFINE_PER_CPU(struct loop_counter, loop_counters);
643
644 static int loop_suppress(struct datapath *dp, struct sw_flow_actions *actions)
645 {
646 if (net_ratelimit())
647 pr_warn("%s: flow looped %d times, dropping\n",
648 ovs_dp_name(dp), MAX_LOOPS);
649 actions->actions_len = 0;
650 return -ELOOP;
651 }
652
653 /* Execute a list of actions against 'skb'. */
654 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb)
655 {
656 struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
657 struct loop_counter *loop;
658 int error;
659
660 /* Check whether we've looped too much. */
661 loop = &__get_cpu_var(loop_counters);
662 if (unlikely(++loop->count > MAX_LOOPS))
663 loop->looping = true;
664 if (unlikely(loop->looping)) {
665 error = loop_suppress(dp, acts);
666 kfree_skb(skb);
667 goto out_loop;
668 }
669
670 OVS_CB(skb)->tun_key = NULL;
671 error = do_execute_actions(dp, skb, acts->actions,
672 acts->actions_len, false);
673
674 /* Check whether sub-actions looped too much. */
675 if (unlikely(loop->looping))
676 error = loop_suppress(dp, acts);
677
678 out_loop:
679 /* Decrement loop counter. */
680 if (!--loop->count)
681 loop->looping = false;
682
683 return error;
684 }