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