1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2007-2017 Nicira, Inc.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/skbuff.h>
11 #include <linux/openvswitch.h>
12 #include <linux/sctp.h>
13 #include <linux/tcp.h>
14 #include <linux/udp.h>
15 #include <linux/in6.h>
16 #include <linux/if_arp.h>
17 #include <linux/if_vlan.h>
23 #include <net/ip6_fib.h>
24 #include <net/checksum.h>
25 #include <net/dsfield.h>
27 #include <net/sctp/checksum.h>
31 #include "conntrack.h"
33 #include "flow_netlink.h"
34 #include "openvswitch_trace.h"
36 struct deferred_action {
38 const struct nlattr *actions;
41 /* Store pkt_key clone when creating deferred action. */
42 struct sw_flow_key pkt_key;
45 #define MAX_L2_LEN (VLAN_ETH_HLEN + 3 * MPLS_HLEN)
46 struct ovs_frag_data {
50 __be16 inner_protocol;
51 u16 network_offset; /* valid only for MPLS */
56 u8 l2_data[MAX_L2_LEN];
59 static DEFINE_PER_CPU(struct ovs_frag_data, ovs_frag_data_storage);
61 #define DEFERRED_ACTION_FIFO_SIZE 10
62 #define OVS_RECURSION_LIMIT 5
63 #define OVS_DEFERRED_ACTION_THRESHOLD (OVS_RECURSION_LIMIT - 2)
67 /* Deferred action fifo queue storage. */
68 struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
71 struct action_flow_keys {
72 struct sw_flow_key key[OVS_DEFERRED_ACTION_THRESHOLD];
75 static struct action_fifo __percpu *action_fifos;
76 static struct action_flow_keys __percpu *flow_keys;
77 static DEFINE_PER_CPU(int, exec_actions_level);
79 /* Make a clone of the 'key', using the pre-allocated percpu 'flow_keys'
80 * space. Return NULL if out of key spaces.
82 static struct sw_flow_key *clone_key(const struct sw_flow_key *key_)
84 struct action_flow_keys *keys = this_cpu_ptr(flow_keys);
85 int level = this_cpu_read(exec_actions_level);
86 struct sw_flow_key *key = NULL;
88 if (level <= OVS_DEFERRED_ACTION_THRESHOLD) {
89 key = &keys->key[level - 1];
96 static void action_fifo_init(struct action_fifo *fifo)
102 static bool action_fifo_is_empty(const struct action_fifo *fifo)
104 return (fifo->head == fifo->tail);
107 static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
109 if (action_fifo_is_empty(fifo))
112 return &fifo->fifo[fifo->tail++];
115 static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
117 if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
120 return &fifo->fifo[fifo->head++];
123 /* Return true if fifo is not full */
124 static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
125 const struct sw_flow_key *key,
126 const struct nlattr *actions,
127 const int actions_len)
129 struct action_fifo *fifo;
130 struct deferred_action *da;
132 fifo = this_cpu_ptr(action_fifos);
133 da = action_fifo_put(fifo);
136 da->actions = actions;
137 da->actions_len = actions_len;
144 static void invalidate_flow_key(struct sw_flow_key *key)
146 key->mac_proto |= SW_FLOW_KEY_INVALID;
149 static bool is_flow_key_valid(const struct sw_flow_key *key)
151 return !(key->mac_proto & SW_FLOW_KEY_INVALID);
154 static int clone_execute(struct datapath *dp, struct sk_buff *skb,
155 struct sw_flow_key *key,
157 const struct nlattr *actions, int len,
158 bool last, bool clone_flow_key);
160 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
161 struct sw_flow_key *key,
162 const struct nlattr *attr, int len);
164 static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
165 __be32 mpls_lse, __be16 mpls_ethertype, __u16 mac_len)
169 err = skb_mpls_push(skb, mpls_lse, mpls_ethertype, mac_len, !!mac_len);
174 key->mac_proto = MAC_PROTO_NONE;
176 invalidate_flow_key(key);
180 static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
181 const __be16 ethertype)
185 err = skb_mpls_pop(skb, ethertype, skb->mac_len,
186 ovs_key_mac_proto(key) == MAC_PROTO_ETHERNET);
190 if (ethertype == htons(ETH_P_TEB))
191 key->mac_proto = MAC_PROTO_ETHERNET;
193 invalidate_flow_key(key);
197 static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
198 const __be32 *mpls_lse, const __be32 *mask)
200 struct mpls_shim_hdr *stack;
204 if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN))
207 stack = mpls_hdr(skb);
208 lse = OVS_MASKED(stack->label_stack_entry, *mpls_lse, *mask);
209 err = skb_mpls_update_lse(skb, lse);
213 flow_key->mpls.lse[0] = lse;
217 static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
221 err = skb_vlan_pop(skb);
222 if (skb_vlan_tag_present(skb)) {
223 invalidate_flow_key(key);
225 key->eth.vlan.tci = 0;
226 key->eth.vlan.tpid = 0;
231 static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
232 const struct ovs_action_push_vlan *vlan)
234 if (skb_vlan_tag_present(skb)) {
235 invalidate_flow_key(key);
237 key->eth.vlan.tci = vlan->vlan_tci;
238 key->eth.vlan.tpid = vlan->vlan_tpid;
240 return skb_vlan_push(skb, vlan->vlan_tpid,
241 ntohs(vlan->vlan_tci) & ~VLAN_CFI_MASK);
244 /* 'src' is already properly masked. */
245 static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
247 u16 *dst = (u16 *)dst_;
248 const u16 *src = (const u16 *)src_;
249 const u16 *mask = (const u16 *)mask_;
251 OVS_SET_MASKED(dst[0], src[0], mask[0]);
252 OVS_SET_MASKED(dst[1], src[1], mask[1]);
253 OVS_SET_MASKED(dst[2], src[2], mask[2]);
256 static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
257 const struct ovs_key_ethernet *key,
258 const struct ovs_key_ethernet *mask)
262 err = skb_ensure_writable(skb, ETH_HLEN);
266 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
268 ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
270 ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
273 skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
275 ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
276 ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
280 /* pop_eth does not support VLAN packets as this action is never called
283 static int pop_eth(struct sk_buff *skb, struct sw_flow_key *key)
287 err = skb_eth_pop(skb);
291 /* safe right before invalidate_flow_key */
292 key->mac_proto = MAC_PROTO_NONE;
293 invalidate_flow_key(key);
297 static int push_eth(struct sk_buff *skb, struct sw_flow_key *key,
298 const struct ovs_action_push_eth *ethh)
302 err = skb_eth_push(skb, ethh->addresses.eth_dst,
303 ethh->addresses.eth_src);
307 /* safe right before invalidate_flow_key */
308 key->mac_proto = MAC_PROTO_ETHERNET;
309 invalidate_flow_key(key);
313 static int push_nsh(struct sk_buff *skb, struct sw_flow_key *key,
314 const struct nshhdr *nh)
318 err = nsh_push(skb, nh);
322 /* safe right before invalidate_flow_key */
323 key->mac_proto = MAC_PROTO_NONE;
324 invalidate_flow_key(key);
328 static int pop_nsh(struct sk_buff *skb, struct sw_flow_key *key)
336 /* safe right before invalidate_flow_key */
337 if (skb->protocol == htons(ETH_P_TEB))
338 key->mac_proto = MAC_PROTO_ETHERNET;
340 key->mac_proto = MAC_PROTO_NONE;
341 invalidate_flow_key(key);
345 static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh,
346 __be32 addr, __be32 new_addr)
348 int transport_len = skb->len - skb_transport_offset(skb);
350 if (nh->frag_off & htons(IP_OFFSET))
353 if (nh->protocol == IPPROTO_TCP) {
354 if (likely(transport_len >= sizeof(struct tcphdr)))
355 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
356 addr, new_addr, true);
357 } else if (nh->protocol == IPPROTO_UDP) {
358 if (likely(transport_len >= sizeof(struct udphdr))) {
359 struct udphdr *uh = udp_hdr(skb);
361 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
362 inet_proto_csum_replace4(&uh->check, skb,
363 addr, new_addr, true);
365 uh->check = CSUM_MANGLED_0;
371 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
372 __be32 *addr, __be32 new_addr)
374 update_ip_l4_checksum(skb, nh, *addr, new_addr);
375 csum_replace4(&nh->check, *addr, new_addr);
377 ovs_ct_clear(skb, NULL);
381 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
382 __be32 addr[4], const __be32 new_addr[4])
384 int transport_len = skb->len - skb_transport_offset(skb);
386 if (l4_proto == NEXTHDR_TCP) {
387 if (likely(transport_len >= sizeof(struct tcphdr)))
388 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
389 addr, new_addr, true);
390 } else if (l4_proto == NEXTHDR_UDP) {
391 if (likely(transport_len >= sizeof(struct udphdr))) {
392 struct udphdr *uh = udp_hdr(skb);
394 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
395 inet_proto_csum_replace16(&uh->check, skb,
396 addr, new_addr, true);
398 uh->check = CSUM_MANGLED_0;
401 } else if (l4_proto == NEXTHDR_ICMP) {
402 if (likely(transport_len >= sizeof(struct icmp6hdr)))
403 inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
404 skb, addr, new_addr, true);
408 static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
409 const __be32 mask[4], __be32 masked[4])
411 masked[0] = OVS_MASKED(old[0], addr[0], mask[0]);
412 masked[1] = OVS_MASKED(old[1], addr[1], mask[1]);
413 masked[2] = OVS_MASKED(old[2], addr[2], mask[2]);
414 masked[3] = OVS_MASKED(old[3], addr[3], mask[3]);
417 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
418 __be32 addr[4], const __be32 new_addr[4],
419 bool recalculate_csum)
421 if (recalculate_csum)
422 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
425 ovs_ct_clear(skb, NULL);
426 memcpy(addr, new_addr, sizeof(__be32[4]));
429 static void set_ipv6_dsfield(struct sk_buff *skb, struct ipv6hdr *nh, u8 ipv6_tclass, u8 mask)
431 u8 old_ipv6_tclass = ipv6_get_dsfield(nh);
433 ipv6_tclass = OVS_MASKED(old_ipv6_tclass, ipv6_tclass, mask);
435 if (skb->ip_summed == CHECKSUM_COMPLETE)
436 csum_replace(&skb->csum, (__force __wsum)(old_ipv6_tclass << 12),
437 (__force __wsum)(ipv6_tclass << 12));
439 ipv6_change_dsfield(nh, ~mask, ipv6_tclass);
442 static void set_ipv6_fl(struct sk_buff *skb, struct ipv6hdr *nh, u32 fl, u32 mask)
446 ofl = nh->flow_lbl[0] << 16 | nh->flow_lbl[1] << 8 | nh->flow_lbl[2];
447 fl = OVS_MASKED(ofl, fl, mask);
449 /* Bits 21-24 are always unmasked, so this retains their values. */
450 nh->flow_lbl[0] = (u8)(fl >> 16);
451 nh->flow_lbl[1] = (u8)(fl >> 8);
452 nh->flow_lbl[2] = (u8)fl;
454 if (skb->ip_summed == CHECKSUM_COMPLETE)
455 csum_replace(&skb->csum, (__force __wsum)htonl(ofl), (__force __wsum)htonl(fl));
458 static void set_ipv6_ttl(struct sk_buff *skb, struct ipv6hdr *nh, u8 new_ttl, u8 mask)
460 new_ttl = OVS_MASKED(nh->hop_limit, new_ttl, mask);
462 if (skb->ip_summed == CHECKSUM_COMPLETE)
463 csum_replace(&skb->csum, (__force __wsum)(nh->hop_limit << 8),
464 (__force __wsum)(new_ttl << 8));
465 nh->hop_limit = new_ttl;
468 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
471 new_ttl = OVS_MASKED(nh->ttl, new_ttl, mask);
473 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
477 static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
478 const struct ovs_key_ipv4 *key,
479 const struct ovs_key_ipv4 *mask)
485 err = skb_ensure_writable(skb, skb_network_offset(skb) +
486 sizeof(struct iphdr));
492 /* Setting an IP addresses is typically only a side effect of
493 * matching on them in the current userspace implementation, so it
494 * makes sense to check if the value actually changed.
496 if (mask->ipv4_src) {
497 new_addr = OVS_MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
499 if (unlikely(new_addr != nh->saddr)) {
500 set_ip_addr(skb, nh, &nh->saddr, new_addr);
501 flow_key->ipv4.addr.src = new_addr;
504 if (mask->ipv4_dst) {
505 new_addr = OVS_MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
507 if (unlikely(new_addr != nh->daddr)) {
508 set_ip_addr(skb, nh, &nh->daddr, new_addr);
509 flow_key->ipv4.addr.dst = new_addr;
512 if (mask->ipv4_tos) {
513 ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
514 flow_key->ip.tos = nh->tos;
516 if (mask->ipv4_ttl) {
517 set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
518 flow_key->ip.ttl = nh->ttl;
524 static bool is_ipv6_mask_nonzero(const __be32 addr[4])
526 return !!(addr[0] | addr[1] | addr[2] | addr[3]);
529 static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
530 const struct ovs_key_ipv6 *key,
531 const struct ovs_key_ipv6 *mask)
536 err = skb_ensure_writable(skb, skb_network_offset(skb) +
537 sizeof(struct ipv6hdr));
543 /* Setting an IP addresses is typically only a side effect of
544 * matching on them in the current userspace implementation, so it
545 * makes sense to check if the value actually changed.
547 if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
548 __be32 *saddr = (__be32 *)&nh->saddr;
551 mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
553 if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
554 set_ipv6_addr(skb, flow_key->ip.proto, saddr, masked,
556 memcpy(&flow_key->ipv6.addr.src, masked,
557 sizeof(flow_key->ipv6.addr.src));
560 if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
561 unsigned int offset = 0;
562 int flags = IP6_FH_F_SKIP_RH;
563 bool recalc_csum = true;
564 __be32 *daddr = (__be32 *)&nh->daddr;
567 mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
569 if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
570 if (ipv6_ext_hdr(nh->nexthdr))
571 recalc_csum = (ipv6_find_hdr(skb, &offset,
576 set_ipv6_addr(skb, flow_key->ip.proto, daddr, masked,
578 memcpy(&flow_key->ipv6.addr.dst, masked,
579 sizeof(flow_key->ipv6.addr.dst));
582 if (mask->ipv6_tclass) {
583 set_ipv6_dsfield(skb, nh, key->ipv6_tclass, mask->ipv6_tclass);
584 flow_key->ip.tos = ipv6_get_dsfield(nh);
586 if (mask->ipv6_label) {
587 set_ipv6_fl(skb, nh, ntohl(key->ipv6_label),
588 ntohl(mask->ipv6_label));
589 flow_key->ipv6.label =
590 *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
592 if (mask->ipv6_hlimit) {
593 set_ipv6_ttl(skb, nh, key->ipv6_hlimit, mask->ipv6_hlimit);
594 flow_key->ip.ttl = nh->hop_limit;
599 static int set_nsh(struct sk_buff *skb, struct sw_flow_key *flow_key,
600 const struct nlattr *a)
609 struct ovs_key_nsh key;
610 struct ovs_key_nsh mask;
612 err = nsh_key_from_nlattr(a, &key, &mask);
616 /* Make sure the NSH base header is there */
617 if (!pskb_may_pull(skb, skb_network_offset(skb) + NSH_BASE_HDR_LEN))
621 length = nsh_hdr_len(nh);
623 /* Make sure the whole NSH header is there */
624 err = skb_ensure_writable(skb, skb_network_offset(skb) +
630 skb_postpull_rcsum(skb, nh, length);
631 flags = nsh_get_flags(nh);
632 flags = OVS_MASKED(flags, key.base.flags, mask.base.flags);
633 flow_key->nsh.base.flags = flags;
634 ttl = nsh_get_ttl(nh);
635 ttl = OVS_MASKED(ttl, key.base.ttl, mask.base.ttl);
636 flow_key->nsh.base.ttl = ttl;
637 nsh_set_flags_and_ttl(nh, flags, ttl);
638 nh->path_hdr = OVS_MASKED(nh->path_hdr, key.base.path_hdr,
640 flow_key->nsh.base.path_hdr = nh->path_hdr;
641 switch (nh->mdtype) {
643 for (i = 0; i < NSH_MD1_CONTEXT_SIZE; i++) {
645 OVS_MASKED(nh->md1.context[i], key.context[i],
648 memcpy(flow_key->nsh.context, nh->md1.context,
649 sizeof(nh->md1.context));
652 memset(flow_key->nsh.context, 0,
653 sizeof(flow_key->nsh.context));
658 skb_postpush_rcsum(skb, nh, length);
662 /* Must follow skb_ensure_writable() since that can move the skb data. */
663 static void set_tp_port(struct sk_buff *skb, __be16 *port,
664 __be16 new_port, __sum16 *check)
666 ovs_ct_clear(skb, NULL);
667 inet_proto_csum_replace2(check, skb, *port, new_port, false);
671 static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
672 const struct ovs_key_udp *key,
673 const struct ovs_key_udp *mask)
679 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
680 sizeof(struct udphdr));
685 /* Either of the masks is non-zero, so do not bother checking them. */
686 src = OVS_MASKED(uh->source, key->udp_src, mask->udp_src);
687 dst = OVS_MASKED(uh->dest, key->udp_dst, mask->udp_dst);
689 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
690 if (likely(src != uh->source)) {
691 set_tp_port(skb, &uh->source, src, &uh->check);
692 flow_key->tp.src = src;
694 if (likely(dst != uh->dest)) {
695 set_tp_port(skb, &uh->dest, dst, &uh->check);
696 flow_key->tp.dst = dst;
699 if (unlikely(!uh->check))
700 uh->check = CSUM_MANGLED_0;
704 flow_key->tp.src = src;
705 flow_key->tp.dst = dst;
706 ovs_ct_clear(skb, NULL);
714 static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
715 const struct ovs_key_tcp *key,
716 const struct ovs_key_tcp *mask)
722 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
723 sizeof(struct tcphdr));
728 src = OVS_MASKED(th->source, key->tcp_src, mask->tcp_src);
729 if (likely(src != th->source)) {
730 set_tp_port(skb, &th->source, src, &th->check);
731 flow_key->tp.src = src;
733 dst = OVS_MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
734 if (likely(dst != th->dest)) {
735 set_tp_port(skb, &th->dest, dst, &th->check);
736 flow_key->tp.dst = dst;
743 static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
744 const struct ovs_key_sctp *key,
745 const struct ovs_key_sctp *mask)
747 unsigned int sctphoff = skb_transport_offset(skb);
749 __le32 old_correct_csum, new_csum, old_csum;
752 err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
757 old_csum = sh->checksum;
758 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
760 sh->source = OVS_MASKED(sh->source, key->sctp_src, mask->sctp_src);
761 sh->dest = OVS_MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
763 new_csum = sctp_compute_cksum(skb, sctphoff);
765 /* Carry any checksum errors through. */
766 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
769 ovs_ct_clear(skb, NULL);
771 flow_key->tp.src = sh->source;
772 flow_key->tp.dst = sh->dest;
777 static int ovs_vport_output(struct net *net, struct sock *sk,
780 struct ovs_frag_data *data = this_cpu_ptr(&ovs_frag_data_storage);
781 struct vport *vport = data->vport;
783 if (skb_cow_head(skb, data->l2_len) < 0) {
788 __skb_dst_copy(skb, data->dst);
789 *OVS_CB(skb) = data->cb;
790 skb->inner_protocol = data->inner_protocol;
791 if (data->vlan_tci & VLAN_CFI_MASK)
792 __vlan_hwaccel_put_tag(skb, data->vlan_proto, data->vlan_tci & ~VLAN_CFI_MASK);
794 __vlan_hwaccel_clear_tag(skb);
796 /* Reconstruct the MAC header. */
797 skb_push(skb, data->l2_len);
798 memcpy(skb->data, &data->l2_data, data->l2_len);
799 skb_postpush_rcsum(skb, skb->data, data->l2_len);
800 skb_reset_mac_header(skb);
802 if (eth_p_mpls(skb->protocol)) {
803 skb->inner_network_header = skb->network_header;
804 skb_set_network_header(skb, data->network_offset);
805 skb_reset_mac_len(skb);
808 ovs_vport_send(vport, skb, data->mac_proto);
813 ovs_dst_get_mtu(const struct dst_entry *dst)
815 return dst->dev->mtu;
818 static struct dst_ops ovs_dst_ops = {
820 .mtu = ovs_dst_get_mtu,
823 /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
824 * ovs_vport_output(), which is called once per fragmented packet.
826 static void prepare_frag(struct vport *vport, struct sk_buff *skb,
827 u16 orig_network_offset, u8 mac_proto)
829 unsigned int hlen = skb_network_offset(skb);
830 struct ovs_frag_data *data;
832 data = this_cpu_ptr(&ovs_frag_data_storage);
833 data->dst = skb->_skb_refdst;
835 data->cb = *OVS_CB(skb);
836 data->inner_protocol = skb->inner_protocol;
837 data->network_offset = orig_network_offset;
838 if (skb_vlan_tag_present(skb))
839 data->vlan_tci = skb_vlan_tag_get(skb) | VLAN_CFI_MASK;
842 data->vlan_proto = skb->vlan_proto;
843 data->mac_proto = mac_proto;
845 memcpy(&data->l2_data, skb->data, hlen);
847 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
851 static void ovs_fragment(struct net *net, struct vport *vport,
852 struct sk_buff *skb, u16 mru,
853 struct sw_flow_key *key)
855 u16 orig_network_offset = 0;
857 if (eth_p_mpls(skb->protocol)) {
858 orig_network_offset = skb_network_offset(skb);
859 skb->network_header = skb->inner_network_header;
862 if (skb_network_offset(skb) > MAX_L2_LEN) {
863 OVS_NLERR(1, "L2 header too long to fragment");
867 if (key->eth.type == htons(ETH_P_IP)) {
868 struct rtable ovs_rt = { 0 };
869 unsigned long orig_dst;
871 prepare_frag(vport, skb, orig_network_offset,
872 ovs_key_mac_proto(key));
873 dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 1,
874 DST_OBSOLETE_NONE, DST_NOCOUNT);
875 ovs_rt.dst.dev = vport->dev;
877 orig_dst = skb->_skb_refdst;
878 skb_dst_set_noref(skb, &ovs_rt.dst);
879 IPCB(skb)->frag_max_size = mru;
881 ip_do_fragment(net, skb->sk, skb, ovs_vport_output);
882 refdst_drop(orig_dst);
883 } else if (key->eth.type == htons(ETH_P_IPV6)) {
884 unsigned long orig_dst;
885 struct rt6_info ovs_rt;
887 prepare_frag(vport, skb, orig_network_offset,
888 ovs_key_mac_proto(key));
889 memset(&ovs_rt, 0, sizeof(ovs_rt));
890 dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 1,
891 DST_OBSOLETE_NONE, DST_NOCOUNT);
892 ovs_rt.dst.dev = vport->dev;
894 orig_dst = skb->_skb_refdst;
895 skb_dst_set_noref(skb, &ovs_rt.dst);
896 IP6CB(skb)->frag_max_size = mru;
898 ipv6_stub->ipv6_fragment(net, skb->sk, skb, ovs_vport_output);
899 refdst_drop(orig_dst);
901 WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
902 ovs_vport_name(vport), ntohs(key->eth.type), mru,
912 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port,
913 struct sw_flow_key *key)
915 struct vport *vport = ovs_vport_rcu(dp, out_port);
917 if (likely(vport && netif_carrier_ok(vport->dev))) {
918 u16 mru = OVS_CB(skb)->mru;
919 u32 cutlen = OVS_CB(skb)->cutlen;
921 if (unlikely(cutlen > 0)) {
922 if (skb->len - cutlen > ovs_mac_header_len(key))
923 pskb_trim(skb, skb->len - cutlen);
925 pskb_trim(skb, ovs_mac_header_len(key));
929 (skb->len <= mru + vport->dev->hard_header_len))) {
930 ovs_vport_send(vport, skb, ovs_key_mac_proto(key));
931 } else if (mru <= vport->dev->mtu) {
932 struct net *net = read_pnet(&dp->net);
934 ovs_fragment(net, vport, skb, mru, key);
943 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
944 struct sw_flow_key *key, const struct nlattr *attr,
945 const struct nlattr *actions, int actions_len,
948 struct dp_upcall_info upcall;
949 const struct nlattr *a;
952 memset(&upcall, 0, sizeof(upcall));
953 upcall.cmd = OVS_PACKET_CMD_ACTION;
954 upcall.mru = OVS_CB(skb)->mru;
956 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
957 a = nla_next(a, &rem)) {
958 switch (nla_type(a)) {
959 case OVS_USERSPACE_ATTR_USERDATA:
963 case OVS_USERSPACE_ATTR_PID:
964 if (dp->user_features &
965 OVS_DP_F_DISPATCH_UPCALL_PER_CPU)
967 ovs_dp_get_upcall_portid(dp,
970 upcall.portid = nla_get_u32(a);
973 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
974 /* Get out tunnel info. */
977 vport = ovs_vport_rcu(dp, nla_get_u32(a));
981 err = dev_fill_metadata_dst(vport->dev, skb);
983 upcall.egress_tun_info = skb_tunnel_info(skb);
989 case OVS_USERSPACE_ATTR_ACTIONS: {
990 /* Include actions. */
991 upcall.actions = actions;
992 upcall.actions_len = actions_len;
996 } /* End of switch. */
999 return ovs_dp_upcall(dp, skb, key, &upcall, cutlen);
1002 static int dec_ttl_exception_handler(struct datapath *dp, struct sk_buff *skb,
1003 struct sw_flow_key *key,
1004 const struct nlattr *attr)
1006 /* The first attribute is always 'OVS_DEC_TTL_ATTR_ACTION'. */
1007 struct nlattr *actions = nla_data(attr);
1009 if (nla_len(actions))
1010 return clone_execute(dp, skb, key, 0, nla_data(actions),
1011 nla_len(actions), true, false);
1017 /* When 'last' is true, sample() should always consume the 'skb'.
1018 * Otherwise, sample() should keep 'skb' intact regardless what
1019 * actions are executed within sample().
1021 static int sample(struct datapath *dp, struct sk_buff *skb,
1022 struct sw_flow_key *key, const struct nlattr *attr,
1025 struct nlattr *actions;
1026 struct nlattr *sample_arg;
1027 int rem = nla_len(attr);
1028 const struct sample_arg *arg;
1029 bool clone_flow_key;
1031 /* The first action is always 'OVS_SAMPLE_ATTR_ARG'. */
1032 sample_arg = nla_data(attr);
1033 arg = nla_data(sample_arg);
1034 actions = nla_next(sample_arg, &rem);
1036 if ((arg->probability != U32_MAX) &&
1037 (!arg->probability || get_random_u32() > arg->probability)) {
1043 clone_flow_key = !arg->exec;
1044 return clone_execute(dp, skb, key, 0, actions, rem, last,
1048 /* When 'last' is true, clone() should always consume the 'skb'.
1049 * Otherwise, clone() should keep 'skb' intact regardless what
1050 * actions are executed within clone().
1052 static int clone(struct datapath *dp, struct sk_buff *skb,
1053 struct sw_flow_key *key, const struct nlattr *attr,
1056 struct nlattr *actions;
1057 struct nlattr *clone_arg;
1058 int rem = nla_len(attr);
1059 bool dont_clone_flow_key;
1061 /* The first action is always 'OVS_CLONE_ATTR_EXEC'. */
1062 clone_arg = nla_data(attr);
1063 dont_clone_flow_key = nla_get_u32(clone_arg);
1064 actions = nla_next(clone_arg, &rem);
1066 return clone_execute(dp, skb, key, 0, actions, rem, last,
1067 !dont_clone_flow_key);
1070 static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
1071 const struct nlattr *attr)
1073 struct ovs_action_hash *hash_act = nla_data(attr);
1076 if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
1077 /* OVS_HASH_ALG_L4 hasing type. */
1078 hash = skb_get_hash(skb);
1079 } else if (hash_act->hash_alg == OVS_HASH_ALG_SYM_L4) {
1080 /* OVS_HASH_ALG_SYM_L4 hashing type. NOTE: this doesn't
1081 * extend past an encapsulated header.
1083 hash = __skb_get_hash_symmetric(skb);
1086 hash = jhash_1word(hash, hash_act->hash_basis);
1090 key->ovs_flow_hash = hash;
1093 static int execute_set_action(struct sk_buff *skb,
1094 struct sw_flow_key *flow_key,
1095 const struct nlattr *a)
1097 /* Only tunnel set execution is supported without a mask. */
1098 if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
1099 struct ovs_tunnel_info *tun = nla_data(a);
1102 dst_hold((struct dst_entry *)tun->tun_dst);
1103 skb_dst_set(skb, (struct dst_entry *)tun->tun_dst);
1110 /* Mask is at the midpoint of the data. */
1111 #define get_mask(a, type) ((const type)nla_data(a) + 1)
1113 static int execute_masked_set_action(struct sk_buff *skb,
1114 struct sw_flow_key *flow_key,
1115 const struct nlattr *a)
1119 switch (nla_type(a)) {
1120 case OVS_KEY_ATTR_PRIORITY:
1121 OVS_SET_MASKED(skb->priority, nla_get_u32(a),
1122 *get_mask(a, u32 *));
1123 flow_key->phy.priority = skb->priority;
1126 case OVS_KEY_ATTR_SKB_MARK:
1127 OVS_SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
1128 flow_key->phy.skb_mark = skb->mark;
1131 case OVS_KEY_ATTR_TUNNEL_INFO:
1132 /* Masked data not supported for tunnel. */
1136 case OVS_KEY_ATTR_ETHERNET:
1137 err = set_eth_addr(skb, flow_key, nla_data(a),
1138 get_mask(a, struct ovs_key_ethernet *));
1141 case OVS_KEY_ATTR_NSH:
1142 err = set_nsh(skb, flow_key, a);
1145 case OVS_KEY_ATTR_IPV4:
1146 err = set_ipv4(skb, flow_key, nla_data(a),
1147 get_mask(a, struct ovs_key_ipv4 *));
1150 case OVS_KEY_ATTR_IPV6:
1151 err = set_ipv6(skb, flow_key, nla_data(a),
1152 get_mask(a, struct ovs_key_ipv6 *));
1155 case OVS_KEY_ATTR_TCP:
1156 err = set_tcp(skb, flow_key, nla_data(a),
1157 get_mask(a, struct ovs_key_tcp *));
1160 case OVS_KEY_ATTR_UDP:
1161 err = set_udp(skb, flow_key, nla_data(a),
1162 get_mask(a, struct ovs_key_udp *));
1165 case OVS_KEY_ATTR_SCTP:
1166 err = set_sctp(skb, flow_key, nla_data(a),
1167 get_mask(a, struct ovs_key_sctp *));
1170 case OVS_KEY_ATTR_MPLS:
1171 err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
1175 case OVS_KEY_ATTR_CT_STATE:
1176 case OVS_KEY_ATTR_CT_ZONE:
1177 case OVS_KEY_ATTR_CT_MARK:
1178 case OVS_KEY_ATTR_CT_LABELS:
1179 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4:
1180 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6:
1188 static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
1189 struct sw_flow_key *key,
1190 const struct nlattr *a, bool last)
1194 if (!is_flow_key_valid(key)) {
1197 err = ovs_flow_key_update(skb, key);
1201 BUG_ON(!is_flow_key_valid(key));
1203 recirc_id = nla_get_u32(a);
1204 return clone_execute(dp, skb, key, recirc_id, NULL, 0, last, true);
1207 static int execute_check_pkt_len(struct datapath *dp, struct sk_buff *skb,
1208 struct sw_flow_key *key,
1209 const struct nlattr *attr, bool last)
1211 struct ovs_skb_cb *ovs_cb = OVS_CB(skb);
1212 const struct nlattr *actions, *cpl_arg;
1213 int len, max_len, rem = nla_len(attr);
1214 const struct check_pkt_len_arg *arg;
1215 bool clone_flow_key;
1217 /* The first netlink attribute in 'attr' is always
1218 * 'OVS_CHECK_PKT_LEN_ATTR_ARG'.
1220 cpl_arg = nla_data(attr);
1221 arg = nla_data(cpl_arg);
1223 len = ovs_cb->mru ? ovs_cb->mru + skb->mac_len : skb->len;
1224 max_len = arg->pkt_len;
1226 if ((skb_is_gso(skb) && skb_gso_validate_mac_len(skb, max_len)) ||
1228 /* Second netlink attribute in 'attr' is always
1229 * 'OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL'.
1231 actions = nla_next(cpl_arg, &rem);
1232 clone_flow_key = !arg->exec_for_lesser_equal;
1234 /* Third netlink attribute in 'attr' is always
1235 * 'OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER'.
1237 actions = nla_next(cpl_arg, &rem);
1238 actions = nla_next(actions, &rem);
1239 clone_flow_key = !arg->exec_for_greater;
1242 return clone_execute(dp, skb, key, 0, nla_data(actions),
1243 nla_len(actions), last, clone_flow_key);
1246 static int execute_dec_ttl(struct sk_buff *skb, struct sw_flow_key *key)
1250 if (skb->protocol == htons(ETH_P_IPV6)) {
1253 err = skb_ensure_writable(skb, skb_network_offset(skb) +
1260 if (nh->hop_limit <= 1)
1261 return -EHOSTUNREACH;
1263 key->ip.ttl = --nh->hop_limit;
1264 } else if (skb->protocol == htons(ETH_P_IP)) {
1268 err = skb_ensure_writable(skb, skb_network_offset(skb) +
1275 return -EHOSTUNREACH;
1277 old_ttl = nh->ttl--;
1278 csum_replace2(&nh->check, htons(old_ttl << 8),
1279 htons(nh->ttl << 8));
1280 key->ip.ttl = nh->ttl;
1285 /* Execute a list of actions against 'skb'. */
1286 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
1287 struct sw_flow_key *key,
1288 const struct nlattr *attr, int len)
1290 const struct nlattr *a;
1293 for (a = attr, rem = len; rem > 0;
1294 a = nla_next(a, &rem)) {
1297 if (trace_ovs_do_execute_action_enabled())
1298 trace_ovs_do_execute_action(dp, skb, key, a, rem);
1300 switch (nla_type(a)) {
1301 case OVS_ACTION_ATTR_OUTPUT: {
1302 int port = nla_get_u32(a);
1303 struct sk_buff *clone;
1305 /* Every output action needs a separate clone
1306 * of 'skb', In case the output action is the
1307 * last action, cloning can be avoided.
1309 if (nla_is_last(a, rem)) {
1310 do_output(dp, skb, port, key);
1311 /* 'skb' has been used for output.
1316 clone = skb_clone(skb, GFP_ATOMIC);
1318 do_output(dp, clone, port, key);
1319 OVS_CB(skb)->cutlen = 0;
1323 case OVS_ACTION_ATTR_TRUNC: {
1324 struct ovs_action_trunc *trunc = nla_data(a);
1326 if (skb->len > trunc->max_len)
1327 OVS_CB(skb)->cutlen = skb->len - trunc->max_len;
1331 case OVS_ACTION_ATTR_USERSPACE:
1332 output_userspace(dp, skb, key, a, attr,
1333 len, OVS_CB(skb)->cutlen);
1334 OVS_CB(skb)->cutlen = 0;
1337 case OVS_ACTION_ATTR_HASH:
1338 execute_hash(skb, key, a);
1341 case OVS_ACTION_ATTR_PUSH_MPLS: {
1342 struct ovs_action_push_mpls *mpls = nla_data(a);
1344 err = push_mpls(skb, key, mpls->mpls_lse,
1345 mpls->mpls_ethertype, skb->mac_len);
1348 case OVS_ACTION_ATTR_ADD_MPLS: {
1349 struct ovs_action_add_mpls *mpls = nla_data(a);
1352 if (mpls->tun_flags & OVS_MPLS_L3_TUNNEL_FLAG_MASK)
1353 mac_len = skb->mac_len;
1355 err = push_mpls(skb, key, mpls->mpls_lse,
1356 mpls->mpls_ethertype, mac_len);
1359 case OVS_ACTION_ATTR_POP_MPLS:
1360 err = pop_mpls(skb, key, nla_get_be16(a));
1363 case OVS_ACTION_ATTR_PUSH_VLAN:
1364 err = push_vlan(skb, key, nla_data(a));
1367 case OVS_ACTION_ATTR_POP_VLAN:
1368 err = pop_vlan(skb, key);
1371 case OVS_ACTION_ATTR_RECIRC: {
1372 bool last = nla_is_last(a, rem);
1374 err = execute_recirc(dp, skb, key, a, last);
1376 /* If this is the last action, the skb has
1377 * been consumed or freed.
1378 * Return immediately.
1385 case OVS_ACTION_ATTR_SET:
1386 err = execute_set_action(skb, key, nla_data(a));
1389 case OVS_ACTION_ATTR_SET_MASKED:
1390 case OVS_ACTION_ATTR_SET_TO_MASKED:
1391 err = execute_masked_set_action(skb, key, nla_data(a));
1394 case OVS_ACTION_ATTR_SAMPLE: {
1395 bool last = nla_is_last(a, rem);
1397 err = sample(dp, skb, key, a, last);
1404 case OVS_ACTION_ATTR_CT:
1405 if (!is_flow_key_valid(key)) {
1406 err = ovs_flow_key_update(skb, key);
1411 err = ovs_ct_execute(ovs_dp_get_net(dp), skb, key,
1414 /* Hide stolen IP fragments from user space. */
1416 return err == -EINPROGRESS ? 0 : err;
1419 case OVS_ACTION_ATTR_CT_CLEAR:
1420 err = ovs_ct_clear(skb, key);
1423 case OVS_ACTION_ATTR_PUSH_ETH:
1424 err = push_eth(skb, key, nla_data(a));
1427 case OVS_ACTION_ATTR_POP_ETH:
1428 err = pop_eth(skb, key);
1431 case OVS_ACTION_ATTR_PUSH_NSH: {
1432 u8 buffer[NSH_HDR_MAX_LEN];
1433 struct nshhdr *nh = (struct nshhdr *)buffer;
1435 err = nsh_hdr_from_nlattr(nla_data(a), nh,
1439 err = push_nsh(skb, key, nh);
1443 case OVS_ACTION_ATTR_POP_NSH:
1444 err = pop_nsh(skb, key);
1447 case OVS_ACTION_ATTR_METER:
1448 if (ovs_meter_execute(dp, skb, key, nla_get_u32(a))) {
1454 case OVS_ACTION_ATTR_CLONE: {
1455 bool last = nla_is_last(a, rem);
1457 err = clone(dp, skb, key, a, last);
1464 case OVS_ACTION_ATTR_CHECK_PKT_LEN: {
1465 bool last = nla_is_last(a, rem);
1467 err = execute_check_pkt_len(dp, skb, key, a, last);
1474 case OVS_ACTION_ATTR_DEC_TTL:
1475 err = execute_dec_ttl(skb, key);
1476 if (err == -EHOSTUNREACH)
1477 return dec_ttl_exception_handler(dp, skb,
1482 if (unlikely(err)) {
1492 /* Execute the actions on the clone of the packet. The effect of the
1493 * execution does not affect the original 'skb' nor the original 'key'.
1495 * The execution may be deferred in case the actions can not be executed
1498 static int clone_execute(struct datapath *dp, struct sk_buff *skb,
1499 struct sw_flow_key *key, u32 recirc_id,
1500 const struct nlattr *actions, int len,
1501 bool last, bool clone_flow_key)
1503 struct deferred_action *da;
1504 struct sw_flow_key *clone;
1506 skb = last ? skb : skb_clone(skb, GFP_ATOMIC);
1508 /* Out of memory, skip this action.
1513 /* When clone_flow_key is false, the 'key' will not be change
1514 * by the actions, then the 'key' can be used directly.
1515 * Otherwise, try to clone key from the next recursion level of
1516 * 'flow_keys'. If clone is successful, execute the actions
1517 * without deferring.
1519 clone = clone_flow_key ? clone_key(key) : key;
1523 if (actions) { /* Sample action */
1525 __this_cpu_inc(exec_actions_level);
1527 err = do_execute_actions(dp, skb, clone,
1531 __this_cpu_dec(exec_actions_level);
1532 } else { /* Recirc action */
1533 clone->recirc_id = recirc_id;
1534 ovs_dp_process_packet(skb, clone);
1539 /* Out of 'flow_keys' space. Defer actions */
1540 da = add_deferred_actions(skb, key, actions, len);
1542 if (!actions) { /* Recirc action */
1544 key->recirc_id = recirc_id;
1547 /* Out of per CPU action FIFO space. Drop the 'skb' and
1552 if (net_ratelimit()) {
1553 if (actions) { /* Sample action */
1554 pr_warn("%s: deferred action limit reached, drop sample action\n",
1556 } else { /* Recirc action */
1557 pr_warn("%s: deferred action limit reached, drop recirc action (recirc_id=%#x)\n",
1558 ovs_dp_name(dp), recirc_id);
1565 static void process_deferred_actions(struct datapath *dp)
1567 struct action_fifo *fifo = this_cpu_ptr(action_fifos);
1569 /* Do not touch the FIFO in case there is no deferred actions. */
1570 if (action_fifo_is_empty(fifo))
1573 /* Finishing executing all deferred actions. */
1575 struct deferred_action *da = action_fifo_get(fifo);
1576 struct sk_buff *skb = da->skb;
1577 struct sw_flow_key *key = &da->pkt_key;
1578 const struct nlattr *actions = da->actions;
1579 int actions_len = da->actions_len;
1582 do_execute_actions(dp, skb, key, actions, actions_len);
1584 ovs_dp_process_packet(skb, key);
1585 } while (!action_fifo_is_empty(fifo));
1587 /* Reset FIFO for the next packet. */
1588 action_fifo_init(fifo);
1591 /* Execute a list of actions against 'skb'. */
1592 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
1593 const struct sw_flow_actions *acts,
1594 struct sw_flow_key *key)
1598 level = __this_cpu_inc_return(exec_actions_level);
1599 if (unlikely(level > OVS_RECURSION_LIMIT)) {
1600 net_crit_ratelimited("ovs: recursion limit reached on datapath %s, probable configuration error\n",
1607 OVS_CB(skb)->acts_origlen = acts->orig_len;
1608 err = do_execute_actions(dp, skb, key,
1609 acts->actions, acts->actions_len);
1612 process_deferred_actions(dp);
1615 __this_cpu_dec(exec_actions_level);
1619 int action_fifos_init(void)
1621 action_fifos = alloc_percpu(struct action_fifo);
1625 flow_keys = alloc_percpu(struct action_flow_keys);
1627 free_percpu(action_fifos);
1634 void action_fifos_exit(void)
1636 free_percpu(action_fifos);
1637 free_percpu(flow_keys);