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