Skip to content

Commit

Permalink
lib/odp: Masked set action execution and printing.
Browse files Browse the repository at this point in the history
Add a new action type OVS_ACTION_ATTR_SET_MASKED, and support for
parsing, printing, and committing them.

Masked set actions add a mask, immediately following the netlink
attribute data, within the netlink attribute itself.  Thus the key
attribute size for a masked set action is exactly double of the
non-masked set action.

Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
  • Loading branch information
Jarno Rajahalme committed Sep 8, 2014
1 parent 2c622e5 commit 6d670e7
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 23 deletions.
10 changes: 10 additions & 0 deletions datapath/linux/compat/include/linux/openvswitch.h
Expand Up @@ -592,6 +592,12 @@ struct ovs_action_hash {
* @OVS_ACTION_ATTR_SET: Replaces the contents of an existing header. The
* single nested %OVS_KEY_ATTR_* attribute specifies a header to modify and its
* value.
* @OVS_ACTION_ATTR_SET_MASKED: Replaces the contents of an existing header. A
* nested %OVS_KEY_ATTR_* attribute specifies a header to modify, its value,
* and a mask. For every bit set in the mask, the corresponding bit value
* is copied from the value to the packet header field, rest of the bits are
* left unchanged. The non-masked value bits must be passed in as zeroes.
* Masking is not supported for the %OVS_KEY_ATTR_TUNNEL attribute.
* @OVS_ACTION_RECIRC: Recirculate within the data path.
* @OVS_ACTION_HASH: Compute and set flow hash value.
* @OVS_ACTION_ATTR_PUSH_MPLS: Push a new MPLS label stack entry onto the
Expand Down Expand Up @@ -621,6 +627,10 @@ enum ovs_action_attr {
OVS_ACTION_ATTR_HASH, /* struct ovs_action_hash. */
OVS_ACTION_ATTR_PUSH_MPLS, /* struct ovs_action_push_mpls. */
OVS_ACTION_ATTR_POP_MPLS, /* __be16 ethertype. */
OVS_ACTION_ATTR_SET_MASKED, /* One nested OVS_KEY_ATTR_* including
* data immediately followed by a mask.
* The data must be zero for the unmasked
* bits. */
__OVS_ACTION_ATTR_MAX
};

Expand Down
1 change: 1 addition & 0 deletions lib/dpif-netdev.c
Expand Up @@ -2558,6 +2558,7 @@ dp_execute_cb(void *aux_, struct dpif_packet **packets, int cnt,
case OVS_ACTION_ATTR_PUSH_MPLS:
case OVS_ACTION_ATTR_POP_MPLS:
case OVS_ACTION_ATTR_SET:
case OVS_ACTION_ATTR_SET_MASKED:
case OVS_ACTION_ATTR_SAMPLE:
case OVS_ACTION_ATTR_UNSPEC:
case __OVS_ACTION_ATTR_MAX:
Expand Down
1 change: 1 addition & 0 deletions lib/dpif.c
Expand Up @@ -1041,6 +1041,7 @@ dpif_execute_helper_cb(void *aux_, struct dpif_packet **packets, int cnt,
case OVS_ACTION_ATTR_PUSH_MPLS:
case OVS_ACTION_ATTR_POP_MPLS:
case OVS_ACTION_ATTR_SET:
case OVS_ACTION_ATTR_SET_MASKED:
case OVS_ACTION_ATTR_SAMPLE:
case OVS_ACTION_ATTR_UNSPEC:
case __OVS_ACTION_ATTR_MAX:
Expand Down
239 changes: 221 additions & 18 deletions lib/odp-execute.c
Expand Up @@ -17,6 +17,8 @@

#include <config.h>
#include "odp-execute.h"
#include <arpa/inet.h>
#include <netinet/ip6.h>
#include <stdlib.h>
#include <string.h>

Expand All @@ -31,18 +33,112 @@
#include "unaligned.h"
#include "util.h"

/* Masked copy of an ethernet address. 'src' is already properly masked. */
static void
odp_eth_set_addrs(struct ofpbuf *packet,
const struct ovs_key_ethernet *eth_key)
ether_addr_copy_masked(uint8_t *dst, const uint8_t *src,
const uint8_t *mask)
{
int i;

for (i = 0; i < ETH_ADDR_LEN; i++) {
dst[i] = src[i] | (dst[i] & ~mask[i]);
}
}

static void
odp_eth_set_addrs(struct ofpbuf *packet, const struct ovs_key_ethernet *key,
const struct ovs_key_ethernet *mask)
{
struct eth_header *eh = ofpbuf_l2(packet);

if (eh) {
memcpy(eh->eth_src, eth_key->eth_src, sizeof eh->eth_src);
memcpy(eh->eth_dst, eth_key->eth_dst, sizeof eh->eth_dst);
if (!mask) {
memcpy(eh->eth_src, key->eth_src, sizeof eh->eth_src);
memcpy(eh->eth_dst, key->eth_dst, sizeof eh->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);
}
}
}

static void
odp_set_ipv4(struct ofpbuf *packet, const struct ovs_key_ipv4 *key,
const struct ovs_key_ipv4 *mask)
{
struct ip_header *nh = ofpbuf_l3(packet);

packet_set_ipv4(
packet,
key->ipv4_src | (get_16aligned_be32(&nh->ip_src) & ~mask->ipv4_src),
key->ipv4_dst | (get_16aligned_be32(&nh->ip_dst) & ~mask->ipv4_dst),
key->ipv4_tos | (nh->ip_tos & ~mask->ipv4_tos),
key->ipv4_ttl | (nh->ip_ttl & ~mask->ipv4_ttl));
}

static const ovs_be32 *
mask_ipv6_addr(const ovs_16aligned_be32 *old, const ovs_be32 *addr,
const ovs_be32 *mask, ovs_be32 *masked)
{
for (int i = 0; i < 4; i++) {
masked[i] = addr[i] | (get_16aligned_be32(&old[i]) & ~mask[i]);
}

return masked;
}

static void
odp_set_ipv6(struct ofpbuf *packet, const struct ovs_key_ipv6 *key,
const struct ovs_key_ipv6 *mask)
{
struct ovs_16aligned_ip6_hdr *nh = ofpbuf_l3(packet);
ovs_be32 sbuf[4], dbuf[4];
uint8_t old_tc = ntohl(get_16aligned_be32(&nh->ip6_flow)) >> 20;
ovs_be32 old_fl = get_16aligned_be32(&nh->ip6_flow) & htonl(0xfffff);

packet_set_ipv6(
packet,
key->ipv6_proto,
mask_ipv6_addr(nh->ip6_src.be32, key->ipv6_src, mask->ipv6_src, sbuf),
mask_ipv6_addr(nh->ip6_dst.be32, key->ipv6_dst, mask->ipv6_dst, dbuf),
key->ipv6_tclass | (old_tc & ~mask->ipv6_tclass),
key->ipv6_label | (old_fl & ~mask->ipv6_label),
key->ipv6_hlimit | (nh->ip6_hlim & ~mask->ipv6_hlimit));
}

static void
odp_set_tcp(struct ofpbuf *packet, const struct ovs_key_tcp *key,
const struct ovs_key_tcp *mask)
{
struct tcp_header *th = ofpbuf_l4(packet);

packet_set_tcp_port(packet,
key->tcp_src | (th->tcp_src & ~mask->tcp_src),
key->tcp_dst | (th->tcp_dst & ~mask->tcp_dst));
}

static void
odp_set_udp(struct ofpbuf *packet, const struct ovs_key_udp *key,
const struct ovs_key_udp *mask)
{
struct udp_header *uh = ofpbuf_l4(packet);

packet_set_udp_port(packet,
key->udp_src | (uh->udp_src & ~mask->udp_src),
key->udp_dst | (uh->udp_dst & ~mask->udp_dst));
}

static void
odp_set_sctp(struct ofpbuf *packet, const struct ovs_key_sctp *key,
const struct ovs_key_sctp *mask)
{
struct sctp_header *sh = ofpbuf_l4(packet);

packet_set_sctp_port(packet,
key->sctp_src | (sh->sctp_src & ~mask->sctp_src),
key->sctp_dst | (sh->sctp_dst & ~mask->sctp_dst));
}

static void
odp_set_tunnel_action(const struct nlattr *a, struct flow_tnl *tun_key)
{
Expand All @@ -53,15 +149,29 @@ odp_set_tunnel_action(const struct nlattr *a, struct flow_tnl *tun_key)
}

static void
set_arp(struct ofpbuf *packet, const struct ovs_key_arp *arp_key)
set_arp(struct ofpbuf *packet, const struct ovs_key_arp *key,
const struct ovs_key_arp *mask)
{
struct arp_eth_header *arp = ofpbuf_l3(packet);

arp->ar_op = arp_key->arp_op;
memcpy(arp->ar_sha, arp_key->arp_sha, ETH_ADDR_LEN);
put_16aligned_be32(&arp->ar_spa, arp_key->arp_sip);
memcpy(arp->ar_tha, arp_key->arp_tha, ETH_ADDR_LEN);
put_16aligned_be32(&arp->ar_tpa, arp_key->arp_tip);
if (!mask) {
arp->ar_op = key->arp_op;
memcpy(arp->ar_sha, key->arp_sha, ETH_ADDR_LEN);
put_16aligned_be32(&arp->ar_spa, key->arp_sip);
memcpy(arp->ar_tha, key->arp_tha, ETH_ADDR_LEN);
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);
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);
put_16aligned_be32(&arp->ar_tpa,
key->arp_tip | (ar_tpa & ~mask->arp_tip));
}
}

static void
Expand Down Expand Up @@ -89,8 +199,7 @@ odp_execute_set_action(struct dpif_packet *packet, const struct nlattr *a,
break;

case OVS_KEY_ATTR_ETHERNET:
odp_eth_set_addrs(&packet->ofpbuf,
nl_attr_get_unspec(a, sizeof(struct ovs_key_ethernet)));
odp_eth_set_addrs(&packet->ofpbuf, nl_attr_get(a), NULL);
break;

case OVS_KEY_ATTR_IPV4:
Expand Down Expand Up @@ -127,12 +236,11 @@ odp_execute_set_action(struct dpif_packet *packet, const struct nlattr *a,
break;

case OVS_KEY_ATTR_MPLS:
set_mpls_lse(&packet->ofpbuf, nl_attr_get_be32(a));
break;
set_mpls_lse(&packet->ofpbuf, nl_attr_get_be32(a));
break;

case OVS_KEY_ATTR_ARP:
set_arp(&packet->ofpbuf,
nl_attr_get_unspec(a, sizeof(struct ovs_key_arp)));
set_arp(&packet->ofpbuf, nl_attr_get(a), NULL);
break;

case OVS_KEY_ATTR_DP_HASH:
Expand All @@ -159,6 +267,96 @@ odp_execute_set_action(struct dpif_packet *packet, const struct nlattr *a,
}
}

#define get_mask(a, type) ((const type *)(const void *)(a + 1) + 1)

static void
odp_execute_masked_set_action(struct dpif_packet *packet,
const struct nlattr *a, struct pkt_metadata *md)
{
enum ovs_key_attr type = nl_attr_type(a);
struct mpls_hdr *mh;

switch (type) {
case OVS_KEY_ATTR_PRIORITY:
md->skb_priority = nl_attr_get_u32(a)
| (md->skb_priority & ~*get_mask(a, uint32_t));
break;

case OVS_KEY_ATTR_SKB_MARK:
md->pkt_mark = nl_attr_get_u32(a)
| (md->pkt_mark & ~*get_mask(a, uint32_t));
break;

case OVS_KEY_ATTR_ETHERNET:
odp_eth_set_addrs(&packet->ofpbuf, nl_attr_get(a),
get_mask(a, struct ovs_key_ethernet));
break;

case OVS_KEY_ATTR_IPV4:
odp_set_ipv4(&packet->ofpbuf, nl_attr_get(a),
get_mask(a, struct ovs_key_ipv4));
break;

case OVS_KEY_ATTR_IPV6:
odp_set_ipv6(&packet->ofpbuf, nl_attr_get(a),
get_mask(a, struct ovs_key_ipv6));
break;

case OVS_KEY_ATTR_TCP:
odp_set_tcp(&packet->ofpbuf, nl_attr_get(a),
get_mask(a, struct ovs_key_tcp));
break;

case OVS_KEY_ATTR_UDP:
odp_set_udp(&packet->ofpbuf, nl_attr_get(a),
get_mask(a, struct ovs_key_udp));
break;

case OVS_KEY_ATTR_SCTP:
odp_set_sctp(&packet->ofpbuf, nl_attr_get(a),
get_mask(a, struct ovs_key_sctp));
break;

case OVS_KEY_ATTR_MPLS:
mh = ofpbuf_l2_5(&packet->ofpbuf);
if (mh) {
put_16aligned_be32(&mh->mpls_lse, nl_attr_get_be32(a)
| (get_16aligned_be32(&mh->mpls_lse)
& ~*get_mask(a, ovs_be32)));
}
break;

case OVS_KEY_ATTR_ARP:
set_arp(&packet->ofpbuf, nl_attr_get(a),
get_mask(a, struct ovs_key_arp));
break;

case OVS_KEY_ATTR_DP_HASH:
packet->dp_hash = md->dp_hash = nl_attr_get_u32(a)
| (md->dp_hash & ~*get_mask(a, uint32_t));
break;

case OVS_KEY_ATTR_RECIRC_ID:
md->recirc_id = nl_attr_get_u32(a)
| (md->recirc_id & ~*get_mask(a, uint32_t));
break;

case OVS_KEY_ATTR_TUNNEL: /* Masked data not supported for tunnel. */
case OVS_KEY_ATTR_UNSPEC:
case OVS_KEY_ATTR_ENCAP:
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_ND:
case OVS_KEY_ATTR_TCP_FLAGS:
case __OVS_KEY_ATTR_MAX:
default:
OVS_NOT_REACHED();
}
}

static void
odp_execute_actions__(void *dp, struct dpif_packet **packets, int cnt,
bool steal, struct pkt_metadata *,
Expand Down Expand Up @@ -301,8 +499,13 @@ odp_execute_actions__(void *dp, struct dpif_packet **packets, int cnt,

case OVS_ACTION_ATTR_SET:
for (i = 0; i < cnt; i++) {
odp_execute_set_action(packets[i], nl_attr_get(a),
md);
odp_execute_set_action(packets[i], nl_attr_get(a), md);
}
break;

case OVS_ACTION_ATTR_SET_MASKED:
for (i = 0; i < cnt; i++) {
odp_execute_masked_set_action(packets[i], nl_attr_get(a), md);
}
break;

Expand Down

0 comments on commit 6d670e7

Please sign in to comment.