openvswitch: Optimize recirc action.
[firefly-linux-kernel-4.4.55.git] / net / openvswitch / actions.c
index 006886dbee36b075fc7054ec1ddc87781a75d739..d4c2f735d9992ecf1613a9536b8bc16c52cceb05 100644 (file)
 #include <linux/in6.h>
 #include <linux/if_arp.h>
 #include <linux/if_vlan.h>
+
 #include <net/ip.h>
 #include <net/ipv6.h>
 #include <net/checksum.h>
 #include <net/dsfield.h>
+#include <net/mpls.h>
 #include <net/sctp/checksum.h>
 
 #include "datapath.h"
@@ -107,6 +109,16 @@ static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
        return da;
 }
 
+static void invalidate_flow_key(struct sw_flow_key *key)
+{
+       key->eth.type = htons(0);
+}
+
+static bool is_flow_key_valid(const struct sw_flow_key *key)
+{
+       return !!key->eth.type;
+}
+
 static int make_writable(struct sk_buff *skb, int write_len)
 {
        if (!pskb_may_pull(skb, write_len))
@@ -118,6 +130,96 @@ static int make_writable(struct sk_buff *skb, int write_len)
        return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
 }
 
+static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
+                    const struct ovs_action_push_mpls *mpls)
+{
+       __be32 *new_mpls_lse;
+       struct ethhdr *hdr;
+
+       /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
+       if (skb->encapsulation)
+               return -ENOTSUPP;
+
+       if (skb_cow_head(skb, MPLS_HLEN) < 0)
+               return -ENOMEM;
+
+       skb_push(skb, MPLS_HLEN);
+       memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
+               skb->mac_len);
+       skb_reset_mac_header(skb);
+
+       new_mpls_lse = (__be32 *)skb_mpls_header(skb);
+       *new_mpls_lse = mpls->mpls_lse;
+
+       if (skb->ip_summed == CHECKSUM_COMPLETE)
+               skb->csum = csum_add(skb->csum, csum_partial(new_mpls_lse,
+                                                            MPLS_HLEN, 0));
+
+       hdr = eth_hdr(skb);
+       hdr->h_proto = mpls->mpls_ethertype;
+
+       skb_set_inner_protocol(skb, skb->protocol);
+       skb->protocol = mpls->mpls_ethertype;
+
+       invalidate_flow_key(key);
+       return 0;
+}
+
+static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
+                   const __be16 ethertype)
+{
+       struct ethhdr *hdr;
+       int err;
+
+       err = make_writable(skb, skb->mac_len + MPLS_HLEN);
+       if (unlikely(err))
+               return err;
+
+       if (skb->ip_summed == CHECKSUM_COMPLETE)
+               skb->csum = csum_sub(skb->csum,
+                                    csum_partial(skb_mpls_header(skb),
+                                                 MPLS_HLEN, 0));
+
+       memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
+               skb->mac_len);
+
+       __skb_pull(skb, MPLS_HLEN);
+       skb_reset_mac_header(skb);
+
+       /* skb_mpls_header() is used to locate the ethertype
+        * field correctly in the presence of VLAN tags.
+        */
+       hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
+       hdr->h_proto = ethertype;
+       if (eth_p_mpls(skb->protocol))
+               skb->protocol = ethertype;
+
+       invalidate_flow_key(key);
+       return 0;
+}
+
+static int set_mpls(struct sk_buff *skb, struct sw_flow_key *key,
+                   const __be32 *mpls_lse)
+{
+       __be32 *stack;
+       int err;
+
+       err = make_writable(skb, skb->mac_len + MPLS_HLEN);
+       if (unlikely(err))
+               return err;
+
+       stack = (__be32 *)skb_mpls_header(skb);
+       if (skb->ip_summed == CHECKSUM_COMPLETE) {
+               __be32 diff[] = { ~(*stack), *mpls_lse };
+               skb->csum = ~csum_partial((char *)diff, sizeof(diff),
+                                         ~skb->csum);
+       }
+
+       *stack = *mpls_lse;
+       key->mpls.top_lse = *mpls_lse;
+       return 0;
+}
+
 /* remove VLAN header from packet and update csum accordingly. */
 static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
 {
@@ -140,14 +242,16 @@ static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
 
        vlan_set_encap_proto(skb, vhdr);
        skb->mac_header += VLAN_HLEN;
+
        if (skb_network_offset(skb) < ETH_HLEN)
                skb_set_network_header(skb, ETH_HLEN);
-       skb_reset_mac_len(skb);
 
+       /* Update mac_len for subsequent MPLS actions */
+       skb_reset_mac_len(skb);
        return 0;
 }
 
-static int pop_vlan(struct sk_buff *skb)
+static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
 {
        __be16 tci;
        int err;
@@ -165,9 +269,12 @@ static int pop_vlan(struct sk_buff *skb)
        }
        /* move next vlan tag to hw accel tag */
        if (likely(skb->protocol != htons(ETH_P_8021Q) ||
-                  skb->len < VLAN_ETH_HLEN))
+                  skb->len < VLAN_ETH_HLEN)) {
+               key->eth.tci = 0;
                return 0;
+       }
 
+       invalidate_flow_key(key);
        err = __pop_vlan_tci(skb, &tci);
        if (unlikely(err))
                return err;
@@ -176,7 +283,8 @@ static int pop_vlan(struct sk_buff *skb)
        return 0;
 }
 
-static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan)
+static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
+                    const struct ovs_action_push_vlan *vlan)
 {
        if (unlikely(vlan_tx_tag_present(skb))) {
                u16 current_tag;
@@ -186,17 +294,22 @@ static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vla
 
                if (!__vlan_put_tag(skb, skb->vlan_proto, current_tag))
                        return -ENOMEM;
+               /* Update mac_len for subsequent MPLS actions */
+               skb->mac_len += VLAN_HLEN;
 
                if (skb->ip_summed == CHECKSUM_COMPLETE)
                        skb->csum = csum_add(skb->csum, csum_partial(skb->data
                                        + (2 * ETH_ALEN), VLAN_HLEN, 0));
 
+               invalidate_flow_key(key);
+       } else {
+               key->eth.tci = vlan->vlan_tci;
        }
        __vlan_hwaccel_put_tag(skb, vlan->vlan_tpid, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
        return 0;
 }
 
-static int set_eth_addr(struct sk_buff *skb,
+static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *key,
                        const struct ovs_key_ethernet *eth_key)
 {
        int err;
@@ -211,11 +324,13 @@ static int set_eth_addr(struct sk_buff *skb,
 
        ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
 
+       ether_addr_copy(key->eth.src, eth_key->eth_src);
+       ether_addr_copy(key->eth.dst, eth_key->eth_dst);
        return 0;
 }
 
 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
-                               __be32 *addr, __be32 new_addr)
+                       __be32 *addr, __be32 new_addr)
 {
        int transport_len = skb->len - skb_transport_offset(skb);
 
@@ -294,7 +409,8 @@ static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
        nh->ttl = new_ttl;
 }
 
-static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
+static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *key,
+                   const struct ovs_key_ipv4 *ipv4_key)
 {
        struct iphdr *nh;
        int err;
@@ -306,22 +422,31 @@ static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
 
        nh = ip_hdr(skb);
 
-       if (ipv4_key->ipv4_src != nh->saddr)
+       if (ipv4_key->ipv4_src != nh->saddr) {
                set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
+               key->ipv4.addr.src = ipv4_key->ipv4_src;
+       }
 
-       if (ipv4_key->ipv4_dst != nh->daddr)
+       if (ipv4_key->ipv4_dst != nh->daddr) {
                set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
+               key->ipv4.addr.dst = ipv4_key->ipv4_dst;
+       }
 
-       if (ipv4_key->ipv4_tos != nh->tos)
+       if (ipv4_key->ipv4_tos != nh->tos) {
                ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
+               key->ip.tos = nh->tos;
+       }
 
-       if (ipv4_key->ipv4_ttl != nh->ttl)
+       if (ipv4_key->ipv4_ttl != nh->ttl) {
                set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
+               key->ip.ttl = ipv4_key->ipv4_ttl;
+       }
 
        return 0;
 }
 
-static int set_ipv6(struct sk_buff *skb, const struct ovs_key_ipv6 *ipv6_key)
+static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *key,
+                   const struct ovs_key_ipv6 *ipv6_key)
 {
        struct ipv6hdr *nh;
        int err;
@@ -337,9 +462,12 @@ static int set_ipv6(struct sk_buff *skb, const struct ovs_key_ipv6 *ipv6_key)
        saddr = (__be32 *)&nh->saddr;
        daddr = (__be32 *)&nh->daddr;
 
-       if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src)))
+       if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src))) {
                set_ipv6_addr(skb, ipv6_key->ipv6_proto, saddr,
                              ipv6_key->ipv6_src, true);
+               memcpy(&key->ipv6.addr.src, ipv6_key->ipv6_src,
+                      sizeof(ipv6_key->ipv6_src));
+       }
 
        if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) {
                unsigned int offset = 0;
@@ -353,12 +481,18 @@ static int set_ipv6(struct sk_buff *skb, const struct ovs_key_ipv6 *ipv6_key)
 
                set_ipv6_addr(skb, ipv6_key->ipv6_proto, daddr,
                              ipv6_key->ipv6_dst, recalc_csum);
+               memcpy(&key->ipv6.addr.dst, ipv6_key->ipv6_dst,
+                      sizeof(ipv6_key->ipv6_dst));
        }
 
        set_ipv6_tc(nh, ipv6_key->ipv6_tclass);
+       key->ip.tos = ipv6_get_dsfield(nh);
+
        set_ipv6_fl(nh, ntohl(ipv6_key->ipv6_label));
-       nh->hop_limit = ipv6_key->ipv6_hlimit;
+       key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
 
+       nh->hop_limit = ipv6_key->ipv6_hlimit;
+       key->ip.ttl = ipv6_key->ipv6_hlimit;
        return 0;
 }
 
@@ -386,7 +520,8 @@ static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
        }
 }
 
-static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key)
+static int set_udp(struct sk_buff *skb, struct sw_flow_key *key,
+                  const struct ovs_key_udp *udp_port_key)
 {
        struct udphdr *uh;
        int err;
@@ -397,16 +532,21 @@ static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key)
                return err;
 
        uh = udp_hdr(skb);
-       if (udp_port_key->udp_src != uh->source)
+       if (udp_port_key->udp_src != uh->source) {
                set_udp_port(skb, &uh->source, udp_port_key->udp_src);
+               key->tp.src = udp_port_key->udp_src;
+       }
 
-       if (udp_port_key->udp_dst != uh->dest)
+       if (udp_port_key->udp_dst != uh->dest) {
                set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
+               key->tp.dst = udp_port_key->udp_dst;
+       }
 
        return 0;
 }
 
-static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key)
+static int set_tcp(struct sk_buff *skb, struct sw_flow_key *key,
+                  const struct ovs_key_tcp *tcp_port_key)
 {
        struct tcphdr *th;
        int err;
@@ -417,17 +557,21 @@ static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key)
                return err;
 
        th = tcp_hdr(skb);
-       if (tcp_port_key->tcp_src != th->source)
+       if (tcp_port_key->tcp_src != th->source) {
                set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
+               key->tp.src = tcp_port_key->tcp_src;
+       }
 
-       if (tcp_port_key->tcp_dst != th->dest)
+       if (tcp_port_key->tcp_dst != th->dest) {
                set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
+               key->tp.dst = tcp_port_key->tcp_dst;
+       }
 
        return 0;
 }
 
-static int set_sctp(struct sk_buff *skb,
-                    const struct ovs_key_sctp *sctp_port_key)
+static int set_sctp(struct sk_buff *skb, struct sw_flow_key *key,
+                   const struct ovs_key_sctp *sctp_port_key)
 {
        struct sctphdr *sh;
        int err;
@@ -454,31 +598,27 @@ static int set_sctp(struct sk_buff *skb,
                sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
 
                skb_clear_hash(skb);
+               key->tp.src = sctp_port_key->sctp_src;
+               key->tp.dst = sctp_port_key->sctp_dst;
        }
 
        return 0;
 }
 
-static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
+static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
 {
-       struct vport *vport;
-
-       if (unlikely(!skb))
-               return -ENOMEM;
+       struct vport *vport = ovs_vport_rcu(dp, out_port);
 
-       vport = ovs_vport_rcu(dp, out_port);
-       if (unlikely(!vport)) {
+       if (likely(vport))
+               ovs_vport_send(vport, skb);
+       else
                kfree_skb(skb);
-               return -ENODEV;
-       }
-
-       ovs_vport_send(vport, skb);
-       return 0;
 }
 
 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
                            struct sw_flow_key *key, const struct nlattr *attr)
 {
+       struct ovs_tunnel_info info;
        struct dp_upcall_info upcall;
        const struct nlattr *a;
        int rem;
@@ -487,6 +627,7 @@ static int output_userspace(struct datapath *dp, struct sk_buff *skb,
        upcall.key = key;
        upcall.userdata = NULL;
        upcall.portid = 0;
+       upcall.egress_tun_info = NULL;
 
        for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
                 a = nla_next(a, &rem)) {
@@ -498,17 +639,29 @@ static int output_userspace(struct datapath *dp, struct sk_buff *skb,
                case OVS_USERSPACE_ATTR_PID:
                        upcall.portid = nla_get_u32(a);
                        break;
+
+               case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
+                       /* Get out tunnel info. */
+                       struct vport *vport;
+
+                       vport = ovs_vport_rcu(dp, nla_get_u32(a));
+                       if (vport) {
+                               int err;
+
+                               err = ovs_vport_get_egress_tun_info(vport, skb,
+                                                                   &info);
+                               if (!err)
+                                       upcall.egress_tun_info = &info;
+                       }
+                       break;
                }
+
+               } /* End of switch. */
        }
 
        return ovs_dp_upcall(dp, skb, &upcall);
 }
 
-static bool last_action(const struct nlattr *a, int rem)
-{
-       return a->nla_len == rem;
-}
-
 static int sample(struct datapath *dp, struct sk_buff *skb,
                  struct sw_flow_key *key, const struct nlattr *attr)
 {
@@ -543,7 +696,7 @@ static int sample(struct datapath *dp, struct sk_buff *skb,
         * user space. This skb will be consumed by its caller.
         */
        if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
-                  last_action(a, rem)))
+                  nla_is_last(a, rem)))
                return output_userspace(dp, skb, key, a);
 
        skb = skb_clone(skb, GFP_ATOMIC);
@@ -576,18 +729,20 @@ static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
        key->ovs_flow_hash = hash;
 }
 
-static int execute_set_action(struct sk_buff *skb,
-                                const struct nlattr *nested_attr)
+static int execute_set_action(struct sk_buff *skb, struct sw_flow_key *key,
+                             const struct nlattr *nested_attr)
 {
        int err = 0;
 
        switch (nla_type(nested_attr)) {
        case OVS_KEY_ATTR_PRIORITY:
                skb->priority = nla_get_u32(nested_attr);
+               key->phy.priority = skb->priority;
                break;
 
        case OVS_KEY_ATTR_SKB_MARK:
                skb->mark = nla_get_u32(nested_attr);
+               key->phy.skb_mark = skb->mark;
                break;
 
        case OVS_KEY_ATTR_TUNNEL_INFO:
@@ -595,27 +750,31 @@ static int execute_set_action(struct sk_buff *skb,
                break;
 
        case OVS_KEY_ATTR_ETHERNET:
-               err = set_eth_addr(skb, nla_data(nested_attr));
+               err = set_eth_addr(skb, key, nla_data(nested_attr));
                break;
 
        case OVS_KEY_ATTR_IPV4:
-               err = set_ipv4(skb, nla_data(nested_attr));
+               err = set_ipv4(skb, key, nla_data(nested_attr));
                break;
 
        case OVS_KEY_ATTR_IPV6:
-               err = set_ipv6(skb, nla_data(nested_attr));
+               err = set_ipv6(skb, key, nla_data(nested_attr));
                break;
 
        case OVS_KEY_ATTR_TCP:
-               err = set_tcp(skb, nla_data(nested_attr));
+               err = set_tcp(skb, key, nla_data(nested_attr));
                break;
 
        case OVS_KEY_ATTR_UDP:
-               err = set_udp(skb, nla_data(nested_attr));
+               err = set_udp(skb, key, nla_data(nested_attr));
                break;
 
        case OVS_KEY_ATTR_SCTP:
-               err = set_sctp(skb, nla_data(nested_attr));
+               err = set_sctp(skb, key, nla_data(nested_attr));
+               break;
+
+       case OVS_KEY_ATTR_MPLS:
+               err = set_mpls(skb, key, nla_data(nested_attr));
                break;
        }
 
@@ -627,13 +786,17 @@ static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
                          const struct nlattr *a, int rem)
 {
        struct deferred_action *da;
-       int err;
 
-       err = ovs_flow_key_update(skb, key);
-       if (err)
-               return err;
+       if (!is_flow_key_valid(key)) {
+               int err;
 
-       if (!last_action(a, rem)) {
+               err = ovs_flow_key_update(skb, key);
+               if (err)
+                       return err;
+       }
+       BUG_ON(!is_flow_key_valid(key));
+
+       if (!nla_is_last(a, rem)) {
                /* Recirc action is the not the last action
                 * of the action list, need to clone the skb.
                 */
@@ -668,7 +831,8 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
        /* Every output action needs a separate clone of 'skb', but the common
         * case is just a single output action, so that doing a clone and
         * then freeing the original skbuff is wasteful.  So the following code
-        * is slightly obscure just to avoid that. */
+        * is slightly obscure just to avoid that.
+        */
        int prev_port = -1;
        const struct nlattr *a;
        int rem;
@@ -677,8 +841,12 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
             a = nla_next(a, &rem)) {
                int err = 0;
 
-               if (prev_port != -1) {
-                       do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
+               if (unlikely(prev_port != -1)) {
+                       struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
+
+                       if (out_skb)
+                               do_output(dp, out_skb, prev_port);
+
                        prev_port = -1;
                }
 
@@ -695,19 +863,27 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
                        execute_hash(skb, key, a);
                        break;
 
+               case OVS_ACTION_ATTR_PUSH_MPLS:
+                       err = push_mpls(skb, key, nla_data(a));
+                       break;
+
+               case OVS_ACTION_ATTR_POP_MPLS:
+                       err = pop_mpls(skb, key, nla_get_be16(a));
+                       break;
+
                case OVS_ACTION_ATTR_PUSH_VLAN:
-                       err = push_vlan(skb, nla_data(a));
+                       err = push_vlan(skb, key, nla_data(a));
                        if (unlikely(err)) /* skb already freed. */
                                return err;
                        break;
 
                case OVS_ACTION_ATTR_POP_VLAN:
-                       err = pop_vlan(skb);
+                       err = pop_vlan(skb, key);
                        break;
 
                case OVS_ACTION_ATTR_RECIRC:
                        err = execute_recirc(dp, skb, key, a, rem);
-                       if (last_action(a, rem)) {
+                       if (nla_is_last(a, rem)) {
                                /* If this is the last action, the skb has
                                 * been consumed or freed.
                                 * Return immediately.
@@ -717,7 +893,7 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
                        break;
 
                case OVS_ACTION_ATTR_SET:
-                       err = execute_set_action(skb, nla_data(a));
+                       err = execute_set_action(skb, key, nla_data(a));
                        break;
 
                case OVS_ACTION_ATTR_SAMPLE:
@@ -769,14 +945,11 @@ static void process_deferred_actions(struct datapath *dp)
 
 /* Execute a list of actions against 'skb'. */
 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
-                       struct sw_flow_key *key)
+                       struct sw_flow_actions *acts, struct sw_flow_key *key)
 {
        int level = this_cpu_read(exec_actions_level);
-       struct sw_flow_actions *acts;
        int err;
 
-       acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
-
        this_cpu_inc(exec_actions_level);
        OVS_CB(skb)->egress_tun_info = NULL;
        err = do_execute_actions(dp, skb, key,