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