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>
31 odp_eth_set_addrs(struct ofpbuf
*packet
, const struct ovs_key_ethernet
*eth_key
)
33 struct eth_header
*eh
= packet
->l2
;
35 memcpy(eh
->eth_src
, eth_key
->eth_src
, sizeof eh
->eth_src
);
36 memcpy(eh
->eth_dst
, eth_key
->eth_dst
, sizeof eh
->eth_dst
);
40 odp_set_tunnel_action(const struct nlattr
*a
, struct flow_tnl
*tun_key
)
42 enum odp_key_fitness fitness
;
44 fitness
= odp_tun_key_from_attr(a
, tun_key
);
45 ovs_assert(fitness
!= ODP_FIT_ERROR
);
49 odp_execute_set_action(struct ofpbuf
*packet
, const struct nlattr
*a
,
52 enum ovs_key_attr type
= nl_attr_type(a
);
53 const struct ovs_key_ipv4
*ipv4_key
;
54 const struct ovs_key_ipv6
*ipv6_key
;
55 const struct ovs_key_tcp
*tcp_key
;
56 const struct ovs_key_udp
*udp_key
;
59 case OVS_KEY_ATTR_PRIORITY
:
60 flow
->skb_priority
= nl_attr_get_u32(a
);
63 case OVS_KEY_ATTR_TUNNEL
:
64 odp_set_tunnel_action(a
, &flow
->tunnel
);
67 case OVS_KEY_ATTR_SKB_MARK
:
68 flow
->skb_mark
= nl_attr_get_u32(a
);
71 case OVS_KEY_ATTR_ETHERNET
:
72 odp_eth_set_addrs(packet
,
73 nl_attr_get_unspec(a
, sizeof(struct ovs_key_ethernet
)));
76 case OVS_KEY_ATTR_IPV4
:
77 ipv4_key
= nl_attr_get_unspec(a
, sizeof(struct ovs_key_ipv4
));
78 packet_set_ipv4(packet
, ipv4_key
->ipv4_src
, ipv4_key
->ipv4_dst
,
79 ipv4_key
->ipv4_tos
, ipv4_key
->ipv4_ttl
);
82 case OVS_KEY_ATTR_IPV6
:
83 ipv6_key
= nl_attr_get_unspec(a
, sizeof(struct ovs_key_ipv6
));
84 packet_set_ipv6(packet
, ipv6_key
->ipv6_proto
, ipv6_key
->ipv6_src
,
85 ipv6_key
->ipv6_dst
, ipv6_key
->ipv6_tclass
,
86 ipv6_key
->ipv6_label
, ipv6_key
->ipv6_hlimit
);
89 case OVS_KEY_ATTR_TCP
:
90 tcp_key
= nl_attr_get_unspec(a
, sizeof(struct ovs_key_tcp
));
91 packet_set_tcp_port(packet
, tcp_key
->tcp_src
, tcp_key
->tcp_dst
);
94 case OVS_KEY_ATTR_UDP
:
95 udp_key
= nl_attr_get_unspec(a
, sizeof(struct ovs_key_udp
));
96 packet_set_udp_port(packet
, udp_key
->udp_src
, udp_key
->udp_dst
);
99 case OVS_KEY_ATTR_MPLS
:
100 set_mpls_lse(packet
, nl_attr_get_be32(a
));
103 case OVS_KEY_ATTR_UNSPEC
:
104 case OVS_KEY_ATTR_ENCAP
:
105 case OVS_KEY_ATTR_ETHERTYPE
:
106 case OVS_KEY_ATTR_IN_PORT
:
107 case OVS_KEY_ATTR_VLAN
:
108 case OVS_KEY_ATTR_ICMP
:
109 case OVS_KEY_ATTR_ICMPV6
:
110 case OVS_KEY_ATTR_ARP
:
111 case OVS_KEY_ATTR_ND
:
112 case __OVS_KEY_ATTR_MAX
:
119 odp_execute_sample(void *dp
, struct ofpbuf
*packet
, struct flow
*key
,
120 const struct nlattr
*action
,
121 void (*output
)(void *dp
, struct ofpbuf
*packet
,
123 void (*userspace
)(void *dp
, struct ofpbuf
*packet
,
124 const struct flow
*key
,
125 const struct nlattr
*a
))
127 const struct nlattr
*subactions
= NULL
;
128 const struct nlattr
*a
;
131 NL_NESTED_FOR_EACH_UNSAFE (a
, left
, action
) {
132 int type
= nl_attr_type(a
);
134 switch ((enum ovs_sample_attr
) type
) {
135 case OVS_SAMPLE_ATTR_PROBABILITY
:
136 if (random_uint32() >= nl_attr_get_u32(a
)) {
141 case OVS_SAMPLE_ATTR_ACTIONS
:
145 case OVS_SAMPLE_ATTR_UNSPEC
:
146 case __OVS_SAMPLE_ATTR_MAX
:
152 odp_execute_actions(dp
, packet
, key
, nl_attr_get(subactions
),
153 nl_attr_get_size(subactions
), output
, userspace
);
157 odp_execute_actions(void *dp
, struct ofpbuf
*packet
, struct flow
*key
,
158 const struct nlattr
*actions
, size_t actions_len
,
159 void (*output
)(void *dp
, struct ofpbuf
*packet
,
161 void (*userspace
)(void *dp
, struct ofpbuf
*packet
,
162 const struct flow
*key
,
163 const struct nlattr
*a
))
165 const struct nlattr
*a
;
168 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, actions
, actions_len
) {
169 int type
= nl_attr_type(a
);
171 switch ((enum ovs_action_attr
) type
) {
172 case OVS_ACTION_ATTR_OUTPUT
:
174 output(dp
, packet
, nl_attr_get_u32(a
));
178 case OVS_ACTION_ATTR_USERSPACE
: {
179 const struct nlattr
*userdata
;
181 userdata
= nl_attr_find_nested(a
, OVS_USERSPACE_ATTR_USERDATA
);
182 userspace(dp
, packet
, key
, userdata
);
186 case OVS_ACTION_ATTR_PUSH_VLAN
: {
187 const struct ovs_action_push_vlan
*vlan
= nl_attr_get(a
);
188 eth_push_vlan(packet
, vlan
->vlan_tci
);
192 case OVS_ACTION_ATTR_POP_VLAN
:
193 eth_pop_vlan(packet
);
196 case OVS_ACTION_ATTR_PUSH_MPLS
: {
197 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
198 push_mpls(packet
, mpls
->mpls_ethertype
, mpls
->mpls_lse
);
202 case OVS_ACTION_ATTR_POP_MPLS
:
203 pop_mpls(packet
, nl_attr_get_be16(a
));
206 case OVS_ACTION_ATTR_SET
:
207 odp_execute_set_action(packet
, nl_attr_get(a
), key
);
210 case OVS_ACTION_ATTR_SAMPLE
:
211 odp_execute_sample(dp
, packet
, key
, a
, output
, userspace
);
214 case OVS_ACTION_ATTR_UNSPEC
:
215 case __OVS_ACTION_ATTR_MAX
: