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>
34 #include <net/checksum.h>
35 #include <net/dsfield.h>
37 #include <net/sctp/checksum.h>
43 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
44 struct sw_flow_key *key,
45 const struct nlattr *attr, int len);
47 struct deferred_action {
49 const struct nlattr *actions;
51 /* Store pkt_key clone when creating deferred action. */
52 struct sw_flow_key pkt_key;
55 #define DEFERRED_ACTION_FIFO_SIZE 10
59 /* Deferred action fifo queue storage. */
60 struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
63 static struct action_fifo __percpu *action_fifos;
64 static DEFINE_PER_CPU(int, exec_actions_level);
66 static void action_fifo_init(struct action_fifo *fifo)
72 static bool action_fifo_is_empty(const struct action_fifo *fifo)
74 return (fifo->head == fifo->tail);
77 static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
79 if (action_fifo_is_empty(fifo))
82 return &fifo->fifo[fifo->tail++];
85 static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
87 if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
90 return &fifo->fifo[fifo->head++];
93 /* Return true if fifo is not full */
94 static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
95 const struct sw_flow_key *key,
96 const struct nlattr *attr)
98 struct action_fifo *fifo;
99 struct deferred_action *da;
101 fifo = this_cpu_ptr(action_fifos);
102 da = action_fifo_put(fifo);
112 static void invalidate_flow_key(struct sw_flow_key *key)
114 key->eth.type = htons(0);
117 static bool is_flow_key_valid(const struct sw_flow_key *key)
119 return !!key->eth.type;
122 static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
123 const struct ovs_action_push_mpls *mpls)
125 __be32 *new_mpls_lse;
128 /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
129 if (skb->encapsulation)
132 if (skb_cow_head(skb, MPLS_HLEN) < 0)
135 skb_push(skb, MPLS_HLEN);
136 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
138 skb_reset_mac_header(skb);
140 new_mpls_lse = (__be32 *)skb_mpls_header(skb);
141 *new_mpls_lse = mpls->mpls_lse;
143 if (skb->ip_summed == CHECKSUM_COMPLETE)
144 skb->csum = csum_add(skb->csum, csum_partial(new_mpls_lse,
148 hdr->h_proto = mpls->mpls_ethertype;
150 skb_set_inner_protocol(skb, skb->protocol);
151 skb->protocol = mpls->mpls_ethertype;
153 invalidate_flow_key(key);
157 static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
158 const __be16 ethertype)
163 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
167 skb_postpull_rcsum(skb, skb_mpls_header(skb), MPLS_HLEN);
169 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
172 __skb_pull(skb, MPLS_HLEN);
173 skb_reset_mac_header(skb);
175 /* skb_mpls_header() is used to locate the ethertype
176 * field correctly in the presence of VLAN tags.
178 hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
179 hdr->h_proto = ethertype;
180 if (eth_p_mpls(skb->protocol))
181 skb->protocol = ethertype;
183 invalidate_flow_key(key);
187 static int set_mpls(struct sk_buff *skb, struct sw_flow_key *key,
188 const __be32 *mpls_lse)
193 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
197 stack = (__be32 *)skb_mpls_header(skb);
198 if (skb->ip_summed == CHECKSUM_COMPLETE) {
199 __be32 diff[] = { ~(*stack), *mpls_lse };
200 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
205 key->mpls.top_lse = *mpls_lse;
209 static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
213 err = skb_vlan_pop(skb);
214 if (vlan_tx_tag_present(skb))
215 invalidate_flow_key(key);
221 static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
222 const struct ovs_action_push_vlan *vlan)
224 if (vlan_tx_tag_present(skb))
225 invalidate_flow_key(key);
227 key->eth.tci = vlan->vlan_tci;
228 return skb_vlan_push(skb, vlan->vlan_tpid,
229 ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
232 static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *key,
233 const struct ovs_key_ethernet *eth_key)
236 err = skb_ensure_writable(skb, ETH_HLEN);
240 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
242 ether_addr_copy(eth_hdr(skb)->h_source, eth_key->eth_src);
243 ether_addr_copy(eth_hdr(skb)->h_dest, eth_key->eth_dst);
245 ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
247 ether_addr_copy(key->eth.src, eth_key->eth_src);
248 ether_addr_copy(key->eth.dst, eth_key->eth_dst);
252 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
253 __be32 *addr, __be32 new_addr)
255 int transport_len = skb->len - skb_transport_offset(skb);
257 if (nh->protocol == IPPROTO_TCP) {
258 if (likely(transport_len >= sizeof(struct tcphdr)))
259 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
261 } else if (nh->protocol == IPPROTO_UDP) {
262 if (likely(transport_len >= sizeof(struct udphdr))) {
263 struct udphdr *uh = udp_hdr(skb);
265 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
266 inet_proto_csum_replace4(&uh->check, skb,
269 uh->check = CSUM_MANGLED_0;
274 csum_replace4(&nh->check, *addr, new_addr);
279 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
280 __be32 addr[4], const __be32 new_addr[4])
282 int transport_len = skb->len - skb_transport_offset(skb);
284 if (l4_proto == NEXTHDR_TCP) {
285 if (likely(transport_len >= sizeof(struct tcphdr)))
286 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
288 } else if (l4_proto == NEXTHDR_UDP) {
289 if (likely(transport_len >= sizeof(struct udphdr))) {
290 struct udphdr *uh = udp_hdr(skb);
292 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
293 inet_proto_csum_replace16(&uh->check, skb,
296 uh->check = CSUM_MANGLED_0;
299 } else if (l4_proto == NEXTHDR_ICMP) {
300 if (likely(transport_len >= sizeof(struct icmp6hdr)))
301 inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
302 skb, addr, new_addr, 1);
306 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
307 __be32 addr[4], const __be32 new_addr[4],
308 bool recalculate_csum)
310 if (recalculate_csum)
311 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
314 memcpy(addr, new_addr, sizeof(__be32[4]));
317 static void set_ipv6_tc(struct ipv6hdr *nh, u8 tc)
319 nh->priority = tc >> 4;
320 nh->flow_lbl[0] = (nh->flow_lbl[0] & 0x0F) | ((tc & 0x0F) << 4);
323 static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl)
325 nh->flow_lbl[0] = (nh->flow_lbl[0] & 0xF0) | (fl & 0x000F0000) >> 16;
326 nh->flow_lbl[1] = (fl & 0x0000FF00) >> 8;
327 nh->flow_lbl[2] = fl & 0x000000FF;
330 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
332 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
336 static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *key,
337 const struct ovs_key_ipv4 *ipv4_key)
342 err = skb_ensure_writable(skb, skb_network_offset(skb) +
343 sizeof(struct iphdr));
349 if (ipv4_key->ipv4_src != nh->saddr) {
350 set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
351 key->ipv4.addr.src = ipv4_key->ipv4_src;
354 if (ipv4_key->ipv4_dst != nh->daddr) {
355 set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
356 key->ipv4.addr.dst = ipv4_key->ipv4_dst;
359 if (ipv4_key->ipv4_tos != nh->tos) {
360 ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
361 key->ip.tos = nh->tos;
364 if (ipv4_key->ipv4_ttl != nh->ttl) {
365 set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
366 key->ip.ttl = ipv4_key->ipv4_ttl;
372 static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *key,
373 const struct ovs_key_ipv6 *ipv6_key)
380 err = skb_ensure_writable(skb, skb_network_offset(skb) +
381 sizeof(struct ipv6hdr));
386 saddr = (__be32 *)&nh->saddr;
387 daddr = (__be32 *)&nh->daddr;
389 if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src))) {
390 set_ipv6_addr(skb, ipv6_key->ipv6_proto, saddr,
391 ipv6_key->ipv6_src, true);
392 memcpy(&key->ipv6.addr.src, ipv6_key->ipv6_src,
393 sizeof(ipv6_key->ipv6_src));
396 if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) {
397 unsigned int offset = 0;
398 int flags = IP6_FH_F_SKIP_RH;
399 bool recalc_csum = true;
401 if (ipv6_ext_hdr(nh->nexthdr))
402 recalc_csum = ipv6_find_hdr(skb, &offset,
403 NEXTHDR_ROUTING, NULL,
404 &flags) != NEXTHDR_ROUTING;
406 set_ipv6_addr(skb, ipv6_key->ipv6_proto, daddr,
407 ipv6_key->ipv6_dst, recalc_csum);
408 memcpy(&key->ipv6.addr.dst, ipv6_key->ipv6_dst,
409 sizeof(ipv6_key->ipv6_dst));
412 set_ipv6_tc(nh, ipv6_key->ipv6_tclass);
413 key->ip.tos = ipv6_get_dsfield(nh);
415 set_ipv6_fl(nh, ntohl(ipv6_key->ipv6_label));
416 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
418 nh->hop_limit = ipv6_key->ipv6_hlimit;
419 key->ip.ttl = ipv6_key->ipv6_hlimit;
423 /* Must follow skb_ensure_writable() since that can move the skb data. */
424 static void set_tp_port(struct sk_buff *skb, __be16 *port,
425 __be16 new_port, __sum16 *check)
427 inet_proto_csum_replace2(check, skb, *port, new_port, 0);
432 static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
434 struct udphdr *uh = udp_hdr(skb);
436 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
437 set_tp_port(skb, port, new_port, &uh->check);
440 uh->check = CSUM_MANGLED_0;
447 static int set_udp(struct sk_buff *skb, struct sw_flow_key *key,
448 const struct ovs_key_udp *udp_port_key)
453 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
454 sizeof(struct udphdr));
459 if (udp_port_key->udp_src != uh->source) {
460 set_udp_port(skb, &uh->source, udp_port_key->udp_src);
461 key->tp.src = udp_port_key->udp_src;
464 if (udp_port_key->udp_dst != uh->dest) {
465 set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
466 key->tp.dst = udp_port_key->udp_dst;
472 static int set_tcp(struct sk_buff *skb, struct sw_flow_key *key,
473 const struct ovs_key_tcp *tcp_port_key)
478 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
479 sizeof(struct tcphdr));
484 if (tcp_port_key->tcp_src != th->source) {
485 set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
486 key->tp.src = tcp_port_key->tcp_src;
489 if (tcp_port_key->tcp_dst != th->dest) {
490 set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
491 key->tp.dst = tcp_port_key->tcp_dst;
497 static int set_sctp(struct sk_buff *skb, struct sw_flow_key *key,
498 const struct ovs_key_sctp *sctp_port_key)
502 unsigned int sctphoff = skb_transport_offset(skb);
504 err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
509 if (sctp_port_key->sctp_src != sh->source ||
510 sctp_port_key->sctp_dst != sh->dest) {
511 __le32 old_correct_csum, new_csum, old_csum;
513 old_csum = sh->checksum;
514 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
516 sh->source = sctp_port_key->sctp_src;
517 sh->dest = sctp_port_key->sctp_dst;
519 new_csum = sctp_compute_cksum(skb, sctphoff);
521 /* Carry any checksum errors through. */
522 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
525 key->tp.src = sctp_port_key->sctp_src;
526 key->tp.dst = sctp_port_key->sctp_dst;
532 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
534 struct vport *vport = ovs_vport_rcu(dp, out_port);
537 ovs_vport_send(vport, skb);
542 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
543 struct sw_flow_key *key, const struct nlattr *attr)
545 struct ovs_tunnel_info info;
546 struct dp_upcall_info upcall;
547 const struct nlattr *a;
550 upcall.cmd = OVS_PACKET_CMD_ACTION;
551 upcall.userdata = NULL;
553 upcall.egress_tun_info = NULL;
555 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
556 a = nla_next(a, &rem)) {
557 switch (nla_type(a)) {
558 case OVS_USERSPACE_ATTR_USERDATA:
562 case OVS_USERSPACE_ATTR_PID:
563 upcall.portid = nla_get_u32(a);
566 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
567 /* Get out tunnel info. */
570 vport = ovs_vport_rcu(dp, nla_get_u32(a));
574 err = ovs_vport_get_egress_tun_info(vport, skb,
577 upcall.egress_tun_info = &info;
582 } /* End of switch. */
585 return ovs_dp_upcall(dp, skb, key, &upcall);
588 static int sample(struct datapath *dp, struct sk_buff *skb,
589 struct sw_flow_key *key, const struct nlattr *attr)
591 const struct nlattr *acts_list = NULL;
592 const struct nlattr *a;
595 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
596 a = nla_next(a, &rem)) {
597 switch (nla_type(a)) {
598 case OVS_SAMPLE_ATTR_PROBABILITY:
599 if (prandom_u32() >= nla_get_u32(a))
603 case OVS_SAMPLE_ATTR_ACTIONS:
609 rem = nla_len(acts_list);
610 a = nla_data(acts_list);
612 /* Actions list is empty, do nothing */
616 /* The only known usage of sample action is having a single user-space
617 * action. Treat this usage as a special case.
618 * The output_userspace() should clone the skb to be sent to the
619 * user space. This skb will be consumed by its caller.
621 if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
622 nla_is_last(a, rem)))
623 return output_userspace(dp, skb, key, a);
625 skb = skb_clone(skb, GFP_ATOMIC);
627 /* Skip the sample action when out of memory. */
630 if (!add_deferred_actions(skb, key, a)) {
632 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
640 static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
641 const struct nlattr *attr)
643 struct ovs_action_hash *hash_act = nla_data(attr);
646 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
647 hash = skb_get_hash(skb);
648 hash = jhash_1word(hash, hash_act->hash_basis);
652 key->ovs_flow_hash = hash;
655 static int execute_set_action(struct sk_buff *skb, struct sw_flow_key *key,
656 const struct nlattr *nested_attr)
660 switch (nla_type(nested_attr)) {
661 case OVS_KEY_ATTR_PRIORITY:
662 skb->priority = nla_get_u32(nested_attr);
663 key->phy.priority = skb->priority;
666 case OVS_KEY_ATTR_SKB_MARK:
667 skb->mark = nla_get_u32(nested_attr);
668 key->phy.skb_mark = skb->mark;
671 case OVS_KEY_ATTR_TUNNEL_INFO:
672 OVS_CB(skb)->egress_tun_info = nla_data(nested_attr);
675 case OVS_KEY_ATTR_ETHERNET:
676 err = set_eth_addr(skb, key, nla_data(nested_attr));
679 case OVS_KEY_ATTR_IPV4:
680 err = set_ipv4(skb, key, nla_data(nested_attr));
683 case OVS_KEY_ATTR_IPV6:
684 err = set_ipv6(skb, key, nla_data(nested_attr));
687 case OVS_KEY_ATTR_TCP:
688 err = set_tcp(skb, key, nla_data(nested_attr));
691 case OVS_KEY_ATTR_UDP:
692 err = set_udp(skb, key, nla_data(nested_attr));
695 case OVS_KEY_ATTR_SCTP:
696 err = set_sctp(skb, key, nla_data(nested_attr));
699 case OVS_KEY_ATTR_MPLS:
700 err = set_mpls(skb, key, nla_data(nested_attr));
707 static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
708 struct sw_flow_key *key,
709 const struct nlattr *a, int rem)
711 struct deferred_action *da;
713 if (!is_flow_key_valid(key)) {
716 err = ovs_flow_key_update(skb, key);
720 BUG_ON(!is_flow_key_valid(key));
722 if (!nla_is_last(a, rem)) {
723 /* Recirc action is the not the last action
724 * of the action list, need to clone the skb.
726 skb = skb_clone(skb, GFP_ATOMIC);
728 /* Skip the recirc action when out of memory, but
729 * continue on with the rest of the action list.
735 da = add_deferred_actions(skb, key, NULL);
737 da->pkt_key.recirc_id = nla_get_u32(a);
742 pr_warn("%s: deferred action limit reached, drop recirc action\n",
749 /* Execute a list of actions against 'skb'. */
750 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
751 struct sw_flow_key *key,
752 const struct nlattr *attr, int len)
754 /* Every output action needs a separate clone of 'skb', but the common
755 * case is just a single output action, so that doing a clone and
756 * then freeing the original skbuff is wasteful. So the following code
757 * is slightly obscure just to avoid that.
760 const struct nlattr *a;
763 for (a = attr, rem = len; rem > 0;
764 a = nla_next(a, &rem)) {
767 if (unlikely(prev_port != -1)) {
768 struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
771 do_output(dp, out_skb, prev_port);
776 switch (nla_type(a)) {
777 case OVS_ACTION_ATTR_OUTPUT:
778 prev_port = nla_get_u32(a);
781 case OVS_ACTION_ATTR_USERSPACE:
782 output_userspace(dp, skb, key, a);
785 case OVS_ACTION_ATTR_HASH:
786 execute_hash(skb, key, a);
789 case OVS_ACTION_ATTR_PUSH_MPLS:
790 err = push_mpls(skb, key, nla_data(a));
793 case OVS_ACTION_ATTR_POP_MPLS:
794 err = pop_mpls(skb, key, nla_get_be16(a));
797 case OVS_ACTION_ATTR_PUSH_VLAN:
798 err = push_vlan(skb, key, nla_data(a));
801 case OVS_ACTION_ATTR_POP_VLAN:
802 err = pop_vlan(skb, key);
805 case OVS_ACTION_ATTR_RECIRC:
806 err = execute_recirc(dp, skb, key, a, rem);
807 if (nla_is_last(a, rem)) {
808 /* If this is the last action, the skb has
809 * been consumed or freed.
810 * Return immediately.
816 case OVS_ACTION_ATTR_SET:
817 err = execute_set_action(skb, key, nla_data(a));
820 case OVS_ACTION_ATTR_SAMPLE:
821 err = sample(dp, skb, key, a);
832 do_output(dp, skb, prev_port);
839 static void process_deferred_actions(struct datapath *dp)
841 struct action_fifo *fifo = this_cpu_ptr(action_fifos);
843 /* Do not touch the FIFO in case there is no deferred actions. */
844 if (action_fifo_is_empty(fifo))
847 /* Finishing executing all deferred actions. */
849 struct deferred_action *da = action_fifo_get(fifo);
850 struct sk_buff *skb = da->skb;
851 struct sw_flow_key *key = &da->pkt_key;
852 const struct nlattr *actions = da->actions;
855 do_execute_actions(dp, skb, key, actions,
858 ovs_dp_process_packet(skb, key);
859 } while (!action_fifo_is_empty(fifo));
861 /* Reset FIFO for the next packet. */
862 action_fifo_init(fifo);
865 /* Execute a list of actions against 'skb'. */
866 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
867 const struct sw_flow_actions *acts,
868 struct sw_flow_key *key)
870 int level = this_cpu_read(exec_actions_level);
873 this_cpu_inc(exec_actions_level);
874 OVS_CB(skb)->egress_tun_info = NULL;
875 err = do_execute_actions(dp, skb, key,
876 acts->actions, acts->actions_len);
879 process_deferred_actions(dp);
881 this_cpu_dec(exec_actions_level);
885 int action_fifos_init(void)
887 action_fifos = alloc_percpu(struct action_fifo);
894 void action_fifos_exit(void)
896 free_percpu(action_fifos);