]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc. | |
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" | |
20 | #include <arpa/inet.h> | |
21 | #include <netinet/in.h> | |
22 | #include <netinet/icmp6.h> | |
23 | #include <netinet/ip6.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | ||
27 | #include "dp-packet.h" | |
28 | #include "dpif.h" | |
29 | #include "netlink.h" | |
30 | #include "odp-netlink.h" | |
31 | #include "odp-util.h" | |
32 | #include "packets.h" | |
33 | #include "flow.h" | |
34 | #include "unaligned.h" | |
35 | #include "util.h" | |
36 | #include "csum.h" | |
37 | ||
38 | /* Masked copy of an ethernet address. 'src' is already properly masked. */ | |
39 | static void | |
40 | ether_addr_copy_masked(struct eth_addr *dst, const struct eth_addr src, | |
41 | const struct eth_addr mask) | |
42 | { | |
43 | int i; | |
44 | ||
45 | for (i = 0; i < ARRAY_SIZE(dst->be16); i++) { | |
46 | dst->be16[i] = src.be16[i] | (dst->be16[i] & ~mask.be16[i]); | |
47 | } | |
48 | } | |
49 | ||
50 | static void | |
51 | odp_eth_set_addrs(struct dp_packet *packet, const struct ovs_key_ethernet *key, | |
52 | const struct ovs_key_ethernet *mask) | |
53 | { | |
54 | struct eth_header *eh = dp_packet_l2(packet); | |
55 | ||
56 | if (eh) { | |
57 | if (!mask) { | |
58 | eh->eth_src = key->eth_src; | |
59 | eh->eth_dst = key->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 | } | |
64 | } | |
65 | } | |
66 | ||
67 | static void | |
68 | odp_set_ipv4(struct dp_packet *packet, const struct ovs_key_ipv4 *key, | |
69 | const struct ovs_key_ipv4 *mask) | |
70 | { | |
71 | struct ip_header *nh = dp_packet_l3(packet); | |
72 | ovs_be32 ip_src_nh; | |
73 | ovs_be32 ip_dst_nh; | |
74 | ovs_be32 new_ip_src; | |
75 | ovs_be32 new_ip_dst; | |
76 | uint8_t new_tos; | |
77 | uint8_t new_ttl; | |
78 | ||
79 | if (mask->ipv4_src) { | |
80 | ip_src_nh = get_16aligned_be32(&nh->ip_src); | |
81 | new_ip_src = key->ipv4_src | (ip_src_nh & ~mask->ipv4_src); | |
82 | ||
83 | if (ip_src_nh != new_ip_src) { | |
84 | packet_set_ipv4_addr(packet, &nh->ip_src, new_ip_src); | |
85 | } | |
86 | } | |
87 | ||
88 | if (mask->ipv4_dst) { | |
89 | ip_dst_nh = get_16aligned_be32(&nh->ip_dst); | |
90 | new_ip_dst = key->ipv4_dst | (ip_dst_nh & ~mask->ipv4_dst); | |
91 | ||
92 | if (ip_dst_nh != new_ip_dst) { | |
93 | packet_set_ipv4_addr(packet, &nh->ip_dst, new_ip_dst); | |
94 | } | |
95 | } | |
96 | ||
97 | if (mask->ipv4_tos) { | |
98 | new_tos = key->ipv4_tos | (nh->ip_tos & ~mask->ipv4_tos); | |
99 | ||
100 | if (nh->ip_tos != new_tos) { | |
101 | nh->ip_csum = recalc_csum16(nh->ip_csum, | |
102 | htons((uint16_t) nh->ip_tos), | |
103 | htons((uint16_t) new_tos)); | |
104 | nh->ip_tos = new_tos; | |
105 | } | |
106 | } | |
107 | ||
108 | if (OVS_LIKELY(mask->ipv4_ttl)) { | |
109 | new_ttl = key->ipv4_ttl | (nh->ip_ttl & ~mask->ipv4_ttl); | |
110 | ||
111 | if (OVS_LIKELY(nh->ip_ttl != new_ttl)) { | |
112 | nh->ip_csum = recalc_csum16(nh->ip_csum, htons(nh->ip_ttl << 8), | |
113 | htons(new_ttl << 8)); | |
114 | nh->ip_ttl = new_ttl; | |
115 | } | |
116 | } | |
117 | } | |
118 | ||
119 | static struct in6_addr * | |
120 | mask_ipv6_addr(const ovs_16aligned_be32 *old, const struct in6_addr *addr, | |
121 | const struct in6_addr *mask, struct in6_addr *masked) | |
122 | { | |
123 | #ifdef s6_addr32 | |
124 | for (int i = 0; i < 4; i++) { | |
125 | masked->s6_addr32[i] = addr->s6_addr32[i] | |
126 | | (get_16aligned_be32(&old[i]) & ~mask->s6_addr32[i]); | |
127 | } | |
128 | #else | |
129 | const uint8_t *old8 = (const uint8_t *)old; | |
130 | for (int i = 0; i < 16; i++) { | |
131 | masked->s6_addr[i] = addr->s6_addr[i] | (old8[i] & ~mask->s6_addr[i]); | |
132 | } | |
133 | #endif | |
134 | return masked; | |
135 | } | |
136 | ||
137 | static void | |
138 | odp_set_ipv6(struct dp_packet *packet, const struct ovs_key_ipv6 *key, | |
139 | const struct ovs_key_ipv6 *mask) | |
140 | { | |
141 | struct ovs_16aligned_ip6_hdr *nh = dp_packet_l3(packet); | |
142 | struct in6_addr sbuf, dbuf; | |
143 | uint8_t old_tc = ntohl(get_16aligned_be32(&nh->ip6_flow)) >> 20; | |
144 | ovs_be32 old_fl = get_16aligned_be32(&nh->ip6_flow) & htonl(0xfffff); | |
145 | ||
146 | packet_set_ipv6( | |
147 | packet, | |
148 | mask_ipv6_addr(nh->ip6_src.be32, &key->ipv6_src, &mask->ipv6_src, | |
149 | &sbuf), | |
150 | mask_ipv6_addr(nh->ip6_dst.be32, &key->ipv6_dst, &mask->ipv6_dst, | |
151 | &dbuf), | |
152 | key->ipv6_tclass | (old_tc & ~mask->ipv6_tclass), | |
153 | key->ipv6_label | (old_fl & ~mask->ipv6_label), | |
154 | key->ipv6_hlimit | (nh->ip6_hlim & ~mask->ipv6_hlimit)); | |
155 | } | |
156 | ||
157 | static void | |
158 | odp_set_tcp(struct dp_packet *packet, const struct ovs_key_tcp *key, | |
159 | const struct ovs_key_tcp *mask) | |
160 | { | |
161 | struct tcp_header *th = dp_packet_l4(packet); | |
162 | ||
163 | if (OVS_LIKELY(th && dp_packet_get_tcp_payload(packet))) { | |
164 | packet_set_tcp_port(packet, | |
165 | key->tcp_src | (th->tcp_src & ~mask->tcp_src), | |
166 | key->tcp_dst | (th->tcp_dst & ~mask->tcp_dst)); | |
167 | } | |
168 | } | |
169 | ||
170 | static void | |
171 | odp_set_udp(struct dp_packet *packet, const struct ovs_key_udp *key, | |
172 | const struct ovs_key_udp *mask) | |
173 | { | |
174 | struct udp_header *uh = dp_packet_l4(packet); | |
175 | ||
176 | if (OVS_LIKELY(uh && dp_packet_get_udp_payload(packet))) { | |
177 | packet_set_udp_port(packet, | |
178 | key->udp_src | (uh->udp_src & ~mask->udp_src), | |
179 | key->udp_dst | (uh->udp_dst & ~mask->udp_dst)); | |
180 | } | |
181 | } | |
182 | ||
183 | static void | |
184 | odp_set_sctp(struct dp_packet *packet, const struct ovs_key_sctp *key, | |
185 | const struct ovs_key_sctp *mask) | |
186 | { | |
187 | struct sctp_header *sh = dp_packet_l4(packet); | |
188 | ||
189 | if (OVS_LIKELY(sh && dp_packet_get_sctp_payload(packet))) { | |
190 | packet_set_sctp_port(packet, | |
191 | key->sctp_src | (sh->sctp_src & ~mask->sctp_src), | |
192 | key->sctp_dst | (sh->sctp_dst & ~mask->sctp_dst)); | |
193 | } | |
194 | } | |
195 | ||
196 | static void | |
197 | odp_set_tunnel_action(const struct nlattr *a, struct flow_tnl *tun_key) | |
198 | { | |
199 | enum odp_key_fitness fitness; | |
200 | ||
201 | fitness = odp_tun_key_from_attr(a, tun_key); | |
202 | ovs_assert(fitness != ODP_FIT_ERROR); | |
203 | } | |
204 | ||
205 | static void | |
206 | set_arp(struct dp_packet *packet, const struct ovs_key_arp *key, | |
207 | const struct ovs_key_arp *mask) | |
208 | { | |
209 | struct arp_eth_header *arp = dp_packet_l3(packet); | |
210 | ||
211 | if (!mask) { | |
212 | arp->ar_op = key->arp_op; | |
213 | arp->ar_sha = key->arp_sha; | |
214 | put_16aligned_be32(&arp->ar_spa, key->arp_sip); | |
215 | arp->ar_tha = key->arp_tha; | |
216 | put_16aligned_be32(&arp->ar_tpa, key->arp_tip); | |
217 | } else { | |
218 | ovs_be32 ar_spa = get_16aligned_be32(&arp->ar_spa); | |
219 | ovs_be32 ar_tpa = get_16aligned_be32(&arp->ar_tpa); | |
220 | ||
221 | arp->ar_op = key->arp_op | (arp->ar_op & ~mask->arp_op); | |
222 | ether_addr_copy_masked(&arp->ar_sha, key->arp_sha, mask->arp_sha); | |
223 | put_16aligned_be32(&arp->ar_spa, | |
224 | key->arp_sip | (ar_spa & ~mask->arp_sip)); | |
225 | ether_addr_copy_masked(&arp->ar_tha, key->arp_tha, mask->arp_tha); | |
226 | put_16aligned_be32(&arp->ar_tpa, | |
227 | key->arp_tip | (ar_tpa & ~mask->arp_tip)); | |
228 | } | |
229 | } | |
230 | ||
231 | static void | |
232 | odp_set_nd(struct dp_packet *packet, const struct ovs_key_nd *key, | |
233 | const struct ovs_key_nd *mask) | |
234 | { | |
235 | const struct ovs_nd_msg *ns = dp_packet_l4(packet); | |
236 | const struct ovs_nd_opt *nd_opt = dp_packet_get_nd_payload(packet); | |
237 | ||
238 | if (OVS_LIKELY(ns && nd_opt)) { | |
239 | int bytes_remain = dp_packet_l4_size(packet) - sizeof(*ns); | |
240 | struct in6_addr tgt_buf; | |
241 | struct eth_addr sll_buf = eth_addr_zero; | |
242 | struct eth_addr tll_buf = eth_addr_zero; | |
243 | ||
244 | while (bytes_remain >= ND_OPT_LEN && nd_opt->nd_opt_len != 0) { | |
245 | if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LINKADDR | |
246 | && nd_opt->nd_opt_len == 1) { | |
247 | sll_buf = nd_opt->nd_opt_mac; | |
248 | ether_addr_copy_masked(&sll_buf, key->nd_sll, mask->nd_sll); | |
249 | ||
250 | /* A packet can only contain one SLL or TLL option */ | |
251 | break; | |
252 | } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LINKADDR | |
253 | && nd_opt->nd_opt_len == 1) { | |
254 | tll_buf = nd_opt->nd_opt_mac; | |
255 | ether_addr_copy_masked(&tll_buf, key->nd_tll, mask->nd_tll); | |
256 | ||
257 | /* A packet can only contain one SLL or TLL option */ | |
258 | break; | |
259 | } | |
260 | ||
261 | nd_opt += nd_opt->nd_opt_len; | |
262 | bytes_remain -= nd_opt->nd_opt_len * ND_OPT_LEN; | |
263 | } | |
264 | ||
265 | packet_set_nd(packet, | |
266 | mask_ipv6_addr(ns->target.be32, &key->nd_target, | |
267 | &mask->nd_target, &tgt_buf), | |
268 | sll_buf, | |
269 | tll_buf); | |
270 | } | |
271 | } | |
272 | ||
273 | static void | |
274 | odp_execute_set_action(struct dp_packet *packet, const struct nlattr *a) | |
275 | { | |
276 | enum ovs_key_attr type = nl_attr_type(a); | |
277 | const struct ovs_key_ipv4 *ipv4_key; | |
278 | const struct ovs_key_ipv6 *ipv6_key; | |
279 | struct pkt_metadata *md = &packet->md; | |
280 | ||
281 | switch (type) { | |
282 | case OVS_KEY_ATTR_PRIORITY: | |
283 | md->skb_priority = nl_attr_get_u32(a); | |
284 | break; | |
285 | ||
286 | case OVS_KEY_ATTR_TUNNEL: | |
287 | odp_set_tunnel_action(a, &md->tunnel); | |
288 | break; | |
289 | ||
290 | case OVS_KEY_ATTR_SKB_MARK: | |
291 | md->pkt_mark = nl_attr_get_u32(a); | |
292 | break; | |
293 | ||
294 | case OVS_KEY_ATTR_ETHERNET: | |
295 | odp_eth_set_addrs(packet, nl_attr_get(a), NULL); | |
296 | break; | |
297 | ||
298 | case OVS_KEY_ATTR_IPV4: | |
299 | ipv4_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv4)); | |
300 | packet_set_ipv4(packet, ipv4_key->ipv4_src, | |
301 | ipv4_key->ipv4_dst, ipv4_key->ipv4_tos, | |
302 | ipv4_key->ipv4_ttl); | |
303 | break; | |
304 | ||
305 | case OVS_KEY_ATTR_IPV6: | |
306 | ipv6_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv6)); | |
307 | packet_set_ipv6(packet, &ipv6_key->ipv6_src, &ipv6_key->ipv6_dst, | |
308 | ipv6_key->ipv6_tclass, ipv6_key->ipv6_label, | |
309 | ipv6_key->ipv6_hlimit); | |
310 | break; | |
311 | ||
312 | case OVS_KEY_ATTR_TCP: | |
313 | if (OVS_LIKELY(dp_packet_get_tcp_payload(packet))) { | |
314 | const struct ovs_key_tcp *tcp_key | |
315 | = nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp)); | |
316 | ||
317 | packet_set_tcp_port(packet, tcp_key->tcp_src, | |
318 | tcp_key->tcp_dst); | |
319 | } | |
320 | break; | |
321 | ||
322 | case OVS_KEY_ATTR_UDP: | |
323 | if (OVS_LIKELY(dp_packet_get_udp_payload(packet))) { | |
324 | const struct ovs_key_udp *udp_key | |
325 | = nl_attr_get_unspec(a, sizeof(struct ovs_key_udp)); | |
326 | ||
327 | packet_set_udp_port(packet, udp_key->udp_src, | |
328 | udp_key->udp_dst); | |
329 | } | |
330 | break; | |
331 | ||
332 | case OVS_KEY_ATTR_SCTP: | |
333 | if (OVS_LIKELY(dp_packet_get_sctp_payload(packet))) { | |
334 | const struct ovs_key_sctp *sctp_key | |
335 | = nl_attr_get_unspec(a, sizeof(struct ovs_key_sctp)); | |
336 | ||
337 | packet_set_sctp_port(packet, sctp_key->sctp_src, | |
338 | sctp_key->sctp_dst); | |
339 | } | |
340 | break; | |
341 | ||
342 | case OVS_KEY_ATTR_MPLS: | |
343 | set_mpls_lse(packet, nl_attr_get_be32(a)); | |
344 | break; | |
345 | ||
346 | case OVS_KEY_ATTR_ARP: | |
347 | set_arp(packet, nl_attr_get(a), NULL); | |
348 | break; | |
349 | ||
350 | case OVS_KEY_ATTR_ICMP: | |
351 | case OVS_KEY_ATTR_ICMPV6: | |
352 | if (OVS_LIKELY(dp_packet_get_icmp_payload(packet))) { | |
353 | const struct ovs_key_icmp *icmp_key | |
354 | = nl_attr_get_unspec(a, sizeof(struct ovs_key_icmp)); | |
355 | ||
356 | packet_set_icmp(packet, icmp_key->icmp_type, icmp_key->icmp_code); | |
357 | } | |
358 | break; | |
359 | ||
360 | case OVS_KEY_ATTR_ND: | |
361 | if (OVS_LIKELY(dp_packet_get_nd_payload(packet))) { | |
362 | const struct ovs_key_nd *nd_key | |
363 | = nl_attr_get_unspec(a, sizeof(struct ovs_key_nd)); | |
364 | packet_set_nd(packet, &nd_key->nd_target, nd_key->nd_sll, | |
365 | nd_key->nd_tll); | |
366 | } | |
367 | break; | |
368 | ||
369 | case OVS_KEY_ATTR_DP_HASH: | |
370 | md->dp_hash = nl_attr_get_u32(a); | |
371 | break; | |
372 | ||
373 | case OVS_KEY_ATTR_RECIRC_ID: | |
374 | md->recirc_id = nl_attr_get_u32(a); | |
375 | break; | |
376 | ||
377 | case OVS_KEY_ATTR_UNSPEC: | |
378 | case OVS_KEY_ATTR_ENCAP: | |
379 | case OVS_KEY_ATTR_ETHERTYPE: | |
380 | case OVS_KEY_ATTR_IN_PORT: | |
381 | case OVS_KEY_ATTR_VLAN: | |
382 | case OVS_KEY_ATTR_TCP_FLAGS: | |
383 | case OVS_KEY_ATTR_CT_STATE: | |
384 | case OVS_KEY_ATTR_CT_ZONE: | |
385 | case OVS_KEY_ATTR_CT_MARK: | |
386 | case OVS_KEY_ATTR_CT_LABELS: | |
387 | case __OVS_KEY_ATTR_MAX: | |
388 | default: | |
389 | OVS_NOT_REACHED(); | |
390 | } | |
391 | } | |
392 | ||
393 | #define get_mask(a, type) ((const type *)(const void *)(a + 1) + 1) | |
394 | ||
395 | static void | |
396 | odp_execute_masked_set_action(struct dp_packet *packet, | |
397 | const struct nlattr *a) | |
398 | { | |
399 | struct pkt_metadata *md = &packet->md; | |
400 | enum ovs_key_attr type = nl_attr_type(a); | |
401 | struct mpls_hdr *mh; | |
402 | ||
403 | switch (type) { | |
404 | case OVS_KEY_ATTR_PRIORITY: | |
405 | md->skb_priority = nl_attr_get_u32(a) | |
406 | | (md->skb_priority & ~*get_mask(a, uint32_t)); | |
407 | break; | |
408 | ||
409 | case OVS_KEY_ATTR_SKB_MARK: | |
410 | md->pkt_mark = nl_attr_get_u32(a) | |
411 | | (md->pkt_mark & ~*get_mask(a, uint32_t)); | |
412 | break; | |
413 | ||
414 | case OVS_KEY_ATTR_ETHERNET: | |
415 | odp_eth_set_addrs(packet, nl_attr_get(a), | |
416 | get_mask(a, struct ovs_key_ethernet)); | |
417 | break; | |
418 | ||
419 | case OVS_KEY_ATTR_IPV4: | |
420 | odp_set_ipv4(packet, nl_attr_get(a), | |
421 | get_mask(a, struct ovs_key_ipv4)); | |
422 | break; | |
423 | ||
424 | case OVS_KEY_ATTR_IPV6: | |
425 | odp_set_ipv6(packet, nl_attr_get(a), | |
426 | get_mask(a, struct ovs_key_ipv6)); | |
427 | break; | |
428 | ||
429 | case OVS_KEY_ATTR_TCP: | |
430 | odp_set_tcp(packet, nl_attr_get(a), | |
431 | get_mask(a, struct ovs_key_tcp)); | |
432 | break; | |
433 | ||
434 | case OVS_KEY_ATTR_UDP: | |
435 | odp_set_udp(packet, nl_attr_get(a), | |
436 | get_mask(a, struct ovs_key_udp)); | |
437 | break; | |
438 | ||
439 | case OVS_KEY_ATTR_SCTP: | |
440 | odp_set_sctp(packet, nl_attr_get(a), | |
441 | get_mask(a, struct ovs_key_sctp)); | |
442 | break; | |
443 | ||
444 | case OVS_KEY_ATTR_MPLS: | |
445 | mh = dp_packet_l2_5(packet); | |
446 | if (mh) { | |
447 | put_16aligned_be32(&mh->mpls_lse, nl_attr_get_be32(a) | |
448 | | (get_16aligned_be32(&mh->mpls_lse) | |
449 | & ~*get_mask(a, ovs_be32))); | |
450 | } | |
451 | break; | |
452 | ||
453 | case OVS_KEY_ATTR_ARP: | |
454 | set_arp(packet, nl_attr_get(a), | |
455 | get_mask(a, struct ovs_key_arp)); | |
456 | break; | |
457 | ||
458 | case OVS_KEY_ATTR_ND: | |
459 | odp_set_nd(packet, nl_attr_get(a), | |
460 | get_mask(a, struct ovs_key_nd)); | |
461 | break; | |
462 | ||
463 | case OVS_KEY_ATTR_DP_HASH: | |
464 | md->dp_hash = nl_attr_get_u32(a) | |
465 | | (md->dp_hash & ~*get_mask(a, uint32_t)); | |
466 | break; | |
467 | ||
468 | case OVS_KEY_ATTR_RECIRC_ID: | |
469 | md->recirc_id = nl_attr_get_u32(a) | |
470 | | (md->recirc_id & ~*get_mask(a, uint32_t)); | |
471 | break; | |
472 | ||
473 | case OVS_KEY_ATTR_TUNNEL: /* Masked data not supported for tunnel. */ | |
474 | case OVS_KEY_ATTR_UNSPEC: | |
475 | case OVS_KEY_ATTR_CT_STATE: | |
476 | case OVS_KEY_ATTR_CT_ZONE: | |
477 | case OVS_KEY_ATTR_CT_MARK: | |
478 | case OVS_KEY_ATTR_CT_LABELS: | |
479 | case OVS_KEY_ATTR_ENCAP: | |
480 | case OVS_KEY_ATTR_ETHERTYPE: | |
481 | case OVS_KEY_ATTR_IN_PORT: | |
482 | case OVS_KEY_ATTR_VLAN: | |
483 | case OVS_KEY_ATTR_ICMP: | |
484 | case OVS_KEY_ATTR_ICMPV6: | |
485 | case OVS_KEY_ATTR_TCP_FLAGS: | |
486 | case __OVS_KEY_ATTR_MAX: | |
487 | default: | |
488 | OVS_NOT_REACHED(); | |
489 | } | |
490 | } | |
491 | ||
492 | static void | |
493 | odp_execute_sample(void *dp, struct dp_packet *packet, bool steal, | |
494 | const struct nlattr *action, | |
495 | odp_execute_cb dp_execute_action) | |
496 | { | |
497 | const struct nlattr *subactions = NULL; | |
498 | const struct nlattr *a; | |
499 | struct dp_packet_batch pb; | |
500 | size_t left; | |
501 | ||
502 | NL_NESTED_FOR_EACH_UNSAFE (a, left, action) { | |
503 | int type = nl_attr_type(a); | |
504 | ||
505 | switch ((enum ovs_sample_attr) type) { | |
506 | case OVS_SAMPLE_ATTR_PROBABILITY: | |
507 | if (random_uint32() >= nl_attr_get_u32(a)) { | |
508 | if (steal) { | |
509 | dp_packet_delete(packet); | |
510 | } | |
511 | return; | |
512 | } | |
513 | break; | |
514 | ||
515 | case OVS_SAMPLE_ATTR_ACTIONS: | |
516 | subactions = a; | |
517 | break; | |
518 | ||
519 | case OVS_SAMPLE_ATTR_UNSPEC: | |
520 | case __OVS_SAMPLE_ATTR_MAX: | |
521 | default: | |
522 | OVS_NOT_REACHED(); | |
523 | } | |
524 | } | |
525 | ||
526 | if (!steal) { | |
527 | /* The 'subactions' may modify the packet, but the modification | |
528 | * should not propagate beyond this sample action. Make a copy | |
529 | * the packet in case we don't own the packet, so that the | |
530 | * 'subactions' are only applid to the clone. 'odp_execute_actions' | |
531 | * will free the clone. */ | |
532 | packet = dp_packet_clone(packet); | |
533 | } | |
534 | dp_packet_batch_init_packet(&pb, packet); | |
535 | odp_execute_actions(dp, &pb, true, nl_attr_get(subactions), | |
536 | nl_attr_get_size(subactions), dp_execute_action); | |
537 | } | |
538 | ||
539 | static void | |
540 | odp_execute_clone(void *dp, struct dp_packet *packet, bool steal, | |
541 | const struct nlattr *actions, | |
542 | odp_execute_cb dp_execute_action) | |
543 | { | |
544 | struct dp_packet_batch pb; | |
545 | ||
546 | if (!steal) { | |
547 | /* The 'actions' may modify the packet, but the modification | |
548 | * should not propagate beyond this clone action. Make a copy | |
549 | * the packet in case we don't own the packet, so that the | |
550 | * 'actions' are only applied to the clone. 'odp_execute_actions' | |
551 | * will free the clone. */ | |
552 | packet = dp_packet_clone(packet); | |
553 | } | |
554 | dp_packet_batch_init_packet(&pb, packet); | |
555 | odp_execute_actions(dp, &pb, true, nl_attr_get(actions), | |
556 | nl_attr_get_size(actions), dp_execute_action); | |
557 | } | |
558 | ||
559 | static bool | |
560 | requires_datapath_assistance(const struct nlattr *a) | |
561 | { | |
562 | enum ovs_action_attr type = nl_attr_type(a); | |
563 | ||
564 | switch (type) { | |
565 | /* These only make sense in the context of a datapath. */ | |
566 | case OVS_ACTION_ATTR_OUTPUT: | |
567 | case OVS_ACTION_ATTR_TUNNEL_PUSH: | |
568 | case OVS_ACTION_ATTR_TUNNEL_POP: | |
569 | case OVS_ACTION_ATTR_USERSPACE: | |
570 | case OVS_ACTION_ATTR_RECIRC: | |
571 | case OVS_ACTION_ATTR_CT: | |
572 | return true; | |
573 | ||
574 | case OVS_ACTION_ATTR_SET: | |
575 | case OVS_ACTION_ATTR_SET_MASKED: | |
576 | case OVS_ACTION_ATTR_PUSH_VLAN: | |
577 | case OVS_ACTION_ATTR_POP_VLAN: | |
578 | case OVS_ACTION_ATTR_SAMPLE: | |
579 | case OVS_ACTION_ATTR_HASH: | |
580 | case OVS_ACTION_ATTR_PUSH_MPLS: | |
581 | case OVS_ACTION_ATTR_POP_MPLS: | |
582 | case OVS_ACTION_ATTR_TRUNC: | |
583 | case OVS_ACTION_ATTR_CLONE: | |
584 | return false; | |
585 | ||
586 | case OVS_ACTION_ATTR_UNSPEC: | |
587 | case __OVS_ACTION_ATTR_MAX: | |
588 | OVS_NOT_REACHED(); | |
589 | } | |
590 | ||
591 | return false; | |
592 | } | |
593 | ||
594 | void | |
595 | odp_execute_actions(void *dp, struct dp_packet_batch *batch, bool steal, | |
596 | const struct nlattr *actions, size_t actions_len, | |
597 | odp_execute_cb dp_execute_action) | |
598 | { | |
599 | struct dp_packet *packet; | |
600 | const struct nlattr *a; | |
601 | unsigned int left; | |
602 | ||
603 | NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) { | |
604 | int type = nl_attr_type(a); | |
605 | bool last_action = (left <= NLA_ALIGN(a->nla_len)); | |
606 | ||
607 | if (requires_datapath_assistance(a)) { | |
608 | if (dp_execute_action) { | |
609 | /* Allow 'dp_execute_action' to steal the packet data if we do | |
610 | * not need it any more. */ | |
611 | bool may_steal = steal && last_action; | |
612 | ||
613 | dp_execute_action(dp, batch, a, may_steal); | |
614 | ||
615 | if (last_action) { | |
616 | /* We do not need to free the packets. dp_execute_actions() | |
617 | * has stolen them */ | |
618 | return; | |
619 | } | |
620 | } | |
621 | continue; | |
622 | } | |
623 | ||
624 | switch ((enum ovs_action_attr) type) { | |
625 | case OVS_ACTION_ATTR_HASH: { | |
626 | const struct ovs_action_hash *hash_act = nl_attr_get(a); | |
627 | ||
628 | /* Calculate a hash value directly. This might not match the | |
629 | * value computed by the datapath, but it is much less expensive, | |
630 | * and the current use case (bonding) does not require a strict | |
631 | * match to work properly. */ | |
632 | if (hash_act->hash_alg == OVS_HASH_ALG_L4) { | |
633 | struct flow flow; | |
634 | uint32_t hash; | |
635 | ||
636 | DP_PACKET_BATCH_FOR_EACH (packet, batch) { | |
637 | flow_extract(packet, &flow); | |
638 | hash = flow_hash_5tuple(&flow, hash_act->hash_basis); | |
639 | packet->md.dp_hash = hash; | |
640 | } | |
641 | } else { | |
642 | /* Assert on unknown hash algorithm. */ | |
643 | OVS_NOT_REACHED(); | |
644 | } | |
645 | break; | |
646 | } | |
647 | ||
648 | case OVS_ACTION_ATTR_PUSH_VLAN: { | |
649 | const struct ovs_action_push_vlan *vlan = nl_attr_get(a); | |
650 | ||
651 | DP_PACKET_BATCH_FOR_EACH (packet, batch) { | |
652 | eth_push_vlan(packet, vlan->vlan_tpid, vlan->vlan_tci); | |
653 | } | |
654 | break; | |
655 | } | |
656 | ||
657 | case OVS_ACTION_ATTR_POP_VLAN: | |
658 | DP_PACKET_BATCH_FOR_EACH (packet, batch) { | |
659 | eth_pop_vlan(packet); | |
660 | } | |
661 | break; | |
662 | ||
663 | case OVS_ACTION_ATTR_PUSH_MPLS: { | |
664 | const struct ovs_action_push_mpls *mpls = nl_attr_get(a); | |
665 | ||
666 | DP_PACKET_BATCH_FOR_EACH (packet, batch) { | |
667 | push_mpls(packet, mpls->mpls_ethertype, mpls->mpls_lse); | |
668 | } | |
669 | break; | |
670 | } | |
671 | ||
672 | case OVS_ACTION_ATTR_POP_MPLS: | |
673 | DP_PACKET_BATCH_FOR_EACH (packet, batch) { | |
674 | pop_mpls(packet, nl_attr_get_be16(a)); | |
675 | } | |
676 | break; | |
677 | ||
678 | case OVS_ACTION_ATTR_SET: | |
679 | DP_PACKET_BATCH_FOR_EACH (packet, batch) { | |
680 | odp_execute_set_action(packet, nl_attr_get(a)); | |
681 | } | |
682 | break; | |
683 | ||
684 | case OVS_ACTION_ATTR_SET_MASKED: | |
685 | DP_PACKET_BATCH_FOR_EACH(packet, batch) { | |
686 | odp_execute_masked_set_action(packet, nl_attr_get(a)); | |
687 | } | |
688 | break; | |
689 | ||
690 | case OVS_ACTION_ATTR_SAMPLE: | |
691 | DP_PACKET_BATCH_FOR_EACH (packet, batch) { | |
692 | odp_execute_sample(dp, packet, steal && last_action, a, | |
693 | dp_execute_action); | |
694 | } | |
695 | ||
696 | if (last_action) { | |
697 | /* We do not need to free the packets. odp_execute_sample() has | |
698 | * stolen them*/ | |
699 | return; | |
700 | } | |
701 | break; | |
702 | ||
703 | case OVS_ACTION_ATTR_TRUNC: { | |
704 | const struct ovs_action_trunc *trunc = | |
705 | nl_attr_get_unspec(a, sizeof *trunc); | |
706 | ||
707 | batch->trunc = true; | |
708 | DP_PACKET_BATCH_FOR_EACH (packet, batch) { | |
709 | dp_packet_set_cutlen(packet, trunc->max_len); | |
710 | } | |
711 | break; | |
712 | } | |
713 | ||
714 | case OVS_ACTION_ATTR_CLONE: | |
715 | DP_PACKET_BATCH_FOR_EACH (packet, batch) { | |
716 | odp_execute_clone(dp, packet, steal && last_action, a, | |
717 | dp_execute_action); | |
718 | } | |
719 | ||
720 | if (last_action) { | |
721 | /* We do not need to free the packets. odp_execute_clone() has | |
722 | * stolen them. */ | |
723 | return; | |
724 | } | |
725 | break; | |
726 | ||
727 | case OVS_ACTION_ATTR_OUTPUT: | |
728 | case OVS_ACTION_ATTR_TUNNEL_PUSH: | |
729 | case OVS_ACTION_ATTR_TUNNEL_POP: | |
730 | case OVS_ACTION_ATTR_USERSPACE: | |
731 | case OVS_ACTION_ATTR_RECIRC: | |
732 | case OVS_ACTION_ATTR_CT: | |
733 | case OVS_ACTION_ATTR_UNSPEC: | |
734 | case __OVS_ACTION_ATTR_MAX: | |
735 | OVS_NOT_REACHED(); | |
736 | } | |
737 | } | |
738 | ||
739 | dp_packet_delete_batch(batch, steal); | |
740 | } |