]>
Commit | Line | Data |
---|---|---|
f094af7b | 1 | /* |
e60e935b | 2 | * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc. |
f094af7b SH |
3 | * Copyright (c) 2013 Simon Horman |
4 | * | |
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: | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
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. | |
16 | */ | |
17 | ||
18 | #include <config.h> | |
19 | #include "odp-execute.h" | |
6d670e7f | 20 | #include <arpa/inet.h> |
1368af0c | 21 | #include <netinet/in.h> |
e60e935b | 22 | #include <netinet/icmp6.h> |
6d670e7f | 23 | #include <netinet/ip6.h> |
f094af7b SH |
24 | #include <stdlib.h> |
25 | #include <string.h> | |
26 | ||
e14deea0 | 27 | #include "dp-packet.h" |
758c456d | 28 | #include "dpif.h" |
f094af7b SH |
29 | #include "netlink.h" |
30 | #include "ofpbuf.h" | |
837eefc7 | 31 | #include "odp-netlink.h" |
6c13071b | 32 | #include "odp-util.h" |
f094af7b | 33 | #include "packets.h" |
c6bf49f3 | 34 | #include "flow.h" |
f6c8a6b1 | 35 | #include "unaligned.h" |
f094af7b SH |
36 | #include "util.h" |
37 | ||
6d670e7f | 38 | /* Masked copy of an ethernet address. 'src' is already properly masked. */ |
f094af7b | 39 | static void |
6d670e7f JR |
40 | ether_addr_copy_masked(uint8_t *dst, const uint8_t *src, |
41 | const uint8_t *mask) | |
42 | { | |
43 | int i; | |
44 | ||
45 | for (i = 0; i < ETH_ADDR_LEN; i++) { | |
46 | dst[i] = src[i] | (dst[i] & ~mask[i]); | |
47 | } | |
48 | } | |
49 | ||
50 | static void | |
51 | odp_eth_set_addrs(struct ofpbuf *packet, const struct ovs_key_ethernet *key, | |
52 | const struct ovs_key_ethernet *mask) | |
f094af7b | 53 | { |
cf3b7538 | 54 | struct eth_header *eh = ofpbuf_l2(packet); |
f094af7b | 55 | |
cf3b7538 | 56 | if (eh) { |
6d670e7f JR |
57 | if (!mask) { |
58 | memcpy(eh->eth_src, key->eth_src, sizeof eh->eth_src); | |
59 | memcpy(eh->eth_dst, key->eth_dst, sizeof eh->eth_dst); | |
60 | } else { | |
61 | ether_addr_copy_masked(eh->eth_src, key->eth_src, mask->eth_src); | |
62 | ether_addr_copy_masked(eh->eth_dst, key->eth_dst, mask->eth_dst); | |
63 | } | |
cf3b7538 | 64 | } |
f094af7b SH |
65 | } |
66 | ||
6d670e7f JR |
67 | static void |
68 | odp_set_ipv4(struct ofpbuf *packet, const struct ovs_key_ipv4 *key, | |
69 | const struct ovs_key_ipv4 *mask) | |
70 | { | |
71 | struct ip_header *nh = ofpbuf_l3(packet); | |
72 | ||
73 | packet_set_ipv4( | |
74 | packet, | |
75 | key->ipv4_src | (get_16aligned_be32(&nh->ip_src) & ~mask->ipv4_src), | |
76 | key->ipv4_dst | (get_16aligned_be32(&nh->ip_dst) & ~mask->ipv4_dst), | |
77 | key->ipv4_tos | (nh->ip_tos & ~mask->ipv4_tos), | |
78 | key->ipv4_ttl | (nh->ip_ttl & ~mask->ipv4_ttl)); | |
79 | } | |
80 | ||
81 | static const ovs_be32 * | |
82 | mask_ipv6_addr(const ovs_16aligned_be32 *old, const ovs_be32 *addr, | |
83 | const ovs_be32 *mask, ovs_be32 *masked) | |
84 | { | |
85 | for (int i = 0; i < 4; i++) { | |
86 | masked[i] = addr[i] | (get_16aligned_be32(&old[i]) & ~mask[i]); | |
87 | } | |
88 | ||
89 | return masked; | |
90 | } | |
91 | ||
92 | static void | |
93 | odp_set_ipv6(struct ofpbuf *packet, const struct ovs_key_ipv6 *key, | |
94 | const struct ovs_key_ipv6 *mask) | |
95 | { | |
96 | struct ovs_16aligned_ip6_hdr *nh = ofpbuf_l3(packet); | |
97 | ovs_be32 sbuf[4], dbuf[4]; | |
98 | uint8_t old_tc = ntohl(get_16aligned_be32(&nh->ip6_flow)) >> 20; | |
99 | ovs_be32 old_fl = get_16aligned_be32(&nh->ip6_flow) & htonl(0xfffff); | |
100 | ||
101 | packet_set_ipv6( | |
102 | packet, | |
103 | key->ipv6_proto, | |
104 | mask_ipv6_addr(nh->ip6_src.be32, key->ipv6_src, mask->ipv6_src, sbuf), | |
105 | mask_ipv6_addr(nh->ip6_dst.be32, key->ipv6_dst, mask->ipv6_dst, dbuf), | |
106 | key->ipv6_tclass | (old_tc & ~mask->ipv6_tclass), | |
107 | key->ipv6_label | (old_fl & ~mask->ipv6_label), | |
108 | key->ipv6_hlimit | (nh->ip6_hlim & ~mask->ipv6_hlimit)); | |
109 | } | |
110 | ||
111 | static void | |
112 | odp_set_tcp(struct ofpbuf *packet, const struct ovs_key_tcp *key, | |
113 | const struct ovs_key_tcp *mask) | |
114 | { | |
115 | struct tcp_header *th = ofpbuf_l4(packet); | |
116 | ||
b8778a0d JR |
117 | if (OVS_LIKELY(th && ofpbuf_get_tcp_payload(packet))) { |
118 | packet_set_tcp_port(packet, | |
119 | key->tcp_src | (th->tcp_src & ~mask->tcp_src), | |
120 | key->tcp_dst | (th->tcp_dst & ~mask->tcp_dst)); | |
121 | } | |
6d670e7f JR |
122 | } |
123 | ||
124 | static void | |
125 | odp_set_udp(struct ofpbuf *packet, const struct ovs_key_udp *key, | |
126 | const struct ovs_key_udp *mask) | |
127 | { | |
128 | struct udp_header *uh = ofpbuf_l4(packet); | |
129 | ||
b8778a0d JR |
130 | if (OVS_LIKELY(uh && ofpbuf_get_udp_payload(packet))) { |
131 | packet_set_udp_port(packet, | |
132 | key->udp_src | (uh->udp_src & ~mask->udp_src), | |
133 | key->udp_dst | (uh->udp_dst & ~mask->udp_dst)); | |
134 | } | |
6d670e7f JR |
135 | } |
136 | ||
137 | static void | |
138 | odp_set_sctp(struct ofpbuf *packet, const struct ovs_key_sctp *key, | |
139 | const struct ovs_key_sctp *mask) | |
140 | { | |
141 | struct sctp_header *sh = ofpbuf_l4(packet); | |
142 | ||
b8778a0d JR |
143 | if (OVS_LIKELY(sh && ofpbuf_get_sctp_payload(packet))) { |
144 | packet_set_sctp_port(packet, | |
145 | key->sctp_src | (sh->sctp_src & ~mask->sctp_src), | |
146 | key->sctp_dst | (sh->sctp_dst & ~mask->sctp_dst)); | |
147 | } | |
6d670e7f JR |
148 | } |
149 | ||
f094af7b | 150 | static void |
6c13071b SH |
151 | odp_set_tunnel_action(const struct nlattr *a, struct flow_tnl *tun_key) |
152 | { | |
153 | enum odp_key_fitness fitness; | |
154 | ||
155 | fitness = odp_tun_key_from_attr(a, tun_key); | |
156 | ovs_assert(fitness != ODP_FIT_ERROR); | |
157 | } | |
158 | ||
f6c8a6b1 | 159 | static void |
6d670e7f JR |
160 | set_arp(struct ofpbuf *packet, const struct ovs_key_arp *key, |
161 | const struct ovs_key_arp *mask) | |
f6c8a6b1 | 162 | { |
6b8c377a | 163 | struct arp_eth_header *arp = ofpbuf_l3(packet); |
f6c8a6b1 | 164 | |
6d670e7f JR |
165 | if (!mask) { |
166 | arp->ar_op = key->arp_op; | |
167 | memcpy(arp->ar_sha, key->arp_sha, ETH_ADDR_LEN); | |
168 | put_16aligned_be32(&arp->ar_spa, key->arp_sip); | |
169 | memcpy(arp->ar_tha, key->arp_tha, ETH_ADDR_LEN); | |
170 | put_16aligned_be32(&arp->ar_tpa, key->arp_tip); | |
171 | } else { | |
172 | ovs_be32 ar_spa = get_16aligned_be32(&arp->ar_spa); | |
173 | ovs_be32 ar_tpa = get_16aligned_be32(&arp->ar_tpa); | |
174 | ||
175 | arp->ar_op = key->arp_op | (arp->ar_op & ~mask->arp_op); | |
176 | ether_addr_copy_masked(arp->ar_sha, key->arp_sha, mask->arp_sha); | |
177 | put_16aligned_be32(&arp->ar_spa, | |
178 | key->arp_sip | (ar_spa & ~mask->arp_sip)); | |
179 | ether_addr_copy_masked(arp->ar_tha, key->arp_tha, mask->arp_tha); | |
180 | put_16aligned_be32(&arp->ar_tpa, | |
181 | key->arp_tip | (ar_tpa & ~mask->arp_tip)); | |
182 | } | |
f6c8a6b1 BP |
183 | } |
184 | ||
e60e935b SRCSA |
185 | static void |
186 | odp_set_nd(struct ofpbuf *packet, const struct ovs_key_nd *key, | |
187 | const struct ovs_key_nd *mask) | |
188 | { | |
189 | const struct ovs_nd_msg *ns = ofpbuf_l4(packet); | |
190 | const struct ovs_nd_opt *nd_opt = ofpbuf_get_nd_payload(packet); | |
191 | ||
192 | if (OVS_LIKELY(ns && nd_opt)) { | |
193 | int bytes_remain = ofpbuf_l4_size(packet) - sizeof(*ns); | |
194 | ovs_be32 tgt_buf[4]; | |
195 | uint8_t sll_buf[ETH_ADDR_LEN] = {0}; | |
196 | uint8_t tll_buf[ETH_ADDR_LEN] = {0}; | |
197 | ||
198 | while (bytes_remain >= ND_OPT_LEN && nd_opt->nd_opt_len != 0) { | |
199 | if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LINKADDR | |
200 | && nd_opt->nd_opt_len == 1) { | |
201 | memcpy(sll_buf, nd_opt->nd_opt_data, ETH_ADDR_LEN); | |
202 | ether_addr_copy_masked(sll_buf, key->nd_sll, mask->nd_sll); | |
203 | ||
204 | /* A packet can only contain one SLL or TLL option */ | |
205 | break; | |
206 | } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LINKADDR | |
207 | && nd_opt->nd_opt_len == 1) { | |
208 | memcpy(tll_buf, nd_opt->nd_opt_data, ETH_ADDR_LEN); | |
209 | ether_addr_copy_masked(tll_buf, key->nd_tll, mask->nd_tll); | |
210 | ||
211 | /* A packet can only contain one SLL or TLL option */ | |
212 | break; | |
213 | } | |
214 | ||
215 | nd_opt += nd_opt->nd_opt_len; | |
216 | bytes_remain -= nd_opt->nd_opt_len * ND_OPT_LEN; | |
217 | } | |
218 | ||
219 | packet_set_nd(packet, | |
220 | mask_ipv6_addr(ns->target.be32, | |
221 | key->nd_target, mask->nd_target, tgt_buf), | |
222 | sll_buf, | |
223 | tll_buf); | |
224 | } | |
225 | } | |
226 | ||
6c13071b | 227 | static void |
e14deea0 | 228 | odp_execute_set_action(struct dp_packet *packet, const struct nlattr *a) |
f094af7b SH |
229 | { |
230 | enum ovs_key_attr type = nl_attr_type(a); | |
231 | const struct ovs_key_ipv4 *ipv4_key; | |
232 | const struct ovs_key_ipv6 *ipv6_key; | |
41ccaa24 | 233 | struct pkt_metadata *md = &packet->md; |
f094af7b SH |
234 | |
235 | switch (type) { | |
236 | case OVS_KEY_ATTR_PRIORITY: | |
09f9da0b | 237 | md->skb_priority = nl_attr_get_u32(a); |
6c13071b SH |
238 | break; |
239 | ||
f094af7b | 240 | case OVS_KEY_ATTR_TUNNEL: |
09f9da0b | 241 | odp_set_tunnel_action(a, &md->tunnel); |
6c13071b SH |
242 | break; |
243 | ||
f094af7b | 244 | case OVS_KEY_ATTR_SKB_MARK: |
09f9da0b | 245 | md->pkt_mark = nl_attr_get_u32(a); |
f094af7b SH |
246 | break; |
247 | ||
248 | case OVS_KEY_ATTR_ETHERNET: | |
6d670e7f | 249 | odp_eth_set_addrs(&packet->ofpbuf, nl_attr_get(a), NULL); |
f094af7b SH |
250 | break; |
251 | ||
252 | case OVS_KEY_ATTR_IPV4: | |
253 | ipv4_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv4)); | |
91088554 DDP |
254 | packet_set_ipv4(&packet->ofpbuf, ipv4_key->ipv4_src, |
255 | ipv4_key->ipv4_dst, ipv4_key->ipv4_tos, | |
256 | ipv4_key->ipv4_ttl); | |
f094af7b SH |
257 | break; |
258 | ||
259 | case OVS_KEY_ATTR_IPV6: | |
260 | ipv6_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv6)); | |
91088554 DDP |
261 | packet_set_ipv6(&packet->ofpbuf, ipv6_key->ipv6_proto, |
262 | ipv6_key->ipv6_src, ipv6_key->ipv6_dst, | |
263 | ipv6_key->ipv6_tclass, ipv6_key->ipv6_label, | |
264 | ipv6_key->ipv6_hlimit); | |
f094af7b SH |
265 | break; |
266 | ||
267 | case OVS_KEY_ATTR_TCP: | |
b8778a0d JR |
268 | if (OVS_LIKELY(ofpbuf_get_tcp_payload(&packet->ofpbuf))) { |
269 | const struct ovs_key_tcp *tcp_key | |
270 | = nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp)); | |
271 | ||
272 | packet_set_tcp_port(&packet->ofpbuf, tcp_key->tcp_src, | |
273 | tcp_key->tcp_dst); | |
274 | } | |
f094af7b SH |
275 | break; |
276 | ||
2b06df54 | 277 | case OVS_KEY_ATTR_UDP: |
b8778a0d JR |
278 | if (OVS_LIKELY(ofpbuf_get_udp_payload(&packet->ofpbuf))) { |
279 | const struct ovs_key_udp *udp_key | |
280 | = nl_attr_get_unspec(a, sizeof(struct ovs_key_udp)); | |
281 | ||
282 | packet_set_udp_port(&packet->ofpbuf, udp_key->udp_src, | |
283 | udp_key->udp_dst); | |
284 | } | |
f094af7b SH |
285 | break; |
286 | ||
c6bcb685 | 287 | case OVS_KEY_ATTR_SCTP: |
b8778a0d JR |
288 | if (OVS_LIKELY(ofpbuf_get_sctp_payload(&packet->ofpbuf))) { |
289 | const struct ovs_key_sctp *sctp_key | |
290 | = nl_attr_get_unspec(a, sizeof(struct ovs_key_sctp)); | |
291 | ||
292 | packet_set_sctp_port(&packet->ofpbuf, sctp_key->sctp_src, | |
293 | sctp_key->sctp_dst); | |
294 | } | |
c6bcb685 JS |
295 | break; |
296 | ||
2b06df54 | 297 | case OVS_KEY_ATTR_MPLS: |
6d670e7f JR |
298 | set_mpls_lse(&packet->ofpbuf, nl_attr_get_be32(a)); |
299 | break; | |
f094af7b | 300 | |
f6c8a6b1 | 301 | case OVS_KEY_ATTR_ARP: |
6d670e7f | 302 | set_arp(&packet->ofpbuf, nl_attr_get(a), NULL); |
f6c8a6b1 BP |
303 | break; |
304 | ||
e60e935b SRCSA |
305 | case OVS_KEY_ATTR_ND: |
306 | if (OVS_LIKELY(ofpbuf_get_nd_payload(&packet->ofpbuf))) { | |
307 | const struct ovs_key_nd *nd_key | |
308 | = nl_attr_get_unspec(a, sizeof(struct ovs_key_nd)); | |
309 | packet_set_nd(&packet->ofpbuf, nd_key->nd_target, | |
310 | nd_key->nd_sll, nd_key->nd_tll); | |
311 | } | |
312 | break; | |
313 | ||
572f732a | 314 | case OVS_KEY_ATTR_DP_HASH: |
61a2647e | 315 | md->dp_hash = nl_attr_get_u32(a); |
e14deea0 | 316 | dp_packet_set_dp_hash(packet, md->dp_hash); |
572f732a AZ |
317 | break; |
318 | ||
319 | case OVS_KEY_ATTR_RECIRC_ID: | |
320 | md->recirc_id = nl_attr_get_u32(a); | |
321 | break; | |
322 | ||
2b06df54 JS |
323 | case OVS_KEY_ATTR_UNSPEC: |
324 | case OVS_KEY_ATTR_ENCAP: | |
325 | case OVS_KEY_ATTR_ETHERTYPE: | |
326 | case OVS_KEY_ATTR_IN_PORT: | |
327 | case OVS_KEY_ATTR_VLAN: | |
328 | case OVS_KEY_ATTR_ICMP: | |
329 | case OVS_KEY_ATTR_ICMPV6: | |
dc235f7f | 330 | case OVS_KEY_ATTR_TCP_FLAGS: |
2b06df54 JS |
331 | case __OVS_KEY_ATTR_MAX: |
332 | default: | |
428b2edd | 333 | OVS_NOT_REACHED(); |
f094af7b SH |
334 | } |
335 | } | |
336 | ||
6d670e7f JR |
337 | #define get_mask(a, type) ((const type *)(const void *)(a + 1) + 1) |
338 | ||
339 | static void | |
e14deea0 | 340 | odp_execute_masked_set_action(struct dp_packet *packet, |
41ccaa24 | 341 | const struct nlattr *a) |
6d670e7f | 342 | { |
41ccaa24 | 343 | struct pkt_metadata *md = &packet->md; |
6d670e7f JR |
344 | enum ovs_key_attr type = nl_attr_type(a); |
345 | struct mpls_hdr *mh; | |
346 | ||
347 | switch (type) { | |
348 | case OVS_KEY_ATTR_PRIORITY: | |
349 | md->skb_priority = nl_attr_get_u32(a) | |
350 | | (md->skb_priority & ~*get_mask(a, uint32_t)); | |
351 | break; | |
352 | ||
353 | case OVS_KEY_ATTR_SKB_MARK: | |
354 | md->pkt_mark = nl_attr_get_u32(a) | |
355 | | (md->pkt_mark & ~*get_mask(a, uint32_t)); | |
356 | break; | |
357 | ||
358 | case OVS_KEY_ATTR_ETHERNET: | |
359 | odp_eth_set_addrs(&packet->ofpbuf, nl_attr_get(a), | |
360 | get_mask(a, struct ovs_key_ethernet)); | |
361 | break; | |
362 | ||
363 | case OVS_KEY_ATTR_IPV4: | |
364 | odp_set_ipv4(&packet->ofpbuf, nl_attr_get(a), | |
365 | get_mask(a, struct ovs_key_ipv4)); | |
366 | break; | |
367 | ||
368 | case OVS_KEY_ATTR_IPV6: | |
369 | odp_set_ipv6(&packet->ofpbuf, nl_attr_get(a), | |
370 | get_mask(a, struct ovs_key_ipv6)); | |
371 | break; | |
372 | ||
373 | case OVS_KEY_ATTR_TCP: | |
374 | odp_set_tcp(&packet->ofpbuf, nl_attr_get(a), | |
375 | get_mask(a, struct ovs_key_tcp)); | |
376 | break; | |
377 | ||
378 | case OVS_KEY_ATTR_UDP: | |
379 | odp_set_udp(&packet->ofpbuf, nl_attr_get(a), | |
380 | get_mask(a, struct ovs_key_udp)); | |
381 | break; | |
382 | ||
383 | case OVS_KEY_ATTR_SCTP: | |
384 | odp_set_sctp(&packet->ofpbuf, nl_attr_get(a), | |
385 | get_mask(a, struct ovs_key_sctp)); | |
386 | break; | |
387 | ||
388 | case OVS_KEY_ATTR_MPLS: | |
389 | mh = ofpbuf_l2_5(&packet->ofpbuf); | |
390 | if (mh) { | |
391 | put_16aligned_be32(&mh->mpls_lse, nl_attr_get_be32(a) | |
392 | | (get_16aligned_be32(&mh->mpls_lse) | |
393 | & ~*get_mask(a, ovs_be32))); | |
394 | } | |
395 | break; | |
396 | ||
397 | case OVS_KEY_ATTR_ARP: | |
398 | set_arp(&packet->ofpbuf, nl_attr_get(a), | |
399 | get_mask(a, struct ovs_key_arp)); | |
400 | break; | |
401 | ||
e60e935b SRCSA |
402 | case OVS_KEY_ATTR_ND: |
403 | odp_set_nd(&packet->ofpbuf, nl_attr_get(a), | |
404 | get_mask(a, struct ovs_key_nd)); | |
405 | break; | |
406 | ||
6d670e7f | 407 | case OVS_KEY_ATTR_DP_HASH: |
f7c2f97d | 408 | md->dp_hash = nl_attr_get_u32(a) |
e14deea0 PS |
409 | | (dp_packet_get_dp_hash(packet) & ~*get_mask(a, uint32_t)); |
410 | dp_packet_set_dp_hash(packet, md->dp_hash); | |
6d670e7f JR |
411 | break; |
412 | ||
413 | case OVS_KEY_ATTR_RECIRC_ID: | |
414 | md->recirc_id = nl_attr_get_u32(a) | |
415 | | (md->recirc_id & ~*get_mask(a, uint32_t)); | |
416 | break; | |
417 | ||
418 | case OVS_KEY_ATTR_TUNNEL: /* Masked data not supported for tunnel. */ | |
419 | case OVS_KEY_ATTR_UNSPEC: | |
420 | case OVS_KEY_ATTR_ENCAP: | |
421 | case OVS_KEY_ATTR_ETHERTYPE: | |
422 | case OVS_KEY_ATTR_IN_PORT: | |
423 | case OVS_KEY_ATTR_VLAN: | |
424 | case OVS_KEY_ATTR_ICMP: | |
425 | case OVS_KEY_ATTR_ICMPV6: | |
6d670e7f JR |
426 | case OVS_KEY_ATTR_TCP_FLAGS: |
427 | case __OVS_KEY_ATTR_MAX: | |
428 | default: | |
429 | OVS_NOT_REACHED(); | |
430 | } | |
431 | } | |
432 | ||
f094af7b | 433 | static void |
e14deea0 | 434 | odp_execute_sample(void *dp, struct dp_packet *packet, bool steal, |
41ccaa24 | 435 | const struct nlattr *action, |
1164afb6 | 436 | odp_execute_cb dp_execute_action) |
f094af7b SH |
437 | { |
438 | const struct nlattr *subactions = NULL; | |
439 | const struct nlattr *a; | |
440 | size_t left; | |
441 | ||
442 | NL_NESTED_FOR_EACH_UNSAFE (a, left, action) { | |
443 | int type = nl_attr_type(a); | |
444 | ||
445 | switch ((enum ovs_sample_attr) type) { | |
446 | case OVS_SAMPLE_ATTR_PROBABILITY: | |
447 | if (random_uint32() >= nl_attr_get_u32(a)) { | |
1164afb6 | 448 | if (steal) { |
e14deea0 | 449 | dp_packet_delete(packet); |
1164afb6 | 450 | } |
f094af7b SH |
451 | return; |
452 | } | |
453 | break; | |
454 | ||
455 | case OVS_SAMPLE_ATTR_ACTIONS: | |
456 | subactions = a; | |
457 | break; | |
458 | ||
459 | case OVS_SAMPLE_ATTR_UNSPEC: | |
460 | case __OVS_SAMPLE_ATTR_MAX: | |
461 | default: | |
428b2edd | 462 | OVS_NOT_REACHED(); |
f094af7b SH |
463 | } |
464 | } | |
465 | ||
41ccaa24 | 466 | odp_execute_actions(dp, &packet, 1, steal, nl_attr_get(subactions), |
1164afb6 | 467 | nl_attr_get_size(subactions), dp_execute_action); |
f094af7b SH |
468 | } |
469 | ||
1164afb6 | 470 | void |
e14deea0 | 471 | odp_execute_actions(void *dp, struct dp_packet **packets, int cnt, bool steal, |
1164afb6 DDP |
472 | const struct nlattr *actions, size_t actions_len, |
473 | odp_execute_cb dp_execute_action) | |
f094af7b SH |
474 | { |
475 | const struct nlattr *a; | |
476 | unsigned int left; | |
8cbf4f47 DDP |
477 | int i; |
478 | ||
f094af7b SH |
479 | NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) { |
480 | int type = nl_attr_type(a); | |
1164afb6 | 481 | bool last_action = (left <= NLA_ALIGN(a->nla_len)); |
f094af7b SH |
482 | |
483 | switch ((enum ovs_action_attr) type) { | |
09f9da0b | 484 | /* These only make sense in the context of a datapath. */ |
f094af7b | 485 | case OVS_ACTION_ATTR_OUTPUT: |
a36de779 PS |
486 | case OVS_ACTION_ATTR_TUNNEL_PUSH: |
487 | case OVS_ACTION_ATTR_TUNNEL_POP: | |
09f9da0b | 488 | case OVS_ACTION_ATTR_USERSPACE: |
572f732a | 489 | case OVS_ACTION_ATTR_RECIRC: |
09f9da0b JR |
490 | if (dp_execute_action) { |
491 | /* Allow 'dp_execute_action' to steal the packet data if we do | |
492 | * not need it any more. */ | |
1164afb6 DDP |
493 | bool may_steal = steal && last_action; |
494 | ||
41ccaa24 | 495 | dp_execute_action(dp, packets, cnt, a, may_steal); |
1164afb6 DDP |
496 | |
497 | if (last_action) { | |
498 | /* We do not need to free the packets. dp_execute_actions() | |
499 | * has stolen them */ | |
500 | return; | |
501 | } | |
c51876c3 | 502 | } |
f094af7b | 503 | break; |
f094af7b | 504 | |
c6bf49f3 AZ |
505 | case OVS_ACTION_ATTR_HASH: { |
506 | const struct ovs_action_hash *hash_act = nl_attr_get(a); | |
507 | ||
508 | /* Calculate a hash value directly. This might not match the | |
509 | * value computed by the datapath, but it is much less expensive, | |
510 | * and the current use case (bonding) does not require a strict | |
511 | * match to work properly. */ | |
512 | if (hash_act->hash_alg == OVS_HASH_ALG_L4) { | |
513 | struct flow flow; | |
514 | uint32_t hash; | |
515 | ||
8cbf4f47 | 516 | for (i = 0; i < cnt; i++) { |
41ccaa24 | 517 | flow_extract(&packets[i]->ofpbuf, &packets[i]->md, &flow); |
8cbf4f47 DDP |
518 | hash = flow_hash_5tuple(&flow, hash_act->hash_basis); |
519 | ||
8cbf4f47 | 520 | /* We also store the hash value with each packet */ |
e14deea0 | 521 | dp_packet_set_dp_hash(packets[i], hash ? hash : 1); |
8cbf4f47 | 522 | } |
c6bf49f3 AZ |
523 | } else { |
524 | /* Assert on unknown hash algorithm. */ | |
525 | OVS_NOT_REACHED(); | |
526 | } | |
527 | break; | |
528 | } | |
529 | ||
f094af7b SH |
530 | case OVS_ACTION_ATTR_PUSH_VLAN: { |
531 | const struct ovs_action_push_vlan *vlan = nl_attr_get(a); | |
8cbf4f47 DDP |
532 | |
533 | for (i = 0; i < cnt; i++) { | |
e381def9 | 534 | struct ofpbuf *buf = &packets[i]->ofpbuf; |
8cbf4f47 | 535 | |
e381def9 | 536 | eth_push_vlan(buf, htons(ETH_TYPE_VLAN), vlan->vlan_tci); |
8cbf4f47 | 537 | } |
f094af7b SH |
538 | break; |
539 | } | |
540 | ||
541 | case OVS_ACTION_ATTR_POP_VLAN: | |
8cbf4f47 | 542 | for (i = 0; i < cnt; i++) { |
e381def9 | 543 | struct ofpbuf *buf = &packets[i]->ofpbuf; |
8cbf4f47 | 544 | |
e381def9 | 545 | eth_pop_vlan(buf); |
8cbf4f47 | 546 | } |
f094af7b SH |
547 | break; |
548 | ||
549 | case OVS_ACTION_ATTR_PUSH_MPLS: { | |
550 | const struct ovs_action_push_mpls *mpls = nl_attr_get(a); | |
8cbf4f47 DDP |
551 | |
552 | for (i = 0; i < cnt; i++) { | |
e381def9 | 553 | struct ofpbuf *buf = &packets[i]->ofpbuf; |
8cbf4f47 | 554 | |
e381def9 | 555 | push_mpls(buf, mpls->mpls_ethertype, mpls->mpls_lse); |
8cbf4f47 | 556 | } |
f094af7b SH |
557 | break; |
558 | } | |
559 | ||
560 | case OVS_ACTION_ATTR_POP_MPLS: | |
8cbf4f47 | 561 | for (i = 0; i < cnt; i++) { |
e381def9 | 562 | struct ofpbuf *buf = &packets[i]->ofpbuf; |
8cbf4f47 | 563 | |
e381def9 | 564 | pop_mpls(buf, nl_attr_get_be16(a)); |
8cbf4f47 | 565 | } |
f094af7b SH |
566 | break; |
567 | ||
568 | case OVS_ACTION_ATTR_SET: | |
8cbf4f47 | 569 | for (i = 0; i < cnt; i++) { |
41ccaa24 | 570 | odp_execute_set_action(packets[i], nl_attr_get(a)); |
6d670e7f JR |
571 | } |
572 | break; | |
573 | ||
574 | case OVS_ACTION_ATTR_SET_MASKED: | |
575 | for (i = 0; i < cnt; i++) { | |
41ccaa24 | 576 | odp_execute_masked_set_action(packets[i], nl_attr_get(a)); |
8cbf4f47 | 577 | } |
f094af7b SH |
578 | break; |
579 | ||
580 | case OVS_ACTION_ATTR_SAMPLE: | |
8cbf4f47 | 581 | for (i = 0; i < cnt; i++) { |
41ccaa24 | 582 | odp_execute_sample(dp, packets[i], steal && last_action, a, |
1164afb6 DDP |
583 | dp_execute_action); |
584 | } | |
585 | ||
586 | if (last_action) { | |
587 | /* We do not need to free the packets. odp_execute_sample() has | |
588 | * stolen them*/ | |
589 | return; | |
8cbf4f47 | 590 | } |
f094af7b SH |
591 | break; |
592 | ||
593 | case OVS_ACTION_ATTR_UNSPEC: | |
594 | case __OVS_ACTION_ATTR_MAX: | |
428b2edd | 595 | OVS_NOT_REACHED(); |
f094af7b SH |
596 | } |
597 | } | |
8cbf4f47 | 598 | |
1164afb6 | 599 | if (steal) { |
8cbf4f47 | 600 | for (i = 0; i < cnt; i++) { |
e14deea0 | 601 | dp_packet_delete(packets[i]); |
8cbf4f47 | 602 | } |
df1e5a3b | 603 | } |
da546e07 | 604 | } |