datapath: move vlan pop/push functions into common code
[cascardo/ovs.git] / datapath / actions.c
index 00c1fd4..0ac6684 100644 (file)
 #include <net/ipv6.h>
 #include <net/checksum.h>
 #include <net/dsfield.h>
+#include <net/mpls.h>
 #include <net/sctp/checksum.h>
 
 #include "datapath.h"
 #include "gso.h"
-#include "mpls.h"
 #include "vlan.h"
 #include "vport.h"
 
@@ -122,36 +122,16 @@ 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))
-               return -ENOMEM;
-
-       if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
-               return 0;
-
-       return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
-}
-
-/* The end of the mac header.
- *
- * For non-MPLS skbs this will correspond to the network header.
- * For MPLS skbs it will be before the network_header as the MPLS
- * label stack lies between the end of the mac header and the network
- * header. That is, for MPLS skbs the end of the mac header
- * is the top of the MPLS label stack.
- */
-static unsigned char *mac_header_end(const struct sk_buff *skb)
-{
-       return skb_mac_header(skb) + skb->mac_len;
-}
-
 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(skb))
+               return -ENOTSUPP;
+
        if (skb_cow_head(skb, MPLS_HLEN) < 0)
                return -ENOMEM;
 
@@ -160,7 +140,7 @@ static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
                skb->mac_len);
        skb_reset_mac_header(skb);
 
-       new_mpls_lse = (__be32 *)mac_header_end(skb);
+       new_mpls_lse = (__be32 *)skb_mpls_header(skb);
        *new_mpls_lse = mpls->mpls_lse;
 
        if (skb->ip_summed == CHECKSUM_COMPLETE)
@@ -172,6 +152,7 @@ static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
        if (!ovs_skb_get_inner_protocol(skb))
                ovs_skb_set_inner_protocol(skb, skb->protocol);
        skb->protocol = mpls->mpls_ethertype;
+
        invalidate_flow_key(key);
        return 0;
 }
@@ -182,13 +163,13 @@ static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
        struct ethhdr *hdr;
        int err;
 
-       err = make_writable(skb, skb->mac_len + MPLS_HLEN);
+       err = skb_ensure_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(mac_header_end(skb),
+                                    csum_partial(skb_mpls_header(skb),
                                                  MPLS_HLEN, 0));
 
        memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
@@ -197,13 +178,14 @@ static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
        __skb_pull(skb, MPLS_HLEN);
        skb_reset_mac_header(skb);
 
-       /* mac_header_end() is used to locate the ethertype
+       /* skb_mpls_header() is used to locate the ethertype
         * field correctly in the presence of VLAN tags.
         */
-       hdr = (struct ethhdr *)(mac_header_end(skb) - ETH_HLEN);
+       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;
 }
@@ -211,13 +193,14 @@ static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
 static int set_mpls(struct sk_buff *skb, struct sw_flow_key *key,
                    const __be32 *mpls_lse)
 {
-       __be32 *stack = (__be32 *)mac_header_end(skb);
+       __be32 *stack;
        int err;
 
-       err = make_writable(skb, skb->mac_len + MPLS_HLEN);
+       err = skb_ensure_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),
@@ -229,98 +212,36 @@ static int set_mpls(struct sk_buff *skb, struct sw_flow_key *key,
        return 0;
 }
 
-/* remove VLAN header from packet and update csum accordingly. */
-static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
-{
-       struct vlan_hdr *vhdr;
-       int err;
-
-       err = make_writable(skb, VLAN_ETH_HLEN);
-       if (unlikely(err))
-               return err;
-
-       if (skb->ip_summed == CHECKSUM_COMPLETE)
-               skb->csum = csum_sub(skb->csum, csum_partial(skb->data
-                                       + (2 * ETH_ALEN), VLAN_HLEN, 0));
-
-       vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
-       *current_tci = vhdr->h_vlan_TCI;
-
-       memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
-       __skb_pull(skb, VLAN_HLEN);
-
-       vlan_set_encap_proto(skb, vhdr);
-       skb->mac_header += VLAN_HLEN;
-       /* Update mac_len for subsequent MPLS actions */
-       skb->mac_len -= VLAN_HLEN;
-
-       return 0;
-}
-
 static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
 {
-       __be16 tci;
        int err;
 
-       if (likely(vlan_tx_tag_present(skb))) {
-               vlan_set_tci(skb, 0);
-       } else {
-               if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
-                            skb->len < VLAN_ETH_HLEN))
-                       return 0;
-
-               err = __pop_vlan_tci(skb, &tci);
-               if (err)
-                       return err;
-       }
-       /* move next vlan tag to hw accel tag */
-       if (likely(skb->protocol != htons(ETH_P_8021Q) ||
-                  skb->len < VLAN_ETH_HLEN)) {
+       err = skb_vlan_pop(skb);
+       if (vlan_tx_tag_present(skb))
+               invalidate_flow_key(key);
+       else
                key->eth.tci = 0;
-               return 0;
-       }
-
-       invalidate_flow_key(key);
-       err = __pop_vlan_tci(skb, &tci);
-       if (unlikely(err))
-               return err;
 
-       __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(tci));
-       return 0;
+       return err;
 }
 
 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;
-
-               /* push down current VLAN tag */
-               current_tag = vlan_tx_tag_get(skb);
-
-               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));
-
+       if (vlan_tx_tag_present(skb))
                invalidate_flow_key(key);
-       } else {
+       else
                key->eth.tci = vlan->vlan_tci;
-       }
-       __vlan_hwaccel_put_tag(skb, vlan->vlan_tpid, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
-       return 0;
+
+       return skb_vlan_push(skb, vlan->vlan_tpid,
+                            ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
 }
 
 static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *key,
                        const struct ovs_key_ethernet *eth_key)
 {
        int err;
-       err = make_writable(skb, ETH_HLEN);
+       err = skb_ensure_writable(skb, ETH_HLEN);
        if (unlikely(err))
                return err;
 
@@ -426,8 +347,8 @@ static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *key,
        struct iphdr *nh;
        int err;
 
-       err = make_writable(skb, skb_network_offset(skb) +
-                                sizeof(struct iphdr));
+       err = skb_ensure_writable(skb, skb_network_offset(skb) +
+                                 sizeof(struct iphdr));
        if (unlikely(err))
                return err;
 
@@ -464,8 +385,8 @@ static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *key,
        __be32 *saddr;
        __be32 *daddr;
 
-       err = make_writable(skb, skb_network_offset(skb) +
-                           sizeof(struct ipv6hdr));
+       err = skb_ensure_writable(skb, skb_network_offset(skb) +
+                                 sizeof(struct ipv6hdr));
        if (unlikely(err))
                return err;
 
@@ -482,7 +403,7 @@ static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *key,
 
        if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) {
                unsigned int offset = 0;
-               int flags = OVS_IP6T_FH_F_SKIP_RH;
+               int flags = IP6_FH_F_SKIP_RH;
                bool recalc_csum = true;
 
                if (ipv6_ext_hdr(nh->nexthdr))
@@ -507,7 +428,7 @@ static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *key,
        return 0;
 }
 
-/* Must follow make_writable() since that can move the skb data. */
+/* Must follow skb_ensure_writable() since that can move the skb data. */
 static void set_tp_port(struct sk_buff *skb, __be16 *port,
                         __be16 new_port, __sum16 *check)
 {
@@ -537,8 +458,8 @@ static int set_udp(struct sk_buff *skb, struct sw_flow_key *key,
        struct udphdr *uh;
        int err;
 
-       err = make_writable(skb, skb_transport_offset(skb) +
-                                sizeof(struct udphdr));
+       err = skb_ensure_writable(skb, skb_transport_offset(skb) +
+                                 sizeof(struct udphdr));
        if (unlikely(err))
                return err;
 
@@ -562,8 +483,8 @@ static int set_tcp(struct sk_buff *skb, struct sw_flow_key *key,
        struct tcphdr *th;
        int err;
 
-       err = make_writable(skb, skb_transport_offset(skb) +
-                                sizeof(struct tcphdr));
+       err = skb_ensure_writable(skb, skb_transport_offset(skb) +
+                                 sizeof(struct tcphdr));
        if (unlikely(err))
                return err;
 
@@ -588,7 +509,7 @@ static int set_sctp(struct sk_buff *skb, struct sw_flow_key *key,
        int err;
        unsigned int sctphoff = skb_transport_offset(skb);
 
-       err = make_writable(skb, sctphoff + sizeof(struct sctphdr));
+       err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
        if (unlikely(err))
                return err;
 
@@ -629,10 +550,10 @@ static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
 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;
-       struct ovs_tunnel_info info;
 
        upcall.cmd = OVS_PACKET_CMD_ACTION;
        upcall.userdata = NULL;
@@ -672,11 +593,6 @@ static int output_userspace(struct datapath *dp, struct sk_buff *skb,
        return ovs_dp_upcall(dp, skb, key, &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)
 {
@@ -711,7 +627,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);
@@ -797,7 +713,8 @@ static int execute_set_action(struct sk_buff *skb, struct sw_flow_key *key,
 }
 
 static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
-                         struct sw_flow_key *key, const struct nlattr *a, int rem)
+                         struct sw_flow_key *key,
+                         const struct nlattr *a, int rem)
 {
        struct deferred_action *da;
 
@@ -807,11 +724,10 @@ static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
                err = ovs_flow_key_update(skb, key);
                if (err)
                        return err;
-
        }
        BUG_ON(!is_flow_key_valid(key));
 
-       if (!last_action(a, rem)) {
+       if (!nla_is_last(a, rem)) {
                /* Recirc action is the not the last action
                 * of the action list, need to clone the skb.
                 */
@@ -888,8 +804,6 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
 
                case OVS_ACTION_ATTR_PUSH_VLAN:
                        err = push_vlan(skb, key, nla_data(a));
-                       if (unlikely(err)) /* skb already freed. */
-                               return err;
                        break;
 
                case OVS_ACTION_ATTR_POP_VLAN:
@@ -898,7 +812,7 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
 
                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.
@@ -942,13 +856,14 @@ static void process_deferred_actions(struct datapath *dp)
        do {
                struct deferred_action *da = action_fifo_get(fifo);
                struct sk_buff *skb = da->skb;
+               struct sw_flow_key *key = &da->pkt_key;
                const struct nlattr *actions = da->actions;
 
                if (actions)
-                       do_execute_actions(dp, skb, &da->pkt_key, actions,
+                       do_execute_actions(dp, skb, key, actions,
                                           nla_len(actions));
                else
-                       ovs_dp_process_packet(skb, &da->pkt_key);
+                       ovs_dp_process_packet(skb, key);
        } while (!action_fifo_is_empty(fifo));
 
        /* Reset FIFO for the next packet.  */
@@ -957,8 +872,8 @@ 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,
-                       const struct sw_flow_actions *acts)
+                       const struct sw_flow_actions *acts,
+                       struct sw_flow_key *key)
 {
        int level = this_cpu_read(exec_actions_level);
        int err;
@@ -973,8 +888,8 @@ int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
        }
 
        this_cpu_inc(exec_actions_level);
-
-       err = do_execute_actions(dp, skb, key, acts->actions, acts->actions_len);
+       err = do_execute_actions(dp, skb, key,
+                                acts->actions, acts->actions_len);
 
        if (!level)
                process_deferred_actions(dp);