netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / lib / odp-execute.c
index ccd29d7..b5204b2 100644 (file)
 
 /* Masked copy of an ethernet address. 'src' is already properly masked. */
 static void
-ether_addr_copy_masked(uint8_t *dst, const uint8_t *src,
-                       const uint8_t *mask)
+ether_addr_copy_masked(struct eth_addr *dst, const struct eth_addr src,
+                       const struct eth_addr mask)
 {
     int i;
 
-    for (i = 0; i < ETH_ADDR_LEN; i++) {
-        dst[i] = src[i] | (dst[i] & ~mask[i]);
+    for (i = 0; i < ARRAY_SIZE(dst->be16); i++) {
+        dst->be16[i] = src.be16[i] | (dst->be16[i] & ~mask.be16[i]);
     }
 }
 
@@ -54,11 +54,11 @@ odp_eth_set_addrs(struct dp_packet *packet, const struct ovs_key_ethernet *key,
 
     if (eh) {
         if (!mask) {
-            memcpy(eh->eth_src, key->eth_src, sizeof eh->eth_src);
-            memcpy(eh->eth_dst, key->eth_dst, sizeof eh->eth_dst);
+            eh->eth_src = key->eth_src;
+            eh->eth_dst = key->eth_dst;
         } else {
-            ether_addr_copy_masked(eh->eth_src, key->eth_src, mask->eth_src);
-            ether_addr_copy_masked(eh->eth_dst, key->eth_dst, mask->eth_dst);
+            ether_addr_copy_masked(&eh->eth_src, key->eth_src, mask->eth_src);
+            ether_addr_copy_masked(&eh->eth_dst, key->eth_dst, mask->eth_dst);
         }
     }
 }
@@ -151,7 +151,7 @@ odp_set_tunnel_action(const struct nlattr *a, struct flow_tnl *tun_key)
 {
     enum odp_key_fitness fitness;
 
-    fitness = odp_tun_key_from_attr(a, tun_key);
+    fitness = odp_tun_key_from_attr(a, true, tun_key);
     ovs_assert(fitness != ODP_FIT_ERROR);
 }
 
@@ -163,19 +163,19 @@ set_arp(struct dp_packet *packet, const struct ovs_key_arp *key,
 
     if (!mask) {
         arp->ar_op = key->arp_op;
-        memcpy(arp->ar_sha, key->arp_sha, ETH_ADDR_LEN);
+        arp->ar_sha = key->arp_sha;
         put_16aligned_be32(&arp->ar_spa, key->arp_sip);
-        memcpy(arp->ar_tha, key->arp_tha, ETH_ADDR_LEN);
+        arp->ar_tha = key->arp_tha;
         put_16aligned_be32(&arp->ar_tpa, key->arp_tip);
     } else {
         ovs_be32 ar_spa = get_16aligned_be32(&arp->ar_spa);
         ovs_be32 ar_tpa = get_16aligned_be32(&arp->ar_tpa);
 
         arp->ar_op = key->arp_op | (arp->ar_op & ~mask->arp_op);
-        ether_addr_copy_masked(arp->ar_sha, key->arp_sha, mask->arp_sha);
+        ether_addr_copy_masked(&arp->ar_sha, key->arp_sha, mask->arp_sha);
         put_16aligned_be32(&arp->ar_spa,
                            key->arp_sip | (ar_spa & ~mask->arp_sip));
-        ether_addr_copy_masked(arp->ar_tha, key->arp_tha, mask->arp_tha);
+        ether_addr_copy_masked(&arp->ar_tha, key->arp_tha, mask->arp_tha);
         put_16aligned_be32(&arp->ar_tpa,
                            key->arp_tip | (ar_tpa & ~mask->arp_tip));
     }
@@ -191,21 +191,21 @@ odp_set_nd(struct dp_packet *packet, const struct ovs_key_nd *key,
     if (OVS_LIKELY(ns && nd_opt)) {
         int bytes_remain = dp_packet_l4_size(packet) - sizeof(*ns);
         ovs_be32 tgt_buf[4];
-        uint8_t sll_buf[ETH_ADDR_LEN] = {0};
-        uint8_t tll_buf[ETH_ADDR_LEN] = {0};
+        struct eth_addr sll_buf = eth_addr_zero;
+        struct eth_addr tll_buf = eth_addr_zero;
 
         while (bytes_remain >= ND_OPT_LEN && nd_opt->nd_opt_len != 0) {
             if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LINKADDR
                 && nd_opt->nd_opt_len == 1) {
-                memcpy(sll_buf, nd_opt->nd_opt_data, ETH_ADDR_LEN);
-                ether_addr_copy_masked(sll_buf, key->nd_sll, mask->nd_sll);
+                sll_buf = nd_opt->nd_opt_mac;
+                ether_addr_copy_masked(&sll_buf, key->nd_sll, mask->nd_sll);
 
                 /* A packet can only contain one SLL or TLL option */
                 break;
             } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LINKADDR
                        && nd_opt->nd_opt_len == 1) {
-                memcpy(tll_buf, nd_opt->nd_opt_data, ETH_ADDR_LEN);
-                ether_addr_copy_masked(tll_buf, key->nd_tll, mask->nd_tll);
+                tll_buf = nd_opt->nd_opt_mac;
+                ether_addr_copy_masked(&tll_buf, key->nd_tll, mask->nd_tll);
 
                 /* A packet can only contain one SLL or TLL option */
                 break;
@@ -301,18 +301,27 @@ odp_execute_set_action(struct dp_packet *packet, const struct nlattr *a)
         set_arp(packet, nl_attr_get(a), NULL);
         break;
 
+    case OVS_KEY_ATTR_ICMP:
+    case OVS_KEY_ATTR_ICMPV6:
+        if (OVS_LIKELY(dp_packet_get_icmp_payload(packet))) {
+            const struct ovs_key_icmp *icmp_key
+                = nl_attr_get_unspec(a, sizeof(struct ovs_key_icmp));
+
+            packet_set_icmp(packet, icmp_key->icmp_type, icmp_key->icmp_code);
+        }
+        break;
+
     case OVS_KEY_ATTR_ND:
         if (OVS_LIKELY(dp_packet_get_nd_payload(packet))) {
             const struct ovs_key_nd *nd_key
                    = nl_attr_get_unspec(a, sizeof(struct ovs_key_nd));
-            packet_set_nd(packet, nd_key->nd_target,
-                          nd_key->nd_sll, nd_key->nd_tll);
+            packet_set_nd(packet, nd_key->nd_target, nd_key->nd_sll,
+                          nd_key->nd_tll);
         }
         break;
 
     case OVS_KEY_ATTR_DP_HASH:
         md->dp_hash = nl_attr_get_u32(a);
-        dp_packet_set_dp_hash(packet, md->dp_hash);
         break;
 
     case OVS_KEY_ATTR_RECIRC_ID:
@@ -324,9 +333,11 @@ odp_execute_set_action(struct dp_packet *packet, const struct nlattr *a)
     case OVS_KEY_ATTR_ETHERTYPE:
     case OVS_KEY_ATTR_IN_PORT:
     case OVS_KEY_ATTR_VLAN:
-    case OVS_KEY_ATTR_ICMP:
-    case OVS_KEY_ATTR_ICMPV6:
     case OVS_KEY_ATTR_TCP_FLAGS:
+    case OVS_KEY_ATTR_CT_STATE:
+    case OVS_KEY_ATTR_CT_ZONE:
+    case OVS_KEY_ATTR_CT_MARK:
+    case OVS_KEY_ATTR_CT_LABELS:
     case __OVS_KEY_ATTR_MAX:
     default:
         OVS_NOT_REACHED();
@@ -405,8 +416,7 @@ odp_execute_masked_set_action(struct dp_packet *packet,
 
     case OVS_KEY_ATTR_DP_HASH:
         md->dp_hash = nl_attr_get_u32(a)
-            | (dp_packet_get_dp_hash(packet) & ~*get_mask(a, uint32_t));
-        dp_packet_set_dp_hash(packet, md->dp_hash);
+            | (md->dp_hash & ~*get_mask(a, uint32_t));
         break;
 
     case OVS_KEY_ATTR_RECIRC_ID:
@@ -416,6 +426,10 @@ odp_execute_masked_set_action(struct dp_packet *packet,
 
     case OVS_KEY_ATTR_TUNNEL:    /* Masked data not supported for tunnel. */
     case OVS_KEY_ATTR_UNSPEC:
+    case OVS_KEY_ATTR_CT_STATE:
+    case OVS_KEY_ATTR_CT_ZONE:
+    case OVS_KEY_ATTR_CT_MARK:
+    case OVS_KEY_ATTR_CT_LABELS:
     case OVS_KEY_ATTR_ENCAP:
     case OVS_KEY_ATTR_ETHERTYPE:
     case OVS_KEY_ATTR_IN_PORT:
@@ -466,6 +480,39 @@ odp_execute_sample(void *dp, struct dp_packet *packet, bool steal,
                         nl_attr_get_size(subactions), dp_execute_action);
 }
 
+static bool
+requires_datapath_assistance(const struct nlattr *a)
+{
+    enum ovs_action_attr type = nl_attr_type(a);
+
+    switch (type) {
+        /* These only make sense in the context of a datapath. */
+    case OVS_ACTION_ATTR_OUTPUT:
+    case OVS_ACTION_ATTR_TUNNEL_PUSH:
+    case OVS_ACTION_ATTR_TUNNEL_POP:
+    case OVS_ACTION_ATTR_USERSPACE:
+    case OVS_ACTION_ATTR_RECIRC:
+    case OVS_ACTION_ATTR_CT:
+        return true;
+
+    case OVS_ACTION_ATTR_SET:
+    case OVS_ACTION_ATTR_SET_MASKED:
+    case OVS_ACTION_ATTR_PUSH_VLAN:
+    case OVS_ACTION_ATTR_POP_VLAN:
+    case OVS_ACTION_ATTR_SAMPLE:
+    case OVS_ACTION_ATTR_HASH:
+    case OVS_ACTION_ATTR_PUSH_MPLS:
+    case OVS_ACTION_ATTR_POP_MPLS:
+        return false;
+
+    case OVS_ACTION_ATTR_UNSPEC:
+    case __OVS_ACTION_ATTR_MAX:
+        OVS_NOT_REACHED();
+    }
+
+    return false;
+}
+
 void
 odp_execute_actions(void *dp, struct dp_packet **packets, int cnt, bool steal,
                     const struct nlattr *actions, size_t actions_len,
@@ -479,13 +526,7 @@ odp_execute_actions(void *dp, struct dp_packet **packets, int cnt, bool steal,
         int type = nl_attr_type(a);
         bool last_action = (left <= NLA_ALIGN(a->nla_len));
 
-        switch ((enum ovs_action_attr) type) {
-            /* These only make sense in the context of a datapath. */
-        case OVS_ACTION_ATTR_OUTPUT:
-        case OVS_ACTION_ATTR_TUNNEL_PUSH:
-        case OVS_ACTION_ATTR_TUNNEL_POP:
-        case OVS_ACTION_ATTR_USERSPACE:
-        case OVS_ACTION_ATTR_RECIRC:
+        if (requires_datapath_assistance(a)) {
             if (dp_execute_action) {
                 /* Allow 'dp_execute_action' to steal the packet data if we do
                  * not need it any more. */
@@ -499,8 +540,10 @@ odp_execute_actions(void *dp, struct dp_packet **packets, int cnt, bool steal,
                     return;
                 }
             }
-            break;
+            continue;
+        }
 
+        switch ((enum ovs_action_attr) type) {
         case OVS_ACTION_ATTR_HASH: {
             const struct ovs_action_hash *hash_act = nl_attr_get(a);
 
@@ -516,8 +559,7 @@ odp_execute_actions(void *dp, struct dp_packet **packets, int cnt, bool steal,
                     flow_extract(packets[i], &flow);
                     hash = flow_hash_5tuple(&flow, hash_act->hash_basis);
 
-                    /* We also store the hash value with each packet */
-                    dp_packet_set_dp_hash(packets[i], hash ? hash : 1);
+                    packets[i]->md.dp_hash = hash;
                 }
             } else {
                 /* Assert on unknown hash algorithm.  */
@@ -530,7 +572,7 @@ odp_execute_actions(void *dp, struct dp_packet **packets, int cnt, bool steal,
             const struct ovs_action_push_vlan *vlan = nl_attr_get(a);
 
             for (i = 0; i < cnt; i++) {
-                eth_push_vlan(packets[i], htons(ETH_TYPE_VLAN), vlan->vlan_tci);
+                eth_push_vlan(packets[i], vlan->vlan_tpid, vlan->vlan_tci);
             }
             break;
         }
@@ -581,6 +623,12 @@ odp_execute_actions(void *dp, struct dp_packet **packets, int cnt, bool steal,
             }
             break;
 
+        case OVS_ACTION_ATTR_OUTPUT:
+        case OVS_ACTION_ATTR_TUNNEL_PUSH:
+        case OVS_ACTION_ATTR_TUNNEL_POP:
+        case OVS_ACTION_ATTR_USERSPACE:
+        case OVS_ACTION_ATTR_RECIRC:
+        case OVS_ACTION_ATTR_CT:
         case OVS_ACTION_ATTR_UNSPEC:
         case __OVS_ACTION_ATTR_MAX:
             OVS_NOT_REACHED();