]> git.proxmox.com Git - ovs.git/blob - lib/odp-util.c
Remove encal_dl_type from struct flow
[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
1809 if (eth_type_mpls(flow->dl_type)) {
1810 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
1811
1812 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) {
1813 return ODP_FIT_TOO_LITTLE;
1814 }
1815 flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]);
1816 flow->mpls_depth++;
1817 } else if (flow->dl_type == htons(ETH_TYPE_IP)) {
1818 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
1819 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
1820 const struct ovs_key_ipv4 *ipv4_key;
1821
1822 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
1823 flow->nw_src = ipv4_key->ipv4_src;
1824 flow->nw_dst = ipv4_key->ipv4_dst;
1825 flow->nw_proto = ipv4_key->ipv4_proto;
1826 flow->nw_tos = ipv4_key->ipv4_tos;
1827 flow->nw_ttl = ipv4_key->ipv4_ttl;
1828 if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
1829 return ODP_FIT_ERROR;
1830 }
1831 }
1832 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1833 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
1834 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
1835 const struct ovs_key_ipv6 *ipv6_key;
1836
1837 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
1838 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
1839 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
1840 flow->ipv6_label = ipv6_key->ipv6_label;
1841 flow->nw_proto = ipv6_key->ipv6_proto;
1842 flow->nw_tos = ipv6_key->ipv6_tclass;
1843 flow->nw_ttl = ipv6_key->ipv6_hlimit;
1844 if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
1845 return ODP_FIT_ERROR;
1846 }
1847 }
1848 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1849 flow->dl_type == htons(ETH_TYPE_RARP)) {
1850 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
1851 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
1852 const struct ovs_key_arp *arp_key;
1853
1854 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
1855 flow->nw_src = arp_key->arp_sip;
1856 flow->nw_dst = arp_key->arp_tip;
1857 if (arp_key->arp_op & htons(0xff00)) {
1858 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
1859 "key", ntohs(arp_key->arp_op));
1860 return ODP_FIT_ERROR;
1861 }
1862 flow->nw_proto = ntohs(arp_key->arp_op);
1863 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
1864 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
1865 }
1866 }
1867
1868 if (flow->nw_proto == IPPROTO_TCP
1869 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1870 flow->dl_type == htons(ETH_TYPE_IPV6))
1871 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1872 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
1873 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
1874 const struct ovs_key_tcp *tcp_key;
1875
1876 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
1877 flow->tp_src = tcp_key->tcp_src;
1878 flow->tp_dst = tcp_key->tcp_dst;
1879 }
1880 } else if (flow->nw_proto == IPPROTO_UDP
1881 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1882 flow->dl_type == htons(ETH_TYPE_IPV6))
1883 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1884 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
1885 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
1886 const struct ovs_key_udp *udp_key;
1887
1888 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
1889 flow->tp_src = udp_key->udp_src;
1890 flow->tp_dst = udp_key->udp_dst;
1891 }
1892 } else if (flow->nw_proto == IPPROTO_ICMP
1893 && flow->dl_type == htons(ETH_TYPE_IP)
1894 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1895 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
1896 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
1897 const struct ovs_key_icmp *icmp_key;
1898
1899 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
1900 flow->tp_src = htons(icmp_key->icmp_type);
1901 flow->tp_dst = htons(icmp_key->icmp_code);
1902 }
1903 } else if (flow->nw_proto == IPPROTO_ICMPV6
1904 && flow->dl_type == htons(ETH_TYPE_IPV6)
1905 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1906 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
1907 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
1908 const struct ovs_key_icmpv6 *icmpv6_key;
1909
1910 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
1911 flow->tp_src = htons(icmpv6_key->icmpv6_type);
1912 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
1913
1914 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1915 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
1916 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
1917 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
1918 const struct ovs_key_nd *nd_key;
1919
1920 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
1921 memcpy(&flow->nd_target, nd_key->nd_target,
1922 sizeof flow->nd_target);
1923 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
1924 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
1925 }
1926 }
1927 }
1928 }
1929
1930 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
1931 key, key_len);
1932 }
1933
1934 /* Parse 802.1Q header then encapsulated L3 attributes. */
1935 static enum odp_key_fitness
1936 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1937 uint64_t present_attrs, int out_of_range_attr,
1938 uint64_t expected_attrs, struct flow *flow,
1939 const struct nlattr *key, size_t key_len)
1940 {
1941 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1942
1943 const struct nlattr *encap
1944 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
1945 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
1946 enum odp_key_fitness encap_fitness;
1947 enum odp_key_fitness fitness;
1948 ovs_be16 tci;
1949
1950 /* Calulate fitness of outer attributes. */
1951 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
1952 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
1953 fitness = check_expectations(present_attrs, out_of_range_attr,
1954 expected_attrs, key, key_len);
1955
1956 /* Get the VLAN TCI value. */
1957 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
1958 return ODP_FIT_TOO_LITTLE;
1959 }
1960 tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
1961 if (tci == htons(0)) {
1962 /* Corner case for a truncated 802.1Q header. */
1963 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
1964 return ODP_FIT_TOO_MUCH;
1965 }
1966 return fitness;
1967 } else if (!(tci & htons(VLAN_CFI))) {
1968 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
1969 "but CFI bit is not set", ntohs(tci));
1970 return ODP_FIT_ERROR;
1971 }
1972
1973 /* Set vlan_tci.
1974 * Remove the TPID from dl_type since it's not the real Ethertype. */
1975 flow->vlan_tci = tci;
1976 flow->dl_type = htons(0);
1977
1978 /* Now parse the encapsulated attributes. */
1979 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
1980 attrs, &present_attrs, &out_of_range_attr)) {
1981 return ODP_FIT_ERROR;
1982 }
1983 expected_attrs = 0;
1984
1985 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
1986 return ODP_FIT_ERROR;
1987 }
1988 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
1989 expected_attrs, flow, key, key_len);
1990
1991 /* The overall fitness is the worse of the outer and inner attributes. */
1992 return MAX(fitness, encap_fitness);
1993 }
1994
1995 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
1996 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
1997 * 'key' fits our expectations for what a flow key should contain.
1998 *
1999 * The 'in_port' will be the datapath's understanding of the port. The
2000 * caller will need to translate with odp_port_to_ofp_port() if the
2001 * OpenFlow port is needed.
2002 *
2003 * This function doesn't take the packet itself as an argument because none of
2004 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
2005 * it is always possible to infer which additional attribute(s) should appear
2006 * by looking at the attributes for lower-level protocols, e.g. if the network
2007 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
2008 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
2009 * must be absent. */
2010 enum odp_key_fitness
2011 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
2012 struct flow *flow)
2013 {
2014 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
2015 uint64_t expected_attrs;
2016 uint64_t present_attrs;
2017 int out_of_range_attr;
2018
2019 memset(flow, 0, sizeof *flow);
2020
2021 /* Parse attributes. */
2022 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
2023 &out_of_range_attr)) {
2024 return ODP_FIT_ERROR;
2025 }
2026 expected_attrs = 0;
2027
2028 /* Metadata. */
2029 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
2030 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
2031 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
2032 }
2033
2034 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
2035 flow->skb_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
2036 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
2037 }
2038
2039 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
2040 enum odp_key_fitness res;
2041
2042 res = tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
2043 if (res == ODP_FIT_ERROR) {
2044 return ODP_FIT_ERROR;
2045 } else if (res == ODP_FIT_PERFECT) {
2046 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
2047 }
2048 }
2049
2050 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
2051 flow->in_port = nl_attr_get_u32(attrs[OVS_KEY_ATTR_IN_PORT]);
2052 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
2053 } else {
2054 flow->in_port = OVSP_NONE;
2055 }
2056
2057 /* Ethernet header. */
2058 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
2059 const struct ovs_key_ethernet *eth_key;
2060
2061 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
2062 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
2063 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
2064 }
2065 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
2066
2067 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
2068 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
2069 return ODP_FIT_ERROR;
2070 }
2071
2072 if (flow->dl_type == htons(ETH_TYPE_VLAN)) {
2073 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
2074 expected_attrs, flow, key, key_len);
2075 }
2076 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
2077 expected_attrs, flow, key, key_len);
2078 }
2079
2080 /* Returns 'fitness' as a string, for use in debug messages. */
2081 const char *
2082 odp_key_fitness_to_string(enum odp_key_fitness fitness)
2083 {
2084 switch (fitness) {
2085 case ODP_FIT_PERFECT:
2086 return "OK";
2087 case ODP_FIT_TOO_MUCH:
2088 return "too_much";
2089 case ODP_FIT_TOO_LITTLE:
2090 return "too_little";
2091 case ODP_FIT_ERROR:
2092 return "error";
2093 default:
2094 return "<unknown>";
2095 }
2096 }
2097
2098 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
2099 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
2100 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
2101 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
2102 * null, then the return value is not meaningful.) */
2103 size_t
2104 odp_put_userspace_action(uint32_t pid,
2105 const void *userdata, size_t userdata_size,
2106 struct ofpbuf *odp_actions)
2107 {
2108 size_t userdata_ofs;
2109 size_t offset;
2110
2111 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
2112 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
2113 if (userdata) {
2114 userdata_ofs = odp_actions->size + NLA_HDRLEN;
2115 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
2116 userdata, userdata_size);
2117 } else {
2118 userdata_ofs = 0;
2119 }
2120 nl_msg_end_nested(odp_actions, offset);
2121
2122 return userdata_ofs;
2123 }
2124
2125 void
2126 odp_put_tunnel_action(const struct flow_tnl *tunnel,
2127 struct ofpbuf *odp_actions)
2128 {
2129 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
2130 tun_key_to_attr(odp_actions, tunnel);
2131 nl_msg_end_nested(odp_actions, offset);
2132 }
2133 \f
2134 /* The commit_odp_actions() function and its helpers. */
2135
2136 static void
2137 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
2138 const void *key, size_t key_size)
2139 {
2140 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
2141 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
2142 nl_msg_end_nested(odp_actions, offset);
2143 }
2144
2145 void
2146 odp_put_skb_mark_action(const uint32_t skb_mark,
2147 struct ofpbuf *odp_actions)
2148 {
2149 commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK, &skb_mark,
2150 sizeof(skb_mark));
2151 }
2152
2153 /* If any of the flow key data that ODP actions can modify are different in
2154 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
2155 * 'odp_actions' that change the flow tunneling information in key from
2156 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
2157 * same way. In other words, operates the same as commit_odp_actions(), but
2158 * only on tunneling information. */
2159 void
2160 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
2161 struct ofpbuf *odp_actions)
2162 {
2163 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
2164 return;
2165 }
2166 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
2167
2168 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
2169 if (flow->tunnel.ip_dst) {
2170 odp_put_tunnel_action(&base->tunnel, odp_actions);
2171 }
2172 }
2173
2174 static void
2175 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
2176 struct ofpbuf *odp_actions)
2177 {
2178 struct ovs_key_ethernet eth_key;
2179
2180 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
2181 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
2182 return;
2183 }
2184
2185 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
2186 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
2187
2188 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
2189 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
2190
2191 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
2192 &eth_key, sizeof(eth_key));
2193 }
2194
2195 static void
2196 commit_vlan_action(const struct flow *flow, struct flow *base,
2197 struct ofpbuf *odp_actions)
2198 {
2199 if (base->vlan_tci == flow->vlan_tci) {
2200 return;
2201 }
2202
2203 if (base->vlan_tci & htons(VLAN_CFI)) {
2204 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
2205 }
2206
2207 if (flow->vlan_tci & htons(VLAN_CFI)) {
2208 struct ovs_action_push_vlan vlan;
2209
2210 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
2211 vlan.vlan_tci = flow->vlan_tci;
2212 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
2213 &vlan, sizeof vlan);
2214 }
2215 base->vlan_tci = flow->vlan_tci;
2216 }
2217
2218 static void
2219 commit_mpls_action(const struct flow *flow, struct flow *base,
2220 struct ofpbuf *odp_actions)
2221 {
2222 if (flow->mpls_lse == base->mpls_lse &&
2223 flow->mpls_depth == base->mpls_depth) {
2224 return;
2225 }
2226
2227 if (flow->mpls_depth < base->mpls_depth) {
2228 if (base->mpls_depth - flow->mpls_depth > 1) {
2229 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2230 VLOG_WARN_RL(&rl, "Multiple mpls_pop actions reduced to "
2231 " a single mpls_pop action");
2232 }
2233
2234 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, flow->dl_type);
2235 } else if (flow->mpls_depth > base->mpls_depth) {
2236 struct ovs_action_push_mpls *mpls;
2237
2238 if (flow->mpls_depth - base->mpls_depth > 1) {
2239 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2240 VLOG_WARN_RL(&rl, "Multiple mpls_push actions reduced to "
2241 " a single mpls_push action");
2242 }
2243
2244 mpls = nl_msg_put_unspec_uninit(odp_actions, OVS_ACTION_ATTR_PUSH_MPLS,
2245 sizeof *mpls);
2246 memset(mpls, 0, sizeof *mpls);
2247 mpls->mpls_ethertype = flow->dl_type;
2248 mpls->mpls_lse = flow->mpls_lse;
2249 } else {
2250 struct ovs_key_mpls mpls_key;
2251
2252 mpls_key.mpls_top_lse = flow->mpls_lse;
2253 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
2254 &mpls_key, sizeof(mpls_key));
2255 }
2256
2257 base->dl_type = flow->dl_type;
2258 base->mpls_lse = flow->mpls_lse;
2259 base->mpls_depth = flow->mpls_depth;
2260 }
2261
2262 static void
2263 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
2264 struct ofpbuf *odp_actions)
2265 {
2266 struct ovs_key_ipv4 ipv4_key;
2267
2268 if (base->nw_src == flow->nw_src &&
2269 base->nw_dst == flow->nw_dst &&
2270 base->nw_tos == flow->nw_tos &&
2271 base->nw_ttl == flow->nw_ttl &&
2272 base->nw_frag == flow->nw_frag) {
2273 return;
2274 }
2275
2276 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
2277 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
2278 ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
2279 ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
2280 ipv4_key.ipv4_proto = base->nw_proto;
2281 ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
2282
2283 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
2284 &ipv4_key, sizeof(ipv4_key));
2285 }
2286
2287 static void
2288 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
2289 struct ofpbuf *odp_actions)
2290 {
2291 struct ovs_key_ipv6 ipv6_key;
2292
2293 if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
2294 ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
2295 base->ipv6_label == flow->ipv6_label &&
2296 base->nw_tos == flow->nw_tos &&
2297 base->nw_ttl == flow->nw_ttl &&
2298 base->nw_frag == flow->nw_frag) {
2299 return;
2300 }
2301
2302 base->ipv6_src = flow->ipv6_src;
2303 memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
2304 base->ipv6_dst = flow->ipv6_dst;
2305 memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
2306
2307 ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
2308 ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
2309 ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
2310 ipv6_key.ipv6_proto = base->nw_proto;
2311 ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
2312
2313 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
2314 &ipv6_key, sizeof(ipv6_key));
2315 }
2316
2317 static void
2318 commit_set_nw_action(const struct flow *flow, struct flow *base,
2319 struct ofpbuf *odp_actions)
2320 {
2321 /* Check if flow really have an IP header. */
2322 if (!flow->nw_proto) {
2323 return;
2324 }
2325
2326 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2327 commit_set_ipv4_action(flow, base, odp_actions);
2328 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2329 commit_set_ipv6_action(flow, base, odp_actions);
2330 }
2331 }
2332
2333 static void
2334 commit_set_port_action(const struct flow *flow, struct flow *base,
2335 struct ofpbuf *odp_actions)
2336 {
2337 if (!base->tp_src && !base->tp_dst) {
2338 return;
2339 }
2340
2341 if (base->tp_src == flow->tp_src &&
2342 base->tp_dst == flow->tp_dst) {
2343 return;
2344 }
2345
2346 if (flow->nw_proto == IPPROTO_TCP) {
2347 struct ovs_key_tcp port_key;
2348
2349 port_key.tcp_src = base->tp_src = flow->tp_src;
2350 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
2351
2352 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
2353 &port_key, sizeof(port_key));
2354
2355 } else if (flow->nw_proto == IPPROTO_UDP) {
2356 struct ovs_key_udp port_key;
2357
2358 port_key.udp_src = base->tp_src = flow->tp_src;
2359 port_key.udp_dst = base->tp_dst = flow->tp_dst;
2360
2361 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
2362 &port_key, sizeof(port_key));
2363 }
2364 }
2365
2366 static void
2367 commit_set_priority_action(const struct flow *flow, struct flow *base,
2368 struct ofpbuf *odp_actions)
2369 {
2370 if (base->skb_priority == flow->skb_priority) {
2371 return;
2372 }
2373 base->skb_priority = flow->skb_priority;
2374
2375 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
2376 &base->skb_priority, sizeof(base->skb_priority));
2377 }
2378
2379 static void
2380 commit_set_skb_mark_action(const struct flow *flow, struct flow *base,
2381 struct ofpbuf *odp_actions)
2382 {
2383 if (base->skb_mark == flow->skb_mark) {
2384 return;
2385 }
2386 base->skb_mark = flow->skb_mark;
2387
2388 odp_put_skb_mark_action(base->skb_mark, odp_actions);
2389 }
2390 /* If any of the flow key data that ODP actions can modify are different in
2391 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
2392 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
2393 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
2394 * in addition to this function if needed. */
2395 void
2396 commit_odp_actions(const struct flow *flow, struct flow *base,
2397 struct ofpbuf *odp_actions)
2398 {
2399 commit_set_ether_addr_action(flow, base, odp_actions);
2400 commit_vlan_action(flow, base, odp_actions);
2401 commit_mpls_action(flow, base, odp_actions);
2402 commit_set_nw_action(flow, base, odp_actions);
2403 commit_set_port_action(flow, base, odp_actions);
2404 commit_set_priority_action(flow, base, odp_actions);
2405 commit_set_skb_mark_action(flow, base, odp_actions);
2406 }