2013-05-29 15:06:38 +09:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
|
|
|
|
* Copyright (c) 2013 Simon Horman
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include "odp-execute.h"
|
|
|
|
#include <linux/openvswitch.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-12-30 15:58:58 -08:00
|
|
|
#include "dpif.h"
|
2013-05-29 15:06:38 +09:00
|
|
|
#include "netlink.h"
|
|
|
|
#include "ofpbuf.h"
|
2013-06-05 14:28:49 +09:00
|
|
|
#include "odp-util.h"
|
2013-05-29 15:06:38 +09:00
|
|
|
#include "packets.h"
|
2013-10-09 17:37:30 -07:00
|
|
|
#include "unaligned.h"
|
2013-05-29 15:06:38 +09:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void
|
2013-12-30 15:58:58 -08:00
|
|
|
odp_eth_set_addrs(struct ofpbuf *packet,
|
|
|
|
const struct ovs_key_ethernet *eth_key)
|
2013-05-29 15:06:38 +09:00
|
|
|
{
|
|
|
|
struct eth_header *eh = packet->l2;
|
|
|
|
|
|
|
|
memcpy(eh->eth_src, eth_key->eth_src, sizeof eh->eth_src);
|
|
|
|
memcpy(eh->eth_dst, eth_key->eth_dst, sizeof eh->eth_dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-06-05 14:28:49 +09:00
|
|
|
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);
|
|
|
|
ovs_assert(fitness != ODP_FIT_ERROR);
|
|
|
|
}
|
|
|
|
|
2013-10-09 17:37:30 -07:00
|
|
|
static void
|
|
|
|
set_arp(struct ofpbuf *packet, const struct ovs_key_arp *arp_key)
|
|
|
|
{
|
lib/ofpbuf: Compact
This patch shrinks the struct ofpbuf from 104 to 48 bytes on 64-bit
systems, or from 52 to 36 bytes on 32-bit systems (counting in the
'l7' removal from an earlier patch). This may help contribute to
cache efficiency, and will speed up initializing, copying and
manipulating ofpbufs. This is potentially important for the DPDK
datapath, but the rest of the code base may also see a little benefit.
Changes are:
- Remove 'l7' pointer (previous patch).
- Use offsets instead of layer pointers for l2_5, l3, and l4 using
'l2' as basis. Usually 'data' is the same as 'l2', but this is not
always the case (e.g., when parsing or constructing a packet), so it
can not be easily used as the offset basis. Also, packet parsing is
faster if we do not need to maintain the offsets each time we pull
data from the ofpbuf.
- Use uint32_t for 'allocated' and 'size', as 2^32 is enough even for
largest possible messages/packets.
- Use packed enum for 'source'.
- Rearrange to avoid unnecessary padding.
- Remove 'private_p', which was used only in two cases, both of which
had the invariant ('l2' == 'data'), so we can temporarily use 'l2'
as a private pointer.
Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
2014-03-24 09:17:01 -07:00
|
|
|
struct arp_eth_header *arp = ofpbuf_get_l3(packet);
|
2013-10-09 17:37:30 -07:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-06-05 14:28:49 +09:00
|
|
|
static void
|
|
|
|
odp_execute_set_action(struct ofpbuf *packet, const struct nlattr *a,
|
2013-12-30 15:58:58 -08:00
|
|
|
struct pkt_metadata *md)
|
2013-05-29 15:06:38 +09:00
|
|
|
{
|
|
|
|
enum ovs_key_attr type = nl_attr_type(a);
|
|
|
|
const struct ovs_key_ipv4 *ipv4_key;
|
|
|
|
const struct ovs_key_ipv6 *ipv6_key;
|
|
|
|
const struct ovs_key_tcp *tcp_key;
|
|
|
|
const struct ovs_key_udp *udp_key;
|
2013-08-22 20:24:44 +12:00
|
|
|
const struct ovs_key_sctp *sctp_key;
|
2013-05-29 15:06:38 +09:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case OVS_KEY_ATTR_PRIORITY:
|
2013-12-30 15:58:58 -08:00
|
|
|
md->skb_priority = nl_attr_get_u32(a);
|
2013-06-05 14:28:49 +09:00
|
|
|
break;
|
|
|
|
|
2013-05-29 15:06:38 +09:00
|
|
|
case OVS_KEY_ATTR_TUNNEL:
|
2013-12-30 15:58:58 -08:00
|
|
|
odp_set_tunnel_action(a, &md->tunnel);
|
2013-06-05 14:28:49 +09:00
|
|
|
break;
|
|
|
|
|
2013-05-29 15:06:38 +09:00
|
|
|
case OVS_KEY_ATTR_SKB_MARK:
|
2013-12-30 15:58:58 -08:00
|
|
|
md->pkt_mark = nl_attr_get_u32(a);
|
2013-05-29 15:06:38 +09:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OVS_KEY_ATTR_ETHERNET:
|
|
|
|
odp_eth_set_addrs(packet,
|
|
|
|
nl_attr_get_unspec(a, sizeof(struct ovs_key_ethernet)));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OVS_KEY_ATTR_IPV4:
|
|
|
|
ipv4_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv4));
|
|
|
|
packet_set_ipv4(packet, ipv4_key->ipv4_src, ipv4_key->ipv4_dst,
|
|
|
|
ipv4_key->ipv4_tos, ipv4_key->ipv4_ttl);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OVS_KEY_ATTR_IPV6:
|
|
|
|
ipv6_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv6));
|
|
|
|
packet_set_ipv6(packet, ipv6_key->ipv6_proto, ipv6_key->ipv6_src,
|
|
|
|
ipv6_key->ipv6_dst, ipv6_key->ipv6_tclass,
|
|
|
|
ipv6_key->ipv6_label, ipv6_key->ipv6_hlimit);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OVS_KEY_ATTR_TCP:
|
|
|
|
tcp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp));
|
|
|
|
packet_set_tcp_port(packet, tcp_key->tcp_src, tcp_key->tcp_dst);
|
|
|
|
break;
|
|
|
|
|
2013-06-11 11:05:55 +09:00
|
|
|
case OVS_KEY_ATTR_UDP:
|
2013-05-29 15:06:38 +09:00
|
|
|
udp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_udp));
|
|
|
|
packet_set_udp_port(packet, udp_key->udp_src, udp_key->udp_dst);
|
|
|
|
break;
|
|
|
|
|
2013-08-22 20:24:44 +12:00
|
|
|
case OVS_KEY_ATTR_SCTP:
|
|
|
|
sctp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_sctp));
|
|
|
|
packet_set_sctp_port(packet, sctp_key->sctp_src, sctp_key->sctp_dst);
|
|
|
|
break;
|
|
|
|
|
2013-06-11 11:05:55 +09:00
|
|
|
case OVS_KEY_ATTR_MPLS:
|
2013-05-29 15:06:38 +09:00
|
|
|
set_mpls_lse(packet, nl_attr_get_be32(a));
|
|
|
|
break;
|
|
|
|
|
2013-10-09 17:37:30 -07:00
|
|
|
case OVS_KEY_ATTR_ARP:
|
|
|
|
set_arp(packet, nl_attr_get_unspec(a, sizeof(struct ovs_key_arp)));
|
|
|
|
break;
|
|
|
|
|
2014-03-04 15:36:03 -08:00
|
|
|
case OVS_KEY_ATTR_DP_HASH:
|
|
|
|
md->dp_hash = nl_attr_get_u32(a);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OVS_KEY_ATTR_RECIRC_ID:
|
|
|
|
md->recirc_id = nl_attr_get_u32(a);
|
|
|
|
break;
|
|
|
|
|
2013-06-11 11:05:55 +09:00
|
|
|
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:
|
2013-10-28 13:54:40 -07:00
|
|
|
case OVS_KEY_ATTR_TCP_FLAGS:
|
2013-06-11 11:05:55 +09:00
|
|
|
case __OVS_KEY_ATTR_MAX:
|
|
|
|
default:
|
2013-12-17 10:32:12 -08:00
|
|
|
OVS_NOT_REACHED();
|
2013-05-29 15:06:38 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-16 08:14:52 -08:00
|
|
|
static void
|
2014-03-20 10:54:37 -07:00
|
|
|
odp_execute_actions__(void *dp, struct ofpbuf *packet, bool steal,
|
|
|
|
struct pkt_metadata *,
|
2013-12-16 08:14:52 -08:00
|
|
|
const struct nlattr *actions, size_t actions_len,
|
2013-12-30 15:58:58 -08:00
|
|
|
odp_execute_cb dp_execute_action, bool more_actions);
|
2013-12-16 08:14:52 -08:00
|
|
|
|
2013-05-29 15:06:38 +09:00
|
|
|
static void
|
2014-03-20 10:54:37 -07:00
|
|
|
odp_execute_sample(void *dp, struct ofpbuf *packet, bool steal,
|
|
|
|
struct pkt_metadata *md, const struct nlattr *action,
|
2013-12-30 15:58:58 -08:00
|
|
|
odp_execute_cb dp_execute_action, bool more_actions)
|
2013-05-29 15:06:38 +09:00
|
|
|
{
|
|
|
|
const struct nlattr *subactions = NULL;
|
|
|
|
const struct nlattr *a;
|
|
|
|
size_t left;
|
|
|
|
|
|
|
|
NL_NESTED_FOR_EACH_UNSAFE (a, left, action) {
|
|
|
|
int type = nl_attr_type(a);
|
|
|
|
|
|
|
|
switch ((enum ovs_sample_attr) type) {
|
|
|
|
case OVS_SAMPLE_ATTR_PROBABILITY:
|
|
|
|
if (random_uint32() >= nl_attr_get_u32(a)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OVS_SAMPLE_ATTR_ACTIONS:
|
|
|
|
subactions = a;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OVS_SAMPLE_ATTR_UNSPEC:
|
|
|
|
case __OVS_SAMPLE_ATTR_MAX:
|
|
|
|
default:
|
2013-12-17 10:32:12 -08:00
|
|
|
OVS_NOT_REACHED();
|
2013-05-29 15:06:38 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-20 10:54:37 -07:00
|
|
|
odp_execute_actions__(dp, packet, steal, md, nl_attr_get(subactions),
|
2013-12-30 15:58:58 -08:00
|
|
|
nl_attr_get_size(subactions), dp_execute_action,
|
2013-12-16 08:14:52 -08:00
|
|
|
more_actions);
|
2013-05-29 15:06:38 +09:00
|
|
|
}
|
|
|
|
|
2013-12-16 08:14:52 -08:00
|
|
|
static void
|
2014-03-20 10:54:37 -07:00
|
|
|
odp_execute_actions__(void *dp, struct ofpbuf *packet, bool steal,
|
|
|
|
struct pkt_metadata *md,
|
2013-12-16 08:14:52 -08:00
|
|
|
const struct nlattr *actions, size_t actions_len,
|
2013-12-30 15:58:58 -08:00
|
|
|
odp_execute_cb dp_execute_action, bool more_actions)
|
2013-05-29 15:06:38 +09:00
|
|
|
{
|
|
|
|
const struct nlattr *a;
|
|
|
|
unsigned int left;
|
|
|
|
|
|
|
|
NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
|
|
|
|
int type = nl_attr_type(a);
|
|
|
|
|
|
|
|
switch ((enum ovs_action_attr) type) {
|
2013-12-30 15:58:58 -08:00
|
|
|
/* These only make sense in the context of a datapath. */
|
2013-05-29 15:06:38 +09:00
|
|
|
case OVS_ACTION_ATTR_OUTPUT:
|
2013-12-30 15:58:58 -08:00
|
|
|
case OVS_ACTION_ATTR_USERSPACE:
|
2014-03-04 15:36:03 -08:00
|
|
|
case OVS_ACTION_ATTR_RECIRC:
|
2013-12-30 15:58:58 -08:00
|
|
|
if (dp_execute_action) {
|
2014-03-20 10:54:37 -07:00
|
|
|
bool may_steal;
|
2013-12-30 15:58:58 -08:00
|
|
|
/* Allow 'dp_execute_action' to steal the packet data if we do
|
|
|
|
* not need it any more. */
|
2014-03-20 10:54:37 -07:00
|
|
|
may_steal = steal && (!more_actions && left <= NLA_ALIGN(a->nla_len));
|
|
|
|
dp_execute_action(dp, packet, md, a, may_steal);
|
2013-10-18 17:27:51 -07:00
|
|
|
}
|
2013-05-29 15:06:38 +09:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OVS_ACTION_ATTR_PUSH_VLAN: {
|
|
|
|
const struct ovs_action_push_vlan *vlan = nl_attr_get(a);
|
2014-01-15 17:17:01 +09:00
|
|
|
eth_push_vlan(packet, htons(ETH_TYPE_VLAN), vlan->vlan_tci);
|
2013-05-29 15:06:38 +09:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OVS_ACTION_ATTR_POP_VLAN:
|
|
|
|
eth_pop_vlan(packet);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OVS_ACTION_ATTR_PUSH_MPLS: {
|
|
|
|
const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
|
|
|
|
push_mpls(packet, mpls->mpls_ethertype, mpls->mpls_lse);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OVS_ACTION_ATTR_POP_MPLS:
|
|
|
|
pop_mpls(packet, nl_attr_get_be16(a));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OVS_ACTION_ATTR_SET:
|
2013-12-30 15:58:58 -08:00
|
|
|
odp_execute_set_action(packet, nl_attr_get(a), md);
|
2013-05-29 15:06:38 +09:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OVS_ACTION_ATTR_SAMPLE:
|
2014-03-20 10:54:37 -07:00
|
|
|
odp_execute_sample(dp, packet, steal, md, a, dp_execute_action,
|
2013-12-16 08:14:52 -08:00
|
|
|
more_actions || left > NLA_ALIGN(a->nla_len));
|
2013-05-29 15:06:38 +09:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OVS_ACTION_ATTR_UNSPEC:
|
|
|
|
case __OVS_ACTION_ATTR_MAX:
|
2013-12-17 10:32:12 -08:00
|
|
|
OVS_NOT_REACHED();
|
2013-05-29 15:06:38 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-16 08:14:52 -08:00
|
|
|
|
|
|
|
void
|
2014-03-20 10:54:37 -07:00
|
|
|
odp_execute_actions(void *dp, struct ofpbuf *packet, bool steal,
|
|
|
|
struct pkt_metadata *md,
|
2013-12-16 08:14:52 -08:00
|
|
|
const struct nlattr *actions, size_t actions_len,
|
2013-12-30 15:58:58 -08:00
|
|
|
odp_execute_cb dp_execute_action)
|
2013-12-16 08:14:52 -08:00
|
|
|
{
|
2014-03-20 10:54:37 -07:00
|
|
|
odp_execute_actions__(dp, packet, steal, md, actions, actions_len,
|
2013-12-30 15:58:58 -08:00
|
|
|
dp_execute_action, false);
|
2014-03-20 10:54:37 -07:00
|
|
|
|
|
|
|
if (!actions_len && steal) {
|
|
|
|
/* Drop action. */
|
|
|
|
ofpbuf_delete(packet);
|
|
|
|
}
|
2013-12-16 08:14:52 -08:00
|
|
|
}
|