2 * Copyright (c) 2007-2014 Nicira, Inc.
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.
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.
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
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/skbuff.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>
33 #include <net/checksum.h>
34 #include <net/dsfield.h>
35 #include <net/sctp/checksum.h>
43 struct deferred_action {
45 const struct nlattr *actions;
47 /* Store pkt_key clone when creating deferred action. */
48 struct sw_flow_key pkt_key;
51 #define DEFERRED_ACTION_FIFO_SIZE 10
55 /* Deferred action fifo queue storage. */
56 struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
59 static struct action_fifo __percpu *action_fifos;
60 #define EXEC_ACTIONS_LEVEL_LIMIT 4 /* limit used to detect packet
61 looping by the network stack */
62 static DEFINE_PER_CPU(int, exec_actions_level);
64 static void action_fifo_init(struct action_fifo *fifo)
70 static bool action_fifo_is_empty(struct action_fifo *fifo)
72 return (fifo->head == fifo->tail);
75 static struct deferred_action *
76 action_fifo_get(struct action_fifo *fifo)
78 if (action_fifo_is_empty(fifo))
81 return &fifo->fifo[fifo->tail++];
84 static struct deferred_action *
85 action_fifo_put(struct action_fifo *fifo)
87 if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
90 return &fifo->fifo[fifo->head++];
93 static void flow_key_clone(struct sk_buff *skb, struct sw_flow_key *new_key)
95 *new_key = *OVS_CB(skb)->pkt_key;
96 OVS_CB(skb)->pkt_key = new_key;
99 /* Return true if fifo is not full */
100 static bool add_deferred_actions(struct sk_buff *skb,
101 const struct nlattr *attr)
103 struct action_fifo *fifo;
104 struct deferred_action *da;
106 fifo = this_cpu_ptr(action_fifos);
107 da = action_fifo_put(fifo);
111 flow_key_clone(skb, &da->pkt_key);
117 static void flow_key_set_recirc_id(struct sk_buff *skb, u32 recirc_id)
119 OVS_CB(skb)->pkt_key->recirc_id = recirc_id;
122 static void flow_key_set_priority(struct sk_buff *skb, u32 priority)
124 OVS_CB(skb)->pkt_key->phy.priority = priority;
127 static void flow_key_set_skb_mark(struct sk_buff *skb, u32 skb_mark)
129 OVS_CB(skb)->pkt_key->phy.skb_mark = skb_mark;
132 static void flow_key_set_eth_src(struct sk_buff *skb, const u8 addr[])
134 ether_addr_copy(OVS_CB(skb)->pkt_key->eth.src, addr);
137 static void flow_key_set_eth_dst(struct sk_buff *skb, const u8 addr[])
139 ether_addr_copy(OVS_CB(skb)->pkt_key->eth.dst, addr);
142 static void flow_key_set_vlan_tci(struct sk_buff *skb, __be16 tci)
144 OVS_CB(skb)->pkt_key->eth.tci = tci;
147 static void flow_key_set_mpls_top_lse(struct sk_buff *skb, __be32 top_lse)
149 OVS_CB(skb)->pkt_key->mpls.top_lse = top_lse;
152 static void flow_key_set_ipv4_src(struct sk_buff *skb, __be32 addr)
154 OVS_CB(skb)->pkt_key->ipv4.addr.src = addr;
157 static void flow_key_set_ipv4_dst(struct sk_buff *skb, __be32 addr)
159 OVS_CB(skb)->pkt_key->ipv4.addr.src = addr;
162 static void flow_key_set_ip_tos(struct sk_buff *skb, u8 tos)
164 OVS_CB(skb)->pkt_key->ip.tos = tos;
167 static void flow_key_set_ip_ttl(struct sk_buff *skb, u8 ttl)
169 OVS_CB(skb)->pkt_key->ip.ttl = ttl;
172 static void flow_key_set_ipv6_src(struct sk_buff *skb,
173 const __be32 addr[4])
175 memcpy(&OVS_CB(skb)->pkt_key->ipv6.addr.src, addr, sizeof(__be32[4]));
178 static void flow_key_set_ipv6_dst(struct sk_buff *skb,
179 const __be32 addr[4])
181 memcpy(&OVS_CB(skb)->pkt_key->ipv6.addr.dst, addr, sizeof(__be32[4]));
184 static void flow_key_set_ipv6_fl(struct sk_buff *skb,
185 const struct ipv6hdr *nh)
187 OVS_CB(skb)->pkt_key->ipv6.label = *(__be32 *)nh &
188 htonl(IPV6_FLOWINFO_FLOWLABEL);
191 static void flow_key_set_tp_src(struct sk_buff *skb, __be16 port)
193 OVS_CB(skb)->pkt_key->tp.src = port;
196 static void flow_key_set_tp_dst(struct sk_buff *skb, __be16 port)
198 OVS_CB(skb)->pkt_key->tp.dst = port;
201 static void invalidate_skb_flow_key(struct sk_buff *skb)
203 OVS_CB(skb)->pkt_key->eth.type = htons(0);
206 static bool is_skb_flow_key_valid(struct sk_buff *skb)
208 return !!OVS_CB(skb)->pkt_key->eth.type;
211 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
212 const struct nlattr *attr, int len);
214 static int make_writable(struct sk_buff *skb, int write_len)
216 if (!pskb_may_pull(skb, write_len))
219 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
222 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
225 /* The end of the mac header.
227 * For non-MPLS skbs this will correspond to the network header.
228 * For MPLS skbs it will be before the network_header as the MPLS
229 * label stack lies between the end of the mac header and the network
230 * header. That is, for MPLS skbs the end of the mac header
231 * is the top of the MPLS label stack.
233 static unsigned char *mac_header_end(const struct sk_buff *skb)
235 return skb_mac_header(skb) + skb->mac_len;
238 static int push_mpls(struct sk_buff *skb,
239 const struct ovs_action_push_mpls *mpls)
241 __be32 *new_mpls_lse;
244 if (skb_cow_head(skb, MPLS_HLEN) < 0)
247 skb_push(skb, MPLS_HLEN);
248 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
250 skb_reset_mac_header(skb);
252 new_mpls_lse = (__be32 *)mac_header_end(skb);
253 *new_mpls_lse = mpls->mpls_lse;
255 if (skb->ip_summed == CHECKSUM_COMPLETE)
256 skb->csum = csum_add(skb->csum, csum_partial(new_mpls_lse,
260 hdr->h_proto = mpls->mpls_ethertype;
261 if (!ovs_skb_get_inner_protocol(skb))
262 ovs_skb_set_inner_protocol(skb, skb->protocol);
263 skb->protocol = mpls->mpls_ethertype;
264 invalidate_skb_flow_key(skb);
268 static int pop_mpls(struct sk_buff *skb, const __be16 ethertype)
273 err = make_writable(skb, skb->mac_len + MPLS_HLEN);
277 if (skb->ip_summed == CHECKSUM_COMPLETE)
278 skb->csum = csum_sub(skb->csum,
279 csum_partial(mac_header_end(skb),
282 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
285 __skb_pull(skb, MPLS_HLEN);
286 skb_reset_mac_header(skb);
288 /* mac_header_end() is used to locate the ethertype
289 * field correctly in the presence of VLAN tags.
291 hdr = (struct ethhdr *)(mac_header_end(skb) - ETH_HLEN);
292 hdr->h_proto = ethertype;
293 if (eth_p_mpls(skb->protocol))
294 skb->protocol = ethertype;
295 invalidate_skb_flow_key(skb);
299 static int set_mpls(struct sk_buff *skb, const __be32 *mpls_lse)
301 __be32 *stack = (__be32 *)mac_header_end(skb);
304 err = make_writable(skb, skb->mac_len + MPLS_HLEN);
308 if (skb->ip_summed == CHECKSUM_COMPLETE) {
309 __be32 diff[] = { ~(*stack), *mpls_lse };
310 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
315 flow_key_set_mpls_top_lse(skb, *stack);
319 /* remove VLAN header from packet and update csum accordingly. */
320 static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
322 struct vlan_hdr *vhdr;
325 err = make_writable(skb, VLAN_ETH_HLEN);
329 if (skb->ip_summed == CHECKSUM_COMPLETE)
330 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
331 + (2 * ETH_ALEN), VLAN_HLEN, 0));
333 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
334 *current_tci = vhdr->h_vlan_TCI;
336 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
337 __skb_pull(skb, VLAN_HLEN);
339 vlan_set_encap_proto(skb, vhdr);
340 skb->mac_header += VLAN_HLEN;
341 /* Update mac_len for subsequent MPLS actions */
342 skb->mac_len -= VLAN_HLEN;
347 static int pop_vlan(struct sk_buff *skb)
352 if (likely(vlan_tx_tag_present(skb))) {
353 vlan_set_tci(skb, 0);
355 if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
356 skb->len < VLAN_ETH_HLEN))
359 err = __pop_vlan_tci(skb, &tci);
363 /* move next vlan tag to hw accel tag */
364 if (likely(skb->protocol != htons(ETH_P_8021Q) ||
365 skb->len < VLAN_ETH_HLEN)) {
366 flow_key_set_vlan_tci(skb, 0);
370 invalidate_skb_flow_key(skb);
371 err = __pop_vlan_tci(skb, &tci);
375 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(tci));
379 static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan)
381 if (unlikely(vlan_tx_tag_present(skb))) {
384 /* push down current VLAN tag */
385 current_tag = vlan_tx_tag_get(skb);
387 if (!__vlan_put_tag(skb, skb->vlan_proto, current_tag))
390 /* Update mac_len for subsequent MPLS actions */
391 skb->mac_len += VLAN_HLEN;
393 if (skb->ip_summed == CHECKSUM_COMPLETE)
394 skb->csum = csum_add(skb->csum, csum_partial(skb->data
395 + (2 * ETH_ALEN), VLAN_HLEN, 0));
397 invalidate_skb_flow_key(skb);
399 flow_key_set_vlan_tci(skb, vlan->vlan_tci);
401 __vlan_hwaccel_put_tag(skb, vlan->vlan_tpid, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
405 static int set_eth_addr(struct sk_buff *skb,
406 const struct ovs_key_ethernet *eth_key)
409 err = make_writable(skb, ETH_HLEN);
413 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
415 ether_addr_copy(eth_hdr(skb)->h_source, eth_key->eth_src);
416 ether_addr_copy(eth_hdr(skb)->h_dest, eth_key->eth_dst);
418 ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
420 flow_key_set_eth_src(skb, eth_key->eth_src);
421 flow_key_set_eth_dst(skb, eth_key->eth_dst);
425 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
426 __be32 *addr, __be32 new_addr)
428 int transport_len = skb->len - skb_transport_offset(skb);
430 if (nh->protocol == IPPROTO_TCP) {
431 if (likely(transport_len >= sizeof(struct tcphdr)))
432 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
434 } else if (nh->protocol == IPPROTO_UDP) {
435 if (likely(transport_len >= sizeof(struct udphdr))) {
436 struct udphdr *uh = udp_hdr(skb);
438 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
439 inet_proto_csum_replace4(&uh->check, skb,
442 uh->check = CSUM_MANGLED_0;
447 csum_replace4(&nh->check, *addr, new_addr);
452 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
453 __be32 addr[4], const __be32 new_addr[4])
455 int transport_len = skb->len - skb_transport_offset(skb);
457 if (l4_proto == NEXTHDR_TCP) {
458 if (likely(transport_len >= sizeof(struct tcphdr)))
459 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
461 } else if (l4_proto == NEXTHDR_UDP) {
462 if (likely(transport_len >= sizeof(struct udphdr))) {
463 struct udphdr *uh = udp_hdr(skb);
465 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
466 inet_proto_csum_replace16(&uh->check, skb,
469 uh->check = CSUM_MANGLED_0;
472 } else if (l4_proto == NEXTHDR_ICMP) {
473 if (likely(transport_len >= sizeof(struct icmp6hdr)))
474 inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
475 skb, addr, new_addr, 1);
479 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
480 __be32 addr[4], const __be32 new_addr[4],
481 bool recalculate_csum)
483 if (likely(recalculate_csum))
484 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
487 memcpy(addr, new_addr, sizeof(__be32[4]));
490 static void set_ipv6_tc(struct ipv6hdr *nh, u8 tc)
492 nh->priority = tc >> 4;
493 nh->flow_lbl[0] = (nh->flow_lbl[0] & 0x0F) | ((tc & 0x0F) << 4);
496 static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl)
498 nh->flow_lbl[0] = (nh->flow_lbl[0] & 0xF0) | (fl & 0x000F0000) >> 16;
499 nh->flow_lbl[1] = (fl & 0x0000FF00) >> 8;
500 nh->flow_lbl[2] = fl & 0x000000FF;
503 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
505 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
509 static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
514 err = make_writable(skb, skb_network_offset(skb) +
515 sizeof(struct iphdr));
521 if (ipv4_key->ipv4_src != nh->saddr) {
522 set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
523 flow_key_set_ipv4_src(skb, ipv4_key->ipv4_src);
526 if (ipv4_key->ipv4_dst != nh->daddr) {
527 set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
528 flow_key_set_ipv4_dst(skb, ipv4_key->ipv4_dst);
531 if (ipv4_key->ipv4_tos != nh->tos) {
532 ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
533 flow_key_set_ip_tos(skb, nh->tos);
536 if (ipv4_key->ipv4_ttl != nh->ttl) {
537 set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
538 flow_key_set_ip_ttl(skb, ipv4_key->ipv4_ttl);
544 static int set_ipv6(struct sk_buff *skb, const struct ovs_key_ipv6 *ipv6_key)
551 err = make_writable(skb, skb_network_offset(skb) +
552 sizeof(struct ipv6hdr));
557 saddr = (__be32 *)&nh->saddr;
558 daddr = (__be32 *)&nh->daddr;
560 if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src))) {
561 set_ipv6_addr(skb, ipv6_key->ipv6_proto, saddr,
562 ipv6_key->ipv6_src, true);
563 flow_key_set_ipv6_src(skb, ipv6_key->ipv6_src);
566 if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) {
567 unsigned int offset = 0;
568 int flags = OVS_IP6T_FH_F_SKIP_RH;
569 bool recalc_csum = true;
571 if (ipv6_ext_hdr(nh->nexthdr))
572 recalc_csum = ipv6_find_hdr(skb, &offset,
573 NEXTHDR_ROUTING, NULL,
574 &flags) != NEXTHDR_ROUTING;
576 set_ipv6_addr(skb, ipv6_key->ipv6_proto, daddr,
577 ipv6_key->ipv6_dst, recalc_csum);
578 flow_key_set_ipv6_dst(skb, ipv6_key->ipv6_dst);
581 set_ipv6_tc(nh, ipv6_key->ipv6_tclass);
582 flow_key_set_ip_tos(skb, ipv6_get_dsfield(nh));
584 set_ipv6_fl(nh, ntohl(ipv6_key->ipv6_label));
585 flow_key_set_ipv6_fl(skb, nh);
587 nh->hop_limit = ipv6_key->ipv6_hlimit;
588 flow_key_set_ip_ttl(skb, ipv6_key->ipv6_hlimit);
592 /* Must follow make_writable() since that can move the skb data. */
593 static void set_tp_port(struct sk_buff *skb, __be16 *port,
594 __be16 new_port, __sum16 *check)
596 inet_proto_csum_replace2(check, skb, *port, new_port, 0);
601 static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
603 struct udphdr *uh = udp_hdr(skb);
605 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
606 set_tp_port(skb, port, new_port, &uh->check);
609 uh->check = CSUM_MANGLED_0;
616 static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key)
621 err = make_writable(skb, skb_transport_offset(skb) +
622 sizeof(struct udphdr));
627 if (udp_port_key->udp_src != uh->source) {
628 set_udp_port(skb, &uh->source, udp_port_key->udp_src);
629 flow_key_set_tp_src(skb, udp_port_key->udp_src);
632 if (udp_port_key->udp_dst != uh->dest) {
633 set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
634 flow_key_set_tp_dst(skb, udp_port_key->udp_dst);
640 static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key)
645 err = make_writable(skb, skb_transport_offset(skb) +
646 sizeof(struct tcphdr));
651 if (tcp_port_key->tcp_src != th->source) {
652 set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
653 flow_key_set_tp_src(skb, tcp_port_key->tcp_src);
656 if (tcp_port_key->tcp_dst != th->dest) {
657 set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
658 flow_key_set_tp_dst(skb, tcp_port_key->tcp_dst);
664 static int set_sctp(struct sk_buff *skb,
665 const struct ovs_key_sctp *sctp_port_key)
669 unsigned int sctphoff = skb_transport_offset(skb);
671 err = make_writable(skb, sctphoff + sizeof(struct sctphdr));
676 if (sctp_port_key->sctp_src != sh->source ||
677 sctp_port_key->sctp_dst != sh->dest) {
678 __le32 old_correct_csum, new_csum, old_csum;
680 old_csum = sh->checksum;
681 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
683 sh->source = sctp_port_key->sctp_src;
684 sh->dest = sctp_port_key->sctp_dst;
686 new_csum = sctp_compute_cksum(skb, sctphoff);
688 /* Carry any checksum errors through. */
689 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
692 flow_key_set_tp_src(skb, sctp_port_key->sctp_src);
693 flow_key_set_tp_dst(skb, sctp_port_key->sctp_dst);
699 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
701 struct vport *vport = ovs_vport_rcu(dp, out_port);
704 ovs_vport_send(vport, skb);
709 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
710 const struct nlattr *attr)
712 struct dp_upcall_info upcall;
713 const struct nlattr *a;
715 struct ovs_tunnel_info info;
717 upcall.cmd = OVS_PACKET_CMD_ACTION;
718 upcall.userdata = NULL;
720 upcall.egress_tun_info = NULL;
722 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
723 a = nla_next(a, &rem)) {
724 switch (nla_type(a)) {
725 case OVS_USERSPACE_ATTR_USERDATA:
729 case OVS_USERSPACE_ATTR_PID:
730 upcall.portid = nla_get_u32(a);
733 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
734 /* Get out tunnel info. */
737 vport = ovs_vport_rcu(dp, nla_get_u32(a));
741 err = ovs_vport_get_egress_tun_info(vport, skb,
744 upcall.egress_tun_info = &info;
749 } /* End of switch. */
752 return ovs_dp_upcall(dp, skb, &upcall);
755 static bool last_action(const struct nlattr *a, int rem)
757 return a->nla_len == rem;
760 static int sample(struct datapath *dp, struct sk_buff *skb,
761 const struct nlattr *attr)
763 const struct nlattr *acts_list = NULL;
764 const struct nlattr *a;
767 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
768 a = nla_next(a, &rem)) {
769 switch (nla_type(a)) {
770 case OVS_SAMPLE_ATTR_PROBABILITY:
771 if (prandom_u32() >= nla_get_u32(a))
775 case OVS_SAMPLE_ATTR_ACTIONS:
781 rem = nla_len(acts_list);
782 a = nla_data(acts_list);
784 /* Actions list is empty, do nothing */
788 /* The only known usage of sample action is having a single user-space
789 * action. Treat this usage as a special case.
790 * The output_userspace() should clone the skb to be sent to the
791 * user space. This skb will be consumed by its caller. */
792 if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
793 last_action(a, rem)))
794 return output_userspace(dp, skb, a);
796 skb = skb_clone(skb, GFP_ATOMIC);
798 /* Skip the sample action when out of memory. */
801 if (!add_deferred_actions(skb, a)) {
803 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
812 static void execute_hash(struct sk_buff *skb, const struct nlattr *attr)
814 struct sw_flow_key *key = OVS_CB(skb)->pkt_key;
815 struct ovs_action_hash *hash_act = nla_data(attr);
818 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
819 hash = skb_get_hash(skb);
820 hash = jhash_1word(hash, hash_act->hash_basis);
824 key->ovs_flow_hash = hash;
827 static int execute_set_action(struct sk_buff *skb,
828 const struct nlattr *nested_attr)
832 switch (nla_type(nested_attr)) {
833 case OVS_KEY_ATTR_PRIORITY:
834 skb->priority = nla_get_u32(nested_attr);
835 flow_key_set_priority(skb, skb->priority);
838 case OVS_KEY_ATTR_SKB_MARK:
839 skb->mark = nla_get_u32(nested_attr);
840 flow_key_set_skb_mark(skb, skb->mark);
843 case OVS_KEY_ATTR_TUNNEL_INFO:
844 OVS_CB(skb)->egress_tun_info = nla_data(nested_attr);
847 case OVS_KEY_ATTR_ETHERNET:
848 err = set_eth_addr(skb, nla_data(nested_attr));
851 case OVS_KEY_ATTR_IPV4:
852 err = set_ipv4(skb, nla_data(nested_attr));
855 case OVS_KEY_ATTR_IPV6:
856 err = set_ipv6(skb, nla_data(nested_attr));
859 case OVS_KEY_ATTR_TCP:
860 err = set_tcp(skb, nla_data(nested_attr));
863 case OVS_KEY_ATTR_UDP:
864 err = set_udp(skb, nla_data(nested_attr));
867 case OVS_KEY_ATTR_SCTP:
868 err = set_sctp(skb, nla_data(nested_attr));
871 case OVS_KEY_ATTR_MPLS:
872 err = set_mpls(skb, nla_data(nested_attr));
879 static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
880 const struct nlattr *a, int rem)
882 if (!is_skb_flow_key_valid(skb)) {
885 err = ovs_flow_key_update(skb, OVS_CB(skb)->pkt_key);
890 BUG_ON(!is_skb_flow_key_valid(skb));
892 if (!last_action(a, rem)) {
893 /* Recirc action is the not the last action
894 * of the action list, need to clone the skb. */
895 skb = skb_clone(skb, GFP_ATOMIC);
897 /* Skip the recirc action when out of memory, but
898 * continue on with the rest of the action list. */
903 if (add_deferred_actions(skb, NULL)) {
904 flow_key_set_recirc_id(skb, nla_get_u32(a));
909 pr_warn("%s: deferred action limit reached, drop recirc action\n",
916 /* Execute a list of actions against 'skb'. */
917 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
918 const struct nlattr *attr, int len)
920 /* Every output action needs a separate clone of 'skb', but the common
921 * case is just a single output action, so that doing a clone and
922 * then freeing the original skbuff is wasteful. So the following code
923 * is slightly obscure just to avoid that. */
925 const struct nlattr *a;
928 for (a = attr, rem = len; rem > 0;
929 a = nla_next(a, &rem)) {
932 if (unlikely(prev_port != -1)) {
933 struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
936 do_output(dp, out_skb, prev_port);
941 switch (nla_type(a)) {
942 case OVS_ACTION_ATTR_OUTPUT:
943 prev_port = nla_get_u32(a);
946 case OVS_ACTION_ATTR_USERSPACE:
947 output_userspace(dp, skb, a);
950 case OVS_ACTION_ATTR_HASH:
951 execute_hash(skb, a);
954 case OVS_ACTION_ATTR_PUSH_MPLS:
955 err = push_mpls(skb, nla_data(a));
958 case OVS_ACTION_ATTR_POP_MPLS:
959 err = pop_mpls(skb, nla_get_be16(a));
962 case OVS_ACTION_ATTR_PUSH_VLAN:
963 err = push_vlan(skb, nla_data(a));
964 if (unlikely(err)) /* skb already freed. */
968 case OVS_ACTION_ATTR_POP_VLAN:
972 case OVS_ACTION_ATTR_RECIRC:
973 err = execute_recirc(dp, skb, a, rem);
974 if (last_action(a, rem)) {
975 /* If this is the last action, the skb has
976 * been consumed or freed.
977 * Return immediately. */
982 case OVS_ACTION_ATTR_SET:
983 err = execute_set_action(skb, nla_data(a));
986 case OVS_ACTION_ATTR_SAMPLE:
987 err = sample(dp, skb, a);
998 do_output(dp, skb, prev_port);
1005 static void process_deferred_actions(struct datapath *dp)
1007 struct action_fifo *fifo = this_cpu_ptr(action_fifos);
1009 /* Do not touch the FIFO in case there is no deferred actions. */
1010 if (action_fifo_is_empty(fifo))
1013 /* Finishing executing all deferred actions. */
1015 struct deferred_action *da = action_fifo_get(fifo);
1016 struct sk_buff *skb = da->skb;
1017 const struct nlattr *actions = da->actions;
1020 do_execute_actions(dp, skb, actions,
1023 ovs_dp_process_packet(skb);
1024 } while (!action_fifo_is_empty(fifo));
1026 /* Reset FIFO for the next packet. */
1027 action_fifo_init(fifo);
1030 /* Execute a list of actions against 'skb'. */
1031 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
1032 struct sw_flow_actions *acts)
1034 int level = this_cpu_read(exec_actions_level);
1037 if (unlikely(level >= EXEC_ACTIONS_LEVEL_LIMIT)) {
1038 if (net_ratelimit())
1039 pr_warn("%s: packet loop detected, dropping.\n",
1046 this_cpu_inc(exec_actions_level);
1048 err = do_execute_actions(dp, skb, acts->actions, acts->actions_len);
1051 process_deferred_actions(dp);
1053 this_cpu_dec(exec_actions_level);
1055 /* This return status currently does not reflect the errors
1056 * encounted during deferred actions execution. Probably needs to
1057 * be fixed in the future. */
1061 int action_fifos_init(void)
1063 action_fifos = alloc_percpu(struct action_fifo);
1070 void action_fifos_exit(void)
1072 free_percpu(action_fifos);