]> git.proxmox.com Git - mirror_ovs.git/blob - datapath/actions.c
datapath: openvswitch: Deferred fifo API change.
[mirror_ovs.git] / datapath / actions.c
1 /*
2 * Copyright (c) 2007-2015 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/netfilter_ipv6.h>
26 #include <linux/sctp.h>
27 #include <linux/tcp.h>
28 #include <linux/udp.h>
29 #include <linux/in6.h>
30 #include <linux/if_arp.h>
31 #include <linux/if_vlan.h>
32
33 #include <net/dst.h>
34 #include <net/ip.h>
35 #include <net/ipv6.h>
36 #include <net/checksum.h>
37 #include <net/dsfield.h>
38 #include <net/mpls.h>
39 #include <net/sctp/checksum.h>
40
41 #include "datapath.h"
42 #include "conntrack.h"
43 #include "gso.h"
44 #include "vport.h"
45
46 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
47 struct sw_flow_key *key,
48 const struct nlattr *attr, int len);
49
50 struct deferred_action {
51 struct sk_buff *skb;
52 const struct nlattr *actions;
53 int actions_len;
54
55 /* Store pkt_key clone when creating deferred action. */
56 struct sw_flow_key pkt_key;
57 };
58
59 #define MAX_L2_LEN (VLAN_ETH_HLEN + 3 * MPLS_HLEN)
60 struct ovs_frag_data {
61 unsigned long dst;
62 struct vport *vport;
63 struct ovs_gso_cb cb;
64 __be16 inner_protocol;
65 __u16 vlan_tci;
66 __be16 vlan_proto;
67 unsigned int l2_len;
68 u8 mac_proto;
69 u8 l2_data[MAX_L2_LEN];
70 };
71
72 static DEFINE_PER_CPU(struct ovs_frag_data, ovs_frag_data_storage);
73
74 #define DEFERRED_ACTION_FIFO_SIZE 10
75 #define OVS_RECURSION_LIMIT 4
76 #define OVS_DEFERRED_ACTION_THRESHOLD (OVS_RECURSION_LIMIT - 2)
77 struct action_fifo {
78 int head;
79 int tail;
80 /* Deferred action fifo queue storage. */
81 struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
82 };
83
84 struct recirc_keys {
85 struct sw_flow_key key[OVS_DEFERRED_ACTION_THRESHOLD];
86 };
87
88 static struct action_fifo __percpu *action_fifos;
89 static struct recirc_keys __percpu *recirc_keys;
90 static DEFINE_PER_CPU(int, exec_actions_level);
91
92 static void action_fifo_init(struct action_fifo *fifo)
93 {
94 fifo->head = 0;
95 fifo->tail = 0;
96 }
97
98 static bool action_fifo_is_empty(const struct action_fifo *fifo)
99 {
100 return (fifo->head == fifo->tail);
101 }
102
103 static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
104 {
105 if (action_fifo_is_empty(fifo))
106 return NULL;
107
108 return &fifo->fifo[fifo->tail++];
109 }
110
111 static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
112 {
113 if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
114 return NULL;
115
116 return &fifo->fifo[fifo->head++];
117 }
118
119 /* Return queue entry if fifo is not full */
120 static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
121 const struct sw_flow_key *key,
122 const struct nlattr *actions,
123 const int actions_len)
124 {
125 struct action_fifo *fifo;
126 struct deferred_action *da;
127
128 fifo = this_cpu_ptr(action_fifos);
129 da = action_fifo_put(fifo);
130 if (da) {
131 da->skb = skb;
132 da->actions = actions;
133 da->actions_len = actions_len;
134 da->pkt_key = *key;
135 }
136
137 return da;
138 }
139
140 static void invalidate_flow_key(struct sw_flow_key *key)
141 {
142 key->mac_proto |= SW_FLOW_KEY_INVALID;
143 }
144
145 static bool is_flow_key_valid(const struct sw_flow_key *key)
146 {
147 return !(key->mac_proto & SW_FLOW_KEY_INVALID);
148 }
149
150 static void update_ethertype(struct sk_buff *skb, struct ethhdr *hdr,
151 __be16 ethertype)
152 {
153 if (skb->ip_summed == CHECKSUM_COMPLETE) {
154 __be16 diff[] = { ~(hdr->h_proto), ethertype };
155
156 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
157 ~skb->csum);
158 }
159
160 hdr->h_proto = ethertype;
161 }
162
163 static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
164 const struct ovs_action_push_mpls *mpls)
165 {
166 __be32 *new_mpls_lse;
167
168 /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
169 if (skb->encapsulation)
170 return -ENOTSUPP;
171
172 if (skb_cow_head(skb, MPLS_HLEN) < 0)
173 return -ENOMEM;
174
175 skb_push(skb, MPLS_HLEN);
176 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
177 skb->mac_len);
178 skb_reset_mac_header(skb);
179
180 new_mpls_lse = (__be32 *)skb_mpls_header(skb);
181 *new_mpls_lse = mpls->mpls_lse;
182
183 skb_postpush_rcsum(skb, new_mpls_lse, MPLS_HLEN);
184
185 if (ovs_key_mac_proto(key) == MAC_PROTO_ETHERNET)
186 update_ethertype(skb, eth_hdr(skb), mpls->mpls_ethertype);
187 if (!ovs_skb_get_inner_protocol(skb))
188 ovs_skb_set_inner_protocol(skb, skb->protocol);
189 skb->protocol = mpls->mpls_ethertype;
190
191 invalidate_flow_key(key);
192 return 0;
193 }
194
195 static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
196 const __be16 ethertype)
197 {
198 int err;
199
200 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
201 if (unlikely(err))
202 return err;
203
204 skb_postpull_rcsum(skb, skb_mpls_header(skb), MPLS_HLEN);
205
206 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
207 skb->mac_len);
208
209 __skb_pull(skb, MPLS_HLEN);
210 skb_reset_mac_header(skb);
211
212 if (ovs_key_mac_proto(key) == MAC_PROTO_ETHERNET) {
213 struct ethhdr *hdr;
214
215 /* skb_mpls_header() is used to locate the ethertype
216 * field correctly in the presence of VLAN tags.
217 */
218 hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
219 update_ethertype(skb, hdr, ethertype);
220 }
221 if (eth_p_mpls(skb->protocol))
222 skb->protocol = ethertype;
223
224 invalidate_flow_key(key);
225 return 0;
226 }
227
228 static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
229 const __be32 *mpls_lse, const __be32 *mask)
230 {
231 __be32 *stack;
232 __be32 lse;
233 int err;
234
235 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
236 if (unlikely(err))
237 return err;
238
239 stack = (__be32 *)skb_mpls_header(skb);
240 lse = OVS_MASKED(*stack, *mpls_lse, *mask);
241 if (skb->ip_summed == CHECKSUM_COMPLETE) {
242 __be32 diff[] = { ~(*stack), lse };
243
244 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
245 ~skb->csum);
246 }
247
248 *stack = lse;
249 flow_key->mpls.top_lse = lse;
250 return 0;
251 }
252
253 static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
254 {
255 int err;
256
257 err = skb_vlan_pop(skb);
258 if (skb_vlan_tag_present(skb)) {
259 invalidate_flow_key(key);
260 } else {
261 key->eth.vlan.tci = 0;
262 key->eth.vlan.tpid = 0;
263 }
264 return err;
265 }
266
267 static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
268 const struct ovs_action_push_vlan *vlan)
269 {
270 if (skb_vlan_tag_present(skb)) {
271 invalidate_flow_key(key);
272 } else {
273 key->eth.vlan.tci = vlan->vlan_tci;
274 key->eth.vlan.tpid = vlan->vlan_tpid;
275 }
276 return skb_vlan_push(skb, vlan->vlan_tpid,
277 ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
278 }
279
280 /* 'src' is already properly masked. */
281 static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
282 {
283 u16 *dst = (u16 *)dst_;
284 const u16 *src = (const u16 *)src_;
285 const u16 *mask = (const u16 *)mask_;
286
287 OVS_SET_MASKED(dst[0], src[0], mask[0]);
288 OVS_SET_MASKED(dst[1], src[1], mask[1]);
289 OVS_SET_MASKED(dst[2], src[2], mask[2]);
290 }
291
292 static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
293 const struct ovs_key_ethernet *key,
294 const struct ovs_key_ethernet *mask)
295 {
296 int err;
297
298 err = skb_ensure_writable(skb, ETH_HLEN);
299 if (unlikely(err))
300 return err;
301
302 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
303
304 ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
305 mask->eth_src);
306 ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
307 mask->eth_dst);
308
309 skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
310
311 ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
312 ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
313 return 0;
314 }
315
316 /* pop_eth does not support VLAN packets as this action is never called
317 * for them.
318 */
319 static int pop_eth(struct sk_buff *skb, struct sw_flow_key *key)
320 {
321 skb_pull_rcsum(skb, ETH_HLEN);
322 skb_reset_mac_header(skb);
323 skb_reset_mac_len(skb);
324
325 /* safe right before invalidate_flow_key */
326 key->mac_proto = MAC_PROTO_NONE;
327 invalidate_flow_key(key);
328 return 0;
329 }
330
331 static int push_eth(struct sk_buff *skb, struct sw_flow_key *key,
332 const struct ovs_action_push_eth *ethh)
333 {
334 struct ethhdr *hdr;
335
336 /* Add the new Ethernet header */
337 if (skb_cow_head(skb, ETH_HLEN) < 0)
338 return -ENOMEM;
339
340 skb_push(skb, ETH_HLEN);
341 skb_reset_mac_header(skb);
342 skb_reset_mac_len(skb);
343
344 hdr = eth_hdr(skb);
345 ether_addr_copy(hdr->h_source, ethh->addresses.eth_src);
346 ether_addr_copy(hdr->h_dest, ethh->addresses.eth_dst);
347 hdr->h_proto = skb->protocol;
348
349 skb_postpush_rcsum(skb, hdr, ETH_HLEN);
350
351 /* safe right before invalidate_flow_key */
352 key->mac_proto = MAC_PROTO_ETHERNET;
353 invalidate_flow_key(key);
354 return 0;
355 }
356
357 static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh,
358 __be32 addr, __be32 new_addr)
359 {
360 int transport_len = skb->len - skb_transport_offset(skb);
361
362 if (nh->frag_off & htons(IP_OFFSET))
363 return;
364
365 if (nh->protocol == IPPROTO_TCP) {
366 if (likely(transport_len >= sizeof(struct tcphdr)))
367 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
368 addr, new_addr, true);
369 } else if (nh->protocol == IPPROTO_UDP) {
370 if (likely(transport_len >= sizeof(struct udphdr))) {
371 struct udphdr *uh = udp_hdr(skb);
372
373 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
374 inet_proto_csum_replace4(&uh->check, skb,
375 addr, new_addr, true);
376 if (!uh->check)
377 uh->check = CSUM_MANGLED_0;
378 }
379 }
380 }
381
382 }
383
384 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
385 __be32 *addr, __be32 new_addr)
386 {
387 update_ip_l4_checksum(skb, nh, *addr, new_addr);
388 csum_replace4(&nh->check, *addr, new_addr);
389 skb_clear_hash(skb);
390 *addr = new_addr;
391 }
392
393 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
394 __be32 addr[4], const __be32 new_addr[4])
395 {
396 int transport_len = skb->len - skb_transport_offset(skb);
397
398 if (l4_proto == NEXTHDR_TCP) {
399 if (likely(transport_len >= sizeof(struct tcphdr)))
400 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
401 addr, new_addr, true);
402 } else if (l4_proto == NEXTHDR_UDP) {
403 if (likely(transport_len >= sizeof(struct udphdr))) {
404 struct udphdr *uh = udp_hdr(skb);
405
406 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
407 inet_proto_csum_replace16(&uh->check, skb,
408 addr, new_addr, true);
409 if (!uh->check)
410 uh->check = CSUM_MANGLED_0;
411 }
412 }
413 } else if (l4_proto == NEXTHDR_ICMP) {
414 if (likely(transport_len >= sizeof(struct icmp6hdr)))
415 inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
416 skb, addr, new_addr, true);
417 }
418 }
419
420 static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
421 const __be32 mask[4], __be32 masked[4])
422 {
423 masked[0] = OVS_MASKED(old[0], addr[0], mask[0]);
424 masked[1] = OVS_MASKED(old[1], addr[1], mask[1]);
425 masked[2] = OVS_MASKED(old[2], addr[2], mask[2]);
426 masked[3] = OVS_MASKED(old[3], addr[3], mask[3]);
427 }
428
429 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
430 __be32 addr[4], const __be32 new_addr[4],
431 bool recalculate_csum)
432 {
433 if (likely(recalculate_csum))
434 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
435
436 skb_clear_hash(skb);
437 memcpy(addr, new_addr, sizeof(__be32[4]));
438 }
439
440 static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask)
441 {
442 /* Bits 21-24 are always unmasked, so this retains their values. */
443 OVS_SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16));
444 OVS_SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8));
445 OVS_SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask);
446 }
447
448 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
449 u8 mask)
450 {
451 new_ttl = OVS_MASKED(nh->ttl, new_ttl, mask);
452
453 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
454 nh->ttl = new_ttl;
455 }
456
457 static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
458 const struct ovs_key_ipv4 *key,
459 const struct ovs_key_ipv4 *mask)
460 {
461 struct iphdr *nh;
462 __be32 new_addr;
463 int err;
464
465 err = skb_ensure_writable(skb, skb_network_offset(skb) +
466 sizeof(struct iphdr));
467 if (unlikely(err))
468 return err;
469
470 nh = ip_hdr(skb);
471
472 /* Setting an IP addresses is typically only a side effect of
473 * matching on them in the current userspace implementation, so it
474 * makes sense to check if the value actually changed.
475 */
476 if (mask->ipv4_src) {
477 new_addr = OVS_MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
478
479 if (unlikely(new_addr != nh->saddr)) {
480 set_ip_addr(skb, nh, &nh->saddr, new_addr);
481 flow_key->ipv4.addr.src = new_addr;
482 }
483 }
484 if (mask->ipv4_dst) {
485 new_addr = OVS_MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
486
487 if (unlikely(new_addr != nh->daddr)) {
488 set_ip_addr(skb, nh, &nh->daddr, new_addr);
489 flow_key->ipv4.addr.dst = new_addr;
490 }
491 }
492 if (mask->ipv4_tos) {
493 ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
494 flow_key->ip.tos = nh->tos;
495 }
496 if (mask->ipv4_ttl) {
497 set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
498 flow_key->ip.ttl = nh->ttl;
499 }
500
501 return 0;
502 }
503
504 static bool is_ipv6_mask_nonzero(const __be32 addr[4])
505 {
506 return !!(addr[0] | addr[1] | addr[2] | addr[3]);
507 }
508
509 static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
510 const struct ovs_key_ipv6 *key,
511 const struct ovs_key_ipv6 *mask)
512 {
513 struct ipv6hdr *nh;
514 int err;
515
516 err = skb_ensure_writable(skb, skb_network_offset(skb) +
517 sizeof(struct ipv6hdr));
518 if (unlikely(err))
519 return err;
520
521 nh = ipv6_hdr(skb);
522
523 /* Setting an IP addresses is typically only a side effect of
524 * matching on them in the current userspace implementation, so it
525 * makes sense to check if the value actually changed.
526 */
527 if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
528 __be32 *saddr = (__be32 *)&nh->saddr;
529 __be32 masked[4];
530
531 mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
532
533 if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
534 set_ipv6_addr(skb, flow_key->ip.proto, saddr, masked,
535 true);
536 memcpy(&flow_key->ipv6.addr.src, masked,
537 sizeof(flow_key->ipv6.addr.src));
538 }
539 }
540 if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
541 unsigned int offset = 0;
542 int flags = IP6_FH_F_SKIP_RH;
543 bool recalc_csum = true;
544 __be32 *daddr = (__be32 *)&nh->daddr;
545 __be32 masked[4];
546
547 mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
548
549 if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
550 if (ipv6_ext_hdr(nh->nexthdr))
551 recalc_csum = (ipv6_find_hdr(skb, &offset,
552 NEXTHDR_ROUTING,
553 NULL, &flags)
554 != NEXTHDR_ROUTING);
555
556 set_ipv6_addr(skb, flow_key->ip.proto, daddr, masked,
557 recalc_csum);
558 memcpy(&flow_key->ipv6.addr.dst, masked,
559 sizeof(flow_key->ipv6.addr.dst));
560 }
561 }
562 if (mask->ipv6_tclass) {
563 ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass);
564 flow_key->ip.tos = ipv6_get_dsfield(nh);
565 }
566 if (mask->ipv6_label) {
567 set_ipv6_fl(nh, ntohl(key->ipv6_label),
568 ntohl(mask->ipv6_label));
569 flow_key->ipv6.label =
570 *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
571 }
572 if (mask->ipv6_hlimit) {
573 OVS_SET_MASKED(nh->hop_limit, key->ipv6_hlimit,
574 mask->ipv6_hlimit);
575 flow_key->ip.ttl = nh->hop_limit;
576 }
577 return 0;
578 }
579
580 /* Must follow skb_ensure_writable() since that can move the skb data. */
581 static void set_tp_port(struct sk_buff *skb, __be16 *port,
582 __be16 new_port, __sum16 *check)
583 {
584 inet_proto_csum_replace2(check, skb, *port, new_port, false);
585 *port = new_port;
586 }
587
588 static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
589 const struct ovs_key_udp *key,
590 const struct ovs_key_udp *mask)
591 {
592 struct udphdr *uh;
593 __be16 src, dst;
594 int err;
595
596 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
597 sizeof(struct udphdr));
598 if (unlikely(err))
599 return err;
600
601 uh = udp_hdr(skb);
602 /* Either of the masks is non-zero, so do not bother checking them. */
603 src = OVS_MASKED(uh->source, key->udp_src, mask->udp_src);
604 dst = OVS_MASKED(uh->dest, key->udp_dst, mask->udp_dst);
605
606 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
607 if (likely(src != uh->source)) {
608 set_tp_port(skb, &uh->source, src, &uh->check);
609 flow_key->tp.src = src;
610 }
611 if (likely(dst != uh->dest)) {
612 set_tp_port(skb, &uh->dest, dst, &uh->check);
613 flow_key->tp.dst = dst;
614 }
615
616 if (unlikely(!uh->check))
617 uh->check = CSUM_MANGLED_0;
618 } else {
619 uh->source = src;
620 uh->dest = dst;
621 flow_key->tp.src = src;
622 flow_key->tp.dst = dst;
623 }
624
625 skb_clear_hash(skb);
626
627 return 0;
628 }
629
630 static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
631 const struct ovs_key_tcp *key,
632 const struct ovs_key_tcp *mask)
633 {
634 struct tcphdr *th;
635 __be16 src, dst;
636 int err;
637
638 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
639 sizeof(struct tcphdr));
640 if (unlikely(err))
641 return err;
642
643 th = tcp_hdr(skb);
644 src = OVS_MASKED(th->source, key->tcp_src, mask->tcp_src);
645 if (likely(src != th->source)) {
646 set_tp_port(skb, &th->source, src, &th->check);
647 flow_key->tp.src = src;
648 }
649 dst = OVS_MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
650 if (likely(dst != th->dest)) {
651 set_tp_port(skb, &th->dest, dst, &th->check);
652 flow_key->tp.dst = dst;
653 }
654 skb_clear_hash(skb);
655
656 return 0;
657 }
658
659 static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
660 const struct ovs_key_sctp *key,
661 const struct ovs_key_sctp *mask)
662 {
663 unsigned int sctphoff = skb_transport_offset(skb);
664 struct sctphdr *sh;
665 __le32 old_correct_csum, new_csum, old_csum;
666 int err;
667
668 err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
669 if (unlikely(err))
670 return err;
671
672 sh = sctp_hdr(skb);
673 old_csum = sh->checksum;
674 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
675
676 sh->source = OVS_MASKED(sh->source, key->sctp_src, mask->sctp_src);
677 sh->dest = OVS_MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
678
679 new_csum = sctp_compute_cksum(skb, sctphoff);
680
681 /* Carry any checksum errors through. */
682 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
683
684 skb_clear_hash(skb);
685 flow_key->tp.src = sh->source;
686 flow_key->tp.dst = sh->dest;
687
688 return 0;
689 }
690
691 static int ovs_vport_output(OVS_VPORT_OUTPUT_PARAMS)
692 {
693 struct ovs_frag_data *data = this_cpu_ptr(&ovs_frag_data_storage);
694 struct vport *vport = data->vport;
695
696 if (skb_cow_head(skb, data->l2_len) < 0) {
697 kfree_skb(skb);
698 return -ENOMEM;
699 }
700
701 __skb_dst_copy(skb, data->dst);
702 *OVS_GSO_CB(skb) = data->cb;
703 ovs_skb_set_inner_protocol(skb, data->inner_protocol);
704 skb->vlan_tci = data->vlan_tci;
705 skb->vlan_proto = data->vlan_proto;
706
707 /* Reconstruct the MAC header. */
708 skb_push(skb, data->l2_len);
709 memcpy(skb->data, &data->l2_data, data->l2_len);
710 skb_postpush_rcsum(skb, skb->data, data->l2_len);
711 skb_reset_mac_header(skb);
712
713 ovs_vport_send(vport, skb, data->mac_proto);
714 return 0;
715 }
716
717 static unsigned int
718 ovs_dst_get_mtu(const struct dst_entry *dst)
719 {
720 return dst->dev->mtu;
721 }
722
723 static struct dst_ops ovs_dst_ops = {
724 .family = AF_UNSPEC,
725 .mtu = ovs_dst_get_mtu,
726 };
727
728 /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
729 * ovs_vport_output(), which is called once per fragmented packet.
730 */
731 static void prepare_frag(struct vport *vport, struct sk_buff *skb,
732 u8 mac_proto)
733 {
734 unsigned int hlen = skb_network_offset(skb);
735 struct ovs_frag_data *data;
736
737 data = this_cpu_ptr(&ovs_frag_data_storage);
738 data->dst = (unsigned long) skb_dst(skb);
739 data->vport = vport;
740 data->cb = *OVS_GSO_CB(skb);
741 data->inner_protocol = ovs_skb_get_inner_protocol(skb);
742 data->vlan_tci = skb->vlan_tci;
743 data->vlan_proto = skb->vlan_proto;
744 data->mac_proto = mac_proto;
745 data->l2_len = hlen;
746 memcpy(&data->l2_data, skb->data, hlen);
747
748 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
749 skb_pull(skb, hlen);
750 }
751
752 static void ovs_fragment(struct net *net, struct vport *vport,
753 struct sk_buff *skb, u16 mru,
754 struct sw_flow_key *key)
755 {
756 if (skb_network_offset(skb) > MAX_L2_LEN) {
757 OVS_NLERR(1, "L2 header too long to fragment");
758 goto err;
759 }
760
761 if (key->eth.type == htons(ETH_P_IP)) {
762 struct dst_entry ovs_dst;
763 unsigned long orig_dst;
764
765 prepare_frag(vport, skb, ovs_key_mac_proto(key));
766 dst_init(&ovs_dst, &ovs_dst_ops, NULL, 1,
767 DST_OBSOLETE_NONE, DST_NOCOUNT);
768 ovs_dst.dev = vport->dev;
769
770 orig_dst = (unsigned long) skb_dst(skb);
771 skb_dst_set_noref(skb, &ovs_dst);
772 IPCB(skb)->frag_max_size = mru;
773
774 ip_do_fragment(net, skb->sk, skb, ovs_vport_output);
775 refdst_drop(orig_dst);
776 } else if (key->eth.type == htons(ETH_P_IPV6)) {
777 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
778 unsigned long orig_dst;
779 struct rt6_info ovs_rt;
780
781 if (!v6ops)
782 goto err;
783
784 prepare_frag(vport, skb,
785 ovs_key_mac_proto(key));
786 memset(&ovs_rt, 0, sizeof(ovs_rt));
787 dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 1,
788 DST_OBSOLETE_NONE, DST_NOCOUNT);
789 ovs_rt.dst.dev = vport->dev;
790
791 orig_dst = (unsigned long) skb_dst(skb);
792 skb_dst_set_noref(skb, &ovs_rt.dst);
793 IP6CB(skb)->frag_max_size = mru;
794 #ifdef HAVE_IP_LOCAL_OUT_TAKES_NET
795 v6ops->fragment(net, skb->sk, skb, ovs_vport_output);
796 #else
797 v6ops->fragment(skb->sk, skb, ovs_vport_output);
798 #endif
799 refdst_drop(orig_dst);
800 } else {
801 WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
802 ovs_vport_name(vport), ntohs(key->eth.type), mru,
803 vport->dev->mtu);
804 goto err;
805 }
806
807 return;
808 err:
809 kfree_skb(skb);
810 }
811
812 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port,
813 struct sw_flow_key *key)
814 {
815 struct vport *vport = ovs_vport_rcu(dp, out_port);
816
817 if (likely(vport)) {
818 u16 mru = OVS_CB(skb)->mru;
819 u32 cutlen = OVS_CB(skb)->cutlen;
820
821 if (unlikely(cutlen > 0)) {
822 if (skb->len - cutlen > ovs_mac_header_len(key))
823 pskb_trim(skb, skb->len - cutlen);
824 else
825 pskb_trim(skb, ovs_mac_header_len(key));
826 }
827
828 if (likely(!mru ||
829 (skb->len <= mru + vport->dev->hard_header_len))) {
830 ovs_vport_send(vport, skb, ovs_key_mac_proto(key));
831 } else if (mru <= vport->dev->mtu) {
832 struct net *net = ovs_dp_get_net(dp);
833
834 ovs_fragment(net, vport, skb, mru, key);
835 } else {
836 OVS_NLERR(true, "Cannot fragment IP frames");
837 kfree_skb(skb);
838 }
839 } else {
840 kfree_skb(skb);
841 }
842 }
843
844 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
845 struct sw_flow_key *key, const struct nlattr *attr,
846 const struct nlattr *actions, int actions_len,
847 uint32_t cutlen)
848 {
849 struct dp_upcall_info upcall;
850 const struct nlattr *a;
851 int rem, err;
852
853 memset(&upcall, 0, sizeof(upcall));
854 upcall.cmd = OVS_PACKET_CMD_ACTION;
855 upcall.mru = OVS_CB(skb)->mru;
856
857 SKB_INIT_FILL_METADATA_DST(skb);
858 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
859 a = nla_next(a, &rem)) {
860 switch (nla_type(a)) {
861 case OVS_USERSPACE_ATTR_USERDATA:
862 upcall.userdata = a;
863 break;
864
865 case OVS_USERSPACE_ATTR_PID:
866 upcall.portid = nla_get_u32(a);
867 break;
868
869 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
870 /* Get out tunnel info. */
871 struct vport *vport;
872
873 vport = ovs_vport_rcu(dp, nla_get_u32(a));
874 if (vport) {
875 err = dev_fill_metadata_dst(vport->dev, skb);
876 if (!err)
877 upcall.egress_tun_info = skb_tunnel_info(skb);
878 }
879
880 break;
881 }
882
883 case OVS_USERSPACE_ATTR_ACTIONS: {
884 /* Include actions. */
885 upcall.actions = actions;
886 upcall.actions_len = actions_len;
887 break;
888 }
889
890 } /* End of switch. */
891 }
892
893 err = ovs_dp_upcall(dp, skb, key, &upcall, cutlen);
894 SKB_RESTORE_FILL_METADATA_DST(skb);
895 return err;
896 }
897
898 static int sample(struct datapath *dp, struct sk_buff *skb,
899 struct sw_flow_key *key, const struct nlattr *attr,
900 const struct nlattr *actions, int actions_len)
901 {
902 const struct nlattr *acts_list = NULL;
903 const struct nlattr *a;
904 int rem;
905 u32 cutlen = 0;
906
907 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
908 a = nla_next(a, &rem)) {
909 u32 probability;
910
911 switch (nla_type(a)) {
912 case OVS_SAMPLE_ATTR_PROBABILITY:
913 probability = nla_get_u32(a);
914 if (!probability || prandom_u32() > probability)
915 return 0;
916 break;
917
918 case OVS_SAMPLE_ATTR_ACTIONS:
919 acts_list = a;
920 break;
921 }
922 }
923
924 rem = nla_len(acts_list);
925 a = nla_data(acts_list);
926
927 /* Actions list is empty, do nothing */
928 if (unlikely(!rem))
929 return 0;
930
931 /* The only known usage of sample action is having a single user-space
932 * action, or having a truncate action followed by a single user-space
933 * action. Treat this usage as a special case.
934 * The output_userspace() should clone the skb to be sent to the
935 * user space. This skb will be consumed by its caller.
936 */
937 if (unlikely(nla_type(a) == OVS_ACTION_ATTR_TRUNC)) {
938 struct ovs_action_trunc *trunc = nla_data(a);
939
940 if (skb->len > trunc->max_len)
941 cutlen = skb->len - trunc->max_len;
942
943 a = nla_next(a, &rem);
944 }
945
946 if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
947 nla_is_last(a, rem)))
948 return output_userspace(dp, skb, key, a, actions,
949 actions_len, cutlen);
950
951 skb = skb_clone(skb, GFP_ATOMIC);
952 if (!skb)
953 /* Skip the sample action when out of memory. */
954 return 0;
955
956 if (!add_deferred_actions(skb, key, nla_data(acts_list),
957 nla_len(acts_list))) {
958 if (net_ratelimit())
959 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
960 ovs_dp_name(dp));
961
962 kfree_skb(skb);
963 }
964 return 0;
965 }
966
967 static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
968 const struct nlattr *attr)
969 {
970 struct ovs_action_hash *hash_act = nla_data(attr);
971 u32 hash = 0;
972
973 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
974 hash = skb_get_hash(skb);
975 hash = jhash_1word(hash, hash_act->hash_basis);
976 if (!hash)
977 hash = 0x1;
978
979 key->ovs_flow_hash = hash;
980 }
981
982 static int execute_set_action(struct sk_buff *skb,
983 struct sw_flow_key *flow_key,
984 const struct nlattr *a)
985 {
986 /* Only tunnel set execution is supported without a mask. */
987 if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
988 struct ovs_tunnel_info *tun = nla_data(a);
989
990 ovs_skb_dst_drop(skb);
991 ovs_dst_hold((struct dst_entry *)tun->tun_dst);
992 ovs_skb_dst_set(skb, (struct dst_entry *)tun->tun_dst);
993 return 0;
994 }
995
996 return -EINVAL;
997 }
998
999 /* Mask is at the midpoint of the data. */
1000 #define get_mask(a, type) ((const type)nla_data(a) + 1)
1001
1002 static int execute_masked_set_action(struct sk_buff *skb,
1003 struct sw_flow_key *flow_key,
1004 const struct nlattr *a)
1005 {
1006 int err = 0;
1007
1008 switch (nla_type(a)) {
1009 case OVS_KEY_ATTR_PRIORITY:
1010 OVS_SET_MASKED(skb->priority, nla_get_u32(a),
1011 *get_mask(a, u32 *));
1012 flow_key->phy.priority = skb->priority;
1013 break;
1014
1015 case OVS_KEY_ATTR_SKB_MARK:
1016 OVS_SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
1017 flow_key->phy.skb_mark = skb->mark;
1018 break;
1019
1020 case OVS_KEY_ATTR_TUNNEL_INFO:
1021 /* Masked data not supported for tunnel. */
1022 err = -EINVAL;
1023 break;
1024
1025 case OVS_KEY_ATTR_ETHERNET:
1026 err = set_eth_addr(skb, flow_key, nla_data(a),
1027 get_mask(a, struct ovs_key_ethernet *));
1028 break;
1029
1030 case OVS_KEY_ATTR_IPV4:
1031 err = set_ipv4(skb, flow_key, nla_data(a),
1032 get_mask(a, struct ovs_key_ipv4 *));
1033 break;
1034
1035 case OVS_KEY_ATTR_IPV6:
1036 err = set_ipv6(skb, flow_key, nla_data(a),
1037 get_mask(a, struct ovs_key_ipv6 *));
1038 break;
1039
1040 case OVS_KEY_ATTR_TCP:
1041 err = set_tcp(skb, flow_key, nla_data(a),
1042 get_mask(a, struct ovs_key_tcp *));
1043 break;
1044
1045 case OVS_KEY_ATTR_UDP:
1046 err = set_udp(skb, flow_key, nla_data(a),
1047 get_mask(a, struct ovs_key_udp *));
1048 break;
1049
1050 case OVS_KEY_ATTR_SCTP:
1051 err = set_sctp(skb, flow_key, nla_data(a),
1052 get_mask(a, struct ovs_key_sctp *));
1053 break;
1054
1055 case OVS_KEY_ATTR_MPLS:
1056 err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
1057 __be32 *));
1058 break;
1059
1060 case OVS_KEY_ATTR_CT_STATE:
1061 case OVS_KEY_ATTR_CT_ZONE:
1062 case OVS_KEY_ATTR_CT_MARK:
1063 case OVS_KEY_ATTR_CT_LABELS:
1064 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4:
1065 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6:
1066 err = -EINVAL;
1067 break;
1068 }
1069
1070 return err;
1071 }
1072
1073 static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
1074 struct sw_flow_key *key,
1075 const struct nlattr *a, int rem)
1076 {
1077 struct deferred_action *da;
1078 int level;
1079
1080 if (!is_flow_key_valid(key)) {
1081 int err;
1082
1083 err = ovs_flow_key_update(skb, key);
1084 if (err)
1085 return err;
1086 }
1087 BUG_ON(!is_flow_key_valid(key));
1088
1089 if (!nla_is_last(a, rem)) {
1090 /* Recirc action is the not the last action
1091 * of the action list, need to clone the skb.
1092 */
1093 skb = skb_clone(skb, GFP_ATOMIC);
1094
1095 /* Skip the recirc action when out of memory, but
1096 * continue on with the rest of the action list.
1097 */
1098 if (!skb)
1099 return 0;
1100 }
1101
1102 level = this_cpu_read(exec_actions_level);
1103 if (level <= OVS_DEFERRED_ACTION_THRESHOLD) {
1104 struct recirc_keys *rks = this_cpu_ptr(recirc_keys);
1105 struct sw_flow_key *recirc_key = &rks->key[level - 1];
1106
1107 *recirc_key = *key;
1108 recirc_key->recirc_id = nla_get_u32(a);
1109 ovs_dp_process_packet(skb, recirc_key);
1110
1111 return 0;
1112 }
1113
1114 da = add_deferred_actions(skb, key, NULL, 0);
1115 if (da) {
1116 da->pkt_key.recirc_id = nla_get_u32(a);
1117 } else {
1118 kfree_skb(skb);
1119
1120 if (net_ratelimit())
1121 pr_warn("%s: deferred action limit reached, drop recirc action\n",
1122 ovs_dp_name(dp));
1123 }
1124
1125 return 0;
1126 }
1127
1128 /* Execute a list of actions against 'skb'. */
1129 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
1130 struct sw_flow_key *key,
1131 const struct nlattr *attr, int len)
1132 {
1133 const struct nlattr *a;
1134 int rem;
1135
1136 for (a = attr, rem = len; rem > 0;
1137 a = nla_next(a, &rem)) {
1138 int err = 0;
1139
1140 switch (nla_type(a)) {
1141 case OVS_ACTION_ATTR_OUTPUT: {
1142 int port = nla_get_u32(a);
1143 struct sk_buff *clone;
1144
1145 /* Every output action needs a separate clone
1146 * of 'skb', In case the output action is the
1147 * last action, cloning can be avoided.
1148 */
1149 if (nla_is_last(a, rem)) {
1150 do_output(dp, skb, port, key);
1151 /* 'skb' has been used for output.
1152 */
1153 return 0;
1154 }
1155
1156 clone = skb_clone(skb, GFP_ATOMIC);
1157 if (clone)
1158 do_output(dp, clone, port, key);
1159 OVS_CB(skb)->cutlen = 0;
1160 break;
1161 }
1162
1163 case OVS_ACTION_ATTR_TRUNC: {
1164 struct ovs_action_trunc *trunc = nla_data(a);
1165
1166 if (skb->len > trunc->max_len)
1167 OVS_CB(skb)->cutlen = skb->len - trunc->max_len;
1168 break;
1169 }
1170
1171 case OVS_ACTION_ATTR_USERSPACE:
1172 output_userspace(dp, skb, key, a, attr,
1173 len, OVS_CB(skb)->cutlen);
1174 OVS_CB(skb)->cutlen = 0;
1175 break;
1176
1177 case OVS_ACTION_ATTR_HASH:
1178 execute_hash(skb, key, a);
1179 break;
1180
1181 case OVS_ACTION_ATTR_PUSH_MPLS:
1182 err = push_mpls(skb, key, nla_data(a));
1183 break;
1184
1185 case OVS_ACTION_ATTR_POP_MPLS:
1186 err = pop_mpls(skb, key, nla_get_be16(a));
1187 break;
1188
1189 case OVS_ACTION_ATTR_PUSH_VLAN:
1190 err = push_vlan(skb, key, nla_data(a));
1191 break;
1192
1193 case OVS_ACTION_ATTR_POP_VLAN:
1194 err = pop_vlan(skb, key);
1195 break;
1196
1197 case OVS_ACTION_ATTR_RECIRC:
1198 err = execute_recirc(dp, skb, key, a, rem);
1199 if (nla_is_last(a, rem)) {
1200 /* If this is the last action, the skb has
1201 * been consumed or freed.
1202 * Return immediately.
1203 */
1204 return err;
1205 }
1206 break;
1207
1208 case OVS_ACTION_ATTR_SET:
1209 err = execute_set_action(skb, key, nla_data(a));
1210 break;
1211
1212 case OVS_ACTION_ATTR_SET_MASKED:
1213 case OVS_ACTION_ATTR_SET_TO_MASKED:
1214 err = execute_masked_set_action(skb, key, nla_data(a));
1215 break;
1216
1217 case OVS_ACTION_ATTR_SAMPLE:
1218 err = sample(dp, skb, key, a, attr, len);
1219 break;
1220
1221 case OVS_ACTION_ATTR_CT:
1222 if (!is_flow_key_valid(key)) {
1223 err = ovs_flow_key_update(skb, key);
1224 if (err)
1225 return err;
1226 }
1227
1228 err = ovs_ct_execute(ovs_dp_get_net(dp), skb, key,
1229 nla_data(a));
1230
1231 /* Hide stolen IP fragments from user space. */
1232 if (err)
1233 return err == -EINPROGRESS ? 0 : err;
1234 break;
1235
1236 case OVS_ACTION_ATTR_PUSH_ETH:
1237 err = push_eth(skb, key, nla_data(a));
1238 break;
1239
1240 case OVS_ACTION_ATTR_POP_ETH:
1241 err = pop_eth(skb, key);
1242 break;
1243 }
1244
1245 if (unlikely(err)) {
1246 kfree_skb(skb);
1247 return err;
1248 }
1249 }
1250
1251 consume_skb(skb);
1252 return 0;
1253 }
1254
1255 static void process_deferred_actions(struct datapath *dp)
1256 {
1257 struct action_fifo *fifo = this_cpu_ptr(action_fifos);
1258
1259 /* Do not touch the FIFO in case there is no deferred actions. */
1260 if (action_fifo_is_empty(fifo))
1261 return;
1262
1263 /* Finishing executing all deferred actions. */
1264 do {
1265 struct deferred_action *da = action_fifo_get(fifo);
1266 struct sk_buff *skb = da->skb;
1267 struct sw_flow_key *key = &da->pkt_key;
1268 const struct nlattr *actions = da->actions;
1269 int actions_len = da->actions_len;
1270
1271 if (actions)
1272 do_execute_actions(dp, skb, key, actions, actions_len);
1273 else
1274 ovs_dp_process_packet(skb, key);
1275 } while (!action_fifo_is_empty(fifo));
1276
1277 /* Reset FIFO for the next packet. */
1278 action_fifo_init(fifo);
1279 }
1280
1281 /* Execute a list of actions against 'skb'. */
1282 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
1283 const struct sw_flow_actions *acts,
1284 struct sw_flow_key *key)
1285 {
1286 int err, level;
1287
1288 level = __this_cpu_inc_return(exec_actions_level);
1289 if (unlikely(level > OVS_RECURSION_LIMIT)) {
1290 net_crit_ratelimited("ovs: recursion limit reached on datapath %s, probable configuration error\n",
1291 ovs_dp_name(dp));
1292 kfree_skb(skb);
1293 err = -ENETDOWN;
1294 goto out;
1295 }
1296
1297 err = do_execute_actions(dp, skb, key,
1298 acts->actions, acts->actions_len);
1299
1300 if (level == 1)
1301 process_deferred_actions(dp);
1302
1303 out:
1304 __this_cpu_dec(exec_actions_level);
1305 return err;
1306 }
1307
1308 int action_fifos_init(void)
1309 {
1310 action_fifos = alloc_percpu(struct action_fifo);
1311 if (!action_fifos)
1312 return -ENOMEM;
1313
1314 recirc_keys = alloc_percpu(struct recirc_keys);
1315 if (!recirc_keys) {
1316 free_percpu(action_fifos);
1317 return -ENOMEM;
1318 }
1319
1320 return 0;
1321 }
1322
1323 void action_fifos_exit(void)
1324 {
1325 free_percpu(action_fifos);
1326 free_percpu(recirc_keys);
1327 }