2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3 * Copyright (c) 2013 Simon Horman
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include "odp-execute.h"
20 #include <linux/openvswitch.h>
29 #include "unaligned.h"
33 odp_eth_set_addrs(struct ofpbuf *packet,
34 const struct ovs_key_ethernet *eth_key)
36 struct eth_header *eh = packet->l2;
38 memcpy(eh->eth_src, eth_key->eth_src, sizeof eh->eth_src);
39 memcpy(eh->eth_dst, eth_key->eth_dst, sizeof eh->eth_dst);
43 odp_set_tunnel_action(const struct nlattr *a, struct flow_tnl *tun_key)
45 enum odp_key_fitness fitness;
47 fitness = odp_tun_key_from_attr(a, tun_key);
48 ovs_assert(fitness != ODP_FIT_ERROR);
52 set_arp(struct ofpbuf *packet, const struct ovs_key_arp *arp_key)
54 struct arp_eth_header *arp = packet->l3;
56 arp->ar_op = arp_key->arp_op;
57 memcpy(arp->ar_sha, arp_key->arp_sha, ETH_ADDR_LEN);
58 put_16aligned_be32(&arp->ar_spa, arp_key->arp_sip);
59 memcpy(arp->ar_tha, arp_key->arp_tha, ETH_ADDR_LEN);
60 put_16aligned_be32(&arp->ar_tpa, arp_key->arp_tip);
64 odp_execute_set_action(struct ofpbuf *packet, const struct nlattr *a,
65 struct pkt_metadata *md)
67 enum ovs_key_attr type = nl_attr_type(a);
68 const struct ovs_key_ipv4 *ipv4_key;
69 const struct ovs_key_ipv6 *ipv6_key;
70 const struct ovs_key_tcp *tcp_key;
71 const struct ovs_key_udp *udp_key;
72 const struct ovs_key_sctp *sctp_key;
75 case OVS_KEY_ATTR_PRIORITY:
76 md->skb_priority = nl_attr_get_u32(a);
79 case OVS_KEY_ATTR_TUNNEL:
80 odp_set_tunnel_action(a, &md->tunnel);
83 case OVS_KEY_ATTR_SKB_MARK:
84 md->pkt_mark = nl_attr_get_u32(a);
87 case OVS_KEY_ATTR_ETHERNET:
88 odp_eth_set_addrs(packet,
89 nl_attr_get_unspec(a, sizeof(struct ovs_key_ethernet)));
92 case OVS_KEY_ATTR_IPV4:
93 ipv4_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv4));
94 packet_set_ipv4(packet, ipv4_key->ipv4_src, ipv4_key->ipv4_dst,
95 ipv4_key->ipv4_tos, ipv4_key->ipv4_ttl);
98 case OVS_KEY_ATTR_IPV6:
99 ipv6_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv6));
100 packet_set_ipv6(packet, ipv6_key->ipv6_proto, ipv6_key->ipv6_src,
101 ipv6_key->ipv6_dst, ipv6_key->ipv6_tclass,
102 ipv6_key->ipv6_label, ipv6_key->ipv6_hlimit);
105 case OVS_KEY_ATTR_TCP:
106 tcp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp));
107 packet_set_tcp_port(packet, tcp_key->tcp_src, tcp_key->tcp_dst);
110 case OVS_KEY_ATTR_UDP:
111 udp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_udp));
112 packet_set_udp_port(packet, udp_key->udp_src, udp_key->udp_dst);
115 case OVS_KEY_ATTR_SCTP:
116 sctp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_sctp));
117 packet_set_sctp_port(packet, sctp_key->sctp_src, sctp_key->sctp_dst);
120 case OVS_KEY_ATTR_MPLS:
121 set_mpls_lse(packet, nl_attr_get_be32(a));
124 case OVS_KEY_ATTR_ARP:
125 set_arp(packet, nl_attr_get_unspec(a, sizeof(struct ovs_key_arp)));
128 case OVS_KEY_ATTR_UNSPEC:
129 case OVS_KEY_ATTR_ENCAP:
130 case OVS_KEY_ATTR_ETHERTYPE:
131 case OVS_KEY_ATTR_IN_PORT:
132 case OVS_KEY_ATTR_VLAN:
133 case OVS_KEY_ATTR_ICMP:
134 case OVS_KEY_ATTR_ICMPV6:
135 case OVS_KEY_ATTR_ND:
136 case OVS_KEY_ATTR_TCP_FLAGS:
137 case __OVS_KEY_ATTR_MAX:
144 odp_execute_actions__(void *dp, struct ofpbuf *packet, struct pkt_metadata *,
145 const struct nlattr *actions, size_t actions_len,
146 odp_execute_cb dp_execute_action, bool more_actions);
149 odp_execute_sample(void *dp, struct ofpbuf *packet, struct pkt_metadata *md,
150 const struct nlattr *action,
151 odp_execute_cb dp_execute_action, bool more_actions)
153 const struct nlattr *subactions = NULL;
154 const struct nlattr *a;
157 NL_NESTED_FOR_EACH_UNSAFE (a, left, action) {
158 int type = nl_attr_type(a);
160 switch ((enum ovs_sample_attr) type) {
161 case OVS_SAMPLE_ATTR_PROBABILITY:
162 if (random_uint32() >= nl_attr_get_u32(a)) {
167 case OVS_SAMPLE_ATTR_ACTIONS:
171 case OVS_SAMPLE_ATTR_UNSPEC:
172 case __OVS_SAMPLE_ATTR_MAX:
178 odp_execute_actions__(dp, packet, md, nl_attr_get(subactions),
179 nl_attr_get_size(subactions), dp_execute_action,
184 odp_execute_actions__(void *dp, struct ofpbuf *packet, struct pkt_metadata *md,
185 const struct nlattr *actions, size_t actions_len,
186 odp_execute_cb dp_execute_action, bool more_actions)
188 const struct nlattr *a;
191 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
192 int type = nl_attr_type(a);
194 switch ((enum ovs_action_attr) type) {
195 /* These only make sense in the context of a datapath. */
196 case OVS_ACTION_ATTR_OUTPUT:
197 case OVS_ACTION_ATTR_USERSPACE:
198 if (dp_execute_action) {
199 /* Allow 'dp_execute_action' to steal the packet data if we do
200 * not need it any more. */
201 bool steal = !more_actions && left <= NLA_ALIGN(a->nla_len);
202 dp_execute_action(dp, packet, md, a, steal);
206 case OVS_ACTION_ATTR_PUSH_VLAN: {
207 const struct ovs_action_push_vlan *vlan = nl_attr_get(a);
208 eth_push_vlan(packet, htons(ETH_TYPE_VLAN), vlan->vlan_tci);
212 case OVS_ACTION_ATTR_POP_VLAN:
213 eth_pop_vlan(packet);
216 case OVS_ACTION_ATTR_PUSH_MPLS: {
217 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
218 push_mpls(packet, mpls->mpls_ethertype, mpls->mpls_lse);
222 case OVS_ACTION_ATTR_POP_MPLS:
223 pop_mpls(packet, nl_attr_get_be16(a));
226 case OVS_ACTION_ATTR_SET:
227 odp_execute_set_action(packet, nl_attr_get(a), md);
230 case OVS_ACTION_ATTR_SAMPLE:
231 odp_execute_sample(dp, packet, md, a, dp_execute_action,
232 more_actions || left > NLA_ALIGN(a->nla_len));
235 case OVS_ACTION_ATTR_UNSPEC:
236 case __OVS_ACTION_ATTR_MAX:
243 odp_execute_actions(void *dp, struct ofpbuf *packet, struct pkt_metadata *md,
244 const struct nlattr *actions, size_t actions_len,
245 odp_execute_cb dp_execute_action)
247 odp_execute_actions__(dp, packet, md, actions, actions_len,
248 dp_execute_action, false);