]> git.proxmox.com Git - mirror_ovs.git/blob - lib/odp-util.c
tunneling: Add UDP checksum support for userspace tunnels.
[mirror_ovs.git] / lib / odp-util.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18 #include <arpa/inet.h>
19 #include "odp-util.h"
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <math.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "byte-order.h"
29 #include "coverage.h"
30 #include "dpif.h"
31 #include "dynamic-string.h"
32 #include "flow.h"
33 #include "netlink.h"
34 #include "ofpbuf.h"
35 #include "packets.h"
36 #include "simap.h"
37 #include "timeval.h"
38 #include "unaligned.h"
39 #include "util.h"
40 #include "openvswitch/vlog.h"
41
42 VLOG_DEFINE_THIS_MODULE(odp_util);
43
44 /* The interface between userspace and kernel uses an "OVS_*" prefix.
45 * Since this is fairly non-specific for the OVS userspace components,
46 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
47 * interactions with the datapath.
48 */
49
50 /* The set of characters that may separate one action or one key attribute
51 * from another. */
52 static const char *delimiters = ", \t\r\n";
53
54 static int parse_odp_key_mask_attr(const char *, const struct simap *port_names,
55 struct ofpbuf *, struct ofpbuf *);
56 static void format_odp_key_attr(const struct nlattr *a,
57 const struct nlattr *ma,
58 const struct hmap *portno_names, struct ds *ds,
59 bool verbose);
60
61 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
62 * 'type':
63 *
64 * - For an action whose argument has a fixed length, returned that
65 * nonnegative length in bytes.
66 *
67 * - For an action with a variable-length argument, returns -2.
68 *
69 * - For an invalid 'type', returns -1. */
70 static int
71 odp_action_len(uint16_t type)
72 {
73 if (type > OVS_ACTION_ATTR_MAX) {
74 return -1;
75 }
76
77 switch ((enum ovs_action_attr) type) {
78 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
79 case OVS_ACTION_ATTR_TUNNEL_PUSH: return -2;
80 case OVS_ACTION_ATTR_TUNNEL_POP: return sizeof(uint32_t);
81 case OVS_ACTION_ATTR_USERSPACE: return -2;
82 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
83 case OVS_ACTION_ATTR_POP_VLAN: return 0;
84 case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
85 case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
86 case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t);
87 case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash);
88 case OVS_ACTION_ATTR_SET: return -2;
89 case OVS_ACTION_ATTR_SET_MASKED: return -2;
90 case OVS_ACTION_ATTR_SAMPLE: return -2;
91
92 case OVS_ACTION_ATTR_UNSPEC:
93 case __OVS_ACTION_ATTR_MAX:
94 return -1;
95 }
96
97 return -1;
98 }
99
100 /* Returns a string form of 'attr'. The return value is either a statically
101 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
102 * should be at least OVS_KEY_ATTR_BUFSIZE. */
103 enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
104 static const char *
105 ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
106 {
107 switch (attr) {
108 case OVS_KEY_ATTR_UNSPEC: return "unspec";
109 case OVS_KEY_ATTR_ENCAP: return "encap";
110 case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
111 case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
112 case OVS_KEY_ATTR_TUNNEL: return "tunnel";
113 case OVS_KEY_ATTR_IN_PORT: return "in_port";
114 case OVS_KEY_ATTR_ETHERNET: return "eth";
115 case OVS_KEY_ATTR_VLAN: return "vlan";
116 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
117 case OVS_KEY_ATTR_IPV4: return "ipv4";
118 case OVS_KEY_ATTR_IPV6: return "ipv6";
119 case OVS_KEY_ATTR_TCP: return "tcp";
120 case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags";
121 case OVS_KEY_ATTR_UDP: return "udp";
122 case OVS_KEY_ATTR_SCTP: return "sctp";
123 case OVS_KEY_ATTR_ICMP: return "icmp";
124 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
125 case OVS_KEY_ATTR_ARP: return "arp";
126 case OVS_KEY_ATTR_ND: return "nd";
127 case OVS_KEY_ATTR_MPLS: return "mpls";
128 case OVS_KEY_ATTR_DP_HASH: return "dp_hash";
129 case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id";
130
131 case __OVS_KEY_ATTR_MAX:
132 default:
133 snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
134 return namebuf;
135 }
136 }
137
138 static void
139 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
140 {
141 size_t len = nl_attr_get_size(a);
142
143 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
144 if (len) {
145 const uint8_t *unspec;
146 unsigned int i;
147
148 unspec = nl_attr_get(a);
149 for (i = 0; i < len; i++) {
150 ds_put_char(ds, i ? ' ': '(');
151 ds_put_format(ds, "%02x", unspec[i]);
152 }
153 ds_put_char(ds, ')');
154 }
155 }
156
157 static void
158 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
159 {
160 static const struct nl_policy ovs_sample_policy[] = {
161 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
162 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
163 };
164 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
165 double percentage;
166 const struct nlattr *nla_acts;
167 int len;
168
169 ds_put_cstr(ds, "sample");
170
171 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
172 ds_put_cstr(ds, "(error)");
173 return;
174 }
175
176 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
177 UINT32_MAX;
178
179 ds_put_format(ds, "(sample=%.1f%%,", percentage);
180
181 ds_put_cstr(ds, "actions(");
182 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
183 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
184 format_odp_actions(ds, nla_acts, len);
185 ds_put_format(ds, "))");
186 }
187
188 static const char *
189 slow_path_reason_to_string(uint32_t reason)
190 {
191 switch ((enum slow_path_reason) reason) {
192 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
193 SLOW_PATH_REASONS
194 #undef SPR
195 }
196
197 return NULL;
198 }
199
200 const char *
201 slow_path_reason_to_explanation(enum slow_path_reason reason)
202 {
203 switch (reason) {
204 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
205 SLOW_PATH_REASONS
206 #undef SPR
207 }
208
209 return "<unknown>";
210 }
211
212 static int
213 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
214 uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask)
215 {
216 uint32_t result = 0;
217 int n;
218
219 /* Parse masked flags in numeric format? */
220 if (res_mask && ovs_scan(s, "%"SCNi32"/%"SCNi32"%n",
221 res_flags, res_mask, &n) && n > 0) {
222 if (*res_flags & ~allowed || *res_mask & ~allowed) {
223 return -EINVAL;
224 }
225 return n;
226 }
227
228 n = 0;
229
230 if (res_mask && (*s == '+' || *s == '-')) {
231 uint32_t flags = 0, mask = 0;
232
233 /* Parse masked flags. */
234 while (s[n] != ')') {
235 bool set;
236 uint32_t bit;
237 int name_len;
238
239 if (s[n] == '+') {
240 set = true;
241 } else if (s[n] == '-') {
242 set = false;
243 } else {
244 return -EINVAL;
245 }
246 n++;
247
248 name_len = strcspn(s + n, "+-)");
249
250 for (bit = 1; bit; bit <<= 1) {
251 const char *fname = bit_to_string(bit);
252 size_t len;
253
254 if (!fname) {
255 continue;
256 }
257
258 len = strlen(fname);
259 if (len != name_len) {
260 continue;
261 }
262 if (!strncmp(s + n, fname, len)) {
263 if (mask & bit) {
264 /* bit already set. */
265 return -EINVAL;
266 }
267 if (!(bit & allowed)) {
268 return -EINVAL;
269 }
270 if (set) {
271 flags |= bit;
272 }
273 mask |= bit;
274 break;
275 }
276 }
277
278 if (!bit) {
279 return -EINVAL; /* Unknown flag name */
280 }
281 s += name_len;
282 }
283
284 *res_flags = flags;
285 *res_mask = mask;
286 return n;
287 }
288
289 /* Parse unmasked flags. If a flag is present, it is set, otherwise
290 * it is not set. */
291 while (s[n] != ')') {
292 unsigned long long int flags;
293 uint32_t bit;
294 int n0;
295
296 if (ovs_scan(&s[n], "%lli%n", &flags, &n0)) {
297 if (flags & ~allowed) {
298 return -EINVAL;
299 }
300 n += n0 + (s[n + n0] == ',');
301 result |= flags;
302 continue;
303 }
304
305 for (bit = 1; bit; bit <<= 1) {
306 const char *name = bit_to_string(bit);
307 size_t len;
308
309 if (!name) {
310 continue;
311 }
312
313 len = strlen(name);
314 if (!strncmp(s + n, name, len) &&
315 (s[n + len] == ',' || s[n + len] == ')')) {
316 if (!(bit & allowed)) {
317 return -EINVAL;
318 }
319 result |= bit;
320 n += len + (s[n + len] == ',');
321 break;
322 }
323 }
324
325 if (!bit) {
326 return -EINVAL;
327 }
328 }
329
330 *res_flags = result;
331 if (res_mask) {
332 *res_mask = UINT32_MAX;
333 }
334 return n;
335 }
336
337 static void
338 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
339 {
340 static const struct nl_policy ovs_userspace_policy[] = {
341 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
342 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
343 .optional = true },
344 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32,
345 .optional = true },
346 };
347 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
348 const struct nlattr *userdata_attr;
349 const struct nlattr *tunnel_out_port_attr;
350
351 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
352 ds_put_cstr(ds, "userspace(error)");
353 return;
354 }
355
356 ds_put_format(ds, "userspace(pid=%"PRIu32,
357 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
358
359 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
360
361 if (userdata_attr) {
362 const uint8_t *userdata = nl_attr_get(userdata_attr);
363 size_t userdata_len = nl_attr_get_size(userdata_attr);
364 bool userdata_unspec = true;
365 union user_action_cookie cookie;
366
367 if (userdata_len >= sizeof cookie.type
368 && userdata_len <= sizeof cookie) {
369
370 memset(&cookie, 0, sizeof cookie);
371 memcpy(&cookie, userdata, userdata_len);
372
373 userdata_unspec = false;
374
375 if (userdata_len == sizeof cookie.sflow
376 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
377 ds_put_format(ds, ",sFlow("
378 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
379 vlan_tci_to_vid(cookie.sflow.vlan_tci),
380 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
381 cookie.sflow.output);
382 } else if (userdata_len == sizeof cookie.slow_path
383 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
384 ds_put_cstr(ds, ",slow_path(");
385 format_flags(ds, slow_path_reason_to_string,
386 cookie.slow_path.reason, ',');
387 ds_put_format(ds, ")");
388 } else if (userdata_len == sizeof cookie.flow_sample
389 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
390 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
391 ",collector_set_id=%"PRIu32
392 ",obs_domain_id=%"PRIu32
393 ",obs_point_id=%"PRIu32")",
394 cookie.flow_sample.probability,
395 cookie.flow_sample.collector_set_id,
396 cookie.flow_sample.obs_domain_id,
397 cookie.flow_sample.obs_point_id);
398 } else if (userdata_len >= sizeof cookie.ipfix
399 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
400 ds_put_format(ds, ",ipfix(output_port=%"PRIu32")",
401 cookie.ipfix.output_odp_port);
402 } else {
403 userdata_unspec = true;
404 }
405 }
406
407 if (userdata_unspec) {
408 size_t i;
409 ds_put_format(ds, ",userdata(");
410 for (i = 0; i < userdata_len; i++) {
411 ds_put_format(ds, "%02x", userdata[i]);
412 }
413 ds_put_char(ds, ')');
414 }
415 }
416
417 tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT];
418 if (tunnel_out_port_attr) {
419 ds_put_format(ds, ",tunnel_out_port=%"PRIu32,
420 nl_attr_get_u32(tunnel_out_port_attr));
421 }
422
423 ds_put_char(ds, ')');
424 }
425
426 static void
427 format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose)
428 {
429 if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) {
430 ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci));
431 if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */
432 ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask));
433 };
434 ds_put_char(ds, ',');
435 }
436 if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) {
437 ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci));
438 if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
439 ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask));
440 }
441 ds_put_char(ds, ',');
442 }
443 if (!(tci & htons(VLAN_CFI))) {
444 ds_put_cstr(ds, "cfi=0");
445 ds_put_char(ds, ',');
446 }
447 ds_chomp(ds, ',');
448 }
449
450 static void
451 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
452 {
453 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
454 mpls_lse_to_label(mpls_lse),
455 mpls_lse_to_tc(mpls_lse),
456 mpls_lse_to_ttl(mpls_lse),
457 mpls_lse_to_bos(mpls_lse));
458 }
459
460 static void
461 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
462 const struct ovs_key_mpls *mpls_mask, int n)
463 {
464 if (n == 1) {
465 ovs_be32 key = mpls_key->mpls_lse;
466
467 if (mpls_mask == NULL) {
468 format_mpls_lse(ds, key);
469 } else {
470 ovs_be32 mask = mpls_mask->mpls_lse;
471
472 ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
473 mpls_lse_to_label(key), mpls_lse_to_label(mask),
474 mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
475 mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
476 mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
477 }
478 } else {
479 int i;
480
481 for (i = 0; i < n; i++) {
482 ds_put_format(ds, "lse%d=%#"PRIx32,
483 i, ntohl(mpls_key[i].mpls_lse));
484 if (mpls_mask) {
485 ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse));
486 }
487 ds_put_char(ds, ',');
488 }
489 ds_chomp(ds, ',');
490 }
491 }
492
493 static void
494 format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
495 {
496 ds_put_format(ds, "recirc(%"PRIu32")", recirc_id);
497 }
498
499 static void
500 format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act)
501 {
502 ds_put_format(ds, "hash(");
503
504 if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
505 ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis);
506 } else {
507 ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")",
508 hash_act->hash_alg);
509 }
510 ds_put_format(ds, ")");
511 }
512
513 static const void *
514 format_udp_tnl_push_header(struct ds *ds, const struct ip_header *ip)
515 {
516 const struct udp_header *udp;
517
518 udp = (const struct udp_header *) (ip + 1);
519 ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16",csum=0x%"PRIx16"),",
520 ntohs(udp->udp_src), ntohs(udp->udp_dst),
521 ntohs(udp->udp_csum));
522
523 return udp + 1;
524 }
525
526 static void
527 format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data)
528 {
529 const struct eth_header *eth;
530 const struct ip_header *ip;
531 const void *l3;
532
533 eth = (const struct eth_header *)data->header;
534
535 l3 = eth + 1;
536 ip = (const struct ip_header *)l3;
537
538 /* Ethernet */
539 ds_put_format(ds, "header(size=%"PRIu8",type=%"PRIu8",eth(dst=",
540 data->header_len, data->tnl_type);
541 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_dst));
542 ds_put_format(ds, ",src=");
543 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
544 ds_put_format(ds, ",dl_type=0x%04"PRIx16"),", ntohs(eth->eth_type));
545
546 /* IPv4 */
547 ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
548 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=0x%"PRIx16"),",
549 IP_ARGS(get_16aligned_be32(&ip->ip_src)),
550 IP_ARGS(get_16aligned_be32(&ip->ip_dst)),
551 ip->ip_proto, ip->ip_tos,
552 ip->ip_ttl,
553 ip->ip_frag_off);
554
555 if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) {
556 const struct vxlanhdr *vxh;
557
558 vxh = format_udp_tnl_push_header(ds, ip);
559
560 ds_put_format(ds, "vxlan(flags=0x%"PRIx32",vni=0x%"PRIx32")",
561 ntohl(get_16aligned_be32(&vxh->vx_flags)),
562 ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
563 } else if (data->tnl_type == OVS_VPORT_TYPE_GENEVE) {
564 const struct genevehdr *gnh;
565
566 gnh = format_udp_tnl_push_header(ds, ip);
567
568 ds_put_format(ds, "geneve(%svni=0x%"PRIx32")",
569 gnh->oam ? "oam," : "",
570 ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
571 } else if (data->tnl_type == OVS_VPORT_TYPE_GRE) {
572 const struct gre_base_hdr *greh;
573 ovs_16aligned_be32 *options;
574 void *l4;
575
576 l4 = ((uint8_t *)l3 + sizeof(struct ip_header));
577 greh = (const struct gre_base_hdr *) l4;
578
579 ds_put_format(ds, "gre((flags=0x%"PRIx16",proto=0x%"PRIx16")",
580 greh->flags, ntohs(greh->protocol));
581 options = (ovs_16aligned_be32 *)(greh + 1);
582 if (greh->flags & htons(GRE_CSUM)) {
583 ds_put_format(ds, ",csum=0x%"PRIx16, ntohs(*((ovs_be16 *)options)));
584 options++;
585 }
586 if (greh->flags & htons(GRE_KEY)) {
587 ds_put_format(ds, ",key=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
588 options++;
589 }
590 if (greh->flags & htons(GRE_SEQ)) {
591 ds_put_format(ds, ",seq=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
592 options++;
593 }
594 ds_put_format(ds, ")");
595 }
596 ds_put_format(ds, ")");
597 }
598
599 static void
600 format_odp_tnl_push_action(struct ds *ds, const struct nlattr *attr)
601 {
602 struct ovs_action_push_tnl *data;
603
604 data = (struct ovs_action_push_tnl *) nl_attr_get(attr);
605
606 ds_put_format(ds, "tnl_push(tnl_port(%"PRIu32"),", data->tnl_port);
607 format_odp_tnl_push_header(ds, data);
608 ds_put_format(ds, ",out_port(%"PRIu32"))", data->out_port);
609 }
610
611 static void
612 format_odp_action(struct ds *ds, const struct nlattr *a)
613 {
614 int expected_len;
615 enum ovs_action_attr type = nl_attr_type(a);
616 const struct ovs_action_push_vlan *vlan;
617 size_t size;
618
619 expected_len = odp_action_len(nl_attr_type(a));
620 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
621 ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ",
622 nl_attr_get_size(a), expected_len);
623 format_generic_odp_action(ds, a);
624 return;
625 }
626
627 switch (type) {
628 case OVS_ACTION_ATTR_OUTPUT:
629 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
630 break;
631 case OVS_ACTION_ATTR_TUNNEL_POP:
632 ds_put_format(ds, "tnl_pop(%"PRIu32")", nl_attr_get_u32(a));
633 break;
634 case OVS_ACTION_ATTR_TUNNEL_PUSH:
635 format_odp_tnl_push_action(ds, a);
636 break;
637 case OVS_ACTION_ATTR_USERSPACE:
638 format_odp_userspace_action(ds, a);
639 break;
640 case OVS_ACTION_ATTR_RECIRC:
641 format_odp_recirc_action(ds, nl_attr_get_u32(a));
642 break;
643 case OVS_ACTION_ATTR_HASH:
644 format_odp_hash_action(ds, nl_attr_get(a));
645 break;
646 case OVS_ACTION_ATTR_SET_MASKED:
647 a = nl_attr_get(a);
648 size = nl_attr_get_size(a) / 2;
649 ds_put_cstr(ds, "set(");
650
651 /* Masked set action not supported for tunnel key, which is bigger. */
652 if (size <= sizeof(struct ovs_key_ipv6)) {
653 struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
654 sizeof(struct nlattr))];
655 struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
656 sizeof(struct nlattr))];
657
658 mask->nla_type = attr->nla_type = nl_attr_type(a);
659 mask->nla_len = attr->nla_len = NLA_HDRLEN + size;
660 memcpy(attr + 1, (char *)(a + 1), size);
661 memcpy(mask + 1, (char *)(a + 1) + size, size);
662 format_odp_key_attr(attr, mask, NULL, ds, false);
663 } else {
664 format_odp_key_attr(a, NULL, NULL, ds, false);
665 }
666 ds_put_cstr(ds, ")");
667 break;
668 case OVS_ACTION_ATTR_SET:
669 ds_put_cstr(ds, "set(");
670 format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
671 ds_put_cstr(ds, ")");
672 break;
673 case OVS_ACTION_ATTR_PUSH_VLAN:
674 vlan = nl_attr_get(a);
675 ds_put_cstr(ds, "push_vlan(");
676 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
677 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
678 }
679 format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false);
680 ds_put_char(ds, ')');
681 break;
682 case OVS_ACTION_ATTR_POP_VLAN:
683 ds_put_cstr(ds, "pop_vlan");
684 break;
685 case OVS_ACTION_ATTR_PUSH_MPLS: {
686 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
687 ds_put_cstr(ds, "push_mpls(");
688 format_mpls_lse(ds, mpls->mpls_lse);
689 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
690 break;
691 }
692 case OVS_ACTION_ATTR_POP_MPLS: {
693 ovs_be16 ethertype = nl_attr_get_be16(a);
694 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
695 break;
696 }
697 case OVS_ACTION_ATTR_SAMPLE:
698 format_odp_sample_action(ds, a);
699 break;
700 case OVS_ACTION_ATTR_UNSPEC:
701 case __OVS_ACTION_ATTR_MAX:
702 default:
703 format_generic_odp_action(ds, a);
704 break;
705 }
706 }
707
708 void
709 format_odp_actions(struct ds *ds, const struct nlattr *actions,
710 size_t actions_len)
711 {
712 if (actions_len) {
713 const struct nlattr *a;
714 unsigned int left;
715
716 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
717 if (a != actions) {
718 ds_put_char(ds, ',');
719 }
720 format_odp_action(ds, a);
721 }
722 if (left) {
723 int i;
724
725 if (left == actions_len) {
726 ds_put_cstr(ds, "<empty>");
727 }
728 ds_put_format(ds, ",***%u leftover bytes*** (", left);
729 for (i = 0; i < left; i++) {
730 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
731 }
732 ds_put_char(ds, ')');
733 }
734 } else {
735 ds_put_cstr(ds, "drop");
736 }
737 }
738
739 /* Separate out parse_odp_userspace_action() function. */
740 static int
741 parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
742 {
743 uint32_t pid;
744 union user_action_cookie cookie;
745 struct ofpbuf buf;
746 odp_port_t tunnel_out_port;
747 int n = -1;
748 void *user_data = NULL;
749 size_t user_data_size = 0;
750
751 if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
752 return -EINVAL;
753 }
754
755 {
756 uint32_t output;
757 uint32_t probability;
758 uint32_t collector_set_id;
759 uint32_t obs_domain_id;
760 uint32_t obs_point_id;
761 int vid, pcp;
762 int n1 = -1;
763 if (ovs_scan(&s[n], ",sFlow(vid=%i,"
764 "pcp=%i,output=%"SCNi32")%n",
765 &vid, &pcp, &output, &n1)) {
766 uint16_t tci;
767
768 n += n1;
769 tci = vid | (pcp << VLAN_PCP_SHIFT);
770 if (tci) {
771 tci |= VLAN_CFI;
772 }
773
774 cookie.type = USER_ACTION_COOKIE_SFLOW;
775 cookie.sflow.vlan_tci = htons(tci);
776 cookie.sflow.output = output;
777 user_data = &cookie;
778 user_data_size = sizeof cookie.sflow;
779 } else if (ovs_scan(&s[n], ",slow_path(%n",
780 &n1)) {
781 int res;
782
783 n += n1;
784 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
785 cookie.slow_path.unused = 0;
786 cookie.slow_path.reason = 0;
787
788 res = parse_flags(&s[n], slow_path_reason_to_string,
789 &cookie.slow_path.reason,
790 SLOW_PATH_REASON_MASK, NULL);
791 if (res < 0 || s[n + res] != ')') {
792 return res;
793 }
794 n += res + 1;
795
796 user_data = &cookie;
797 user_data_size = sizeof cookie.slow_path;
798 } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
799 "collector_set_id=%"SCNi32","
800 "obs_domain_id=%"SCNi32","
801 "obs_point_id=%"SCNi32")%n",
802 &probability, &collector_set_id,
803 &obs_domain_id, &obs_point_id, &n1)) {
804 n += n1;
805
806 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
807 cookie.flow_sample.probability = probability;
808 cookie.flow_sample.collector_set_id = collector_set_id;
809 cookie.flow_sample.obs_domain_id = obs_domain_id;
810 cookie.flow_sample.obs_point_id = obs_point_id;
811 user_data = &cookie;
812 user_data_size = sizeof cookie.flow_sample;
813 } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
814 &output, &n1) ) {
815 n += n1;
816 cookie.type = USER_ACTION_COOKIE_IPFIX;
817 cookie.ipfix.output_odp_port = u32_to_odp(output);
818 user_data = &cookie;
819 user_data_size = sizeof cookie.ipfix;
820 } else if (ovs_scan(&s[n], ",userdata(%n",
821 &n1)) {
822 char *end;
823
824 n += n1;
825 ofpbuf_init(&buf, 16);
826 end = ofpbuf_put_hex(&buf, &s[n], NULL);
827 if (end[0] != ')') {
828 return -EINVAL;
829 }
830 user_data = buf.data;
831 user_data_size = buf.size;
832 n = (end + 1) - s;
833 }
834 }
835
836 {
837 int n1 = -1;
838 if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n",
839 &tunnel_out_port, &n1)) {
840 odp_put_userspace_action(pid, user_data, user_data_size, tunnel_out_port, actions);
841 return n + n1;
842 } else if (s[n] == ')') {
843 odp_put_userspace_action(pid, user_data, user_data_size, ODPP_NONE, actions);
844 return n + 1;
845 }
846 }
847
848 return -EINVAL;
849 }
850
851 static int
852 ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
853 {
854 struct eth_header *eth;
855 struct ip_header *ip;
856 struct udp_header *udp;
857 struct gre_base_hdr *greh;
858 uint16_t gre_proto, dl_type, udp_src, udp_dst, csum;
859 ovs_be32 sip, dip;
860 uint32_t tnl_type = 0, header_len = 0;
861 void *l3, *l4;
862 int n = 0;
863
864 if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) {
865 return -EINVAL;
866 }
867 eth = (struct eth_header *) data->header;
868 l3 = (data->header + sizeof *eth);
869 l4 = ((uint8_t *) l3 + sizeof (struct ip_header));
870 ip = (struct ip_header *) l3;
871 if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32","
872 "eth(dst="ETH_ADDR_SCAN_FMT",",
873 &data->header_len,
874 &data->tnl_type,
875 ETH_ADDR_SCAN_ARGS(eth->eth_dst))) {
876 return -EINVAL;
877 }
878
879 if (!ovs_scan_len(s, &n, "src="ETH_ADDR_SCAN_FMT",",
880 ETH_ADDR_SCAN_ARGS(eth->eth_src))) {
881 return -EINVAL;
882 }
883 if (!ovs_scan_len(s, &n, "dl_type=0x%"SCNx16"),", &dl_type)) {
884 return -EINVAL;
885 }
886 eth->eth_type = htons(dl_type);
887
888 /* IPv4 */
889 if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8
890 ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),",
891 IP_SCAN_ARGS(&sip),
892 IP_SCAN_ARGS(&dip),
893 &ip->ip_proto, &ip->ip_tos,
894 &ip->ip_ttl, &ip->ip_frag_off)) {
895 return -EINVAL;
896 }
897 put_16aligned_be32(&ip->ip_src, sip);
898 put_16aligned_be32(&ip->ip_dst, dip);
899
900 /* Tunnel header */
901 udp = (struct udp_header *) l4;
902 greh = (struct gre_base_hdr *) l4;
903 if (ovs_scan_len(s, &n, "udp(src=%"SCNi16",dst=%"SCNi16",csum=0x%"SCNx16"),",
904 &udp_src, &udp_dst, &csum)) {
905 uint32_t vx_flags, vni;
906
907 udp->udp_src = htons(udp_src);
908 udp->udp_dst = htons(udp_dst);
909 udp->udp_len = 0;
910 udp->udp_csum = htons(csum);
911
912 if (ovs_scan_len(s, &n, "vxlan(flags=0x%"SCNx32",vni=0x%"SCNx32"))",
913 &vx_flags, &vni)) {
914 struct vxlanhdr *vxh = (struct vxlanhdr *) (udp + 1);
915
916 put_16aligned_be32(&vxh->vx_flags, htonl(vx_flags));
917 put_16aligned_be32(&vxh->vx_vni, htonl(vni << 8));
918 tnl_type = OVS_VPORT_TYPE_VXLAN;
919 header_len = sizeof *eth + sizeof *ip +
920 sizeof *udp + sizeof *vxh;
921 } else if (ovs_scan_len(s, &n, "geneve(")) {
922 struct genevehdr *gnh = (struct genevehdr *) (udp + 1);
923
924 if (ovs_scan_len(s, &n, "oam,")) {
925 gnh->oam = 1;
926 }
927 if (!ovs_scan_len(s, &n, "vni=0x%"SCNx32"))", &vni)) {
928 return -EINVAL;
929 }
930 gnh->proto_type = htons(ETH_TYPE_TEB);
931 put_16aligned_be32(&gnh->vni, htonl(vni << 8));
932 tnl_type = OVS_VPORT_TYPE_GENEVE;
933 header_len = sizeof *eth + sizeof *ip +
934 sizeof *udp + sizeof *gnh;
935 } else {
936 return -EINVAL;
937 }
938 } else if (ovs_scan_len(s, &n, "gre((flags=0x%"SCNx16",proto=0x%"SCNx16")",
939 &greh->flags, &gre_proto)){
940
941 tnl_type = OVS_VPORT_TYPE_GRE;
942 greh->protocol = htons(gre_proto);
943 ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1);
944
945 if (greh->flags & htons(GRE_CSUM)) {
946 if (!ovs_scan_len(s, &n, ",csum=0x%"SCNx16, &csum)) {
947 return -EINVAL;
948 }
949
950 memset(options, 0, sizeof *options);
951 *((ovs_be16 *)options) = htons(csum);
952 options++;
953 }
954 if (greh->flags & htons(GRE_KEY)) {
955 uint32_t key;
956
957 if (!ovs_scan_len(s, &n, ",key=0x%"SCNx32, &key)) {
958 return -EINVAL;
959 }
960
961 put_16aligned_be32(options, htonl(key));
962 options++;
963 }
964 if (greh->flags & htons(GRE_SEQ)) {
965 uint32_t seq;
966
967 if (!ovs_scan_len(s, &n, ",seq=0x%"SCNx32, &seq)) {
968 return -EINVAL;
969 }
970 put_16aligned_be32(options, htonl(seq));
971 options++;
972 }
973
974 if (!ovs_scan_len(s, &n, "))")) {
975 return -EINVAL;
976 }
977
978 header_len = sizeof *eth + sizeof *ip +
979 ((uint8_t *) options - (uint8_t *) greh);
980 } else {
981 return -EINVAL;
982 }
983
984 /* check tunnel meta data. */
985 if (data->tnl_type != tnl_type) {
986 return -EINVAL;
987 }
988 if (data->header_len != header_len) {
989 return -EINVAL;
990 }
991
992 /* Out port */
993 if (!ovs_scan_len(s, &n, ",out_port(%"SCNi32"))", &data->out_port)) {
994 return -EINVAL;
995 }
996
997 return n;
998 }
999
1000 static int
1001 parse_odp_action(const char *s, const struct simap *port_names,
1002 struct ofpbuf *actions)
1003 {
1004 {
1005 uint32_t port;
1006 int n;
1007
1008 if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
1009 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
1010 return n;
1011 }
1012 }
1013
1014 if (port_names) {
1015 int len = strcspn(s, delimiters);
1016 struct simap_node *node;
1017
1018 node = simap_find_len(port_names, s, len);
1019 if (node) {
1020 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
1021 return len;
1022 }
1023 }
1024
1025 {
1026 uint32_t recirc_id;
1027 int n = -1;
1028
1029 if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
1030 nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
1031 return n;
1032 }
1033 }
1034
1035 if (!strncmp(s, "userspace(", 10)) {
1036 return parse_odp_userspace_action(s, actions);
1037 }
1038
1039 if (!strncmp(s, "set(", 4)) {
1040 size_t start_ofs;
1041 int retval;
1042 struct nlattr mask[128 / sizeof(struct nlattr)];
1043 struct ofpbuf maskbuf;
1044 struct nlattr *nested, *key;
1045 size_t size;
1046
1047 /* 'mask' is big enough to hold any key. */
1048 ofpbuf_use_stack(&maskbuf, mask, sizeof mask);
1049
1050 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
1051 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf);
1052 if (retval < 0) {
1053 return retval;
1054 }
1055 if (s[retval + 4] != ')') {
1056 return -EINVAL;
1057 }
1058
1059 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1060 key = nested + 1;
1061
1062 size = nl_attr_get_size(mask);
1063 if (size == nl_attr_get_size(key)) {
1064 /* Change to masked set action if not fully masked. */
1065 if (!is_all_ones(mask + 1, size)) {
1066 key->nla_len += size;
1067 ofpbuf_put(actions, mask + 1, size);
1068 /* 'actions' may have been reallocated by ofpbuf_put(). */
1069 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1070 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
1071 }
1072 }
1073
1074 nl_msg_end_nested(actions, start_ofs);
1075 return retval + 5;
1076 }
1077
1078 {
1079 struct ovs_action_push_vlan push;
1080 int tpid = ETH_TYPE_VLAN;
1081 int vid, pcp;
1082 int cfi = 1;
1083 int n = -1;
1084
1085 if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
1086 || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1087 &vid, &pcp, &cfi, &n)
1088 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1089 &tpid, &vid, &pcp, &n)
1090 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1091 &tpid, &vid, &pcp, &cfi, &n)) {
1092 push.vlan_tpid = htons(tpid);
1093 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
1094 | (pcp << VLAN_PCP_SHIFT)
1095 | (cfi ? VLAN_CFI : 0));
1096 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
1097 &push, sizeof push);
1098
1099 return n;
1100 }
1101 }
1102
1103 if (!strncmp(s, "pop_vlan", 8)) {
1104 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
1105 return 8;
1106 }
1107
1108 {
1109 double percentage;
1110 int n = -1;
1111
1112 if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
1113 && percentage >= 0. && percentage <= 100.0) {
1114 size_t sample_ofs, actions_ofs;
1115 double probability;
1116
1117 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
1118 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
1119 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
1120 (probability <= 0 ? 0
1121 : probability >= UINT32_MAX ? UINT32_MAX
1122 : probability));
1123
1124 actions_ofs = nl_msg_start_nested(actions,
1125 OVS_SAMPLE_ATTR_ACTIONS);
1126 for (;;) {
1127 int retval;
1128
1129 n += strspn(s + n, delimiters);
1130 if (s[n] == ')') {
1131 break;
1132 }
1133
1134 retval = parse_odp_action(s + n, port_names, actions);
1135 if (retval < 0) {
1136 return retval;
1137 }
1138 n += retval;
1139 }
1140 nl_msg_end_nested(actions, actions_ofs);
1141 nl_msg_end_nested(actions, sample_ofs);
1142
1143 return s[n + 1] == ')' ? n + 2 : -EINVAL;
1144 }
1145 }
1146
1147 {
1148 uint32_t port;
1149 int n;
1150
1151 if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
1152 nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
1153 return n;
1154 }
1155 }
1156
1157 {
1158 struct ovs_action_push_tnl data;
1159 int n;
1160
1161 n = ovs_parse_tnl_push(s, &data);
1162 if (n > 0) {
1163 odp_put_tnl_push_action(actions, &data);
1164 return n;
1165 } else if (n < 0) {
1166 return n;
1167 }
1168 }
1169 return -EINVAL;
1170 }
1171
1172 /* Parses the string representation of datapath actions, in the format output
1173 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
1174 * value. On success, the ODP actions are appended to 'actions' as a series of
1175 * Netlink attributes. On failure, no data is appended to 'actions'. Either
1176 * way, 'actions''s data might be reallocated. */
1177 int
1178 odp_actions_from_string(const char *s, const struct simap *port_names,
1179 struct ofpbuf *actions)
1180 {
1181 size_t old_size;
1182
1183 if (!strcasecmp(s, "drop")) {
1184 return 0;
1185 }
1186
1187 old_size = actions->size;
1188 for (;;) {
1189 int retval;
1190
1191 s += strspn(s, delimiters);
1192 if (!*s) {
1193 return 0;
1194 }
1195
1196 retval = parse_odp_action(s, port_names, actions);
1197 if (retval < 0 || !strchr(delimiters, s[retval])) {
1198 actions->size = old_size;
1199 return -retval;
1200 }
1201 s += retval;
1202 }
1203
1204 return 0;
1205 }
1206 \f
1207 /* Returns the correct length of the payload for a flow key attribute of the
1208 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
1209 * is variable length. */
1210 static int
1211 odp_flow_key_attr_len(uint16_t type)
1212 {
1213 if (type > OVS_KEY_ATTR_MAX) {
1214 return -1;
1215 }
1216
1217 switch ((enum ovs_key_attr) type) {
1218 case OVS_KEY_ATTR_ENCAP: return -2;
1219 case OVS_KEY_ATTR_PRIORITY: return 4;
1220 case OVS_KEY_ATTR_SKB_MARK: return 4;
1221 case OVS_KEY_ATTR_DP_HASH: return 4;
1222 case OVS_KEY_ATTR_RECIRC_ID: return 4;
1223 case OVS_KEY_ATTR_TUNNEL: return -2;
1224 case OVS_KEY_ATTR_IN_PORT: return 4;
1225 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
1226 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
1227 case OVS_KEY_ATTR_ETHERTYPE: return 2;
1228 case OVS_KEY_ATTR_MPLS: return -2;
1229 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
1230 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
1231 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
1232 case OVS_KEY_ATTR_TCP_FLAGS: return 2;
1233 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
1234 case OVS_KEY_ATTR_SCTP: return sizeof(struct ovs_key_sctp);
1235 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
1236 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
1237 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
1238 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
1239
1240 case OVS_KEY_ATTR_UNSPEC:
1241 case __OVS_KEY_ATTR_MAX:
1242 return -1;
1243 }
1244
1245 return -1;
1246 }
1247
1248 static void
1249 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
1250 {
1251 size_t len = nl_attr_get_size(a);
1252 if (len) {
1253 const uint8_t *unspec;
1254 unsigned int i;
1255
1256 unspec = nl_attr_get(a);
1257 for (i = 0; i < len; i++) {
1258 if (i) {
1259 ds_put_char(ds, ' ');
1260 }
1261 ds_put_format(ds, "%02x", unspec[i]);
1262 }
1263 }
1264 }
1265
1266 static const char *
1267 ovs_frag_type_to_string(enum ovs_frag_type type)
1268 {
1269 switch (type) {
1270 case OVS_FRAG_TYPE_NONE:
1271 return "no";
1272 case OVS_FRAG_TYPE_FIRST:
1273 return "first";
1274 case OVS_FRAG_TYPE_LATER:
1275 return "later";
1276 case __OVS_FRAG_TYPE_MAX:
1277 default:
1278 return "<error>";
1279 }
1280 }
1281
1282 static int
1283 tunnel_key_attr_len(int type)
1284 {
1285 switch (type) {
1286 case OVS_TUNNEL_KEY_ATTR_ID: return 8;
1287 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4;
1288 case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4;
1289 case OVS_TUNNEL_KEY_ATTR_TOS: return 1;
1290 case OVS_TUNNEL_KEY_ATTR_TTL: return 1;
1291 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0;
1292 case OVS_TUNNEL_KEY_ATTR_CSUM: return 0;
1293 case OVS_TUNNEL_KEY_ATTR_TP_SRC: return 2;
1294 case OVS_TUNNEL_KEY_ATTR_TP_DST: return 2;
1295 case OVS_TUNNEL_KEY_ATTR_OAM: return 0;
1296 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: return -2;
1297 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: return -2;
1298 case __OVS_TUNNEL_KEY_ATTR_MAX:
1299 return -1;
1300 }
1301 return -1;
1302 }
1303
1304 #define GENEVE_OPT(class, type) ((OVS_FORCE uint32_t)(class) << 8 | (type))
1305 static int
1306 parse_geneve_opts(const struct nlattr *attr)
1307 {
1308 int opts_len = nl_attr_get_size(attr);
1309 const struct geneve_opt *opt = nl_attr_get(attr);
1310
1311 while (opts_len > 0) {
1312 int len;
1313
1314 if (opts_len < sizeof(*opt)) {
1315 return -EINVAL;
1316 }
1317
1318 len = sizeof(*opt) + opt->length * 4;
1319 if (len > opts_len) {
1320 return -EINVAL;
1321 }
1322
1323 switch (GENEVE_OPT(opt->opt_class, opt->type)) {
1324 default:
1325 if (opt->type & GENEVE_CRIT_OPT_TYPE) {
1326 return -EINVAL;
1327 }
1328 };
1329
1330 opt = opt + len / sizeof(*opt);
1331 opts_len -= len;
1332 };
1333
1334 return 0;
1335 }
1336
1337 enum odp_key_fitness
1338 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
1339 {
1340 unsigned int left;
1341 const struct nlattr *a;
1342 bool ttl = false;
1343 bool unknown = false;
1344
1345 NL_NESTED_FOR_EACH(a, left, attr) {
1346 uint16_t type = nl_attr_type(a);
1347 size_t len = nl_attr_get_size(a);
1348 int expected_len = tunnel_key_attr_len(type);
1349
1350 if (len != expected_len && expected_len >= 0) {
1351 return ODP_FIT_ERROR;
1352 }
1353
1354 switch (type) {
1355 case OVS_TUNNEL_KEY_ATTR_ID:
1356 tun->tun_id = nl_attr_get_be64(a);
1357 tun->flags |= FLOW_TNL_F_KEY;
1358 break;
1359 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1360 tun->ip_src = nl_attr_get_be32(a);
1361 break;
1362 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1363 tun->ip_dst = nl_attr_get_be32(a);
1364 break;
1365 case OVS_TUNNEL_KEY_ATTR_TOS:
1366 tun->ip_tos = nl_attr_get_u8(a);
1367 break;
1368 case OVS_TUNNEL_KEY_ATTR_TTL:
1369 tun->ip_ttl = nl_attr_get_u8(a);
1370 ttl = true;
1371 break;
1372 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1373 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
1374 break;
1375 case OVS_TUNNEL_KEY_ATTR_CSUM:
1376 tun->flags |= FLOW_TNL_F_CSUM;
1377 break;
1378 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1379 tun->tp_src = nl_attr_get_be16(a);
1380 break;
1381 case OVS_TUNNEL_KEY_ATTR_TP_DST:
1382 tun->tp_dst = nl_attr_get_be16(a);
1383 break;
1384 case OVS_TUNNEL_KEY_ATTR_OAM:
1385 tun->flags |= FLOW_TNL_F_OAM;
1386 break;
1387 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
1388 static const struct nl_policy vxlan_opts_policy[] = {
1389 [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
1390 };
1391 struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
1392
1393 if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
1394 return ODP_FIT_ERROR;
1395 }
1396
1397 if (ext[OVS_VXLAN_EXT_GBP]) {
1398 uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
1399
1400 tun->gbp_id = htons(gbp & 0xFFFF);
1401 tun->gbp_flags = (gbp >> 16) & 0xFF;
1402 }
1403
1404 break;
1405 }
1406 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: {
1407 if (parse_geneve_opts(a)) {
1408 return ODP_FIT_ERROR;
1409 }
1410 /* It is necessary to reproduce options exactly (including order)
1411 * so it's easiest to just echo them back. */
1412 unknown = true;
1413 break;
1414 }
1415 default:
1416 /* Allow this to show up as unexpected, if there are unknown
1417 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1418 unknown = true;
1419 break;
1420 }
1421 }
1422
1423 if (!ttl) {
1424 return ODP_FIT_ERROR;
1425 }
1426 if (unknown) {
1427 return ODP_FIT_TOO_MUCH;
1428 }
1429 return ODP_FIT_PERFECT;
1430 }
1431
1432 static void
1433 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
1434 {
1435 size_t tun_key_ofs;
1436
1437 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1438
1439 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1440 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
1441 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1442 }
1443 if (tun_key->ip_src) {
1444 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1445 }
1446 if (tun_key->ip_dst) {
1447 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1448 }
1449 if (tun_key->ip_tos) {
1450 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1451 }
1452 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1453 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1454 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1455 }
1456 if (tun_key->flags & FLOW_TNL_F_CSUM) {
1457 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1458 }
1459 if (tun_key->tp_src) {
1460 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1461 }
1462 if (tun_key->tp_dst) {
1463 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1464 }
1465 if (tun_key->flags & FLOW_TNL_F_OAM) {
1466 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1467 }
1468 if (tun_key->gbp_flags || tun_key->gbp_id) {
1469 size_t vxlan_opts_ofs;
1470
1471 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
1472 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP,
1473 (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id));
1474 nl_msg_end_nested(a, vxlan_opts_ofs);
1475 }
1476
1477 nl_msg_end_nested(a, tun_key_ofs);
1478 }
1479
1480 static bool
1481 odp_mask_attr_is_wildcard(const struct nlattr *ma)
1482 {
1483 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
1484 }
1485
1486 static bool
1487 odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
1488 {
1489 if (attr == OVS_KEY_ATTR_TCP_FLAGS) {
1490 return TCP_FLAGS(*(ovs_be16 *)mask) == TCP_FLAGS(OVS_BE16_MAX);
1491 }
1492 if (attr == OVS_KEY_ATTR_IPV6) {
1493 const struct ovs_key_ipv6 *ipv6_mask = mask;
1494
1495 return
1496 ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
1497 == htonl(IPV6_LABEL_MASK))
1498 && ipv6_mask->ipv6_proto == UINT8_MAX
1499 && ipv6_mask->ipv6_tclass == UINT8_MAX
1500 && ipv6_mask->ipv6_hlimit == UINT8_MAX
1501 && ipv6_mask->ipv6_frag == UINT8_MAX
1502 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_src)
1503 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_dst);
1504 }
1505 if (attr == OVS_KEY_ATTR_TUNNEL) {
1506 const struct flow_tnl *tun_mask = mask;
1507
1508 return tun_mask->flags == FLOW_TNL_F_MASK
1509 && tun_mask->tun_id == OVS_BE64_MAX
1510 && tun_mask->ip_src == OVS_BE32_MAX
1511 && tun_mask->ip_dst == OVS_BE32_MAX
1512 && tun_mask->ip_tos == UINT8_MAX
1513 && tun_mask->ip_ttl == UINT8_MAX
1514 && tun_mask->tp_src == OVS_BE16_MAX
1515 && tun_mask->tp_dst == OVS_BE16_MAX
1516 && tun_mask->gbp_id == OVS_BE16_MAX
1517 && tun_mask->gbp_flags == UINT8_MAX;
1518 }
1519
1520 if (attr == OVS_KEY_ATTR_ARP) {
1521 /* ARP key has padding, ignore it. */
1522 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp) == 24);
1523 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp, arp_tha) == 10 + 6);
1524 size = offsetof(struct ovs_key_arp, arp_tha) + ETH_ADDR_LEN;
1525 ovs_assert(((uint16_t *)mask)[size/2] == 0);
1526 }
1527
1528 return is_all_ones(mask, size);
1529 }
1530
1531 static bool
1532 odp_mask_attr_is_exact(const struct nlattr *ma)
1533 {
1534 struct flow_tnl tun_mask;
1535 enum ovs_key_attr attr = nl_attr_type(ma);
1536 const void *mask;
1537 size_t size;
1538
1539 if (attr == OVS_KEY_ATTR_TUNNEL) {
1540 memset(&tun_mask, 0, sizeof tun_mask);
1541 odp_tun_key_from_attr(ma, &tun_mask);
1542 mask = &tun_mask;
1543 size = sizeof tun_mask;
1544 } else {
1545 mask = nl_attr_get(ma);
1546 size = nl_attr_get_size(ma);
1547 }
1548
1549 return odp_mask_is_exact(attr, mask, size);
1550 }
1551
1552 void
1553 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
1554 char *port_name)
1555 {
1556 struct odp_portno_names *odp_portno_names;
1557
1558 odp_portno_names = xmalloc(sizeof *odp_portno_names);
1559 odp_portno_names->port_no = port_no;
1560 odp_portno_names->name = xstrdup(port_name);
1561 hmap_insert(portno_names, &odp_portno_names->hmap_node,
1562 hash_odp_port(port_no));
1563 }
1564
1565 static char *
1566 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
1567 {
1568 struct odp_portno_names *odp_portno_names;
1569
1570 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
1571 hash_odp_port(port_no), portno_names) {
1572 if (odp_portno_names->port_no == port_no) {
1573 return odp_portno_names->name;
1574 }
1575 }
1576 return NULL;
1577 }
1578
1579 void
1580 odp_portno_names_destroy(struct hmap *portno_names)
1581 {
1582 struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
1583 HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
1584 hmap_node, portno_names) {
1585 hmap_remove(portno_names, &odp_portno_names->hmap_node);
1586 free(odp_portno_names->name);
1587 free(odp_portno_names);
1588 }
1589 }
1590
1591 /* Format helpers. */
1592
1593 static void
1594 format_eth(struct ds *ds, const char *name, const uint8_t key[ETH_ADDR_LEN],
1595 const uint8_t (*mask)[ETH_ADDR_LEN], bool verbose)
1596 {
1597 bool mask_empty = mask && eth_addr_is_zero(*mask);
1598
1599 if (verbose || !mask_empty) {
1600 bool mask_full = !mask || eth_mask_is_exact(*mask);
1601
1602 if (mask_full) {
1603 ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
1604 } else {
1605 ds_put_format(ds, "%s=", name);
1606 eth_format_masked(key, *mask, ds);
1607 ds_put_char(ds, ',');
1608 }
1609 }
1610 }
1611
1612 static void
1613 format_be64(struct ds *ds, const char *name, ovs_be64 key,
1614 const ovs_be64 *mask, bool verbose)
1615 {
1616 bool mask_empty = mask && !*mask;
1617
1618 if (verbose || !mask_empty) {
1619 bool mask_full = !mask || *mask == OVS_BE64_MAX;
1620
1621 ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
1622 if (!mask_full) { /* Partially masked. */
1623 ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
1624 }
1625 ds_put_char(ds, ',');
1626 }
1627 }
1628
1629 static void
1630 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
1631 const ovs_be32 *mask, bool verbose)
1632 {
1633 bool mask_empty = mask && !*mask;
1634
1635 if (verbose || !mask_empty) {
1636 bool mask_full = !mask || *mask == OVS_BE32_MAX;
1637
1638 ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
1639 if (!mask_full) { /* Partially masked. */
1640 ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
1641 }
1642 ds_put_char(ds, ',');
1643 }
1644 }
1645
1646 static void
1647 format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4],
1648 const ovs_be32 (*mask_)[4], bool verbose)
1649 {
1650 char buf[INET6_ADDRSTRLEN];
1651 const struct in6_addr *key = (const struct in6_addr *)key_;
1652 const struct in6_addr *mask = mask_ ? (const struct in6_addr *)*mask_
1653 : NULL;
1654 bool mask_empty = mask && ipv6_mask_is_any(mask);
1655
1656 if (verbose || !mask_empty) {
1657 bool mask_full = !mask || ipv6_mask_is_exact(mask);
1658
1659 inet_ntop(AF_INET6, key, buf, sizeof buf);
1660 ds_put_format(ds, "%s=%s", name, buf);
1661 if (!mask_full) { /* Partially masked. */
1662 inet_ntop(AF_INET6, mask, buf, sizeof buf);
1663 ds_put_format(ds, "/%s", buf);
1664 }
1665 ds_put_char(ds, ',');
1666 }
1667 }
1668
1669 static void
1670 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
1671 const ovs_be32 *mask, bool verbose)
1672 {
1673 bool mask_empty = mask && !*mask;
1674
1675 if (verbose || !mask_empty) {
1676 bool mask_full = !mask
1677 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
1678
1679 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
1680 if (!mask_full) { /* Partially masked. */
1681 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
1682 }
1683 ds_put_char(ds, ',');
1684 }
1685 }
1686
1687 static void
1688 format_u8x(struct ds *ds, const char *name, uint8_t key,
1689 const uint8_t *mask, bool verbose)
1690 {
1691 bool mask_empty = mask && !*mask;
1692
1693 if (verbose || !mask_empty) {
1694 bool mask_full = !mask || *mask == UINT8_MAX;
1695
1696 ds_put_format(ds, "%s=%#"PRIx8, name, key);
1697 if (!mask_full) { /* Partially masked. */
1698 ds_put_format(ds, "/%#"PRIx8, *mask);
1699 }
1700 ds_put_char(ds, ',');
1701 }
1702 }
1703
1704 static void
1705 format_u8u(struct ds *ds, const char *name, uint8_t key,
1706 const uint8_t *mask, bool verbose)
1707 {
1708 bool mask_empty = mask && !*mask;
1709
1710 if (verbose || !mask_empty) {
1711 bool mask_full = !mask || *mask == UINT8_MAX;
1712
1713 ds_put_format(ds, "%s=%"PRIu8, name, key);
1714 if (!mask_full) { /* Partially masked. */
1715 ds_put_format(ds, "/%#"PRIx8, *mask);
1716 }
1717 ds_put_char(ds, ',');
1718 }
1719 }
1720
1721 static void
1722 format_be16(struct ds *ds, const char *name, ovs_be16 key,
1723 const ovs_be16 *mask, bool verbose)
1724 {
1725 bool mask_empty = mask && !*mask;
1726
1727 if (verbose || !mask_empty) {
1728 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1729
1730 ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
1731 if (!mask_full) { /* Partially masked. */
1732 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1733 }
1734 ds_put_char(ds, ',');
1735 }
1736 }
1737
1738 static void
1739 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
1740 const uint16_t *mask, bool verbose)
1741 {
1742 bool mask_empty = mask && !*mask;
1743
1744 if (verbose || !mask_empty) {
1745 bool mask_full = !mask || (*mask & FLOW_TNL_F_MASK) == FLOW_TNL_F_MASK;
1746
1747 ds_put_cstr(ds, name);
1748 ds_put_char(ds, '(');
1749 if (!mask_full) { /* Partially masked. */
1750 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key, *mask);
1751 } else { /* Fully masked. */
1752 format_flags(ds, flow_tun_flag_to_string, key, ',');
1753 }
1754 ds_put_cstr(ds, "),");
1755 }
1756 }
1757
1758 static void
1759 format_frag(struct ds *ds, const char *name, uint8_t key,
1760 const uint8_t *mask, bool verbose)
1761 {
1762 bool mask_empty = mask && !*mask;
1763
1764 /* ODP frag is an enumeration field; partial masks are not meaningful. */
1765 if (verbose || !mask_empty) {
1766 bool mask_full = !mask || *mask == UINT8_MAX;
1767
1768 if (!mask_full) { /* Partially masked. */
1769 ds_put_format(ds, "error: partial mask not supported for frag (%#"
1770 PRIx8"),", *mask);
1771 } else {
1772 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
1773 }
1774 }
1775 }
1776
1777 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
1778
1779 static void
1780 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
1781 const struct hmap *portno_names, struct ds *ds,
1782 bool verbose)
1783 {
1784 enum ovs_key_attr attr = nl_attr_type(a);
1785 char namebuf[OVS_KEY_ATTR_BUFSIZE];
1786 int expected_len;
1787 bool is_exact;
1788
1789 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
1790
1791 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
1792
1793 {
1794 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
1795 if (expected_len != -2) {
1796 bool bad_key_len = nl_attr_get_size(a) != expected_len;
1797 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
1798
1799 if (bad_key_len || bad_mask_len) {
1800 if (bad_key_len) {
1801 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
1802 nl_attr_get_size(a), expected_len);
1803 }
1804 format_generic_odp_key(a, ds);
1805 if (ma) {
1806 ds_put_char(ds, '/');
1807 if (bad_mask_len) {
1808 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
1809 nl_attr_get_size(ma), expected_len);
1810 }
1811 format_generic_odp_key(ma, ds);
1812 }
1813 ds_put_char(ds, ')');
1814 return;
1815 }
1816 }
1817 }
1818
1819 ds_put_char(ds, '(');
1820 switch (attr) {
1821 case OVS_KEY_ATTR_ENCAP:
1822 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
1823 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
1824 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
1825 verbose);
1826 } else if (nl_attr_get_size(a)) {
1827 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
1828 ds, verbose);
1829 }
1830 break;
1831
1832 case OVS_KEY_ATTR_PRIORITY:
1833 case OVS_KEY_ATTR_SKB_MARK:
1834 case OVS_KEY_ATTR_DP_HASH:
1835 case OVS_KEY_ATTR_RECIRC_ID:
1836 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
1837 if (!is_exact) {
1838 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1839 }
1840 break;
1841
1842 case OVS_KEY_ATTR_TUNNEL: {
1843 struct flow_tnl key, mask_;
1844 struct flow_tnl *mask = ma ? &mask_ : NULL;
1845
1846 if (mask) {
1847 memset(mask, 0, sizeof *mask);
1848 odp_tun_key_from_attr(ma, mask);
1849 }
1850 memset(&key, 0, sizeof key);
1851 if (odp_tun_key_from_attr(a, &key) == ODP_FIT_ERROR) {
1852 ds_put_format(ds, "error");
1853 return;
1854 }
1855 format_be64(ds, "tun_id", key.tun_id, MASK(mask, tun_id), verbose);
1856 format_ipv4(ds, "src", key.ip_src, MASK(mask, ip_src), verbose);
1857 format_ipv4(ds, "dst", key.ip_dst, MASK(mask, ip_dst), verbose);
1858 format_u8x(ds, "tos", key.ip_tos, MASK(mask, ip_tos), verbose);
1859 format_u8u(ds, "ttl", key.ip_ttl, MASK(mask, ip_ttl), verbose);
1860 format_be16(ds, "tp_src", key.tp_src, MASK(mask, tp_src), verbose);
1861 format_be16(ds, "tp_dst", key.tp_dst, MASK(mask, tp_dst), verbose);
1862 format_be16(ds, "gbp_id", key.gbp_id, MASK(mask, gbp_id), verbose);
1863 format_u8x(ds, "gbp_flags", key.gbp_flags, MASK(mask, gbp_flags), verbose);
1864 format_tun_flags(ds, "flags", key.flags, MASK(mask, flags), verbose);
1865 ds_chomp(ds, ',');
1866 break;
1867 }
1868 case OVS_KEY_ATTR_IN_PORT:
1869 if (portno_names && verbose && is_exact) {
1870 char *name = odp_portno_names_get(portno_names,
1871 u32_to_odp(nl_attr_get_u32(a)));
1872 if (name) {
1873 ds_put_format(ds, "%s", name);
1874 } else {
1875 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1876 }
1877 } else {
1878 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1879 if (!is_exact) {
1880 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1881 }
1882 }
1883 break;
1884
1885 case OVS_KEY_ATTR_ETHERNET: {
1886 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
1887 const struct ovs_key_ethernet *key = nl_attr_get(a);
1888
1889 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
1890 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
1891 ds_chomp(ds, ',');
1892 break;
1893 }
1894 case OVS_KEY_ATTR_VLAN:
1895 format_vlan_tci(ds, nl_attr_get_be16(a),
1896 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
1897 break;
1898
1899 case OVS_KEY_ATTR_MPLS: {
1900 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
1901 const struct ovs_key_mpls *mpls_mask = NULL;
1902 size_t size = nl_attr_get_size(a);
1903
1904 if (!size || size % sizeof *mpls_key) {
1905 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
1906 return;
1907 }
1908 if (!is_exact) {
1909 mpls_mask = nl_attr_get(ma);
1910 if (size != nl_attr_get_size(ma)) {
1911 ds_put_format(ds, "(key length %"PRIuSIZE" != "
1912 "mask length %"PRIuSIZE")",
1913 size, nl_attr_get_size(ma));
1914 return;
1915 }
1916 }
1917 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
1918 break;
1919 }
1920 case OVS_KEY_ATTR_ETHERTYPE:
1921 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
1922 if (!is_exact) {
1923 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
1924 }
1925 break;
1926
1927 case OVS_KEY_ATTR_IPV4: {
1928 const struct ovs_key_ipv4 *key = nl_attr_get(a);
1929 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
1930
1931 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
1932 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
1933 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
1934 verbose);
1935 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
1936 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
1937 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
1938 verbose);
1939 ds_chomp(ds, ',');
1940 break;
1941 }
1942 case OVS_KEY_ATTR_IPV6: {
1943 const struct ovs_key_ipv6 *key = nl_attr_get(a);
1944 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
1945
1946 format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose);
1947 format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose);
1948 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
1949 verbose);
1950 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
1951 verbose);
1952 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
1953 verbose);
1954 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
1955 verbose);
1956 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
1957 verbose);
1958 ds_chomp(ds, ',');
1959 break;
1960 }
1961 /* These have the same structure and format. */
1962 case OVS_KEY_ATTR_TCP:
1963 case OVS_KEY_ATTR_UDP:
1964 case OVS_KEY_ATTR_SCTP: {
1965 const struct ovs_key_tcp *key = nl_attr_get(a);
1966 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
1967
1968 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
1969 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
1970 ds_chomp(ds, ',');
1971 break;
1972 }
1973 case OVS_KEY_ATTR_TCP_FLAGS:
1974 if (!is_exact) {
1975 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
1976 ntohs(nl_attr_get_be16(a)),
1977 ntohs(nl_attr_get_be16(ma)));
1978 } else {
1979 format_flags(ds, packet_tcp_flag_to_string,
1980 ntohs(nl_attr_get_be16(a)), ',');
1981 }
1982 break;
1983
1984 case OVS_KEY_ATTR_ICMP: {
1985 const struct ovs_key_icmp *key = nl_attr_get(a);
1986 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
1987
1988 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
1989 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
1990 ds_chomp(ds, ',');
1991 break;
1992 }
1993 case OVS_KEY_ATTR_ICMPV6: {
1994 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
1995 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
1996
1997 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
1998 verbose);
1999 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
2000 verbose);
2001 ds_chomp(ds, ',');
2002 break;
2003 }
2004 case OVS_KEY_ATTR_ARP: {
2005 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
2006 const struct ovs_key_arp *key = nl_attr_get(a);
2007
2008 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
2009 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
2010 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
2011 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
2012 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
2013 ds_chomp(ds, ',');
2014 break;
2015 }
2016 case OVS_KEY_ATTR_ND: {
2017 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
2018 const struct ovs_key_nd *key = nl_attr_get(a);
2019
2020 format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target),
2021 verbose);
2022 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
2023 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
2024
2025 ds_chomp(ds, ',');
2026 break;
2027 }
2028 case OVS_KEY_ATTR_UNSPEC:
2029 case __OVS_KEY_ATTR_MAX:
2030 default:
2031 format_generic_odp_key(a, ds);
2032 if (!is_exact) {
2033 ds_put_char(ds, '/');
2034 format_generic_odp_key(ma, ds);
2035 }
2036 break;
2037 }
2038 ds_put_char(ds, ')');
2039 }
2040
2041 static struct nlattr *
2042 generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key)
2043 {
2044 const struct nlattr *a;
2045 unsigned int left;
2046 int type = nl_attr_type(key);
2047 int size = nl_attr_get_size(key);
2048
2049 if (odp_flow_key_attr_len(type) >=0) {
2050 nl_msg_put_unspec_zero(ofp, type, size);
2051 } else {
2052 size_t nested_mask;
2053
2054 nested_mask = nl_msg_start_nested(ofp, type);
2055 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
2056 generate_all_wildcard_mask(ofp, nl_attr_get(a));
2057 }
2058 nl_msg_end_nested(ofp, nested_mask);
2059 }
2060
2061 return ofp->base;
2062 }
2063
2064 int
2065 odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
2066 {
2067 const char *s = s_;
2068
2069 if (ovs_scan(s, "ufid:")) {
2070 size_t n;
2071
2072 s += 5;
2073 if (ovs_scan(s, "0x")) {
2074 s += 2;
2075 }
2076
2077 n = strspn(s, "0123456789abcdefABCDEF");
2078 if (n != 32) {
2079 return -EINVAL;
2080 }
2081
2082 if (!ovs_scan(s, "%16"SCNx64"%16"SCNx64, &ufid->u64.hi,
2083 &ufid->u64.lo)) {
2084 return -EINVAL;
2085 }
2086 s += n;
2087 s += strspn(s, delimiters);
2088
2089 return s - s_;
2090 }
2091
2092 return 0;
2093 }
2094
2095 void
2096 odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
2097 {
2098 ds_put_format(ds, "ufid:%016"PRIx64"%016"PRIx64, ufid->u64.hi,
2099 ufid->u64.lo);
2100 }
2101
2102 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2103 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
2104 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
2105 * non-null and 'verbose' is true, translates odp port number to its name. */
2106 void
2107 odp_flow_format(const struct nlattr *key, size_t key_len,
2108 const struct nlattr *mask, size_t mask_len,
2109 const struct hmap *portno_names, struct ds *ds, bool verbose)
2110 {
2111 if (key_len) {
2112 const struct nlattr *a;
2113 unsigned int left;
2114 bool has_ethtype_key = false;
2115 const struct nlattr *ma = NULL;
2116 struct ofpbuf ofp;
2117 bool first_field = true;
2118
2119 ofpbuf_init(&ofp, 100);
2120 NL_ATTR_FOR_EACH (a, left, key, key_len) {
2121 bool is_nested_attr;
2122 bool is_wildcard = false;
2123 int attr_type = nl_attr_type(a);
2124
2125 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
2126 has_ethtype_key = true;
2127 }
2128
2129 is_nested_attr = (odp_flow_key_attr_len(attr_type) == -2);
2130
2131 if (mask && mask_len) {
2132 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
2133 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
2134 }
2135
2136 if (verbose || !is_wildcard || is_nested_attr) {
2137 if (is_wildcard && !ma) {
2138 ma = generate_all_wildcard_mask(&ofp, a);
2139 }
2140 if (!first_field) {
2141 ds_put_char(ds, ',');
2142 }
2143 format_odp_key_attr(a, ma, portno_names, ds, verbose);
2144 first_field = false;
2145 }
2146 ofpbuf_clear(&ofp);
2147 }
2148 ofpbuf_uninit(&ofp);
2149
2150 if (left) {
2151 int i;
2152
2153 if (left == key_len) {
2154 ds_put_cstr(ds, "<empty>");
2155 }
2156 ds_put_format(ds, ",***%u leftover bytes*** (", left);
2157 for (i = 0; i < left; i++) {
2158 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
2159 }
2160 ds_put_char(ds, ')');
2161 }
2162 if (!has_ethtype_key) {
2163 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
2164 if (ma) {
2165 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
2166 ntohs(nl_attr_get_be16(ma)));
2167 }
2168 }
2169 } else {
2170 ds_put_cstr(ds, "<empty>");
2171 }
2172 }
2173
2174 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2175 * OVS_KEY_ATTR_* attributes in 'key'. */
2176 void
2177 odp_flow_key_format(const struct nlattr *key,
2178 size_t key_len, struct ds *ds)
2179 {
2180 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
2181 }
2182
2183 static bool
2184 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
2185 {
2186 if (!strcasecmp(s, "no")) {
2187 *type = OVS_FRAG_TYPE_NONE;
2188 } else if (!strcasecmp(s, "first")) {
2189 *type = OVS_FRAG_TYPE_FIRST;
2190 } else if (!strcasecmp(s, "later")) {
2191 *type = OVS_FRAG_TYPE_LATER;
2192 } else {
2193 return false;
2194 }
2195 return true;
2196 }
2197
2198 /* Parsing. */
2199
2200 static int
2201 scan_eth(const char *s, uint8_t (*key)[ETH_ADDR_LEN],
2202 uint8_t (*mask)[ETH_ADDR_LEN])
2203 {
2204 int n;
2205
2206 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*key), &n)) {
2207 int len = n;
2208
2209 if (mask) {
2210 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
2211 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
2212 len += n;
2213 } else {
2214 memset(mask, 0xff, sizeof *mask);
2215 }
2216 }
2217 return len;
2218 }
2219 return 0;
2220 }
2221
2222 static int
2223 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
2224 {
2225 int n;
2226
2227 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
2228 int len = n;
2229
2230 if (mask) {
2231 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
2232 IP_SCAN_ARGS(mask), &n)) {
2233 len += n;
2234 } else {
2235 *mask = OVS_BE32_MAX;
2236 }
2237 }
2238 return len;
2239 }
2240 return 0;
2241 }
2242
2243 static int
2244 scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4])
2245 {
2246 int n;
2247 char ipv6_s[IPV6_SCAN_LEN + 1];
2248
2249 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
2250 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
2251 int len = n;
2252
2253 if (mask) {
2254 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
2255 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
2256 len += n;
2257 } else {
2258 memset(mask, 0xff, sizeof *mask);
2259 }
2260 }
2261 return len;
2262 }
2263 return 0;
2264 }
2265
2266 static int
2267 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2268 {
2269 int key_, mask_;
2270 int n;
2271
2272 if (ovs_scan(s, "%i%n", &key_, &n)
2273 && (key_ & ~IPV6_LABEL_MASK) == 0) {
2274 int len = n;
2275
2276 *key = htonl(key_);
2277 if (mask) {
2278 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
2279 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
2280 len += n;
2281 *mask = htonl(mask_);
2282 } else {
2283 *mask = htonl(IPV6_LABEL_MASK);
2284 }
2285 }
2286 return len;
2287 }
2288 return 0;
2289 }
2290
2291 static int
2292 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
2293 {
2294 int n;
2295
2296 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
2297 int len = n;
2298
2299 if (mask) {
2300 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
2301 len += n;
2302 } else {
2303 *mask = UINT8_MAX;
2304 }
2305 }
2306 return len;
2307 }
2308 return 0;
2309 }
2310
2311 static int
2312 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
2313 {
2314 int n;
2315
2316 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2317 int len = n;
2318
2319 if (mask) {
2320 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2321 len += n;
2322 } else {
2323 *mask = UINT32_MAX;
2324 }
2325 }
2326 return len;
2327 }
2328 return 0;
2329 }
2330
2331 static int
2332 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
2333 {
2334 uint16_t key_, mask_;
2335 int n;
2336
2337 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2338 int len = n;
2339
2340 *key = htons(key_);
2341 if (mask) {
2342 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2343 len += n;
2344 *mask = htons(mask_);
2345 } else {
2346 *mask = OVS_BE16_MAX;
2347 }
2348 }
2349 return len;
2350 }
2351 return 0;
2352 }
2353
2354 static int
2355 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
2356 {
2357 uint64_t key_, mask_;
2358 int n;
2359
2360 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
2361 int len = n;
2362
2363 *key = htonll(key_);
2364 if (mask) {
2365 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
2366 len += n;
2367 *mask = htonll(mask_);
2368 } else {
2369 *mask = OVS_BE64_MAX;
2370 }
2371 }
2372 return len;
2373 }
2374 return 0;
2375 }
2376
2377 static int
2378 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
2379 {
2380 uint32_t flags, fmask;
2381 int n;
2382
2383 n = parse_flags(s, flow_tun_flag_to_string, &flags,
2384 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
2385 if (n >= 0 && s[n] == ')') {
2386 *key = flags;
2387 if (mask) {
2388 *mask = fmask;
2389 }
2390 return n + 1;
2391 }
2392 return 0;
2393 }
2394
2395 static int
2396 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
2397 {
2398 uint32_t flags, fmask;
2399 int n;
2400
2401 n = parse_flags(s, packet_tcp_flag_to_string, &flags,
2402 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
2403 if (n >= 0) {
2404 *key = htons(flags);
2405 if (mask) {
2406 *mask = htons(fmask);
2407 }
2408 return n;
2409 }
2410 return 0;
2411 }
2412
2413 static int
2414 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
2415 {
2416 int n;
2417 char frag[8];
2418 enum ovs_frag_type frag_type;
2419
2420 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
2421 && ovs_frag_type_from_string(frag, &frag_type)) {
2422 int len = n;
2423
2424 *key = frag_type;
2425 if (mask) {
2426 *mask = UINT8_MAX;
2427 }
2428 return len;
2429 }
2430 return 0;
2431 }
2432
2433 static int
2434 scan_port(const char *s, uint32_t *key, uint32_t *mask,
2435 const struct simap *port_names)
2436 {
2437 int n;
2438
2439 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2440 int len = n;
2441
2442 if (mask) {
2443 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2444 len += n;
2445 } else {
2446 *mask = UINT32_MAX;
2447 }
2448 }
2449 return len;
2450 } else if (port_names) {
2451 const struct simap_node *node;
2452 int len;
2453
2454 len = strcspn(s, ")");
2455 node = simap_find_len(port_names, s, len);
2456 if (node) {
2457 *key = node->data;
2458
2459 if (mask) {
2460 *mask = UINT32_MAX;
2461 }
2462 return len;
2463 }
2464 }
2465 return 0;
2466 }
2467
2468 /* Helper for vlan parsing. */
2469 struct ovs_key_vlan__ {
2470 ovs_be16 tci;
2471 };
2472
2473 static bool
2474 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
2475 {
2476 const uint16_t mask = ((1U << bits) - 1) << offset;
2477
2478 if (value >> bits) {
2479 return false;
2480 }
2481
2482 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
2483 return true;
2484 }
2485
2486 static int
2487 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
2488 uint8_t offset)
2489 {
2490 uint16_t key_, mask_;
2491 int n;
2492
2493 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2494 int len = n;
2495
2496 if (set_be16_bf(key, bits, offset, key_)) {
2497 if (mask) {
2498 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2499 len += n;
2500
2501 if (!set_be16_bf(mask, bits, offset, mask_)) {
2502 return 0;
2503 }
2504 } else {
2505 *mask |= htons(((1U << bits) - 1) << offset);
2506 }
2507 }
2508 return len;
2509 }
2510 }
2511 return 0;
2512 }
2513
2514 static int
2515 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
2516 {
2517 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
2518 }
2519
2520 static int
2521 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
2522 {
2523 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
2524 }
2525
2526 static int
2527 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
2528 {
2529 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
2530 }
2531
2532 /* For MPLS. */
2533 static bool
2534 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
2535 {
2536 const uint32_t mask = ((1U << bits) - 1) << offset;
2537
2538 if (value >> bits) {
2539 return false;
2540 }
2541
2542 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
2543 return true;
2544 }
2545
2546 static int
2547 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
2548 uint8_t offset)
2549 {
2550 uint32_t key_, mask_;
2551 int n;
2552
2553 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
2554 int len = n;
2555
2556 if (set_be32_bf(key, bits, offset, key_)) {
2557 if (mask) {
2558 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
2559 len += n;
2560
2561 if (!set_be32_bf(mask, bits, offset, mask_)) {
2562 return 0;
2563 }
2564 } else {
2565 *mask |= htonl(((1U << bits) - 1) << offset);
2566 }
2567 }
2568 return len;
2569 }
2570 }
2571 return 0;
2572 }
2573
2574 static int
2575 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2576 {
2577 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
2578 }
2579
2580 static int
2581 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
2582 {
2583 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
2584 }
2585
2586 static int
2587 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
2588 {
2589 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
2590 }
2591
2592 static int
2593 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
2594 {
2595 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
2596 }
2597
2598 /* ATTR is compile-time constant, so only the case with correct data type
2599 * will be used. However, the compiler complains about the data type for
2600 * the other cases, so we must cast to make the compiler silent. */
2601 #define SCAN_PUT_ATTR(BUF, ATTR, DATA) \
2602 if ((ATTR) == OVS_KEY_ATTR_TUNNEL) { \
2603 tun_key_to_attr(BUF, (const struct flow_tnl *)(void *)&(DATA)); \
2604 } else { \
2605 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
2606 }
2607
2608 #define SCAN_IF(NAME) \
2609 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
2610 const char *start = s; \
2611 int len; \
2612 \
2613 s += strlen(NAME)
2614
2615 /* Usually no special initialization is needed. */
2616 #define SCAN_BEGIN(NAME, TYPE) \
2617 SCAN_IF(NAME); \
2618 TYPE skey, smask; \
2619 memset(&skey, 0, sizeof skey); \
2620 memset(&smask, 0, sizeof smask); \
2621 do { \
2622 len = 0;
2623
2624 /* Init as fully-masked as mask will not be scanned. */
2625 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
2626 SCAN_IF(NAME); \
2627 TYPE skey, smask; \
2628 memset(&skey, 0, sizeof skey); \
2629 memset(&smask, 0xff, sizeof smask); \
2630 do { \
2631 len = 0;
2632
2633 /* VLAN needs special initialization. */
2634 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
2635 SCAN_IF(NAME); \
2636 TYPE skey = KEY_INIT; \
2637 TYPE smask = MASK_INIT; \
2638 do { \
2639 len = 0;
2640
2641 /* Scan unnamed entry as 'TYPE' */
2642 #define SCAN_TYPE(TYPE, KEY, MASK) \
2643 len = scan_##TYPE(s, KEY, MASK); \
2644 if (len == 0) { \
2645 return -EINVAL; \
2646 } \
2647 s += len
2648
2649 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
2650 #define SCAN_FIELD(NAME, TYPE, FIELD) \
2651 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
2652 s += strlen(NAME); \
2653 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
2654 continue; \
2655 }
2656
2657 #define SCAN_FINISH() \
2658 } while (*s++ == ',' && len != 0); \
2659 if (s[-1] != ')') { \
2660 return -EINVAL; \
2661 }
2662
2663 #define SCAN_FINISH_SINGLE() \
2664 } while (false); \
2665 if (*s++ != ')') { \
2666 return -EINVAL; \
2667 }
2668
2669 #define SCAN_PUT(ATTR) \
2670 if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
2671 SCAN_PUT_ATTR(key, ATTR, skey); \
2672 if (mask) { \
2673 SCAN_PUT_ATTR(mask, ATTR, smask); \
2674 } \
2675 }
2676
2677 #define SCAN_END(ATTR) \
2678 SCAN_FINISH(); \
2679 SCAN_PUT(ATTR); \
2680 return s - start; \
2681 }
2682
2683 #define SCAN_END_SINGLE(ATTR) \
2684 SCAN_FINISH_SINGLE(); \
2685 SCAN_PUT(ATTR); \
2686 return s - start; \
2687 }
2688
2689 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
2690 SCAN_BEGIN(NAME, TYPE) { \
2691 SCAN_TYPE(SCAN_AS, &skey, &smask); \
2692 } SCAN_END_SINGLE(ATTR)
2693
2694 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
2695 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
2696 SCAN_TYPE(SCAN_AS, &skey, NULL); \
2697 } SCAN_END_SINGLE(ATTR)
2698
2699 /* scan_port needs one extra argument. */
2700 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
2701 SCAN_BEGIN(NAME, TYPE) { \
2702 len = scan_port(s, &skey, &smask, port_names); \
2703 if (len == 0) { \
2704 return -EINVAL; \
2705 } \
2706 s += len; \
2707 } SCAN_END_SINGLE(ATTR)
2708
2709 static int
2710 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
2711 struct ofpbuf *key, struct ofpbuf *mask)
2712 {
2713 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
2714 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
2715 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
2716 OVS_KEY_ATTR_RECIRC_ID);
2717 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
2718
2719 SCAN_BEGIN("tunnel(", struct flow_tnl) {
2720 SCAN_FIELD("tun_id=", be64, tun_id);
2721 SCAN_FIELD("src=", ipv4, ip_src);
2722 SCAN_FIELD("dst=", ipv4, ip_dst);
2723 SCAN_FIELD("tos=", u8, ip_tos);
2724 SCAN_FIELD("ttl=", u8, ip_ttl);
2725 SCAN_FIELD("tp_src=", be16, tp_src);
2726 SCAN_FIELD("tp_dst=", be16, tp_dst);
2727 SCAN_FIELD("gbp_id=", be16, gbp_id);
2728 SCAN_FIELD("gbp_flags=", u8, gbp_flags);
2729 SCAN_FIELD("flags(", tun_flags, flags);
2730 } SCAN_END(OVS_KEY_ATTR_TUNNEL);
2731
2732 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
2733
2734 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
2735 SCAN_FIELD("src=", eth, eth_src);
2736 SCAN_FIELD("dst=", eth, eth_dst);
2737 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
2738
2739 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
2740 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
2741 SCAN_FIELD("vid=", vid, tci);
2742 SCAN_FIELD("pcp=", pcp, tci);
2743 SCAN_FIELD("cfi=", cfi, tci);
2744 } SCAN_END(OVS_KEY_ATTR_VLAN);
2745
2746 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
2747
2748 SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
2749 SCAN_FIELD("label=", mpls_label, mpls_lse);
2750 SCAN_FIELD("tc=", mpls_tc, mpls_lse);
2751 SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
2752 SCAN_FIELD("bos=", mpls_bos, mpls_lse);
2753 } SCAN_END(OVS_KEY_ATTR_MPLS);
2754
2755 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
2756 SCAN_FIELD("src=", ipv4, ipv4_src);
2757 SCAN_FIELD("dst=", ipv4, ipv4_dst);
2758 SCAN_FIELD("proto=", u8, ipv4_proto);
2759 SCAN_FIELD("tos=", u8, ipv4_tos);
2760 SCAN_FIELD("ttl=", u8, ipv4_ttl);
2761 SCAN_FIELD("frag=", frag, ipv4_frag);
2762 } SCAN_END(OVS_KEY_ATTR_IPV4);
2763
2764 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
2765 SCAN_FIELD("src=", ipv6, ipv6_src);
2766 SCAN_FIELD("dst=", ipv6, ipv6_dst);
2767 SCAN_FIELD("label=", ipv6_label, ipv6_label);
2768 SCAN_FIELD("proto=", u8, ipv6_proto);
2769 SCAN_FIELD("tclass=", u8, ipv6_tclass);
2770 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
2771 SCAN_FIELD("frag=", frag, ipv6_frag);
2772 } SCAN_END(OVS_KEY_ATTR_IPV6);
2773
2774 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
2775 SCAN_FIELD("src=", be16, tcp_src);
2776 SCAN_FIELD("dst=", be16, tcp_dst);
2777 } SCAN_END(OVS_KEY_ATTR_TCP);
2778
2779 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
2780
2781 SCAN_BEGIN("udp(", struct ovs_key_udp) {
2782 SCAN_FIELD("src=", be16, udp_src);
2783 SCAN_FIELD("dst=", be16, udp_dst);
2784 } SCAN_END(OVS_KEY_ATTR_UDP);
2785
2786 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
2787 SCAN_FIELD("src=", be16, sctp_src);
2788 SCAN_FIELD("dst=", be16, sctp_dst);
2789 } SCAN_END(OVS_KEY_ATTR_SCTP);
2790
2791 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
2792 SCAN_FIELD("type=", u8, icmp_type);
2793 SCAN_FIELD("code=", u8, icmp_code);
2794 } SCAN_END(OVS_KEY_ATTR_ICMP);
2795
2796 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
2797 SCAN_FIELD("type=", u8, icmpv6_type);
2798 SCAN_FIELD("code=", u8, icmpv6_code);
2799 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
2800
2801 SCAN_BEGIN("arp(", struct ovs_key_arp) {
2802 SCAN_FIELD("sip=", ipv4, arp_sip);
2803 SCAN_FIELD("tip=", ipv4, arp_tip);
2804 SCAN_FIELD("op=", be16, arp_op);
2805 SCAN_FIELD("sha=", eth, arp_sha);
2806 SCAN_FIELD("tha=", eth, arp_tha);
2807 } SCAN_END(OVS_KEY_ATTR_ARP);
2808
2809 SCAN_BEGIN("nd(", struct ovs_key_nd) {
2810 SCAN_FIELD("target=", ipv6, nd_target);
2811 SCAN_FIELD("sll=", eth, nd_sll);
2812 SCAN_FIELD("tll=", eth, nd_tll);
2813 } SCAN_END(OVS_KEY_ATTR_ND);
2814
2815 /* Encap open-coded. */
2816 if (!strncmp(s, "encap(", 6)) {
2817 const char *start = s;
2818 size_t encap, encap_mask = 0;
2819
2820 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
2821 if (mask) {
2822 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
2823 }
2824
2825 s += 6;
2826 for (;;) {
2827 int retval;
2828
2829 s += strspn(s, delimiters);
2830 if (!*s) {
2831 return -EINVAL;
2832 } else if (*s == ')') {
2833 break;
2834 }
2835
2836 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2837 if (retval < 0) {
2838 return retval;
2839 }
2840 s += retval;
2841 }
2842 s++;
2843
2844 nl_msg_end_nested(key, encap);
2845 if (mask) {
2846 nl_msg_end_nested(mask, encap_mask);
2847 }
2848
2849 return s - start;
2850 }
2851
2852 return -EINVAL;
2853 }
2854
2855 /* Parses the string representation of a datapath flow key, in the
2856 * format output by odp_flow_key_format(). Returns 0 if successful,
2857 * otherwise a positive errno value. On success, the flow key is
2858 * appended to 'key' as a series of Netlink attributes. On failure, no
2859 * data is appended to 'key'. Either way, 'key''s data might be
2860 * reallocated.
2861 *
2862 * If 'port_names' is nonnull, it points to an simap that maps from a port name
2863 * to a port number. (Port names may be used instead of port numbers in
2864 * in_port.)
2865 *
2866 * On success, the attributes appended to 'key' are individually syntactically
2867 * valid, but they may not be valid as a sequence. 'key' might, for example,
2868 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
2869 int
2870 odp_flow_from_string(const char *s, const struct simap *port_names,
2871 struct ofpbuf *key, struct ofpbuf *mask)
2872 {
2873 const size_t old_size = key->size;
2874 for (;;) {
2875 int retval;
2876
2877 s += strspn(s, delimiters);
2878 if (!*s) {
2879 return 0;
2880 }
2881
2882 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2883 if (retval < 0) {
2884 key->size = old_size;
2885 return -retval;
2886 }
2887 s += retval;
2888 }
2889
2890 return 0;
2891 }
2892
2893 static uint8_t
2894 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
2895 {
2896 if (is_mask) {
2897 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
2898 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
2899 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
2900 * must use a zero mask for the netlink frag field, and all ones mask
2901 * otherwise. */
2902 return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
2903 }
2904 return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
2905 : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
2906 : OVS_FRAG_TYPE_FIRST;
2907 }
2908
2909 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
2910 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
2911 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
2912 bool is_mask);
2913 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
2914 bool is_mask);
2915 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
2916 bool is_mask);
2917 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
2918 bool is_mask);
2919 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
2920 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
2921 static void get_nd_key(const struct flow *, struct ovs_key_nd *);
2922 static void put_nd_key(const struct ovs_key_nd *, struct flow *);
2923
2924 /* These share the same layout. */
2925 union ovs_key_tp {
2926 struct ovs_key_tcp tcp;
2927 struct ovs_key_udp udp;
2928 struct ovs_key_sctp sctp;
2929 };
2930
2931 static void get_tp_key(const struct flow *, union ovs_key_tp *);
2932 static void put_tp_key(const union ovs_key_tp *, struct flow *);
2933
2934 static void
2935 odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *flow,
2936 const struct flow *mask, odp_port_t odp_in_port,
2937 size_t max_mpls_depth, bool recirc, bool export_mask)
2938 {
2939 struct ovs_key_ethernet *eth_key;
2940 size_t encap;
2941 const struct flow *data = export_mask ? mask : flow;
2942
2943 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
2944
2945 if (flow->tunnel.ip_dst || export_mask) {
2946 tun_key_to_attr(buf, &data->tunnel);
2947 }
2948
2949 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
2950
2951 if (recirc) {
2952 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
2953 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
2954 }
2955
2956 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2957 * is not the magical value "ODPP_NONE". */
2958 if (export_mask || odp_in_port != ODPP_NONE) {
2959 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
2960 }
2961
2962 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
2963 sizeof *eth_key);
2964 get_ethernet_key(data, eth_key);
2965
2966 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
2967 if (export_mask) {
2968 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2969 } else {
2970 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
2971 }
2972 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
2973 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
2974 if (flow->vlan_tci == htons(0)) {
2975 goto unencap;
2976 }
2977 } else {
2978 encap = 0;
2979 }
2980
2981 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2982 /* For backwards compatibility with kernels that don't support
2983 * wildcarding, the following convention is used to encode the
2984 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
2985 *
2986 * key mask matches
2987 * -------- -------- -------
2988 * >0x5ff 0xffff Specified Ethernet II Ethertype.
2989 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
2990 * <none> 0xffff Any non-Ethernet II frame (except valid
2991 * 802.3 SNAP packet with valid eth_type).
2992 */
2993 if (export_mask) {
2994 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2995 }
2996 goto unencap;
2997 }
2998
2999 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
3000
3001 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3002 struct ovs_key_ipv4 *ipv4_key;
3003
3004 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
3005 sizeof *ipv4_key);
3006 get_ipv4_key(data, ipv4_key, export_mask);
3007 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
3008 struct ovs_key_ipv6 *ipv6_key;
3009
3010 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
3011 sizeof *ipv6_key);
3012 get_ipv6_key(data, ipv6_key, export_mask);
3013 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
3014 flow->dl_type == htons(ETH_TYPE_RARP)) {
3015 struct ovs_key_arp *arp_key;
3016
3017 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
3018 sizeof *arp_key);
3019 get_arp_key(data, arp_key);
3020 } else if (eth_type_mpls(flow->dl_type)) {
3021 struct ovs_key_mpls *mpls_key;
3022 int i, n;
3023
3024 n = flow_count_mpls_labels(flow, NULL);
3025 n = MIN(n, max_mpls_depth);
3026 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
3027 n * sizeof *mpls_key);
3028 for (i = 0; i < n; i++) {
3029 mpls_key[i].mpls_lse = data->mpls_lse[i];
3030 }
3031 }
3032
3033 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3034 if (flow->nw_proto == IPPROTO_TCP) {
3035 union ovs_key_tp *tcp_key;
3036
3037 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
3038 sizeof *tcp_key);
3039 get_tp_key(data, tcp_key);
3040 if (data->tcp_flags) {
3041 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
3042 }
3043 } else if (flow->nw_proto == IPPROTO_UDP) {
3044 union ovs_key_tp *udp_key;
3045
3046 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
3047 sizeof *udp_key);
3048 get_tp_key(data, udp_key);
3049 } else if (flow->nw_proto == IPPROTO_SCTP) {
3050 union ovs_key_tp *sctp_key;
3051
3052 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
3053 sizeof *sctp_key);
3054 get_tp_key(data, sctp_key);
3055 } else if (flow->dl_type == htons(ETH_TYPE_IP)
3056 && flow->nw_proto == IPPROTO_ICMP) {
3057 struct ovs_key_icmp *icmp_key;
3058
3059 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
3060 sizeof *icmp_key);
3061 icmp_key->icmp_type = ntohs(data->tp_src);
3062 icmp_key->icmp_code = ntohs(data->tp_dst);
3063 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
3064 && flow->nw_proto == IPPROTO_ICMPV6) {
3065 struct ovs_key_icmpv6 *icmpv6_key;
3066
3067 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
3068 sizeof *icmpv6_key);
3069 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
3070 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
3071
3072 if (flow->tp_dst == htons(0)
3073 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
3074 || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
3075 && (!export_mask || (data->tp_src == htons(0xffff)
3076 && data->tp_dst == htons(0xffff)))) {
3077
3078 struct ovs_key_nd *nd_key;
3079
3080 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
3081 sizeof *nd_key);
3082 memcpy(nd_key->nd_target, &data->nd_target,
3083 sizeof nd_key->nd_target);
3084 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
3085 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
3086 }
3087 }
3088 }
3089
3090 unencap:
3091 if (encap) {
3092 nl_msg_end_nested(buf, encap);
3093 }
3094 }
3095
3096 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
3097 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
3098 * number rather than a datapath port number). Instead, if 'odp_in_port'
3099 * is anything other than ODPP_NONE, it is included in 'buf' as the input
3100 * port.
3101 *
3102 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3103 * capable of being expanded to allow for that much space.
3104 *
3105 * 'recirc' indicates support for recirculation fields. If this is true, then
3106 * these fields will always be serialised. */
3107 void
3108 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
3109 const struct flow *mask, odp_port_t odp_in_port,
3110 bool recirc)
3111 {
3112 odp_flow_key_from_flow__(buf, flow, mask, odp_in_port, SIZE_MAX, recirc,
3113 false);
3114 }
3115
3116 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
3117 * 'buf'. 'flow' is used as a template to determine how to interpret
3118 * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but
3119 * it doesn't indicate whether the other fields should be interpreted as
3120 * ARP, IPv4, IPv6, etc.
3121 *
3122 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3123 * capable of being expanded to allow for that much space.
3124 *
3125 * 'recirc' indicates support for recirculation fields. If this is true, then
3126 * these fields will always be serialised. */
3127 void
3128 odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
3129 const struct flow *flow, uint32_t odp_in_port_mask,
3130 size_t max_mpls_depth, bool recirc)
3131 {
3132 odp_flow_key_from_flow__(buf, flow, mask, u32_to_odp(odp_in_port_mask),
3133 max_mpls_depth, recirc, true);
3134 }
3135
3136 /* Generate ODP flow key from the given packet metadata */
3137 void
3138 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
3139 {
3140 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
3141
3142 if (md->tunnel.ip_dst) {
3143 tun_key_to_attr(buf, &md->tunnel);
3144 }
3145
3146 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
3147
3148 /* Add an ingress port attribute if 'odp_in_port' is not the magical
3149 * value "ODPP_NONE". */
3150 if (md->in_port.odp_port != ODPP_NONE) {
3151 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
3152 }
3153 }
3154
3155 /* Generate packet metadata from the given ODP flow key. */
3156 void
3157 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
3158 struct pkt_metadata *md)
3159 {
3160 const struct nlattr *nla;
3161 size_t left;
3162 uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
3163 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
3164 1u << OVS_KEY_ATTR_IN_PORT;
3165
3166 *md = PKT_METADATA_INITIALIZER(ODPP_NONE);
3167
3168 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3169 uint16_t type = nl_attr_type(nla);
3170 size_t len = nl_attr_get_size(nla);
3171 int expected_len = odp_flow_key_attr_len(type);
3172
3173 if (len != expected_len && expected_len >= 0) {
3174 continue;
3175 }
3176
3177 switch (type) {
3178 case OVS_KEY_ATTR_RECIRC_ID:
3179 md->recirc_id = nl_attr_get_u32(nla);
3180 wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
3181 break;
3182 case OVS_KEY_ATTR_DP_HASH:
3183 md->dp_hash = nl_attr_get_u32(nla);
3184 wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
3185 break;
3186 case OVS_KEY_ATTR_PRIORITY:
3187 md->skb_priority = nl_attr_get_u32(nla);
3188 wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
3189 break;
3190 case OVS_KEY_ATTR_SKB_MARK:
3191 md->pkt_mark = nl_attr_get_u32(nla);
3192 wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
3193 break;
3194 case OVS_KEY_ATTR_TUNNEL: {
3195 enum odp_key_fitness res;
3196
3197 res = odp_tun_key_from_attr(nla, &md->tunnel);
3198 if (res == ODP_FIT_ERROR) {
3199 memset(&md->tunnel, 0, sizeof md->tunnel);
3200 } else if (res == ODP_FIT_PERFECT) {
3201 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
3202 }
3203 break;
3204 }
3205 case OVS_KEY_ATTR_IN_PORT:
3206 md->in_port.odp_port = nl_attr_get_odp_port(nla);
3207 wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
3208 break;
3209 default:
3210 break;
3211 }
3212
3213 if (!wanted_attrs) {
3214 return; /* Have everything. */
3215 }
3216 }
3217 }
3218
3219 uint32_t
3220 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
3221 {
3222 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
3223 return hash_words(ALIGNED_CAST(const uint32_t *, key),
3224 key_len / sizeof(uint32_t), 0);
3225 }
3226
3227 static void
3228 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
3229 uint64_t attrs, int out_of_range_attr,
3230 const struct nlattr *key, size_t key_len)
3231 {
3232 struct ds s;
3233 int i;
3234
3235 if (VLOG_DROP_DBG(rl)) {
3236 return;
3237 }
3238
3239 ds_init(&s);
3240 for (i = 0; i < 64; i++) {
3241 if (attrs & (UINT64_C(1) << i)) {
3242 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3243
3244 ds_put_format(&s, " %s",
3245 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
3246 }
3247 }
3248 if (out_of_range_attr) {
3249 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
3250 }
3251
3252 ds_put_cstr(&s, ": ");
3253 odp_flow_key_format(key, key_len, &s);
3254
3255 VLOG_DBG("%s:%s", title, ds_cstr(&s));
3256 ds_destroy(&s);
3257 }
3258
3259 static uint8_t
3260 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
3261 {
3262 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3263
3264 if (is_mask) {
3265 return odp_frag ? FLOW_NW_FRAG_MASK : 0;
3266 }
3267
3268 if (odp_frag > OVS_FRAG_TYPE_LATER) {
3269 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
3270 return 0xff; /* Error. */
3271 }
3272
3273 return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
3274 : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
3275 : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
3276 }
3277
3278 static bool
3279 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
3280 const struct nlattr *attrs[], uint64_t *present_attrsp,
3281 int *out_of_range_attrp)
3282 {
3283 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3284 const struct nlattr *nla;
3285 uint64_t present_attrs;
3286 size_t left;
3287
3288 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
3289 present_attrs = 0;
3290 *out_of_range_attrp = 0;
3291 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3292 uint16_t type = nl_attr_type(nla);
3293 size_t len = nl_attr_get_size(nla);
3294 int expected_len = odp_flow_key_attr_len(type);
3295
3296 if (len != expected_len && expected_len >= 0) {
3297 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3298
3299 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
3300 "length %d", ovs_key_attr_to_string(type, namebuf,
3301 sizeof namebuf),
3302 len, expected_len);
3303 return false;
3304 }
3305
3306 if (type > OVS_KEY_ATTR_MAX) {
3307 *out_of_range_attrp = type;
3308 } else {
3309 if (present_attrs & (UINT64_C(1) << type)) {
3310 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3311
3312 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
3313 ovs_key_attr_to_string(type,
3314 namebuf, sizeof namebuf));
3315 return false;
3316 }
3317
3318 present_attrs |= UINT64_C(1) << type;
3319 attrs[type] = nla;
3320 }
3321 }
3322 if (left) {
3323 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
3324 return false;
3325 }
3326
3327 *present_attrsp = present_attrs;
3328 return true;
3329 }
3330
3331 static enum odp_key_fitness
3332 check_expectations(uint64_t present_attrs, int out_of_range_attr,
3333 uint64_t expected_attrs,
3334 const struct nlattr *key, size_t key_len)
3335 {
3336 uint64_t missing_attrs;
3337 uint64_t extra_attrs;
3338
3339 missing_attrs = expected_attrs & ~present_attrs;
3340 if (missing_attrs) {
3341 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3342 log_odp_key_attributes(&rl, "expected but not present",
3343 missing_attrs, 0, key, key_len);
3344 return ODP_FIT_TOO_LITTLE;
3345 }
3346
3347 extra_attrs = present_attrs & ~expected_attrs;
3348 if (extra_attrs || out_of_range_attr) {
3349 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3350 log_odp_key_attributes(&rl, "present but not expected",
3351 extra_attrs, out_of_range_attr, key, key_len);
3352 return ODP_FIT_TOO_MUCH;
3353 }
3354
3355 return ODP_FIT_PERFECT;
3356 }
3357
3358 static bool
3359 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3360 uint64_t present_attrs, uint64_t *expected_attrs,
3361 struct flow *flow, const struct flow *src_flow)
3362 {
3363 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3364 bool is_mask = flow != src_flow;
3365
3366 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
3367 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
3368 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3369 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
3370 ntohs(flow->dl_type));
3371 return false;
3372 }
3373 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
3374 flow->dl_type != htons(0xffff)) {
3375 return false;
3376 }
3377 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
3378 } else {
3379 if (!is_mask) {
3380 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
3381 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
3382 /* See comments in odp_flow_key_from_flow__(). */
3383 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
3384 return false;
3385 }
3386 }
3387 return true;
3388 }
3389
3390 static enum odp_key_fitness
3391 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3392 uint64_t present_attrs, int out_of_range_attr,
3393 uint64_t expected_attrs, struct flow *flow,
3394 const struct nlattr *key, size_t key_len,
3395 const struct flow *src_flow)
3396 {
3397 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3398 bool is_mask = src_flow != flow;
3399 const void *check_start = NULL;
3400 size_t check_len = 0;
3401 enum ovs_key_attr expected_bit = 0xff;
3402
3403 if (eth_type_mpls(src_flow->dl_type)) {
3404 if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3405 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3406 }
3407 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3408 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
3409 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
3410 int n = size / sizeof(ovs_be32);
3411 int i;
3412
3413 if (!size || size % sizeof(ovs_be32)) {
3414 return ODP_FIT_ERROR;
3415 }
3416 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
3417 return ODP_FIT_ERROR;
3418 }
3419
3420 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
3421 flow->mpls_lse[i] = mpls_lse[i];
3422 }
3423 if (n > FLOW_MAX_MPLS_LABELS) {
3424 return ODP_FIT_TOO_MUCH;
3425 }
3426
3427 if (!is_mask) {
3428 /* BOS may be set only in the innermost label. */
3429 for (i = 0; i < n - 1; i++) {
3430 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
3431 return ODP_FIT_ERROR;
3432 }
3433 }
3434
3435 /* BOS must be set in the innermost label. */
3436 if (n < FLOW_MAX_MPLS_LABELS
3437 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
3438 return ODP_FIT_TOO_LITTLE;
3439 }
3440 }
3441 }
3442
3443 goto done;
3444 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
3445 if (!is_mask) {
3446 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
3447 }
3448 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
3449 const struct ovs_key_ipv4 *ipv4_key;
3450
3451 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
3452 put_ipv4_key(ipv4_key, flow, is_mask);
3453 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3454 return ODP_FIT_ERROR;
3455 }
3456 if (is_mask) {
3457 check_start = ipv4_key;
3458 check_len = sizeof *ipv4_key;
3459 expected_bit = OVS_KEY_ATTR_IPV4;
3460 }
3461 }
3462 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
3463 if (!is_mask) {
3464 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
3465 }
3466 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
3467 const struct ovs_key_ipv6 *ipv6_key;
3468
3469 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
3470 put_ipv6_key(ipv6_key, flow, is_mask);
3471 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3472 return ODP_FIT_ERROR;
3473 }
3474 if (is_mask) {
3475 check_start = ipv6_key;
3476 check_len = sizeof *ipv6_key;
3477 expected_bit = OVS_KEY_ATTR_IPV6;
3478 }
3479 }
3480 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
3481 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
3482 if (!is_mask) {
3483 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
3484 }
3485 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
3486 const struct ovs_key_arp *arp_key;
3487
3488 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
3489 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
3490 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
3491 "key", ntohs(arp_key->arp_op));
3492 return ODP_FIT_ERROR;
3493 }
3494 put_arp_key(arp_key, flow);
3495 if (is_mask) {
3496 check_start = arp_key;
3497 check_len = sizeof *arp_key;
3498 expected_bit = OVS_KEY_ATTR_ARP;
3499 }
3500 }
3501 } else {
3502 goto done;
3503 }
3504 if (check_len > 0) { /* Happens only when 'is_mask'. */
3505 if (!is_all_zeros(check_start, check_len) &&
3506 flow->dl_type != htons(0xffff)) {
3507 return ODP_FIT_ERROR;
3508 } else {
3509 expected_attrs |= UINT64_C(1) << expected_bit;
3510 }
3511 }
3512
3513 expected_bit = OVS_KEY_ATTR_UNSPEC;
3514 if (src_flow->nw_proto == IPPROTO_TCP
3515 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3516 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3517 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3518 if (!is_mask) {
3519 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
3520 }
3521 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
3522 const union ovs_key_tp *tcp_key;
3523
3524 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
3525 put_tp_key(tcp_key, flow);
3526 expected_bit = OVS_KEY_ATTR_TCP;
3527 }
3528 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
3529 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
3530 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
3531 }
3532 } else if (src_flow->nw_proto == IPPROTO_UDP
3533 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3534 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3535 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3536 if (!is_mask) {
3537 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
3538 }
3539 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
3540 const union ovs_key_tp *udp_key;
3541
3542 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
3543 put_tp_key(udp_key, flow);
3544 expected_bit = OVS_KEY_ATTR_UDP;
3545 }
3546 } else if (src_flow->nw_proto == IPPROTO_SCTP
3547 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3548 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3549 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3550 if (!is_mask) {
3551 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
3552 }
3553 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
3554 const union ovs_key_tp *sctp_key;
3555
3556 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
3557 put_tp_key(sctp_key, flow);
3558 expected_bit = OVS_KEY_ATTR_SCTP;
3559 }
3560 } else if (src_flow->nw_proto == IPPROTO_ICMP
3561 && src_flow->dl_type == htons(ETH_TYPE_IP)
3562 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3563 if (!is_mask) {
3564 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
3565 }
3566 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
3567 const struct ovs_key_icmp *icmp_key;
3568
3569 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
3570 flow->tp_src = htons(icmp_key->icmp_type);
3571 flow->tp_dst = htons(icmp_key->icmp_code);
3572 expected_bit = OVS_KEY_ATTR_ICMP;
3573 }
3574 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
3575 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
3576 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3577 if (!is_mask) {
3578 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
3579 }
3580 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
3581 const struct ovs_key_icmpv6 *icmpv6_key;
3582
3583 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
3584 flow->tp_src = htons(icmpv6_key->icmpv6_type);
3585 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
3586 expected_bit = OVS_KEY_ATTR_ICMPV6;
3587 if (src_flow->tp_dst == htons(0) &&
3588 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
3589 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
3590 if (!is_mask) {
3591 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3592 }
3593 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
3594 const struct ovs_key_nd *nd_key;
3595
3596 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
3597 memcpy(&flow->nd_target, nd_key->nd_target,
3598 sizeof flow->nd_target);
3599 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
3600 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
3601 if (is_mask) {
3602 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
3603 (flow->tp_src != htons(0xffff) ||
3604 flow->tp_dst != htons(0xffff))) {
3605 return ODP_FIT_ERROR;
3606 } else {
3607 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3608 }
3609 }
3610 }
3611 }
3612 }
3613 }
3614 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
3615 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
3616 return ODP_FIT_ERROR;
3617 } else {
3618 expected_attrs |= UINT64_C(1) << expected_bit;
3619 }
3620 }
3621
3622 done:
3623 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
3624 key, key_len);
3625 }
3626
3627 /* Parse 802.1Q header then encapsulated L3 attributes. */
3628 static enum odp_key_fitness
3629 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3630 uint64_t present_attrs, int out_of_range_attr,
3631 uint64_t expected_attrs, struct flow *flow,
3632 const struct nlattr *key, size_t key_len,
3633 const struct flow *src_flow)
3634 {
3635 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3636 bool is_mask = src_flow != flow;
3637
3638 const struct nlattr *encap
3639 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
3640 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
3641 enum odp_key_fitness encap_fitness;
3642 enum odp_key_fitness fitness;
3643
3644 /* Calculate fitness of outer attributes. */
3645 if (!is_mask) {
3646 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
3647 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
3648 } else {
3649 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3650 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3651 }
3652 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
3653 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
3654 }
3655 }
3656 fitness = check_expectations(present_attrs, out_of_range_attr,
3657 expected_attrs, key, key_len);
3658
3659 /* Set vlan_tci.
3660 * Remove the TPID from dl_type since it's not the real Ethertype. */
3661 flow->dl_type = htons(0);
3662 flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
3663 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
3664 : htons(0));
3665 if (!is_mask) {
3666 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
3667 return ODP_FIT_TOO_LITTLE;
3668 } else if (flow->vlan_tci == htons(0)) {
3669 /* Corner case for a truncated 802.1Q header. */
3670 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
3671 return ODP_FIT_TOO_MUCH;
3672 }
3673 return fitness;
3674 } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
3675 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
3676 "but CFI bit is not set", ntohs(flow->vlan_tci));
3677 return ODP_FIT_ERROR;
3678 }
3679 } else {
3680 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
3681 return fitness;
3682 }
3683 }
3684
3685 /* Now parse the encapsulated attributes. */
3686 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
3687 attrs, &present_attrs, &out_of_range_attr)) {
3688 return ODP_FIT_ERROR;
3689 }
3690 expected_attrs = 0;
3691
3692 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
3693 return ODP_FIT_ERROR;
3694 }
3695 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3696 expected_attrs, flow, key, key_len,
3697 src_flow);
3698
3699 /* The overall fitness is the worse of the outer and inner attributes. */
3700 return MAX(fitness, encap_fitness);
3701 }
3702
3703 static enum odp_key_fitness
3704 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
3705 struct flow *flow, const struct flow *src_flow)
3706 {
3707 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
3708 uint64_t expected_attrs;
3709 uint64_t present_attrs;
3710 int out_of_range_attr;
3711 bool is_mask = src_flow != flow;
3712
3713 memset(flow, 0, sizeof *flow);
3714
3715 /* Parse attributes. */
3716 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
3717 &out_of_range_attr)) {
3718 return ODP_FIT_ERROR;
3719 }
3720 expected_attrs = 0;
3721
3722 /* Metadata. */
3723 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
3724 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
3725 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
3726 } else if (is_mask) {
3727 /* Always exact match recirc_id if it is not specified. */
3728 flow->recirc_id = UINT32_MAX;
3729 }
3730
3731 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
3732 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
3733 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
3734 }
3735 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
3736 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
3737 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
3738 }
3739
3740 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
3741 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
3742 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
3743 }
3744
3745 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
3746 enum odp_key_fitness res;
3747
3748 res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
3749 if (res == ODP_FIT_ERROR) {
3750 return ODP_FIT_ERROR;
3751 } else if (res == ODP_FIT_PERFECT) {
3752 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
3753 }
3754 }
3755
3756 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
3757 flow->in_port.odp_port
3758 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
3759 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
3760 } else if (!is_mask) {
3761 flow->in_port.odp_port = ODPP_NONE;
3762 }
3763
3764 /* Ethernet header. */
3765 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
3766 const struct ovs_key_ethernet *eth_key;
3767
3768 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
3769 put_ethernet_key(eth_key, flow);
3770 if (is_mask) {
3771 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3772 }
3773 }
3774 if (!is_mask) {
3775 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3776 }
3777
3778 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
3779 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
3780 src_flow)) {
3781 return ODP_FIT_ERROR;
3782 }
3783
3784 if (is_mask
3785 ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
3786 : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
3787 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
3788 expected_attrs, flow, key, key_len, src_flow);
3789 }
3790 if (is_mask) {
3791 flow->vlan_tci = htons(0xffff);
3792 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3793 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
3794 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3795 }
3796 }
3797 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3798 expected_attrs, flow, key, key_len, src_flow);
3799 }
3800
3801 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
3802 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
3803 * 'key' fits our expectations for what a flow key should contain.
3804 *
3805 * The 'in_port' will be the datapath's understanding of the port. The
3806 * caller will need to translate with odp_port_to_ofp_port() if the
3807 * OpenFlow port is needed.
3808 *
3809 * This function doesn't take the packet itself as an argument because none of
3810 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
3811 * it is always possible to infer which additional attribute(s) should appear
3812 * by looking at the attributes for lower-level protocols, e.g. if the network
3813 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
3814 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
3815 * must be absent. */
3816 enum odp_key_fitness
3817 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
3818 struct flow *flow)
3819 {
3820 return odp_flow_key_to_flow__(key, key_len, flow, flow);
3821 }
3822
3823 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask
3824 * structure in 'mask'. 'flow' must be a previously translated flow
3825 * corresponding to 'mask'. Returns an ODP_FIT_* value that indicates how well
3826 * 'key' fits our expectations for what a flow key should contain. */
3827 enum odp_key_fitness
3828 odp_flow_key_to_mask(const struct nlattr *key, size_t key_len,
3829 struct flow *mask, const struct flow *flow)
3830 {
3831 return odp_flow_key_to_flow__(key, key_len, mask, flow);
3832 }
3833
3834 /* Returns 'fitness' as a string, for use in debug messages. */
3835 const char *
3836 odp_key_fitness_to_string(enum odp_key_fitness fitness)
3837 {
3838 switch (fitness) {
3839 case ODP_FIT_PERFECT:
3840 return "OK";
3841 case ODP_FIT_TOO_MUCH:
3842 return "too_much";
3843 case ODP_FIT_TOO_LITTLE:
3844 return "too_little";
3845 case ODP_FIT_ERROR:
3846 return "error";
3847 default:
3848 return "<unknown>";
3849 }
3850 }
3851
3852 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
3853 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
3854 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
3855 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
3856 * null, then the return value is not meaningful.) */
3857 size_t
3858 odp_put_userspace_action(uint32_t pid,
3859 const void *userdata, size_t userdata_size,
3860 odp_port_t tunnel_out_port,
3861 struct ofpbuf *odp_actions)
3862 {
3863 size_t userdata_ofs;
3864 size_t offset;
3865
3866 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3867 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3868 if (userdata) {
3869 userdata_ofs = odp_actions->size + NLA_HDRLEN;
3870
3871 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
3872 * module before Linux 3.10 required the userdata to be exactly 8 bytes
3873 * long:
3874 *
3875 * - The kernel rejected shorter userdata with -ERANGE.
3876 *
3877 * - The kernel silently dropped userdata beyond the first 8 bytes.
3878 *
3879 * Thus, for maximum compatibility, always put at least 8 bytes. (We
3880 * separately disable features that required more than 8 bytes.) */
3881 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3882 MAX(8, userdata_size)),
3883 userdata, userdata_size);
3884 } else {
3885 userdata_ofs = 0;
3886 }
3887 if (tunnel_out_port != ODPP_NONE) {
3888 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
3889 tunnel_out_port);
3890 }
3891 nl_msg_end_nested(odp_actions, offset);
3892
3893 return userdata_ofs;
3894 }
3895
3896 void
3897 odp_put_tunnel_action(const struct flow_tnl *tunnel,
3898 struct ofpbuf *odp_actions)
3899 {
3900 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3901 tun_key_to_attr(odp_actions, tunnel);
3902 nl_msg_end_nested(odp_actions, offset);
3903 }
3904
3905 void
3906 odp_put_tnl_push_action(struct ofpbuf *odp_actions,
3907 struct ovs_action_push_tnl *data)
3908 {
3909 int size = offsetof(struct ovs_action_push_tnl, header);
3910
3911 size += data->header_len;
3912 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
3913 }
3914
3915 \f
3916 /* The commit_odp_actions() function and its helpers. */
3917
3918 static void
3919 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3920 const void *key, size_t key_size)
3921 {
3922 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3923 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3924 nl_msg_end_nested(odp_actions, offset);
3925 }
3926
3927 /* Masked set actions have a mask following the data within the netlink
3928 * attribute. The unmasked bits in the data will be cleared as the data
3929 * is copied to the action. */
3930 void
3931 commit_masked_set_action(struct ofpbuf *odp_actions,
3932 enum ovs_key_attr key_type,
3933 const void *key_, const void *mask_, size_t key_size)
3934 {
3935 size_t offset = nl_msg_start_nested(odp_actions,
3936 OVS_ACTION_ATTR_SET_MASKED);
3937 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
3938 const char *key = key_, *mask = mask_;
3939
3940 memcpy(data + key_size, mask, key_size);
3941 /* Clear unmasked bits while copying. */
3942 while (key_size--) {
3943 *data++ = *key++ & *mask++;
3944 }
3945 nl_msg_end_nested(odp_actions, offset);
3946 }
3947
3948 /* If any of the flow key data that ODP actions can modify are different in
3949 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
3950 * 'odp_actions' that change the flow tunneling information in key from
3951 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
3952 * same way. In other words, operates the same as commit_odp_actions(), but
3953 * only on tunneling information. */
3954 void
3955 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
3956 struct ofpbuf *odp_actions)
3957 {
3958 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
3959 if (flow->tunnel.ip_dst) {
3960 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
3961 return;
3962 }
3963 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
3964 odp_put_tunnel_action(&base->tunnel, odp_actions);
3965 }
3966 }
3967
3968 static bool
3969 commit(enum ovs_key_attr attr, bool use_masked_set,
3970 const void *key, void *base, void *mask, size_t size,
3971 struct ofpbuf *odp_actions)
3972 {
3973 if (memcmp(key, base, size)) {
3974 bool fully_masked = odp_mask_is_exact(attr, mask, size);
3975
3976 if (use_masked_set && !fully_masked) {
3977 commit_masked_set_action(odp_actions, attr, key, mask, size);
3978 } else {
3979 if (!fully_masked) {
3980 memset(mask, 0xff, size);
3981 }
3982 commit_set_action(odp_actions, attr, key, size);
3983 }
3984 memcpy(base, key, size);
3985 return true;
3986 } else {
3987 /* Mask bits are set when we have either read or set the corresponding
3988 * values. Masked bits will be exact-matched, no need to set them
3989 * if the value did not actually change. */
3990 return false;
3991 }
3992 }
3993
3994 static void
3995 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
3996 {
3997 memcpy(eth->eth_src, flow->dl_src, ETH_ADDR_LEN);
3998 memcpy(eth->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
3999 }
4000
4001 static void
4002 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
4003 {
4004 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
4005 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
4006 }
4007
4008 static void
4009 commit_set_ether_addr_action(const struct flow *flow, struct flow *base_flow,
4010 struct ofpbuf *odp_actions,
4011 struct flow_wildcards *wc,
4012 bool use_masked)
4013 {
4014 struct ovs_key_ethernet key, base, mask;
4015
4016 get_ethernet_key(flow, &key);
4017 get_ethernet_key(base_flow, &base);
4018 get_ethernet_key(&wc->masks, &mask);
4019
4020 if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
4021 &key, &base, &mask, sizeof key, odp_actions)) {
4022 put_ethernet_key(&base, base_flow);
4023 put_ethernet_key(&mask, &wc->masks);
4024 }
4025 }
4026
4027 static void
4028 pop_vlan(struct flow *base,
4029 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4030 {
4031 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4032
4033 if (base->vlan_tci & htons(VLAN_CFI)) {
4034 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
4035 base->vlan_tci = 0;
4036 }
4037 }
4038
4039 static void
4040 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
4041 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4042 {
4043 if (base->vlan_tci == vlan_tci) {
4044 return;
4045 }
4046
4047 pop_vlan(base, odp_actions, wc);
4048 if (vlan_tci & htons(VLAN_CFI)) {
4049 struct ovs_action_push_vlan vlan;
4050
4051 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
4052 vlan.vlan_tci = vlan_tci;
4053 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
4054 &vlan, sizeof vlan);
4055 }
4056 base->vlan_tci = vlan_tci;
4057 }
4058
4059 /* Wildcarding already done at action translation time. */
4060 static void
4061 commit_mpls_action(const struct flow *flow, struct flow *base,
4062 struct ofpbuf *odp_actions)
4063 {
4064 int base_n = flow_count_mpls_labels(base, NULL);
4065 int flow_n = flow_count_mpls_labels(flow, NULL);
4066 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
4067 NULL);
4068
4069 while (base_n > common_n) {
4070 if (base_n - 1 == common_n && flow_n > common_n) {
4071 /* If there is only one more LSE in base than there are common
4072 * between base and flow; and flow has at least one more LSE than
4073 * is common then the topmost LSE of base may be updated using
4074 * set */
4075 struct ovs_key_mpls mpls_key;
4076
4077 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
4078 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
4079 &mpls_key, sizeof mpls_key);
4080 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
4081 common_n++;
4082 } else {
4083 /* Otherwise, if there more LSEs in base than are common between
4084 * base and flow then pop the topmost one. */
4085 ovs_be16 dl_type;
4086 bool popped;
4087
4088 /* If all the LSEs are to be popped and this is not the outermost
4089 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
4090 * POP_MPLS action instead of flow->dl_type.
4091 *
4092 * This is because the POP_MPLS action requires its ethertype
4093 * argument to be an MPLS ethernet type but in this case
4094 * flow->dl_type will be a non-MPLS ethernet type.
4095 *
4096 * When the final POP_MPLS action occurs it use flow->dl_type and
4097 * the and the resulting packet will have the desired dl_type. */
4098 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
4099 dl_type = htons(ETH_TYPE_MPLS);
4100 } else {
4101 dl_type = flow->dl_type;
4102 }
4103 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
4104 popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL);
4105 ovs_assert(popped);
4106 base_n--;
4107 }
4108 }
4109
4110 /* If, after the above popping and setting, there are more LSEs in flow
4111 * than base then some LSEs need to be pushed. */
4112 while (base_n < flow_n) {
4113 struct ovs_action_push_mpls *mpls;
4114
4115 mpls = nl_msg_put_unspec_zero(odp_actions,
4116 OVS_ACTION_ATTR_PUSH_MPLS,
4117 sizeof *mpls);
4118 mpls->mpls_ethertype = flow->dl_type;
4119 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
4120 flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL);
4121 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
4122 base_n++;
4123 }
4124 }
4125
4126 static void
4127 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
4128 {
4129 ipv4->ipv4_src = flow->nw_src;
4130 ipv4->ipv4_dst = flow->nw_dst;
4131 ipv4->ipv4_proto = flow->nw_proto;
4132 ipv4->ipv4_tos = flow->nw_tos;
4133 ipv4->ipv4_ttl = flow->nw_ttl;
4134 ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4135 }
4136
4137 static void
4138 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
4139 {
4140 flow->nw_src = ipv4->ipv4_src;
4141 flow->nw_dst = ipv4->ipv4_dst;
4142 flow->nw_proto = ipv4->ipv4_proto;
4143 flow->nw_tos = ipv4->ipv4_tos;
4144 flow->nw_ttl = ipv4->ipv4_ttl;
4145 flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
4146 }
4147
4148 static void
4149 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
4150 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4151 bool use_masked)
4152 {
4153 struct ovs_key_ipv4 key, mask, base;
4154
4155 /* Check that nw_proto and nw_frag remain unchanged. */
4156 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4157 flow->nw_frag == base_flow->nw_frag);
4158
4159 get_ipv4_key(flow, &key, false);
4160 get_ipv4_key(base_flow, &base, false);
4161 get_ipv4_key(&wc->masks, &mask, true);
4162 mask.ipv4_proto = 0; /* Not writeable. */
4163 mask.ipv4_frag = 0; /* Not writable. */
4164
4165 if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
4166 odp_actions)) {
4167 put_ipv4_key(&base, base_flow, false);
4168 if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
4169 put_ipv4_key(&mask, &wc->masks, true);
4170 }
4171 }
4172 }
4173
4174 static void
4175 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
4176 {
4177 memcpy(ipv6->ipv6_src, &flow->ipv6_src, sizeof ipv6->ipv6_src);
4178 memcpy(ipv6->ipv6_dst, &flow->ipv6_dst, sizeof ipv6->ipv6_dst);
4179 ipv6->ipv6_label = flow->ipv6_label;
4180 ipv6->ipv6_proto = flow->nw_proto;
4181 ipv6->ipv6_tclass = flow->nw_tos;
4182 ipv6->ipv6_hlimit = flow->nw_ttl;
4183 ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4184 }
4185
4186 static void
4187 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
4188 {
4189 memcpy(&flow->ipv6_src, ipv6->ipv6_src, sizeof flow->ipv6_src);
4190 memcpy(&flow->ipv6_dst, ipv6->ipv6_dst, sizeof flow->ipv6_dst);
4191 flow->ipv6_label = ipv6->ipv6_label;
4192 flow->nw_proto = ipv6->ipv6_proto;
4193 flow->nw_tos = ipv6->ipv6_tclass;
4194 flow->nw_ttl = ipv6->ipv6_hlimit;
4195 flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
4196 }
4197
4198 static void
4199 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
4200 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4201 bool use_masked)
4202 {
4203 struct ovs_key_ipv6 key, mask, base;
4204
4205 /* Check that nw_proto and nw_frag remain unchanged. */
4206 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4207 flow->nw_frag == base_flow->nw_frag);
4208
4209 get_ipv6_key(flow, &key, false);
4210 get_ipv6_key(base_flow, &base, false);
4211 get_ipv6_key(&wc->masks, &mask, true);
4212 mask.ipv6_proto = 0; /* Not writeable. */
4213 mask.ipv6_frag = 0; /* Not writable. */
4214
4215 if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
4216 odp_actions)) {
4217 put_ipv6_key(&base, base_flow, false);
4218 if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
4219 put_ipv6_key(&mask, &wc->masks, true);
4220 }
4221 }
4222 }
4223
4224 static void
4225 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
4226 {
4227 /* ARP key has padding, clear it. */
4228 memset(arp, 0, sizeof *arp);
4229
4230 arp->arp_sip = flow->nw_src;
4231 arp->arp_tip = flow->nw_dst;
4232 arp->arp_op = htons(flow->nw_proto);
4233 memcpy(arp->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
4234 memcpy(arp->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
4235 }
4236
4237 static void
4238 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
4239 {
4240 flow->nw_src = arp->arp_sip;
4241 flow->nw_dst = arp->arp_tip;
4242 flow->nw_proto = ntohs(arp->arp_op);
4243 memcpy(flow->arp_sha, arp->arp_sha, ETH_ADDR_LEN);
4244 memcpy(flow->arp_tha, arp->arp_tha, ETH_ADDR_LEN);
4245 }
4246
4247 static enum slow_path_reason
4248 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
4249 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4250 {
4251 struct ovs_key_arp key, mask, base;
4252
4253 get_arp_key(flow, &key);
4254 get_arp_key(base_flow, &base);
4255 get_arp_key(&wc->masks, &mask);
4256
4257 if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
4258 odp_actions)) {
4259 put_arp_key(&base, base_flow);
4260 put_arp_key(&mask, &wc->masks);
4261 return SLOW_ACTION;
4262 }
4263 return 0;
4264 }
4265
4266 static void
4267 get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
4268 {
4269 memcpy(nd->nd_target, &flow->nd_target, sizeof flow->nd_target);
4270 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4271 memcpy(nd->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
4272 memcpy(nd->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
4273 }
4274
4275 static void
4276 put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
4277 {
4278 memcpy(&flow->nd_target, &flow->nd_target, sizeof flow->nd_target);
4279 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4280 memcpy(flow->arp_sha, nd->nd_sll, ETH_ADDR_LEN);
4281 memcpy(flow->arp_tha, nd->nd_tll, ETH_ADDR_LEN);
4282 }
4283
4284 static enum slow_path_reason
4285 commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
4286 struct ofpbuf *odp_actions,
4287 struct flow_wildcards *wc, bool use_masked)
4288 {
4289 struct ovs_key_nd key, mask, base;
4290
4291 get_nd_key(flow, &key);
4292 get_nd_key(base_flow, &base);
4293 get_nd_key(&wc->masks, &mask);
4294
4295 if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
4296 odp_actions)) {
4297 put_nd_key(&base, base_flow);
4298 put_nd_key(&mask, &wc->masks);
4299 return SLOW_ACTION;
4300 }
4301
4302 return 0;
4303 }
4304
4305 static enum slow_path_reason
4306 commit_set_nw_action(const struct flow *flow, struct flow *base,
4307 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4308 bool use_masked)
4309 {
4310 /* Check if 'flow' really has an L3 header. */
4311 if (!flow->nw_proto) {
4312 return 0;
4313 }
4314
4315 switch (ntohs(base->dl_type)) {
4316 case ETH_TYPE_IP:
4317 commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
4318 break;
4319
4320 case ETH_TYPE_IPV6:
4321 commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
4322 return commit_set_nd_action(flow, base, odp_actions, wc, use_masked);
4323
4324 case ETH_TYPE_ARP:
4325 return commit_set_arp_action(flow, base, odp_actions, wc);
4326 }
4327
4328 return 0;
4329 }
4330
4331 /* TCP, UDP, and SCTP keys have the same layout. */
4332 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
4333 sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
4334
4335 static void
4336 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
4337 {
4338 tp->tcp.tcp_src = flow->tp_src;
4339 tp->tcp.tcp_dst = flow->tp_dst;
4340 }
4341
4342 static void
4343 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
4344 {
4345 flow->tp_src = tp->tcp.tcp_src;
4346 flow->tp_dst = tp->tcp.tcp_dst;
4347 }
4348
4349 static void
4350 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
4351 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4352 bool use_masked)
4353 {
4354 enum ovs_key_attr key_type;
4355 union ovs_key_tp key, mask, base;
4356
4357 /* Check if 'flow' really has an L3 header. */
4358 if (!flow->nw_proto) {
4359 return;
4360 }
4361
4362 if (!is_ip_any(base_flow)) {
4363 return;
4364 }
4365
4366 if (flow->nw_proto == IPPROTO_TCP) {
4367 key_type = OVS_KEY_ATTR_TCP;
4368 } else if (flow->nw_proto == IPPROTO_UDP) {
4369 key_type = OVS_KEY_ATTR_UDP;
4370 } else if (flow->nw_proto == IPPROTO_SCTP) {
4371 key_type = OVS_KEY_ATTR_SCTP;
4372 } else {
4373 return;
4374 }
4375
4376 get_tp_key(flow, &key);
4377 get_tp_key(base_flow, &base);
4378 get_tp_key(&wc->masks, &mask);
4379
4380 if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
4381 odp_actions)) {
4382 put_tp_key(&base, base_flow);
4383 put_tp_key(&mask, &wc->masks);
4384 }
4385 }
4386
4387 static void
4388 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
4389 struct ofpbuf *odp_actions,
4390 struct flow_wildcards *wc,
4391 bool use_masked)
4392 {
4393 uint32_t key, mask, base;
4394
4395 key = flow->skb_priority;
4396 base = base_flow->skb_priority;
4397 mask = wc->masks.skb_priority;
4398
4399 if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
4400 sizeof key, odp_actions)) {
4401 base_flow->skb_priority = base;
4402 wc->masks.skb_priority = mask;
4403 }
4404 }
4405
4406 static void
4407 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
4408 struct ofpbuf *odp_actions,
4409 struct flow_wildcards *wc,
4410 bool use_masked)
4411 {
4412 uint32_t key, mask, base;
4413
4414 key = flow->pkt_mark;
4415 base = base_flow->pkt_mark;
4416 mask = wc->masks.pkt_mark;
4417
4418 if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
4419 sizeof key, odp_actions)) {
4420 base_flow->pkt_mark = base;
4421 wc->masks.pkt_mark = mask;
4422 }
4423 }
4424
4425 /* If any of the flow key data that ODP actions can modify are different in
4426 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
4427 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
4428 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
4429 * in addition to this function if needed. Sets fields in 'wc' that are
4430 * used as part of the action.
4431 *
4432 * Returns a reason to force processing the flow's packets into the userspace
4433 * slow path, if there is one, otherwise 0. */
4434 enum slow_path_reason
4435 commit_odp_actions(const struct flow *flow, struct flow *base,
4436 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4437 bool use_masked)
4438 {
4439 enum slow_path_reason slow;
4440
4441 commit_set_ether_addr_action(flow, base, odp_actions, wc, use_masked);
4442 slow = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
4443 commit_set_port_action(flow, base, odp_actions, wc, use_masked);
4444 commit_mpls_action(flow, base, odp_actions);
4445 commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
4446 commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
4447 commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);
4448
4449 return slow;
4450 }