]> git.proxmox.com Git - ovs.git/blob - lib/odp-util.c
odp-utils: Refactor slow_path_reason parse and format functions.
[ovs.git] / lib / odp-util.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012 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 #include "byte-order.h"
28 #include "coverage.h"
29 #include "dynamic-string.h"
30 #include "flow.h"
31 #include "netlink.h"
32 #include "ofpbuf.h"
33 #include "packets.h"
34 #include "simap.h"
35 #include "timeval.h"
36 #include "util.h"
37 #include "vlog.h"
38
39 VLOG_DEFINE_THIS_MODULE(odp_util);
40
41 /* The interface between userspace and kernel uses an "OVS_*" prefix.
42 * Since this is fairly non-specific for the OVS userspace components,
43 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
44 * interactions with the datapath.
45 */
46
47 /* The set of characters that may separate one action or one key attribute
48 * from another. */
49 static const char *delimiters = ", \t\r\n";
50
51 static int parse_odp_key_attr(const char *, const struct simap *port_names,
52 struct ofpbuf *);
53 static void format_odp_key_attr(const struct nlattr *a, struct ds *ds);
54
55 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
56 * 'type':
57 *
58 * - For an action whose argument has a fixed length, returned that
59 * nonnegative length in bytes.
60 *
61 * - For an action with a variable-length argument, returns -2.
62 *
63 * - For an invalid 'type', returns -1. */
64 static int
65 odp_action_len(uint16_t type)
66 {
67 if (type > OVS_ACTION_ATTR_MAX) {
68 return -1;
69 }
70
71 switch ((enum ovs_action_attr) type) {
72 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
73 case OVS_ACTION_ATTR_USERSPACE: return -2;
74 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
75 case OVS_ACTION_ATTR_POP_VLAN: return 0;
76 case OVS_ACTION_ATTR_SET: return -2;
77 case OVS_ACTION_ATTR_SAMPLE: return -2;
78
79 case OVS_ACTION_ATTR_UNSPEC:
80 case __OVS_ACTION_ATTR_MAX:
81 return -1;
82 }
83
84 return -1;
85 }
86
87 static const char *
88 ovs_key_attr_to_string(enum ovs_key_attr attr)
89 {
90 static char unknown_attr[3 + INT_STRLEN(unsigned int) + 1];
91
92 switch (attr) {
93 case OVS_KEY_ATTR_UNSPEC: return "unspec";
94 case OVS_KEY_ATTR_ENCAP: return "encap";
95 case OVS_KEY_ATTR_PRIORITY: return "priority";
96 case OVS_KEY_ATTR_TUN_ID: return "tun_id";
97 case OVS_KEY_ATTR_IPV4_TUNNEL: return "ipv4_tunnel";
98 case OVS_KEY_ATTR_IN_PORT: return "in_port";
99 case OVS_KEY_ATTR_ETHERNET: return "eth";
100 case OVS_KEY_ATTR_VLAN: return "vlan";
101 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
102 case OVS_KEY_ATTR_IPV4: return "ipv4";
103 case OVS_KEY_ATTR_IPV6: return "ipv6";
104 case OVS_KEY_ATTR_TCP: return "tcp";
105 case OVS_KEY_ATTR_UDP: return "udp";
106 case OVS_KEY_ATTR_ICMP: return "icmp";
107 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
108 case OVS_KEY_ATTR_ARP: return "arp";
109 case OVS_KEY_ATTR_ND: return "nd";
110
111 case __OVS_KEY_ATTR_MAX:
112 default:
113 snprintf(unknown_attr, sizeof unknown_attr, "key%u",
114 (unsigned int) attr);
115 return unknown_attr;
116 }
117 }
118
119 static void
120 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
121 {
122 size_t len = nl_attr_get_size(a);
123
124 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
125 if (len) {
126 const uint8_t *unspec;
127 unsigned int i;
128
129 unspec = nl_attr_get(a);
130 for (i = 0; i < len; i++) {
131 ds_put_char(ds, i ? ' ': '(');
132 ds_put_format(ds, "%02x", unspec[i]);
133 }
134 ds_put_char(ds, ')');
135 }
136 }
137
138 static void
139 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
140 {
141 static const struct nl_policy ovs_sample_policy[] = {
142 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
143 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
144 };
145 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
146 double percentage;
147 const struct nlattr *nla_acts;
148 int len;
149
150 ds_put_cstr(ds, "sample");
151
152 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
153 ds_put_cstr(ds, "(error)");
154 return;
155 }
156
157 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
158 UINT32_MAX;
159
160 ds_put_format(ds, "(sample=%.1f%%,", percentage);
161
162 ds_put_cstr(ds, "actions(");
163 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
164 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
165 format_odp_actions(ds, nla_acts, len);
166 ds_put_format(ds, "))");
167 }
168
169 static const char *
170 slow_path_reason_to_string(uint32_t data)
171 {
172 enum slow_path_reason bit = (enum slow_path_reason) data;
173
174 switch (bit) {
175 case SLOW_CFM:
176 return "cfm";
177 case SLOW_LACP:
178 return "lacp";
179 case SLOW_STP:
180 return "stp";
181 case SLOW_IN_BAND:
182 return "in_band";
183 case SLOW_CONTROLLER:
184 return "controller";
185 case SLOW_MATCH:
186 return "match";
187 default:
188 return NULL;
189 }
190 }
191
192 static void
193 format_flags(struct ds *ds, const char *(*bit_to_string)(uint32_t),
194 uint32_t flags)
195 {
196 uint32_t bad = 0;
197
198 ds_put_format(ds, "(");
199 if (!flags) {
200 goto out;
201 }
202 while (flags) {
203 uint32_t bit = rightmost_1bit(flags);
204 const char *s;
205
206 s = bit_to_string(bit);
207 if (s) {
208 ds_put_format(ds, "%s,", s);
209 } else {
210 bad |= bit;
211 }
212
213 flags &= ~bit;
214 }
215
216 if (bad) {
217 ds_put_format(ds, "0x%"PRIx32",", bad);
218 }
219 ds_chomp(ds, ',');
220 out:
221 ds_put_format(ds, ")");
222 }
223
224 static int
225 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
226 uint32_t *res)
227 {
228 uint32_t result = 0;
229 int n = 0;
230
231 if (s[n] != '(') {
232 return -EINVAL;
233 }
234 n++;
235
236 while (s[n] != ')') {
237 unsigned long long int flags;
238 uint32_t bit;
239 int n0;
240
241 if (sscanf(&s[n], "%lli%n", &flags, &n0) > 0 && n0 > 0) {
242 n += n0 + (s[n + n0] == ',');
243 result |= flags;
244 continue;
245 }
246
247 for (bit = 1; bit; bit <<= 1) {
248 const char *name = bit_to_string(bit);
249 size_t len;
250
251 if (!name) {
252 continue;
253 }
254
255 len = strlen(name);
256 if (!strncmp(s + n, name, len) &&
257 (s[n + len] == ',' || s[n + len] == ')')) {
258 result |= bit;
259 n += len + (s[n + len] == ',');
260 break;
261 }
262 }
263
264 if (!bit) {
265 return -EINVAL;
266 }
267 }
268 n++;
269
270 *res = result;
271 return n;
272 }
273
274 static void
275 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
276 {
277 static const struct nl_policy ovs_userspace_policy[] = {
278 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
279 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
280 };
281 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
282
283 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
284 ds_put_cstr(ds, "userspace(error)");
285 return;
286 }
287
288 ds_put_format(ds, "userspace(pid=%"PRIu32,
289 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
290
291 if (a[OVS_USERSPACE_ATTR_USERDATA]) {
292 uint64_t userdata = nl_attr_get_u64(a[OVS_USERSPACE_ATTR_USERDATA]);
293 union user_action_cookie cookie;
294
295 memcpy(&cookie, &userdata, sizeof cookie);
296
297 switch (cookie.type) {
298 case USER_ACTION_COOKIE_SFLOW:
299 ds_put_format(ds, ",sFlow("
300 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
301 vlan_tci_to_vid(cookie.sflow.vlan_tci),
302 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
303 cookie.sflow.output);
304 break;
305
306 case USER_ACTION_COOKIE_SLOW_PATH:
307 ds_put_cstr(ds, ",slow_path");
308 format_flags(ds, slow_path_reason_to_string, cookie.slow_path.reason);
309 break;
310
311 case USER_ACTION_COOKIE_UNSPEC:
312 default:
313 ds_put_format(ds, ",userdata=0x%"PRIx64, userdata);
314 break;
315 }
316 }
317
318 ds_put_char(ds, ')');
319 }
320
321 static void
322 format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
323 {
324 ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
325 vlan_tci_to_vid(vlan_tci),
326 vlan_tci_to_pcp(vlan_tci));
327 if (!(vlan_tci & htons(VLAN_CFI))) {
328 ds_put_cstr(ds, ",cfi=0");
329 }
330 }
331
332 static void
333 format_odp_action(struct ds *ds, const struct nlattr *a)
334 {
335 int expected_len;
336 enum ovs_action_attr type = nl_attr_type(a);
337 const struct ovs_action_push_vlan *vlan;
338
339 expected_len = odp_action_len(nl_attr_type(a));
340 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
341 ds_put_format(ds, "bad length %zu, expected %d for: ",
342 nl_attr_get_size(a), expected_len);
343 format_generic_odp_action(ds, a);
344 return;
345 }
346
347 switch (type) {
348 case OVS_ACTION_ATTR_OUTPUT:
349 ds_put_format(ds, "%"PRIu16, nl_attr_get_u32(a));
350 break;
351 case OVS_ACTION_ATTR_USERSPACE:
352 format_odp_userspace_action(ds, a);
353 break;
354 case OVS_ACTION_ATTR_SET:
355 ds_put_cstr(ds, "set(");
356 format_odp_key_attr(nl_attr_get(a), ds);
357 ds_put_cstr(ds, ")");
358 break;
359 case OVS_ACTION_ATTR_PUSH_VLAN:
360 vlan = nl_attr_get(a);
361 ds_put_cstr(ds, "push_vlan(");
362 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
363 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
364 }
365 format_vlan_tci(ds, vlan->vlan_tci);
366 ds_put_char(ds, ')');
367 break;
368 case OVS_ACTION_ATTR_POP_VLAN:
369 ds_put_cstr(ds, "pop_vlan");
370 break;
371 case OVS_ACTION_ATTR_SAMPLE:
372 format_odp_sample_action(ds, a);
373 break;
374 case OVS_ACTION_ATTR_UNSPEC:
375 case __OVS_ACTION_ATTR_MAX:
376 default:
377 format_generic_odp_action(ds, a);
378 break;
379 }
380 }
381
382 void
383 format_odp_actions(struct ds *ds, const struct nlattr *actions,
384 size_t actions_len)
385 {
386 if (actions_len) {
387 const struct nlattr *a;
388 unsigned int left;
389
390 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
391 if (a != actions) {
392 ds_put_char(ds, ',');
393 }
394 format_odp_action(ds, a);
395 }
396 if (left) {
397 int i;
398
399 if (left == actions_len) {
400 ds_put_cstr(ds, "<empty>");
401 }
402 ds_put_format(ds, ",***%u leftover bytes*** (", left);
403 for (i = 0; i < left; i++) {
404 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
405 }
406 ds_put_char(ds, ')');
407 }
408 } else {
409 ds_put_cstr(ds, "drop");
410 }
411 }
412
413 static int
414 parse_odp_action(const char *s, const struct simap *port_names,
415 struct ofpbuf *actions)
416 {
417 /* Many of the sscanf calls in this function use oversized destination
418 * fields because some sscanf() implementations truncate the range of %i
419 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
420 * value of 0x7fff. The other alternatives are to allow only a single
421 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
422 * parsers.
423 *
424 * The tun_id parser has to use an alternative approach because there is no
425 * type larger than 64 bits. */
426
427 {
428 unsigned long long int port;
429 int n = -1;
430
431 if (sscanf(s, "%lli%n", &port, &n) > 0 && n > 0) {
432 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
433 return n;
434 }
435 }
436
437 if (port_names) {
438 int len = strcspn(s, delimiters);
439 struct simap_node *node;
440
441 node = simap_find_len(port_names, s, len);
442 if (node) {
443 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
444 return len;
445 }
446 }
447
448 {
449 unsigned long long int pid;
450 unsigned long long int output;
451 char userdata_s[32];
452 int vid, pcp;
453 int n = -1;
454
455 if (sscanf(s, "userspace(pid=%lli)%n", &pid, &n) > 0 && n > 0) {
456 odp_put_userspace_action(pid, NULL, actions);
457 return n;
458 } else if (sscanf(s, "userspace(pid=%lli,sFlow(vid=%i,"
459 "pcp=%i,output=%lli))%n",
460 &pid, &vid, &pcp, &output, &n) > 0 && n > 0) {
461 union user_action_cookie cookie;
462 uint16_t tci;
463
464 tci = vid | (pcp << VLAN_PCP_SHIFT);
465 if (tci) {
466 tci |= VLAN_CFI;
467 }
468
469 cookie.type = USER_ACTION_COOKIE_SFLOW;
470 cookie.sflow.vlan_tci = htons(tci);
471 cookie.sflow.output = output;
472 odp_put_userspace_action(pid, &cookie, actions);
473 return n;
474 } else if (sscanf(s, "userspace(pid=%lli,slow_path%n", &pid, &n) > 0
475 && n > 0) {
476 union user_action_cookie cookie;
477 int res;
478
479 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
480 cookie.slow_path.unused = 0;
481 cookie.slow_path.reason = 0;
482
483 res = parse_flags(&s[n], slow_path_reason_to_string,
484 &cookie.slow_path.reason);
485 if (res < 0) {
486 return res;
487 }
488 n += res;
489 if (s[n] != ')') {
490 return -EINVAL;
491 }
492 n++;
493
494 odp_put_userspace_action(pid, &cookie, actions);
495 return n;
496 } else if (sscanf(s, "userspace(pid=%lli,userdata="
497 "%31[x0123456789abcdefABCDEF])%n", &pid, userdata_s,
498 &n) > 0 && n > 0) {
499 union user_action_cookie cookie;
500 uint64_t userdata;
501
502 userdata = strtoull(userdata_s, NULL, 0);
503 memcpy(&cookie, &userdata, sizeof cookie);
504 odp_put_userspace_action(pid, &cookie, actions);
505 return n;
506 }
507 }
508
509 if (!strncmp(s, "set(", 4)) {
510 size_t start_ofs;
511 int retval;
512
513 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
514 retval = parse_odp_key_attr(s + 4, port_names, actions);
515 if (retval < 0) {
516 return retval;
517 }
518 if (s[retval + 4] != ')') {
519 return -EINVAL;
520 }
521 nl_msg_end_nested(actions, start_ofs);
522 return retval + 5;
523 }
524
525 {
526 struct ovs_action_push_vlan push;
527 int tpid = ETH_TYPE_VLAN;
528 int vid, pcp;
529 int cfi = 1;
530 int n = -1;
531
532 if ((sscanf(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) > 0
533 && n > 0)
534 || (sscanf(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
535 &vid, &pcp, &cfi, &n) > 0 && n > 0)
536 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
537 &tpid, &vid, &pcp, &n) > 0 && n > 0)
538 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
539 &tpid, &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
540 push.vlan_tpid = htons(tpid);
541 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
542 | (pcp << VLAN_PCP_SHIFT)
543 | (cfi ? VLAN_CFI : 0));
544 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
545 &push, sizeof push);
546
547 return n;
548 }
549 }
550
551 if (!strncmp(s, "pop_vlan", 8)) {
552 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
553 return 8;
554 }
555
556 {
557 double percentage;
558 int n = -1;
559
560 if (sscanf(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) > 0
561 && percentage >= 0. && percentage <= 100.0
562 && n > 0) {
563 size_t sample_ofs, actions_ofs;
564 double probability;
565
566 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
567 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
568 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
569 (probability <= 0 ? 0
570 : probability >= UINT32_MAX ? UINT32_MAX
571 : probability));
572
573 actions_ofs = nl_msg_start_nested(actions,
574 OVS_SAMPLE_ATTR_ACTIONS);
575 for (;;) {
576 int retval;
577
578 n += strspn(s + n, delimiters);
579 if (s[n] == ')') {
580 break;
581 }
582
583 retval = parse_odp_action(s + n, port_names, actions);
584 if (retval < 0) {
585 return retval;
586 }
587 n += retval;
588 }
589 nl_msg_end_nested(actions, actions_ofs);
590 nl_msg_end_nested(actions, sample_ofs);
591
592 return s[n + 1] == ')' ? n + 2 : -EINVAL;
593 }
594 }
595
596 return -EINVAL;
597 }
598
599 /* Parses the string representation of datapath actions, in the format output
600 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
601 * value. On success, the ODP actions are appended to 'actions' as a series of
602 * Netlink attributes. On failure, no data is appended to 'actions'. Either
603 * way, 'actions''s data might be reallocated. */
604 int
605 odp_actions_from_string(const char *s, const struct simap *port_names,
606 struct ofpbuf *actions)
607 {
608 size_t old_size;
609
610 if (!strcasecmp(s, "drop")) {
611 return 0;
612 }
613
614 old_size = actions->size;
615 for (;;) {
616 int retval;
617
618 s += strspn(s, delimiters);
619 if (!*s) {
620 return 0;
621 }
622
623 retval = parse_odp_action(s, port_names, actions);
624 if (retval < 0 || !strchr(delimiters, s[retval])) {
625 actions->size = old_size;
626 return -retval;
627 }
628 s += retval;
629 }
630
631 return 0;
632 }
633 \f
634 /* Returns the correct length of the payload for a flow key attribute of the
635 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
636 * is variable length. */
637 static int
638 odp_flow_key_attr_len(uint16_t type)
639 {
640 if (type > OVS_KEY_ATTR_MAX) {
641 return -1;
642 }
643
644 switch ((enum ovs_key_attr) type) {
645 case OVS_KEY_ATTR_ENCAP: return -2;
646 case OVS_KEY_ATTR_PRIORITY: return 4;
647 case OVS_KEY_ATTR_TUN_ID: return 8;
648 case OVS_KEY_ATTR_IPV4_TUNNEL: return sizeof(struct ovs_key_ipv4_tunnel);
649 case OVS_KEY_ATTR_IN_PORT: return 4;
650 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
651 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
652 case OVS_KEY_ATTR_ETHERTYPE: return 2;
653 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
654 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
655 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
656 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
657 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
658 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
659 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
660 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
661
662 case OVS_KEY_ATTR_UNSPEC:
663 case __OVS_KEY_ATTR_MAX:
664 return -1;
665 }
666
667 return -1;
668 }
669
670 static void
671 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
672 {
673 size_t len = nl_attr_get_size(a);
674 if (len) {
675 const uint8_t *unspec;
676 unsigned int i;
677
678 unspec = nl_attr_get(a);
679 for (i = 0; i < len; i++) {
680 ds_put_char(ds, i ? ' ': '(');
681 ds_put_format(ds, "%02x", unspec[i]);
682 }
683 ds_put_char(ds, ')');
684 }
685 }
686
687 static const char *
688 ovs_frag_type_to_string(enum ovs_frag_type type)
689 {
690 switch (type) {
691 case OVS_FRAG_TYPE_NONE:
692 return "no";
693 case OVS_FRAG_TYPE_FIRST:
694 return "first";
695 case OVS_FRAG_TYPE_LATER:
696 return "later";
697 case __OVS_FRAG_TYPE_MAX:
698 default:
699 return "<error>";
700 }
701 }
702
703 static void
704 format_odp_key_attr(const struct nlattr *a, struct ds *ds)
705 {
706 const struct ovs_key_ethernet *eth_key;
707 const struct ovs_key_ipv4 *ipv4_key;
708 const struct ovs_key_ipv6 *ipv6_key;
709 const struct ovs_key_tcp *tcp_key;
710 const struct ovs_key_udp *udp_key;
711 const struct ovs_key_icmp *icmp_key;
712 const struct ovs_key_icmpv6 *icmpv6_key;
713 const struct ovs_key_arp *arp_key;
714 const struct ovs_key_nd *nd_key;
715 const struct ovs_key_ipv4_tunnel *ipv4_tun_key;
716 enum ovs_key_attr attr = nl_attr_type(a);
717 int expected_len;
718
719 ds_put_cstr(ds, ovs_key_attr_to_string(attr));
720 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
721 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
722 ds_put_format(ds, "(bad length %zu, expected %d)",
723 nl_attr_get_size(a),
724 odp_flow_key_attr_len(nl_attr_type(a)));
725 format_generic_odp_key(a, ds);
726 return;
727 }
728
729 switch (attr) {
730 case OVS_KEY_ATTR_ENCAP:
731 ds_put_cstr(ds, "(");
732 if (nl_attr_get_size(a)) {
733 odp_flow_key_format(nl_attr_get(a), nl_attr_get_size(a), ds);
734 }
735 ds_put_char(ds, ')');
736 break;
737
738 case OVS_KEY_ATTR_PRIORITY:
739 ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
740 break;
741
742 case OVS_KEY_ATTR_TUN_ID:
743 ds_put_format(ds, "(%#"PRIx64")", ntohll(nl_attr_get_be64(a)));
744 break;
745
746 case OVS_KEY_ATTR_IPV4_TUNNEL:
747 ipv4_tun_key = nl_attr_get(a);
748 ds_put_format(ds, "(tun_id=0x%"PRIx64",flags=0x%"PRIx32
749 ",src="IP_FMT",dst="IP_FMT",tos=0x%"PRIx8",ttl=%"PRIu8")",
750 ntohll(ipv4_tun_key->tun_id), ipv4_tun_key->tun_flags,
751 IP_ARGS(&ipv4_tun_key->ipv4_src),
752 IP_ARGS(&ipv4_tun_key->ipv4_dst),
753 ipv4_tun_key->ipv4_tos, ipv4_tun_key->ipv4_ttl);
754 break;
755
756 case OVS_KEY_ATTR_IN_PORT:
757 ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
758 break;
759
760 case OVS_KEY_ATTR_ETHERNET:
761 eth_key = nl_attr_get(a);
762 ds_put_format(ds, "(src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT")",
763 ETH_ADDR_ARGS(eth_key->eth_src),
764 ETH_ADDR_ARGS(eth_key->eth_dst));
765 break;
766
767 case OVS_KEY_ATTR_VLAN:
768 ds_put_char(ds, '(');
769 format_vlan_tci(ds, nl_attr_get_be16(a));
770 ds_put_char(ds, ')');
771 break;
772
773 case OVS_KEY_ATTR_ETHERTYPE:
774 ds_put_format(ds, "(0x%04"PRIx16")",
775 ntohs(nl_attr_get_be16(a)));
776 break;
777
778 case OVS_KEY_ATTR_IPV4:
779 ipv4_key = nl_attr_get(a);
780 ds_put_format(ds, "(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
781 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s)",
782 IP_ARGS(&ipv4_key->ipv4_src),
783 IP_ARGS(&ipv4_key->ipv4_dst),
784 ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
785 ipv4_key->ipv4_ttl,
786 ovs_frag_type_to_string(ipv4_key->ipv4_frag));
787 break;
788
789 case OVS_KEY_ATTR_IPV6: {
790 char src_str[INET6_ADDRSTRLEN];
791 char dst_str[INET6_ADDRSTRLEN];
792
793 ipv6_key = nl_attr_get(a);
794 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
795 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
796
797 ds_put_format(ds, "(src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
798 ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s)",
799 src_str, dst_str, ntohl(ipv6_key->ipv6_label),
800 ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
801 ipv6_key->ipv6_hlimit,
802 ovs_frag_type_to_string(ipv6_key->ipv6_frag));
803 break;
804 }
805
806 case OVS_KEY_ATTR_TCP:
807 tcp_key = nl_attr_get(a);
808 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
809 ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
810 break;
811
812 case OVS_KEY_ATTR_UDP:
813 udp_key = nl_attr_get(a);
814 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
815 ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
816 break;
817
818 case OVS_KEY_ATTR_ICMP:
819 icmp_key = nl_attr_get(a);
820 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
821 icmp_key->icmp_type, icmp_key->icmp_code);
822 break;
823
824 case OVS_KEY_ATTR_ICMPV6:
825 icmpv6_key = nl_attr_get(a);
826 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
827 icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
828 break;
829
830 case OVS_KEY_ATTR_ARP:
831 arp_key = nl_attr_get(a);
832 ds_put_format(ds, "(sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
833 "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT")",
834 IP_ARGS(&arp_key->arp_sip), IP_ARGS(&arp_key->arp_tip),
835 ntohs(arp_key->arp_op), ETH_ADDR_ARGS(arp_key->arp_sha),
836 ETH_ADDR_ARGS(arp_key->arp_tha));
837 break;
838
839 case OVS_KEY_ATTR_ND: {
840 char target[INET6_ADDRSTRLEN];
841
842 nd_key = nl_attr_get(a);
843 inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
844
845 ds_put_format(ds, "(target=%s", target);
846 if (!eth_addr_is_zero(nd_key->nd_sll)) {
847 ds_put_format(ds, ",sll="ETH_ADDR_FMT,
848 ETH_ADDR_ARGS(nd_key->nd_sll));
849 }
850 if (!eth_addr_is_zero(nd_key->nd_tll)) {
851 ds_put_format(ds, ",tll="ETH_ADDR_FMT,
852 ETH_ADDR_ARGS(nd_key->nd_tll));
853 }
854 ds_put_char(ds, ')');
855 break;
856 }
857
858 case OVS_KEY_ATTR_UNSPEC:
859 case __OVS_KEY_ATTR_MAX:
860 default:
861 format_generic_odp_key(a, ds);
862 break;
863 }
864 }
865
866 /* Appends to 'ds' a string representation of the 'key_len' bytes of
867 * OVS_KEY_ATTR_* attributes in 'key'. */
868 void
869 odp_flow_key_format(const struct nlattr *key, size_t key_len, struct ds *ds)
870 {
871 if (key_len) {
872 const struct nlattr *a;
873 unsigned int left;
874
875 NL_ATTR_FOR_EACH (a, left, key, key_len) {
876 if (a != key) {
877 ds_put_char(ds, ',');
878 }
879 format_odp_key_attr(a, ds);
880 }
881 if (left) {
882 int i;
883
884 if (left == key_len) {
885 ds_put_cstr(ds, "<empty>");
886 }
887 ds_put_format(ds, ",***%u leftover bytes*** (", left);
888 for (i = 0; i < left; i++) {
889 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
890 }
891 ds_put_char(ds, ')');
892 }
893 } else {
894 ds_put_cstr(ds, "<empty>");
895 }
896 }
897
898 static int
899 put_nd_key(int n, const char *nd_target_s,
900 const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *key)
901 {
902 struct ovs_key_nd nd_key;
903
904 memset(&nd_key, 0, sizeof nd_key);
905 if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
906 return -EINVAL;
907 }
908 if (nd_sll) {
909 memcpy(nd_key.nd_sll, nd_sll, ETH_ADDR_LEN);
910 }
911 if (nd_tll) {
912 memcpy(nd_key.nd_tll, nd_tll, ETH_ADDR_LEN);
913 }
914 nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, &nd_key, sizeof nd_key);
915 return n;
916 }
917
918 static bool
919 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
920 {
921 if (!strcasecmp(s, "no")) {
922 *type = OVS_FRAG_TYPE_NONE;
923 } else if (!strcasecmp(s, "first")) {
924 *type = OVS_FRAG_TYPE_FIRST;
925 } else if (!strcasecmp(s, "later")) {
926 *type = OVS_FRAG_TYPE_LATER;
927 } else {
928 return false;
929 }
930 return true;
931 }
932
933 static int
934 parse_odp_key_attr(const char *s, const struct simap *port_names,
935 struct ofpbuf *key)
936 {
937 /* Many of the sscanf calls in this function use oversized destination
938 * fields because some sscanf() implementations truncate the range of %i
939 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
940 * value of 0x7fff. The other alternatives are to allow only a single
941 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
942 * parsers.
943 *
944 * The tun_id parser has to use an alternative approach because there is no
945 * type larger than 64 bits. */
946
947 {
948 unsigned long long int priority;
949 int n = -1;
950
951 if (sscanf(s, "priority(%lli)%n", &priority, &n) > 0 && n > 0) {
952 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
953 return n;
954 }
955 }
956
957 {
958 char tun_id_s[32];
959 int n = -1;
960
961 if (sscanf(s, "tun_id(%31[x0123456789abcdefABCDEF])%n",
962 tun_id_s, &n) > 0 && n > 0) {
963 uint64_t tun_id = strtoull(tun_id_s, NULL, 0);
964 nl_msg_put_be64(key, OVS_KEY_ATTR_TUN_ID, htonll(tun_id));
965 return n;
966 }
967 }
968
969 {
970 char tun_id_s[32];
971 unsigned long long int flags;
972 int tos, ttl;
973 struct ovs_key_ipv4_tunnel tun_key;
974 int n = -1;
975
976 if (sscanf(s, "ipv4_tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
977 "flags=%lli,src="IP_SCAN_FMT",dst="IP_SCAN_FMT
978 ",tos=%i,ttl=%i)%n", tun_id_s, &flags,
979 IP_SCAN_ARGS(&tun_key.ipv4_src),
980 IP_SCAN_ARGS(&tun_key.ipv4_dst), &tos, &ttl,
981 &n) > 0 && n > 0) {
982 tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
983 tun_key.tun_flags = flags;
984 tun_key.ipv4_tos = tos;
985 tun_key.ipv4_ttl = ttl;
986 memset(&tun_key.pad, 0, sizeof tun_key.pad);
987 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4_TUNNEL, &tun_key,
988 sizeof tun_key);
989 return n;
990 }
991 }
992
993 {
994 unsigned long long int in_port;
995 int n = -1;
996
997 if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
998 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
999 return n;
1000 }
1001 }
1002
1003 if (port_names && !strncmp(s, "in_port(", 8)) {
1004 const char *name;
1005 const struct simap_node *node;
1006 int name_len;
1007
1008 name = s + 8;
1009 name_len = strcspn(s, ")");
1010 node = simap_find_len(port_names, name, name_len);
1011 if (node) {
1012 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, node->data);
1013 return 8 + name_len + 1;
1014 }
1015 }
1016
1017 {
1018 struct ovs_key_ethernet eth_key;
1019 int n = -1;
1020
1021 if (sscanf(s,
1022 "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
1023 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1024 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
1025 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1026 &eth_key, sizeof eth_key);
1027 return n;
1028 }
1029 }
1030
1031 {
1032 uint16_t vid;
1033 int pcp;
1034 int cfi;
1035 int n = -1;
1036
1037 if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n", &vid, &pcp, &n) > 0
1038 && n > 0)) {
1039 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1040 htons((vid << VLAN_VID_SHIFT) |
1041 (pcp << VLAN_PCP_SHIFT) |
1042 VLAN_CFI));
1043 return n;
1044 } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n",
1045 &vid, &pcp, &cfi, &n) > 0
1046 && n > 0)) {
1047 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1048 htons((vid << VLAN_VID_SHIFT) |
1049 (pcp << VLAN_PCP_SHIFT) |
1050 (cfi ? VLAN_CFI : 0)));
1051 return n;
1052 }
1053 }
1054
1055 {
1056 int eth_type;
1057 int n = -1;
1058
1059 if (sscanf(s, "eth_type(%i)%n", &eth_type, &n) > 0 && n > 0) {
1060 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1061 return n;
1062 }
1063 }
1064
1065 {
1066 ovs_be32 ipv4_src;
1067 ovs_be32 ipv4_dst;
1068 int ipv4_proto;
1069 int ipv4_tos;
1070 int ipv4_ttl;
1071 char frag[8];
1072 enum ovs_frag_type ipv4_frag;
1073 int n = -1;
1074
1075 if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
1076 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1077 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
1078 &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
1079 && n > 0
1080 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1081 struct ovs_key_ipv4 ipv4_key;
1082
1083 ipv4_key.ipv4_src = ipv4_src;
1084 ipv4_key.ipv4_dst = ipv4_dst;
1085 ipv4_key.ipv4_proto = ipv4_proto;
1086 ipv4_key.ipv4_tos = ipv4_tos;
1087 ipv4_key.ipv4_ttl = ipv4_ttl;
1088 ipv4_key.ipv4_frag = ipv4_frag;
1089 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1090 &ipv4_key, sizeof ipv4_key);
1091 return n;
1092 }
1093 }
1094
1095 {
1096 char ipv6_src_s[IPV6_SCAN_LEN + 1];
1097 char ipv6_dst_s[IPV6_SCAN_LEN + 1];
1098 int ipv6_label;
1099 int ipv6_proto;
1100 int ipv6_tclass;
1101 int ipv6_hlimit;
1102 char frag[8];
1103 enum ovs_frag_type ipv6_frag;
1104 int n = -1;
1105
1106 if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
1107 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1108 ipv6_src_s, ipv6_dst_s, &ipv6_label,
1109 &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
1110 && n > 0
1111 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1112 struct ovs_key_ipv6 ipv6_key;
1113
1114 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1115 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
1116 return -EINVAL;
1117 }
1118 ipv6_key.ipv6_label = htonl(ipv6_label);
1119 ipv6_key.ipv6_proto = ipv6_proto;
1120 ipv6_key.ipv6_tclass = ipv6_tclass;
1121 ipv6_key.ipv6_hlimit = ipv6_hlimit;
1122 ipv6_key.ipv6_frag = ipv6_frag;
1123 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1124 &ipv6_key, sizeof ipv6_key);
1125 return n;
1126 }
1127 }
1128
1129 {
1130 int tcp_src;
1131 int tcp_dst;
1132 int n = -1;
1133
1134 if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
1135 && n > 0) {
1136 struct ovs_key_tcp tcp_key;
1137
1138 tcp_key.tcp_src = htons(tcp_src);
1139 tcp_key.tcp_dst = htons(tcp_dst);
1140 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
1141 return n;
1142 }
1143 }
1144
1145 {
1146 int udp_src;
1147 int udp_dst;
1148 int n = -1;
1149
1150 if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
1151 && n > 0) {
1152 struct ovs_key_udp udp_key;
1153
1154 udp_key.udp_src = htons(udp_src);
1155 udp_key.udp_dst = htons(udp_dst);
1156 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
1157 return n;
1158 }
1159 }
1160
1161 {
1162 int icmp_type;
1163 int icmp_code;
1164 int n = -1;
1165
1166 if (sscanf(s, "icmp(type=%i,code=%i)%n",
1167 &icmp_type, &icmp_code, &n) > 0
1168 && n > 0) {
1169 struct ovs_key_icmp icmp_key;
1170
1171 icmp_key.icmp_type = icmp_type;
1172 icmp_key.icmp_code = icmp_code;
1173 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
1174 &icmp_key, sizeof icmp_key);
1175 return n;
1176 }
1177 }
1178
1179 {
1180 struct ovs_key_icmpv6 icmpv6_key;
1181 int n = -1;
1182
1183 if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
1184 &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
1185 && n > 0) {
1186 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
1187 &icmpv6_key, sizeof icmpv6_key);
1188 return n;
1189 }
1190 }
1191
1192 {
1193 ovs_be32 arp_sip;
1194 ovs_be32 arp_tip;
1195 int arp_op;
1196 uint8_t arp_sha[ETH_ADDR_LEN];
1197 uint8_t arp_tha[ETH_ADDR_LEN];
1198 int n = -1;
1199
1200 if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
1201 "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
1202 IP_SCAN_ARGS(&arp_sip),
1203 IP_SCAN_ARGS(&arp_tip),
1204 &arp_op,
1205 ETH_ADDR_SCAN_ARGS(arp_sha),
1206 ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
1207 struct ovs_key_arp arp_key;
1208
1209 memset(&arp_key, 0, sizeof arp_key);
1210 arp_key.arp_sip = arp_sip;
1211 arp_key.arp_tip = arp_tip;
1212 arp_key.arp_op = htons(arp_op);
1213 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
1214 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
1215 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
1216 return n;
1217 }
1218 }
1219
1220 {
1221 char nd_target_s[IPV6_SCAN_LEN + 1];
1222 uint8_t nd_sll[ETH_ADDR_LEN];
1223 uint8_t nd_tll[ETH_ADDR_LEN];
1224 int n = -1;
1225
1226 if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
1227 nd_target_s, &n) > 0 && n > 0) {
1228 return put_nd_key(n, nd_target_s, NULL, NULL, key);
1229 }
1230 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
1231 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
1232 && n > 0) {
1233 return put_nd_key(n, nd_target_s, nd_sll, NULL, key);
1234 }
1235 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
1236 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
1237 && n > 0) {
1238 return put_nd_key(n, nd_target_s, NULL, nd_tll, key);
1239 }
1240 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
1241 "tll="ETH_ADDR_SCAN_FMT")%n",
1242 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
1243 ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
1244 && n > 0) {
1245 return put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
1246 }
1247 }
1248
1249 if (!strncmp(s, "encap(", 6)) {
1250 const char *start = s;
1251 size_t encap;
1252
1253 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
1254
1255 s += 6;
1256 for (;;) {
1257 int retval;
1258
1259 s += strspn(s, ", \t\r\n");
1260 if (!*s) {
1261 return -EINVAL;
1262 } else if (*s == ')') {
1263 break;
1264 }
1265
1266 retval = parse_odp_key_attr(s, port_names, key);
1267 if (retval < 0) {
1268 return retval;
1269 }
1270 s += retval;
1271 }
1272 s++;
1273
1274 nl_msg_end_nested(key, encap);
1275
1276 return s - start;
1277 }
1278
1279 return -EINVAL;
1280 }
1281
1282 /* Parses the string representation of a datapath flow key, in the
1283 * format output by odp_flow_key_format(). Returns 0 if successful,
1284 * otherwise a positive errno value. On success, the flow key is
1285 * appended to 'key' as a series of Netlink attributes. On failure, no
1286 * data is appended to 'key'. Either way, 'key''s data might be
1287 * reallocated.
1288 *
1289 * If 'port_names' is nonnull, it points to an simap that maps from a port name
1290 * to a port number. (Port names may be used instead of port numbers in
1291 * in_port.)
1292 *
1293 * On success, the attributes appended to 'key' are individually syntactically
1294 * valid, but they may not be valid as a sequence. 'key' might, for example,
1295 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
1296 int
1297 odp_flow_key_from_string(const char *s, const struct simap *port_names,
1298 struct ofpbuf *key)
1299 {
1300 const size_t old_size = key->size;
1301 for (;;) {
1302 int retval;
1303
1304 s += strspn(s, delimiters);
1305 if (!*s) {
1306 return 0;
1307 }
1308
1309 retval = parse_odp_key_attr(s, port_names, key);
1310 if (retval < 0) {
1311 key->size = old_size;
1312 return -retval;
1313 }
1314 s += retval;
1315 }
1316
1317 return 0;
1318 }
1319
1320 static uint8_t
1321 ovs_to_odp_frag(uint8_t nw_frag)
1322 {
1323 return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
1324 : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
1325 : OVS_FRAG_TYPE_LATER);
1326 }
1327
1328 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
1329 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
1330 * number rather than a datapath port number). Instead, if 'odp_in_port'
1331 * is anything other than OVSP_NONE, it is included in 'buf' as the input
1332 * port.
1333 *
1334 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
1335 * capable of being expanded to allow for that much space. */
1336 void
1337 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
1338 uint32_t odp_in_port)
1339 {
1340 struct ovs_key_ethernet *eth_key;
1341 size_t encap;
1342
1343 if (flow->skb_priority) {
1344 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, flow->skb_priority);
1345 }
1346
1347 if (flow->tunnel.tun_id != htonll(0)) {
1348 nl_msg_put_be64(buf, OVS_KEY_ATTR_TUN_ID, flow->tunnel.tun_id);
1349 }
1350
1351 if (odp_in_port != OVSP_NONE) {
1352 nl_msg_put_u32(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
1353 }
1354
1355 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
1356 sizeof *eth_key);
1357 memcpy(eth_key->eth_src, flow->dl_src, ETH_ADDR_LEN);
1358 memcpy(eth_key->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
1359
1360 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
1361 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
1362 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, flow->vlan_tci);
1363 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
1364 if (flow->vlan_tci == htons(0)) {
1365 goto unencap;
1366 }
1367 } else {
1368 encap = 0;
1369 }
1370
1371 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
1372 goto unencap;
1373 }
1374
1375 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, flow->dl_type);
1376
1377 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1378 struct ovs_key_ipv4 *ipv4_key;
1379
1380 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
1381 sizeof *ipv4_key);
1382 ipv4_key->ipv4_src = flow->nw_src;
1383 ipv4_key->ipv4_dst = flow->nw_dst;
1384 ipv4_key->ipv4_proto = flow->nw_proto;
1385 ipv4_key->ipv4_tos = flow->nw_tos;
1386 ipv4_key->ipv4_ttl = flow->nw_ttl;
1387 ipv4_key->ipv4_frag = ovs_to_odp_frag(flow->nw_frag);
1388 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1389 struct ovs_key_ipv6 *ipv6_key;
1390
1391 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
1392 sizeof *ipv6_key);
1393 memcpy(ipv6_key->ipv6_src, &flow->ipv6_src, sizeof ipv6_key->ipv6_src);
1394 memcpy(ipv6_key->ipv6_dst, &flow->ipv6_dst, sizeof ipv6_key->ipv6_dst);
1395 ipv6_key->ipv6_label = flow->ipv6_label;
1396 ipv6_key->ipv6_proto = flow->nw_proto;
1397 ipv6_key->ipv6_tclass = flow->nw_tos;
1398 ipv6_key->ipv6_hlimit = flow->nw_ttl;
1399 ipv6_key->ipv6_frag = ovs_to_odp_frag(flow->nw_frag);
1400 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1401 flow->dl_type == htons(ETH_TYPE_RARP)) {
1402 struct ovs_key_arp *arp_key;
1403
1404 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
1405 sizeof *arp_key);
1406 memset(arp_key, 0, sizeof *arp_key);
1407 arp_key->arp_sip = flow->nw_src;
1408 arp_key->arp_tip = flow->nw_dst;
1409 arp_key->arp_op = htons(flow->nw_proto);
1410 memcpy(arp_key->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
1411 memcpy(arp_key->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
1412 }
1413
1414 if ((flow->dl_type == htons(ETH_TYPE_IP)
1415 || flow->dl_type == htons(ETH_TYPE_IPV6))
1416 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1417
1418 if (flow->nw_proto == IPPROTO_TCP) {
1419 struct ovs_key_tcp *tcp_key;
1420
1421 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
1422 sizeof *tcp_key);
1423 tcp_key->tcp_src = flow->tp_src;
1424 tcp_key->tcp_dst = flow->tp_dst;
1425 } else if (flow->nw_proto == IPPROTO_UDP) {
1426 struct ovs_key_udp *udp_key;
1427
1428 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
1429 sizeof *udp_key);
1430 udp_key->udp_src = flow->tp_src;
1431 udp_key->udp_dst = flow->tp_dst;
1432 } else if (flow->dl_type == htons(ETH_TYPE_IP)
1433 && flow->nw_proto == IPPROTO_ICMP) {
1434 struct ovs_key_icmp *icmp_key;
1435
1436 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
1437 sizeof *icmp_key);
1438 icmp_key->icmp_type = ntohs(flow->tp_src);
1439 icmp_key->icmp_code = ntohs(flow->tp_dst);
1440 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
1441 && flow->nw_proto == IPPROTO_ICMPV6) {
1442 struct ovs_key_icmpv6 *icmpv6_key;
1443
1444 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
1445 sizeof *icmpv6_key);
1446 icmpv6_key->icmpv6_type = ntohs(flow->tp_src);
1447 icmpv6_key->icmpv6_code = ntohs(flow->tp_dst);
1448
1449 if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
1450 || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
1451 struct ovs_key_nd *nd_key;
1452
1453 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
1454 sizeof *nd_key);
1455 memcpy(nd_key->nd_target, &flow->nd_target,
1456 sizeof nd_key->nd_target);
1457 memcpy(nd_key->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
1458 memcpy(nd_key->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
1459 }
1460 }
1461 }
1462
1463 unencap:
1464 if (encap) {
1465 nl_msg_end_nested(buf, encap);
1466 }
1467 }
1468
1469 uint32_t
1470 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
1471 {
1472 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
1473 return hash_words((const uint32_t *) key, key_len / sizeof(uint32_t), 0);
1474 }
1475
1476 static void
1477 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
1478 uint64_t attrs, int out_of_range_attr,
1479 const struct nlattr *key, size_t key_len)
1480 {
1481 struct ds s;
1482 int i;
1483
1484 if (VLOG_DROP_DBG(rl)) {
1485 return;
1486 }
1487
1488 ds_init(&s);
1489 for (i = 0; i < 64; i++) {
1490 if (attrs & (UINT64_C(1) << i)) {
1491 ds_put_format(&s, " %s", ovs_key_attr_to_string(i));
1492 }
1493 }
1494 if (out_of_range_attr) {
1495 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
1496 }
1497
1498 ds_put_cstr(&s, ": ");
1499 odp_flow_key_format(key, key_len, &s);
1500
1501 VLOG_DBG("%s:%s", title, ds_cstr(&s));
1502 ds_destroy(&s);
1503 }
1504
1505 static bool
1506 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
1507 {
1508 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1509
1510 if (odp_frag > OVS_FRAG_TYPE_LATER) {
1511 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
1512 return false;
1513 }
1514
1515 if (odp_frag != OVS_FRAG_TYPE_NONE) {
1516 flow->nw_frag |= FLOW_NW_FRAG_ANY;
1517 if (odp_frag == OVS_FRAG_TYPE_LATER) {
1518 flow->nw_frag |= FLOW_NW_FRAG_LATER;
1519 }
1520 }
1521 return true;
1522 }
1523
1524 static bool
1525 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
1526 const struct nlattr *attrs[], uint64_t *present_attrsp,
1527 int *out_of_range_attrp)
1528 {
1529 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1530 const struct nlattr *nla;
1531 uint64_t present_attrs;
1532 size_t left;
1533
1534 present_attrs = 0;
1535 *out_of_range_attrp = 0;
1536 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
1537 uint16_t type = nl_attr_type(nla);
1538 size_t len = nl_attr_get_size(nla);
1539 int expected_len = odp_flow_key_attr_len(type);
1540
1541 if (len != expected_len && expected_len >= 0) {
1542 VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
1543 "length %d", ovs_key_attr_to_string(type),
1544 len, expected_len);
1545 return false;
1546 }
1547
1548 if (type >= CHAR_BIT * sizeof present_attrs) {
1549 *out_of_range_attrp = type;
1550 } else {
1551 if (present_attrs & (UINT64_C(1) << type)) {
1552 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
1553 ovs_key_attr_to_string(type));
1554 return false;
1555 }
1556
1557 present_attrs |= UINT64_C(1) << type;
1558 attrs[type] = nla;
1559 }
1560 }
1561 if (left) {
1562 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
1563 return false;
1564 }
1565
1566 *present_attrsp = present_attrs;
1567 return true;
1568 }
1569
1570 static enum odp_key_fitness
1571 check_expectations(uint64_t present_attrs, int out_of_range_attr,
1572 uint64_t expected_attrs,
1573 const struct nlattr *key, size_t key_len)
1574 {
1575 uint64_t missing_attrs;
1576 uint64_t extra_attrs;
1577
1578 missing_attrs = expected_attrs & ~present_attrs;
1579 if (missing_attrs) {
1580 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1581 log_odp_key_attributes(&rl, "expected but not present",
1582 missing_attrs, 0, key, key_len);
1583 return ODP_FIT_TOO_LITTLE;
1584 }
1585
1586 extra_attrs = present_attrs & ~expected_attrs;
1587 if (extra_attrs || out_of_range_attr) {
1588 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1589 log_odp_key_attributes(&rl, "present but not expected",
1590 extra_attrs, out_of_range_attr, key, key_len);
1591 return ODP_FIT_TOO_MUCH;
1592 }
1593
1594 return ODP_FIT_PERFECT;
1595 }
1596
1597 static bool
1598 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1599 uint64_t present_attrs, uint64_t *expected_attrs,
1600 struct flow *flow)
1601 {
1602 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1603
1604 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
1605 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
1606 if (ntohs(flow->dl_type) < 1536) {
1607 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
1608 ntohs(flow->dl_type));
1609 return false;
1610 }
1611 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
1612 } else {
1613 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
1614 }
1615 return true;
1616 }
1617
1618 static enum odp_key_fitness
1619 parse_l3_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1620 uint64_t present_attrs, int out_of_range_attr,
1621 uint64_t expected_attrs, struct flow *flow,
1622 const struct nlattr *key, size_t key_len)
1623 {
1624 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1625
1626 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1627 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
1628 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
1629 const struct ovs_key_ipv4 *ipv4_key;
1630
1631 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
1632 flow->nw_src = ipv4_key->ipv4_src;
1633 flow->nw_dst = ipv4_key->ipv4_dst;
1634 flow->nw_proto = ipv4_key->ipv4_proto;
1635 flow->nw_tos = ipv4_key->ipv4_tos;
1636 flow->nw_ttl = ipv4_key->ipv4_ttl;
1637 if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
1638 return ODP_FIT_ERROR;
1639 }
1640 }
1641 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1642 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
1643 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
1644 const struct ovs_key_ipv6 *ipv6_key;
1645
1646 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
1647 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
1648 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
1649 flow->ipv6_label = ipv6_key->ipv6_label;
1650 flow->nw_proto = ipv6_key->ipv6_proto;
1651 flow->nw_tos = ipv6_key->ipv6_tclass;
1652 flow->nw_ttl = ipv6_key->ipv6_hlimit;
1653 if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
1654 return ODP_FIT_ERROR;
1655 }
1656 }
1657 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1658 flow->dl_type == htons(ETH_TYPE_RARP)) {
1659 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
1660 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
1661 const struct ovs_key_arp *arp_key;
1662
1663 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
1664 flow->nw_src = arp_key->arp_sip;
1665 flow->nw_dst = arp_key->arp_tip;
1666 if (arp_key->arp_op & htons(0xff00)) {
1667 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
1668 "key", ntohs(arp_key->arp_op));
1669 return ODP_FIT_ERROR;
1670 }
1671 flow->nw_proto = ntohs(arp_key->arp_op);
1672 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
1673 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
1674 }
1675 }
1676
1677 if (flow->nw_proto == IPPROTO_TCP
1678 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1679 flow->dl_type == htons(ETH_TYPE_IPV6))
1680 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1681 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
1682 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
1683 const struct ovs_key_tcp *tcp_key;
1684
1685 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
1686 flow->tp_src = tcp_key->tcp_src;
1687 flow->tp_dst = tcp_key->tcp_dst;
1688 }
1689 } else if (flow->nw_proto == IPPROTO_UDP
1690 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1691 flow->dl_type == htons(ETH_TYPE_IPV6))
1692 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1693 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
1694 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
1695 const struct ovs_key_udp *udp_key;
1696
1697 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
1698 flow->tp_src = udp_key->udp_src;
1699 flow->tp_dst = udp_key->udp_dst;
1700 }
1701 } else if (flow->nw_proto == IPPROTO_ICMP
1702 && flow->dl_type == htons(ETH_TYPE_IP)
1703 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1704 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
1705 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
1706 const struct ovs_key_icmp *icmp_key;
1707
1708 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
1709 flow->tp_src = htons(icmp_key->icmp_type);
1710 flow->tp_dst = htons(icmp_key->icmp_code);
1711 }
1712 } else if (flow->nw_proto == IPPROTO_ICMPV6
1713 && flow->dl_type == htons(ETH_TYPE_IPV6)
1714 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1715 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
1716 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
1717 const struct ovs_key_icmpv6 *icmpv6_key;
1718
1719 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
1720 flow->tp_src = htons(icmpv6_key->icmpv6_type);
1721 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
1722
1723 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1724 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
1725 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
1726 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
1727 const struct ovs_key_nd *nd_key;
1728
1729 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
1730 memcpy(&flow->nd_target, nd_key->nd_target,
1731 sizeof flow->nd_target);
1732 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
1733 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
1734 }
1735 }
1736 }
1737 }
1738
1739 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
1740 key, key_len);
1741 }
1742
1743 /* Parse 802.1Q header then encapsulated L3 attributes. */
1744 static enum odp_key_fitness
1745 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1746 uint64_t present_attrs, int out_of_range_attr,
1747 uint64_t expected_attrs, struct flow *flow,
1748 const struct nlattr *key, size_t key_len)
1749 {
1750 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1751
1752 const struct nlattr *encap
1753 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
1754 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
1755 enum odp_key_fitness encap_fitness;
1756 enum odp_key_fitness fitness;
1757 ovs_be16 tci;
1758
1759 /* Calulate fitness of outer attributes. */
1760 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
1761 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
1762 fitness = check_expectations(present_attrs, out_of_range_attr,
1763 expected_attrs, key, key_len);
1764
1765 /* Get the VLAN TCI value. */
1766 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
1767 return ODP_FIT_TOO_LITTLE;
1768 }
1769 tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
1770 if (tci == htons(0)) {
1771 /* Corner case for a truncated 802.1Q header. */
1772 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
1773 return ODP_FIT_TOO_MUCH;
1774 }
1775 return fitness;
1776 } else if (!(tci & htons(VLAN_CFI))) {
1777 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
1778 "but CFI bit is not set", ntohs(tci));
1779 return ODP_FIT_ERROR;
1780 }
1781
1782 /* Set vlan_tci.
1783 * Remove the TPID from dl_type since it's not the real Ethertype. */
1784 flow->vlan_tci = tci;
1785 flow->dl_type = htons(0);
1786
1787 /* Now parse the encapsulated attributes. */
1788 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
1789 attrs, &present_attrs, &out_of_range_attr)) {
1790 return ODP_FIT_ERROR;
1791 }
1792 expected_attrs = 0;
1793
1794 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
1795 return ODP_FIT_ERROR;
1796 }
1797 encap_fitness = parse_l3_onward(attrs, present_attrs, out_of_range_attr,
1798 expected_attrs, flow, key, key_len);
1799
1800 /* The overall fitness is the worse of the outer and inner attributes. */
1801 return MAX(fitness, encap_fitness);
1802 }
1803
1804 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
1805 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
1806 * 'key' fits our expectations for what a flow key should contain.
1807 *
1808 * The 'in_port' will be the datapath's understanding of the port. The
1809 * caller will need to translate with odp_port_to_ofp_port() if the
1810 * OpenFlow port is needed.
1811 *
1812 * This function doesn't take the packet itself as an argument because none of
1813 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
1814 * it is always possible to infer which additional attribute(s) should appear
1815 * by looking at the attributes for lower-level protocols, e.g. if the network
1816 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
1817 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
1818 * must be absent. */
1819 enum odp_key_fitness
1820 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
1821 struct flow *flow)
1822 {
1823 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
1824 uint64_t expected_attrs;
1825 uint64_t present_attrs;
1826 int out_of_range_attr;
1827
1828 memset(flow, 0, sizeof *flow);
1829
1830 /* Parse attributes. */
1831 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
1832 &out_of_range_attr)) {
1833 return ODP_FIT_ERROR;
1834 }
1835 expected_attrs = 0;
1836
1837 /* Metadata. */
1838 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
1839 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
1840 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
1841 }
1842
1843 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUN_ID)) {
1844 flow->tunnel.tun_id = nl_attr_get_be64(attrs[OVS_KEY_ATTR_TUN_ID]);
1845 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUN_ID;
1846 }
1847
1848 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
1849 flow->in_port = nl_attr_get_u32(attrs[OVS_KEY_ATTR_IN_PORT]);
1850 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
1851 } else {
1852 flow->in_port = OVSP_NONE;
1853 }
1854
1855 /* Ethernet header. */
1856 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
1857 const struct ovs_key_ethernet *eth_key;
1858
1859 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
1860 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
1861 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
1862 }
1863 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
1864
1865 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
1866 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
1867 return ODP_FIT_ERROR;
1868 }
1869
1870 if (flow->dl_type == htons(ETH_TYPE_VLAN)) {
1871 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
1872 expected_attrs, flow, key, key_len);
1873 }
1874 return parse_l3_onward(attrs, present_attrs, out_of_range_attr,
1875 expected_attrs, flow, key, key_len);
1876 }
1877
1878 /* Returns 'fitness' as a string, for use in debug messages. */
1879 const char *
1880 odp_key_fitness_to_string(enum odp_key_fitness fitness)
1881 {
1882 switch (fitness) {
1883 case ODP_FIT_PERFECT:
1884 return "OK";
1885 case ODP_FIT_TOO_MUCH:
1886 return "too_much";
1887 case ODP_FIT_TOO_LITTLE:
1888 return "too_little";
1889 case ODP_FIT_ERROR:
1890 return "error";
1891 default:
1892 return "<unknown>";
1893 }
1894 }
1895
1896 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
1897 * Netlink PID 'pid'. If 'cookie' is nonnull, adds a userdata attribute whose
1898 * contents contains 'cookie' and returns the offset within 'odp_actions' of
1899 * the start of the cookie. (If 'cookie' is null, then the return value is not
1900 * meaningful.) */
1901 size_t
1902 odp_put_userspace_action(uint32_t pid, const union user_action_cookie *cookie,
1903 struct ofpbuf *odp_actions)
1904 {
1905 size_t offset;
1906
1907 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
1908 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
1909 if (cookie) {
1910 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
1911 cookie, sizeof *cookie);
1912 }
1913 nl_msg_end_nested(odp_actions, offset);
1914
1915 return cookie ? odp_actions->size - NLA_ALIGN(sizeof *cookie) : 0;
1916 }
1917 \f
1918 /* The commit_odp_actions() function and its helpers. */
1919
1920 static void
1921 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
1922 const void *key, size_t key_size)
1923 {
1924 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
1925 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
1926 nl_msg_end_nested(odp_actions, offset);
1927 }
1928
1929 static void
1930 commit_set_tun_id_action(const struct flow *flow, struct flow *base,
1931 struct ofpbuf *odp_actions)
1932 {
1933 if (base->tunnel.tun_id == flow->tunnel.tun_id) {
1934 return;
1935 }
1936 base->tunnel.tun_id = flow->tunnel.tun_id;
1937
1938 commit_set_action(odp_actions, OVS_KEY_ATTR_TUN_ID,
1939 &base->tunnel.tun_id, sizeof(base->tunnel.tun_id));
1940 }
1941
1942 static void
1943 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
1944 struct ofpbuf *odp_actions)
1945 {
1946 struct ovs_key_ethernet eth_key;
1947
1948 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
1949 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
1950 return;
1951 }
1952
1953 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
1954 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
1955
1956 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
1957 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
1958
1959 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
1960 &eth_key, sizeof(eth_key));
1961 }
1962
1963 static void
1964 commit_vlan_action(const struct flow *flow, struct flow *base,
1965 struct ofpbuf *odp_actions)
1966 {
1967 if (base->vlan_tci == flow->vlan_tci) {
1968 return;
1969 }
1970
1971 if (base->vlan_tci & htons(VLAN_CFI)) {
1972 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
1973 }
1974
1975 if (flow->vlan_tci & htons(VLAN_CFI)) {
1976 struct ovs_action_push_vlan vlan;
1977
1978 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
1979 vlan.vlan_tci = flow->vlan_tci;
1980 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
1981 &vlan, sizeof vlan);
1982 }
1983 base->vlan_tci = flow->vlan_tci;
1984 }
1985
1986 static void
1987 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
1988 struct ofpbuf *odp_actions)
1989 {
1990 struct ovs_key_ipv4 ipv4_key;
1991
1992 if (base->nw_src == flow->nw_src &&
1993 base->nw_dst == flow->nw_dst &&
1994 base->nw_tos == flow->nw_tos &&
1995 base->nw_ttl == flow->nw_ttl &&
1996 base->nw_frag == flow->nw_frag) {
1997 return;
1998 }
1999
2000 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
2001 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
2002 ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
2003 ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
2004 ipv4_key.ipv4_proto = base->nw_proto;
2005 ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
2006
2007 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
2008 &ipv4_key, sizeof(ipv4_key));
2009 }
2010
2011 static void
2012 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
2013 struct ofpbuf *odp_actions)
2014 {
2015 struct ovs_key_ipv6 ipv6_key;
2016
2017 if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
2018 ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
2019 base->ipv6_label == flow->ipv6_label &&
2020 base->nw_tos == flow->nw_tos &&
2021 base->nw_ttl == flow->nw_ttl &&
2022 base->nw_frag == flow->nw_frag) {
2023 return;
2024 }
2025
2026 base->ipv6_src = flow->ipv6_src;
2027 memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
2028 base->ipv6_dst = flow->ipv6_dst;
2029 memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
2030
2031 ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
2032 ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
2033 ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
2034 ipv6_key.ipv6_proto = base->nw_proto;
2035 ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
2036
2037 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
2038 &ipv6_key, sizeof(ipv6_key));
2039 }
2040
2041 static void
2042 commit_set_nw_action(const struct flow *flow, struct flow *base,
2043 struct ofpbuf *odp_actions)
2044 {
2045 /* Check if flow really have an IP header. */
2046 if (!flow->nw_proto) {
2047 return;
2048 }
2049
2050 if (base->dl_type == htons(ETH_TYPE_IP)) {
2051 commit_set_ipv4_action(flow, base, odp_actions);
2052 } else if (base->dl_type == htons(ETH_TYPE_IPV6)) {
2053 commit_set_ipv6_action(flow, base, odp_actions);
2054 }
2055 }
2056
2057 static void
2058 commit_set_port_action(const struct flow *flow, struct flow *base,
2059 struct ofpbuf *odp_actions)
2060 {
2061 if (!base->tp_src && !base->tp_dst) {
2062 return;
2063 }
2064
2065 if (base->tp_src == flow->tp_src &&
2066 base->tp_dst == flow->tp_dst) {
2067 return;
2068 }
2069
2070 if (flow->nw_proto == IPPROTO_TCP) {
2071 struct ovs_key_tcp port_key;
2072
2073 port_key.tcp_src = base->tp_src = flow->tp_src;
2074 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
2075
2076 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
2077 &port_key, sizeof(port_key));
2078
2079 } else if (flow->nw_proto == IPPROTO_UDP) {
2080 struct ovs_key_udp port_key;
2081
2082 port_key.udp_src = base->tp_src = flow->tp_src;
2083 port_key.udp_dst = base->tp_dst = flow->tp_dst;
2084
2085 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
2086 &port_key, sizeof(port_key));
2087 }
2088 }
2089
2090 static void
2091 commit_set_priority_action(const struct flow *flow, struct flow *base,
2092 struct ofpbuf *odp_actions)
2093 {
2094 if (base->skb_priority == flow->skb_priority) {
2095 return;
2096 }
2097 base->skb_priority = flow->skb_priority;
2098
2099 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
2100 &base->skb_priority, sizeof(base->skb_priority));
2101 }
2102
2103 /* If any of the flow key data that ODP actions can modify are different in
2104 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
2105 * key from 'base' into 'flow', and then changes 'base' the same way. */
2106 void
2107 commit_odp_actions(const struct flow *flow, struct flow *base,
2108 struct ofpbuf *odp_actions)
2109 {
2110 commit_set_tun_id_action(flow, base, odp_actions);
2111 commit_set_ether_addr_action(flow, base, odp_actions);
2112 commit_vlan_action(flow, base, odp_actions);
2113 commit_set_nw_action(flow, base, odp_actions);
2114 commit_set_port_action(flow, base, odp_actions);
2115 commit_set_priority_action(flow, base, odp_actions);
2116 }