]> git.proxmox.com Git - mirror_ovs.git/blob - datapath/actions.c
datapath: Include datapath actions with sampled-packet upcall to userspace.
[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/mpls.h>
36 #include <net/sctp/checksum.h>
37
38 #include "datapath.h"
39 #include "gso.h"
40 #include "vlan.h"
41 #include "vport.h"
42
43 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
44 struct sw_flow_key *key,
45 const struct nlattr *attr, int len);
46
47 struct deferred_action {
48 struct sk_buff *skb;
49 const struct nlattr *actions;
50
51 /* Store pkt_key clone when creating deferred action. */
52 struct sw_flow_key pkt_key;
53 };
54
55 #define DEFERRED_ACTION_FIFO_SIZE 10
56 struct action_fifo {
57 int head;
58 int tail;
59 /* Deferred action fifo queue storage. */
60 struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
61 };
62
63 static struct action_fifo __percpu *action_fifos;
64 #define EXEC_ACTIONS_LEVEL_LIMIT 4 /* limit used to detect packet
65 * looping by the network stack
66 */
67 static DEFINE_PER_CPU(int, exec_actions_level);
68
69 static void action_fifo_init(struct action_fifo *fifo)
70 {
71 fifo->head = 0;
72 fifo->tail = 0;
73 }
74
75 static bool action_fifo_is_empty(const struct action_fifo *fifo)
76 {
77 return (fifo->head == fifo->tail);
78 }
79
80 static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
81 {
82 if (action_fifo_is_empty(fifo))
83 return NULL;
84
85 return &fifo->fifo[fifo->tail++];
86 }
87
88 static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
89 {
90 if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
91 return NULL;
92
93 return &fifo->fifo[fifo->head++];
94 }
95
96 /* Return queue entry if fifo is not full */
97 static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
98 const struct sw_flow_key *key,
99 const struct nlattr *attr)
100 {
101 struct action_fifo *fifo;
102 struct deferred_action *da;
103
104 fifo = this_cpu_ptr(action_fifos);
105 da = action_fifo_put(fifo);
106 if (da) {
107 da->skb = skb;
108 da->actions = attr;
109 da->pkt_key = *key;
110 }
111
112 return da;
113 }
114
115 static void invalidate_flow_key(struct sw_flow_key *key)
116 {
117 key->eth.type = htons(0);
118 }
119
120 static bool is_flow_key_valid(const struct sw_flow_key *key)
121 {
122 return !!key->eth.type;
123 }
124
125 static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
126 const struct ovs_action_push_mpls *mpls)
127 {
128 __be32 *new_mpls_lse;
129 struct ethhdr *hdr;
130
131 /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
132 if (skb_encapsulation(skb))
133 return -ENOTSUPP;
134
135 if (skb_cow_head(skb, MPLS_HLEN) < 0)
136 return -ENOMEM;
137
138 skb_push(skb, MPLS_HLEN);
139 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
140 skb->mac_len);
141 skb_reset_mac_header(skb);
142
143 new_mpls_lse = (__be32 *)skb_mpls_header(skb);
144 *new_mpls_lse = mpls->mpls_lse;
145
146 if (skb->ip_summed == CHECKSUM_COMPLETE)
147 skb->csum = csum_add(skb->csum, csum_partial(new_mpls_lse,
148 MPLS_HLEN, 0));
149
150 hdr = eth_hdr(skb);
151 hdr->h_proto = mpls->mpls_ethertype;
152 if (!ovs_skb_get_inner_protocol(skb))
153 ovs_skb_set_inner_protocol(skb, skb->protocol);
154 skb->protocol = mpls->mpls_ethertype;
155
156 invalidate_flow_key(key);
157 return 0;
158 }
159
160 static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
161 const __be16 ethertype)
162 {
163 struct ethhdr *hdr;
164 int err;
165
166 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
167 if (unlikely(err))
168 return err;
169
170 if (skb->ip_summed == CHECKSUM_COMPLETE)
171 skb->csum = csum_sub(skb->csum,
172 csum_partial(skb_mpls_header(skb),
173 MPLS_HLEN, 0));
174
175 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
176 skb->mac_len);
177
178 __skb_pull(skb, MPLS_HLEN);
179 skb_reset_mac_header(skb);
180
181 /* skb_mpls_header() is used to locate the ethertype
182 * field correctly in the presence of VLAN tags.
183 */
184 hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
185 hdr->h_proto = ethertype;
186 if (eth_p_mpls(skb->protocol))
187 skb->protocol = ethertype;
188
189 invalidate_flow_key(key);
190 return 0;
191 }
192
193 /* 'KEY' must not have any bits set outside of the 'MASK' */
194 #define MASKED(OLD, KEY, MASK) ((KEY) | ((OLD) & ~(MASK)))
195 #define SET_MASKED(OLD, KEY, MASK) ((OLD) = MASKED(OLD, KEY, MASK))
196
197 static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
198 const __be32 *mpls_lse, const __be32 *mask)
199 {
200 __be32 *stack;
201 __be32 lse;
202 int err;
203
204 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
205 if (unlikely(err))
206 return err;
207
208 stack = (__be32 *)skb_mpls_header(skb);
209 lse = MASKED(*stack, *mpls_lse, *mask);
210 if (skb->ip_summed == CHECKSUM_COMPLETE) {
211 __be32 diff[] = { ~(*stack), lse };
212
213 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
214 ~skb->csum);
215 }
216
217 *stack = lse;
218 flow_key->mpls.top_lse = lse;
219 return 0;
220 }
221
222 static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
223 {
224 int err;
225
226 err = skb_vlan_pop(skb);
227 if (skb_vlan_tag_present(skb))
228 invalidate_flow_key(key);
229 else
230 key->eth.tci = 0;
231
232 return err;
233 }
234
235 static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
236 const struct ovs_action_push_vlan *vlan)
237 {
238 if (skb_vlan_tag_present(skb))
239 invalidate_flow_key(key);
240 else
241 key->eth.tci = vlan->vlan_tci;
242
243 return skb_vlan_push(skb, vlan->vlan_tpid,
244 ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
245 }
246
247 /* 'src' is already properly masked. */
248 static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
249 {
250 u16 *dst = (u16 *)dst_;
251 const u16 *src = (const u16 *)src_;
252 const u16 *mask = (const u16 *)mask_;
253
254 SET_MASKED(dst[0], src[0], mask[0]);
255 SET_MASKED(dst[1], src[1], mask[1]);
256 SET_MASKED(dst[2], src[2], mask[2]);
257 }
258
259 static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
260 const struct ovs_key_ethernet *key,
261 const struct ovs_key_ethernet *mask)
262 {
263 int err;
264
265 err = skb_ensure_writable(skb, ETH_HLEN);
266 if (unlikely(err))
267 return err;
268
269 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
270
271 ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
272 mask->eth_src);
273 ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
274 mask->eth_dst);
275
276 ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
277
278 ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
279 ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
280 return 0;
281 }
282
283 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
284 __be32 *addr, __be32 new_addr)
285 {
286 int transport_len = skb->len - skb_transport_offset(skb);
287
288 if (nh->protocol == IPPROTO_TCP) {
289 if (likely(transport_len >= sizeof(struct tcphdr)))
290 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
291 *addr, new_addr, 1);
292 } else if (nh->protocol == IPPROTO_UDP) {
293 if (likely(transport_len >= sizeof(struct udphdr))) {
294 struct udphdr *uh = udp_hdr(skb);
295
296 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
297 inet_proto_csum_replace4(&uh->check, skb,
298 *addr, new_addr, 1);
299 if (!uh->check)
300 uh->check = CSUM_MANGLED_0;
301 }
302 }
303 }
304
305 csum_replace4(&nh->check, *addr, new_addr);
306 skb_clear_hash(skb);
307 *addr = new_addr;
308 }
309
310 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
311 __be32 addr[4], const __be32 new_addr[4])
312 {
313 int transport_len = skb->len - skb_transport_offset(skb);
314
315 if (l4_proto == NEXTHDR_TCP) {
316 if (likely(transport_len >= sizeof(struct tcphdr)))
317 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
318 addr, new_addr, 1);
319 } else if (l4_proto == NEXTHDR_UDP) {
320 if (likely(transport_len >= sizeof(struct udphdr))) {
321 struct udphdr *uh = udp_hdr(skb);
322
323 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
324 inet_proto_csum_replace16(&uh->check, skb,
325 addr, new_addr, 1);
326 if (!uh->check)
327 uh->check = CSUM_MANGLED_0;
328 }
329 }
330 } else if (l4_proto == NEXTHDR_ICMP) {
331 if (likely(transport_len >= sizeof(struct icmp6hdr)))
332 inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
333 skb, addr, new_addr, 1);
334 }
335 }
336
337 static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
338 const __be32 mask[4], __be32 masked[4])
339 {
340 masked[0] = MASKED(old[0], addr[0], mask[0]);
341 masked[1] = MASKED(old[1], addr[1], mask[1]);
342 masked[2] = MASKED(old[2], addr[2], mask[2]);
343 masked[3] = MASKED(old[3], addr[3], mask[3]);
344 }
345
346 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
347 __be32 addr[4], const __be32 new_addr[4],
348 bool recalculate_csum)
349 {
350 if (likely(recalculate_csum))
351 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
352
353 skb_clear_hash(skb);
354 memcpy(addr, new_addr, sizeof(__be32[4]));
355 }
356
357 static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask)
358 {
359 /* Bits 21-24 are always unmasked, so this retains their values. */
360 SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16));
361 SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8));
362 SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask);
363 }
364
365 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
366 u8 mask)
367 {
368 new_ttl = MASKED(nh->ttl, new_ttl, mask);
369
370 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
371 nh->ttl = new_ttl;
372 }
373
374 static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
375 const struct ovs_key_ipv4 *key,
376 const struct ovs_key_ipv4 *mask)
377 {
378 struct iphdr *nh;
379 __be32 new_addr;
380 int err;
381
382 err = skb_ensure_writable(skb, skb_network_offset(skb) +
383 sizeof(struct iphdr));
384 if (unlikely(err))
385 return err;
386
387 nh = ip_hdr(skb);
388
389 /* Setting an IP addresses is typically only a side effect of
390 * matching on them in the current userspace implementation, so it
391 * makes sense to check if the value actually changed.
392 */
393 if (mask->ipv4_src) {
394 new_addr = MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
395
396 if (unlikely(new_addr != nh->saddr)) {
397 set_ip_addr(skb, nh, &nh->saddr, new_addr);
398 flow_key->ipv4.addr.src = new_addr;
399 }
400 }
401 if (mask->ipv4_dst) {
402 new_addr = MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
403
404 if (unlikely(new_addr != nh->daddr)) {
405 set_ip_addr(skb, nh, &nh->daddr, new_addr);
406 flow_key->ipv4.addr.dst = new_addr;
407 }
408 }
409 if (mask->ipv4_tos) {
410 ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
411 flow_key->ip.tos = nh->tos;
412 }
413 if (mask->ipv4_ttl) {
414 set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
415 flow_key->ip.ttl = nh->ttl;
416 }
417
418 return 0;
419 }
420
421 static bool is_ipv6_mask_nonzero(const __be32 addr[4])
422 {
423 return !!(addr[0] | addr[1] | addr[2] | addr[3]);
424 }
425
426 static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
427 const struct ovs_key_ipv6 *key,
428 const struct ovs_key_ipv6 *mask)
429 {
430 struct ipv6hdr *nh;
431 int err;
432
433 err = skb_ensure_writable(skb, skb_network_offset(skb) +
434 sizeof(struct ipv6hdr));
435 if (unlikely(err))
436 return err;
437
438 nh = ipv6_hdr(skb);
439
440 /* Setting an IP addresses is typically only a side effect of
441 * matching on them in the current userspace implementation, so it
442 * makes sense to check if the value actually changed.
443 */
444 if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
445 __be32 *saddr = (__be32 *)&nh->saddr;
446 __be32 masked[4];
447
448 mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
449
450 if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
451 set_ipv6_addr(skb, key->ipv6_proto, saddr, masked,
452 true);
453 memcpy(&flow_key->ipv6.addr.src, masked,
454 sizeof(flow_key->ipv6.addr.src));
455 }
456 }
457 if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
458 unsigned int offset = 0;
459 int flags = IP6_FH_F_SKIP_RH;
460 bool recalc_csum = true;
461 __be32 *daddr = (__be32 *)&nh->daddr;
462 __be32 masked[4];
463
464 mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
465
466 if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
467 if (ipv6_ext_hdr(nh->nexthdr))
468 recalc_csum = (ipv6_find_hdr(skb, &offset,
469 NEXTHDR_ROUTING,
470 NULL, &flags)
471 != NEXTHDR_ROUTING);
472
473 set_ipv6_addr(skb, key->ipv6_proto, daddr, masked,
474 recalc_csum);
475 memcpy(&flow_key->ipv6.addr.dst, masked,
476 sizeof(flow_key->ipv6.addr.dst));
477 }
478 }
479 if (mask->ipv6_tclass) {
480 ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass);
481 flow_key->ip.tos = ipv6_get_dsfield(nh);
482 }
483 if (mask->ipv6_label) {
484 set_ipv6_fl(nh, ntohl(key->ipv6_label),
485 ntohl(mask->ipv6_label));
486 flow_key->ipv6.label =
487 *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
488 }
489 if (mask->ipv6_hlimit) {
490 SET_MASKED(nh->hop_limit, key->ipv6_hlimit, mask->ipv6_hlimit);
491 flow_key->ip.ttl = nh->hop_limit;
492 }
493 return 0;
494 }
495
496 /* Must follow skb_ensure_writable() since that can move the skb data. */
497 static void set_tp_port(struct sk_buff *skb, __be16 *port,
498 __be16 new_port, __sum16 *check)
499 {
500 inet_proto_csum_replace2(check, skb, *port, new_port, 0);
501 *port = new_port;
502 }
503
504 static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
505 const struct ovs_key_udp *key,
506 const struct ovs_key_udp *mask)
507 {
508 struct udphdr *uh;
509 __be16 src, dst;
510 int err;
511
512 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
513 sizeof(struct udphdr));
514 if (unlikely(err))
515 return err;
516
517 uh = udp_hdr(skb);
518 /* Either of the masks is non-zero, so do not bother checking them. */
519 src = MASKED(uh->source, key->udp_src, mask->udp_src);
520 dst = MASKED(uh->dest, key->udp_dst, mask->udp_dst);
521
522 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
523 if (likely(src != uh->source)) {
524 set_tp_port(skb, &uh->source, src, &uh->check);
525 flow_key->tp.src = src;
526 }
527 if (likely(dst != uh->dest)) {
528 set_tp_port(skb, &uh->dest, dst, &uh->check);
529 flow_key->tp.dst = dst;
530 }
531
532 if (unlikely(!uh->check))
533 uh->check = CSUM_MANGLED_0;
534 } else {
535 uh->source = src;
536 uh->dest = dst;
537 flow_key->tp.src = src;
538 flow_key->tp.dst = dst;
539 }
540
541 skb_clear_hash(skb);
542
543 return 0;
544 }
545
546 static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
547 const struct ovs_key_tcp *key,
548 const struct ovs_key_tcp *mask)
549 {
550 struct tcphdr *th;
551 __be16 src, dst;
552 int err;
553
554 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
555 sizeof(struct tcphdr));
556 if (unlikely(err))
557 return err;
558
559 th = tcp_hdr(skb);
560
561 src = MASKED(th->source, key->tcp_src, mask->tcp_src);
562 if (likely(src != th->source)) {
563 set_tp_port(skb, &th->source, src, &th->check);
564 flow_key->tp.src = src;
565 }
566 dst = MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
567 if (likely(dst != th->dest)) {
568 set_tp_port(skb, &th->dest, dst, &th->check);
569 flow_key->tp.dst = dst;
570 }
571 skb_clear_hash(skb);
572
573 return 0;
574 }
575
576 static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
577 const struct ovs_key_sctp *key,
578 const struct ovs_key_sctp *mask)
579 {
580 unsigned int sctphoff = skb_transport_offset(skb);
581 struct sctphdr *sh;
582 __le32 old_correct_csum, new_csum, old_csum;
583 int err;
584
585 err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
586 if (unlikely(err))
587 return err;
588
589 sh = sctp_hdr(skb);
590
591 old_csum = sh->checksum;
592 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
593
594 sh->source = MASKED(sh->source, key->sctp_src, mask->sctp_src);
595 sh->dest = MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
596
597 new_csum = sctp_compute_cksum(skb, sctphoff);
598
599 /* Carry any checksum errors through. */
600 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
601
602 skb_clear_hash(skb);
603 flow_key->tp.src = sh->source;
604 flow_key->tp.dst = sh->dest;
605
606 return 0;
607 }
608
609 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
610 {
611 struct vport *vport = ovs_vport_rcu(dp, out_port);
612
613 if (likely(vport))
614 ovs_vport_send(vport, skb);
615 else
616 kfree_skb(skb);
617 }
618
619 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
620 struct sw_flow_key *key, const struct nlattr *attr,
621 const struct nlattr *actions, int actions_len)
622 {
623 struct ovs_tunnel_info info;
624 struct dp_upcall_info upcall;
625 const struct nlattr *a;
626 int rem;
627
628 memset(&upcall, 0, sizeof(upcall));
629 upcall.cmd = OVS_PACKET_CMD_ACTION;
630
631 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
632 a = nla_next(a, &rem)) {
633 switch (nla_type(a)) {
634 case OVS_USERSPACE_ATTR_USERDATA:
635 upcall.userdata = a;
636 break;
637
638 case OVS_USERSPACE_ATTR_PID:
639 upcall.portid = nla_get_u32(a);
640 break;
641
642 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
643 /* Get out tunnel info. */
644 struct vport *vport;
645
646 vport = ovs_vport_rcu(dp, nla_get_u32(a));
647 if (vport) {
648 int err;
649
650 err = ovs_vport_get_egress_tun_info(vport, skb,
651 &info);
652 if (!err)
653 upcall.egress_tun_info = &info;
654 }
655 break;
656 }
657
658 case OVS_USERSPACE_ATTR_ACTIONS: {
659 /* Include actions. */
660 upcall.actions = actions;
661 upcall.actions_len = actions_len;
662 break;
663 }
664
665 } /* End of switch. */
666 }
667
668 return ovs_dp_upcall(dp, skb, key, &upcall);
669 }
670
671 static int sample(struct datapath *dp, struct sk_buff *skb,
672 struct sw_flow_key *key, const struct nlattr *attr,
673 const struct nlattr *actions, int actions_len)
674 {
675 const struct nlattr *acts_list = NULL;
676 const struct nlattr *a;
677 int rem;
678
679 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
680 a = nla_next(a, &rem)) {
681 switch (nla_type(a)) {
682 case OVS_SAMPLE_ATTR_PROBABILITY:
683 if (prandom_u32() >= nla_get_u32(a))
684 return 0;
685 break;
686
687 case OVS_SAMPLE_ATTR_ACTIONS:
688 acts_list = a;
689 break;
690 }
691 }
692
693 rem = nla_len(acts_list);
694 a = nla_data(acts_list);
695
696 /* Actions list is empty, do nothing */
697 if (unlikely(!rem))
698 return 0;
699
700 /* The only known usage of sample action is having a single user-space
701 * action. Treat this usage as a special case.
702 * The output_userspace() should clone the skb to be sent to the
703 * user space. This skb will be consumed by its caller.
704 */
705 if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
706 nla_is_last(a, rem)))
707 return output_userspace(dp, skb, key, a, actions, actions_len);
708
709 skb = skb_clone(skb, GFP_ATOMIC);
710 if (!skb)
711 /* Skip the sample action when out of memory. */
712 return 0;
713
714 if (!add_deferred_actions(skb, key, a)) {
715 if (net_ratelimit())
716 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
717 ovs_dp_name(dp));
718
719 kfree_skb(skb);
720 }
721 return 0;
722 }
723
724 static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
725 const struct nlattr *attr)
726 {
727 struct ovs_action_hash *hash_act = nla_data(attr);
728 u32 hash = 0;
729
730 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
731 hash = skb_get_hash(skb);
732 hash = jhash_1word(hash, hash_act->hash_basis);
733 if (!hash)
734 hash = 0x1;
735
736 key->ovs_flow_hash = hash;
737 }
738
739 static int execute_set_action(struct sk_buff *skb,
740 struct sw_flow_key *flow_key,
741 const struct nlattr *a)
742 {
743 /* Only tunnel set execution is supported without a mask. */
744 if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
745 OVS_CB(skb)->egress_tun_info = nla_data(a);
746 return 0;
747 }
748
749 return -EINVAL;
750
751 }
752
753 /* Mask is at the midpoint of the data. */
754 #define get_mask(a, type) ((const type)nla_data(a) + 1)
755
756 static int execute_masked_set_action(struct sk_buff *skb,
757 struct sw_flow_key *flow_key,
758 const struct nlattr *a)
759 {
760 int err = 0;
761
762 switch (nla_type(a)) {
763 case OVS_KEY_ATTR_PRIORITY:
764 SET_MASKED(skb->priority, nla_get_u32(a), *get_mask(a, u32 *));
765 flow_key->phy.priority = skb->priority;
766 break;
767
768 case OVS_KEY_ATTR_SKB_MARK:
769 SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
770 flow_key->phy.skb_mark = skb->mark;
771 break;
772
773 case OVS_KEY_ATTR_TUNNEL_INFO:
774 /* Masked data not supported for tunnel. */
775 err = -EINVAL;
776 break;
777
778 case OVS_KEY_ATTR_ETHERNET:
779 err = set_eth_addr(skb, flow_key, nla_data(a),
780 get_mask(a, struct ovs_key_ethernet *));
781 break;
782
783 case OVS_KEY_ATTR_IPV4:
784 err = set_ipv4(skb, flow_key, nla_data(a),
785 get_mask(a, struct ovs_key_ipv4 *));
786 break;
787
788 case OVS_KEY_ATTR_IPV6:
789 err = set_ipv6(skb, flow_key, nla_data(a),
790 get_mask(a, struct ovs_key_ipv6 *));
791 break;
792
793 case OVS_KEY_ATTR_TCP:
794 err = set_tcp(skb, flow_key, nla_data(a),
795 get_mask(a, struct ovs_key_tcp *));
796 break;
797
798 case OVS_KEY_ATTR_UDP:
799 err = set_udp(skb, flow_key, nla_data(a),
800 get_mask(a, struct ovs_key_udp *));
801 break;
802
803 case OVS_KEY_ATTR_SCTP:
804 err = set_sctp(skb, flow_key, nla_data(a),
805 get_mask(a, struct ovs_key_sctp *));
806 break;
807
808 case OVS_KEY_ATTR_MPLS:
809 err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
810 __be32 *));
811 break;
812 }
813
814 return err;
815 }
816
817 static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
818 struct sw_flow_key *key,
819 const struct nlattr *a, int rem)
820 {
821 struct deferred_action *da;
822
823 if (!is_flow_key_valid(key)) {
824 int err;
825
826 err = ovs_flow_key_update(skb, key);
827 if (err)
828 return err;
829 }
830 BUG_ON(!is_flow_key_valid(key));
831
832 if (!nla_is_last(a, rem)) {
833 /* Recirc action is the not the last action
834 * of the action list, need to clone the skb.
835 */
836 skb = skb_clone(skb, GFP_ATOMIC);
837
838 /* Skip the recirc action when out of memory, but
839 * continue on with the rest of the action list.
840 */
841 if (!skb)
842 return 0;
843 }
844
845 da = add_deferred_actions(skb, key, NULL);
846 if (da) {
847 da->pkt_key.recirc_id = nla_get_u32(a);
848 } else {
849 kfree_skb(skb);
850
851 if (net_ratelimit())
852 pr_warn("%s: deferred action limit reached, drop recirc action\n",
853 ovs_dp_name(dp));
854 }
855
856 return 0;
857 }
858
859 /* Execute a list of actions against 'skb'. */
860 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
861 struct sw_flow_key *key,
862 const struct nlattr *attr, int len)
863 {
864 /* Every output action needs a separate clone of 'skb', but the common
865 * case is just a single output action, so that doing a clone and
866 * then freeing the original skbuff is wasteful. So the following code
867 * is slightly obscure just to avoid that.
868 */
869 int prev_port = -1;
870 const struct nlattr *a;
871 int rem;
872
873 for (a = attr, rem = len; rem > 0;
874 a = nla_next(a, &rem)) {
875 int err = 0;
876
877 if (unlikely(prev_port != -1)) {
878 struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
879
880 if (out_skb)
881 do_output(dp, out_skb, prev_port);
882
883 prev_port = -1;
884 }
885
886 switch (nla_type(a)) {
887 case OVS_ACTION_ATTR_OUTPUT:
888 prev_port = nla_get_u32(a);
889 break;
890
891 case OVS_ACTION_ATTR_USERSPACE:
892 output_userspace(dp, skb, key, a, attr, len);
893 break;
894
895 case OVS_ACTION_ATTR_HASH:
896 execute_hash(skb, key, a);
897 break;
898
899 case OVS_ACTION_ATTR_PUSH_MPLS:
900 err = push_mpls(skb, key, nla_data(a));
901 break;
902
903 case OVS_ACTION_ATTR_POP_MPLS:
904 err = pop_mpls(skb, key, nla_get_be16(a));
905 break;
906
907 case OVS_ACTION_ATTR_PUSH_VLAN:
908 err = push_vlan(skb, key, nla_data(a));
909 break;
910
911 case OVS_ACTION_ATTR_POP_VLAN:
912 err = pop_vlan(skb, key);
913 break;
914
915 case OVS_ACTION_ATTR_RECIRC:
916 err = execute_recirc(dp, skb, key, a, rem);
917 if (nla_is_last(a, rem)) {
918 /* If this is the last action, the skb has
919 * been consumed or freed.
920 * Return immediately.
921 */
922 return err;
923 }
924 break;
925
926 case OVS_ACTION_ATTR_SET:
927 err = execute_set_action(skb, key, nla_data(a));
928 break;
929
930 case OVS_ACTION_ATTR_SET_MASKED:
931 case OVS_ACTION_ATTR_SET_TO_MASKED:
932 err = execute_masked_set_action(skb, key, nla_data(a));
933 break;
934
935 case OVS_ACTION_ATTR_SAMPLE:
936 err = sample(dp, skb, key, a, attr, len);
937 break;
938 }
939
940 if (unlikely(err)) {
941 kfree_skb(skb);
942 return err;
943 }
944 }
945
946 if (prev_port != -1)
947 do_output(dp, skb, prev_port);
948 else
949 consume_skb(skb);
950
951 return 0;
952 }
953
954 static void process_deferred_actions(struct datapath *dp)
955 {
956 struct action_fifo *fifo = this_cpu_ptr(action_fifos);
957
958 /* Do not touch the FIFO in case there is no deferred actions. */
959 if (action_fifo_is_empty(fifo))
960 return;
961
962 /* Finishing executing all deferred actions. */
963 do {
964 struct deferred_action *da = action_fifo_get(fifo);
965 struct sk_buff *skb = da->skb;
966 struct sw_flow_key *key = &da->pkt_key;
967 const struct nlattr *actions = da->actions;
968
969 if (actions)
970 do_execute_actions(dp, skb, key, actions,
971 nla_len(actions));
972 else
973 ovs_dp_process_packet(skb, key);
974 } while (!action_fifo_is_empty(fifo));
975
976 /* Reset FIFO for the next packet. */
977 action_fifo_init(fifo);
978 }
979
980 /* Execute a list of actions against 'skb'. */
981 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
982 const struct sw_flow_actions *acts,
983 struct sw_flow_key *key)
984 {
985 int level = this_cpu_read(exec_actions_level);
986 int err;
987
988 if (unlikely(level >= EXEC_ACTIONS_LEVEL_LIMIT)) {
989 if (net_ratelimit())
990 pr_warn("%s: packet loop detected, dropping.\n",
991 ovs_dp_name(dp));
992
993 kfree_skb(skb);
994 return -ELOOP;
995 }
996
997 this_cpu_inc(exec_actions_level);
998 err = do_execute_actions(dp, skb, key,
999 acts->actions, acts->actions_len);
1000
1001 if (!level)
1002 process_deferred_actions(dp);
1003
1004 this_cpu_dec(exec_actions_level);
1005
1006 /* This return status currently does not reflect the errors
1007 * encounted during deferred actions execution. Probably needs to
1008 * be fixed in the future.
1009 */
1010 return err;
1011 }
1012
1013 int action_fifos_init(void)
1014 {
1015 action_fifos = alloc_percpu(struct action_fifo);
1016 if (!action_fifos)
1017 return -ENOMEM;
1018
1019 return 0;
1020 }
1021
1022 void action_fifos_exit(void)
1023 {
1024 free_percpu(action_fifos);
1025 }