CodingStyle: Allow designated initializers.
[sliver-openvswitch.git] / lib / odp-util.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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 "dpif.h"
30 #include "dynamic-string.h"
31 #include "flow.h"
32 #include "netlink.h"
33 #include "ofpbuf.h"
34 #include "packets.h"
35 #include "simap.h"
36 #include "timeval.h"
37 #include "util.h"
38 #include "vlog.h"
39
40 VLOG_DEFINE_THIS_MODULE(odp_util);
41
42 /* The interface between userspace and kernel uses an "OVS_*" prefix.
43  * Since this is fairly non-specific for the OVS userspace components,
44  * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
45  * interactions with the datapath.
46  */
47
48 /* The set of characters that may separate one action or one key attribute
49  * from another. */
50 static const char *delimiters = ", \t\r\n";
51
52 static int parse_odp_key_mask_attr(const char *, const struct simap *port_names,
53                               struct ofpbuf *, struct ofpbuf *);
54 static void format_odp_key_attr(const struct nlattr *a,
55                                 const struct nlattr *ma,
56                                 const struct hmap *portno_names, struct ds *ds,
57                                 bool verbose);
58
59 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
60  * 'type':
61  *
62  *   - For an action whose argument has a fixed length, returned that
63  *     nonnegative length in bytes.
64  *
65  *   - For an action with a variable-length argument, returns -2.
66  *
67  *   - For an invalid 'type', returns -1. */
68 static int
69 odp_action_len(uint16_t type)
70 {
71     if (type > OVS_ACTION_ATTR_MAX) {
72         return -1;
73     }
74
75     switch ((enum ovs_action_attr) type) {
76     case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
77     case OVS_ACTION_ATTR_USERSPACE: return -2;
78     case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
79     case OVS_ACTION_ATTR_POP_VLAN: return 0;
80     case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
81     case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
82     case OVS_ACTION_ATTR_SET: return -2;
83     case OVS_ACTION_ATTR_SAMPLE: return -2;
84
85     case OVS_ACTION_ATTR_UNSPEC:
86     case __OVS_ACTION_ATTR_MAX:
87         return -1;
88     }
89
90     return -1;
91 }
92
93 /* Returns a string form of 'attr'.  The return value is either a statically
94  * allocated constant string or the 'bufsize'-byte buffer 'namebuf'.  'bufsize'
95  * should be at least OVS_KEY_ATTR_BUFSIZE. */
96 enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
97 static const char *
98 ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
99 {
100     switch (attr) {
101     case OVS_KEY_ATTR_UNSPEC: return "unspec";
102     case OVS_KEY_ATTR_ENCAP: return "encap";
103     case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
104     case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
105     case OVS_KEY_ATTR_TUNNEL: return "tunnel";
106     case OVS_KEY_ATTR_IN_PORT: return "in_port";
107     case OVS_KEY_ATTR_ETHERNET: return "eth";
108     case OVS_KEY_ATTR_VLAN: return "vlan";
109     case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
110     case OVS_KEY_ATTR_IPV4: return "ipv4";
111     case OVS_KEY_ATTR_IPV6: return "ipv6";
112     case OVS_KEY_ATTR_TCP: return "tcp";
113     case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags";
114     case OVS_KEY_ATTR_UDP: return "udp";
115     case OVS_KEY_ATTR_SCTP: return "sctp";
116     case OVS_KEY_ATTR_ICMP: return "icmp";
117     case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
118     case OVS_KEY_ATTR_ARP: return "arp";
119     case OVS_KEY_ATTR_ND: return "nd";
120     case OVS_KEY_ATTR_MPLS: return "mpls";
121
122     case __OVS_KEY_ATTR_MAX:
123     default:
124         snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
125         return namebuf;
126     }
127 }
128
129 static void
130 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
131 {
132     size_t len = nl_attr_get_size(a);
133
134     ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
135     if (len) {
136         const uint8_t *unspec;
137         unsigned int i;
138
139         unspec = nl_attr_get(a);
140         for (i = 0; i < len; i++) {
141             ds_put_char(ds, i ? ' ': '(');
142             ds_put_format(ds, "%02x", unspec[i]);
143         }
144         ds_put_char(ds, ')');
145     }
146 }
147
148 static void
149 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
150 {
151     static const struct nl_policy ovs_sample_policy[] = {
152         [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
153         [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
154     };
155     struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
156     double percentage;
157     const struct nlattr *nla_acts;
158     int len;
159
160     ds_put_cstr(ds, "sample");
161
162     if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
163         ds_put_cstr(ds, "(error)");
164         return;
165     }
166
167     percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
168                         UINT32_MAX;
169
170     ds_put_format(ds, "(sample=%.1f%%,", percentage);
171
172     ds_put_cstr(ds, "actions(");
173     nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
174     len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
175     format_odp_actions(ds, nla_acts, len);
176     ds_put_format(ds, "))");
177 }
178
179 static const char *
180 slow_path_reason_to_string(uint32_t reason)
181 {
182     switch ((enum slow_path_reason) reason) {
183 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
184         SLOW_PATH_REASONS
185 #undef SPR
186     }
187
188     return NULL;
189 }
190
191 const char *
192 slow_path_reason_to_explanation(enum slow_path_reason reason)
193 {
194     switch (reason) {
195 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
196         SLOW_PATH_REASONS
197 #undef SPR
198     }
199
200     return "<unknown>";
201 }
202
203 static int
204 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
205             uint32_t *res)
206 {
207     uint32_t result = 0;
208     int n = 0;
209
210     if (s[n] != '(') {
211         return -EINVAL;
212     }
213     n++;
214
215     while (s[n] != ')') {
216         unsigned long long int flags;
217         uint32_t bit;
218         int n0;
219
220         if (ovs_scan(&s[n], "%lli%n", &flags, &n0)) {
221             n += n0 + (s[n + n0] == ',');
222             result |= flags;
223             continue;
224         }
225
226         for (bit = 1; bit; bit <<= 1) {
227             const char *name = bit_to_string(bit);
228             size_t len;
229
230             if (!name) {
231                 continue;
232             }
233
234             len = strlen(name);
235             if (!strncmp(s + n, name, len) &&
236                 (s[n + len] == ',' || s[n + len] == ')')) {
237                 result |= bit;
238                 n += len + (s[n + len] == ',');
239                 break;
240             }
241         }
242
243         if (!bit) {
244             return -EINVAL;
245         }
246     }
247     n++;
248
249     *res = result;
250     return n;
251 }
252
253 static void
254 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
255 {
256     static const struct nl_policy ovs_userspace_policy[] = {
257         [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
258         [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
259                                           .optional = true },
260     };
261     struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
262     const struct nlattr *userdata_attr;
263
264     if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
265         ds_put_cstr(ds, "userspace(error)");
266         return;
267     }
268
269     ds_put_format(ds, "userspace(pid=%"PRIu32,
270                   nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
271
272     userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
273
274     if (userdata_attr) {
275         const uint8_t *userdata = nl_attr_get(userdata_attr);
276         size_t userdata_len = nl_attr_get_size(userdata_attr);
277         bool userdata_unspec = true;
278         union user_action_cookie cookie;
279
280         if (userdata_len >= sizeof cookie.type
281             && userdata_len <= sizeof cookie) {
282
283             memset(&cookie, 0, sizeof cookie);
284             memcpy(&cookie, userdata, userdata_len);
285
286             userdata_unspec = false;
287
288             if (userdata_len == sizeof cookie.sflow
289                 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
290                 ds_put_format(ds, ",sFlow("
291                               "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
292                               vlan_tci_to_vid(cookie.sflow.vlan_tci),
293                               vlan_tci_to_pcp(cookie.sflow.vlan_tci),
294                               cookie.sflow.output);
295             } else if (userdata_len == sizeof cookie.slow_path
296                        && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
297                 ds_put_cstr(ds, ",slow_path(");
298                 format_flags(ds, slow_path_reason_to_string,
299                              cookie.slow_path.reason, ',');
300                 ds_put_format(ds, ")");
301             } else if (userdata_len == sizeof cookie.flow_sample
302                        && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
303                 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
304                               ",collector_set_id=%"PRIu32
305                               ",obs_domain_id=%"PRIu32
306                               ",obs_point_id=%"PRIu32")",
307                               cookie.flow_sample.probability,
308                               cookie.flow_sample.collector_set_id,
309                               cookie.flow_sample.obs_domain_id,
310                               cookie.flow_sample.obs_point_id);
311             } else if (userdata_len >= sizeof cookie.ipfix
312                        && cookie.type == USER_ACTION_COOKIE_IPFIX) {
313                 ds_put_format(ds, ",ipfix");
314             } else {
315                 userdata_unspec = true;
316             }
317         }
318
319         if (userdata_unspec) {
320             size_t i;
321             ds_put_format(ds, ",userdata(");
322             for (i = 0; i < userdata_len; i++) {
323                 ds_put_format(ds, "%02x", userdata[i]);
324             }
325             ds_put_char(ds, ')');
326         }
327     }
328
329     ds_put_char(ds, ')');
330 }
331
332 static void
333 format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
334 {
335     ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
336                   vlan_tci_to_vid(vlan_tci),
337                   vlan_tci_to_pcp(vlan_tci));
338     if (!(vlan_tci & htons(VLAN_CFI))) {
339         ds_put_cstr(ds, ",cfi=0");
340     }
341 }
342
343 static void
344 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
345 {
346     ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
347                   mpls_lse_to_label(mpls_lse),
348                   mpls_lse_to_tc(mpls_lse),
349                   mpls_lse_to_ttl(mpls_lse),
350                   mpls_lse_to_bos(mpls_lse));
351 }
352
353 static void
354 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
355             const struct ovs_key_mpls *mpls_mask, int n)
356 {
357     if (n == 1) {
358         ovs_be32 key = mpls_key->mpls_lse;
359
360         if (mpls_mask == NULL) {
361             format_mpls_lse(ds, key);
362         } else {
363             ovs_be32 mask = mpls_mask->mpls_lse;
364
365             ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
366                           mpls_lse_to_label(key), mpls_lse_to_label(mask),
367                           mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
368                           mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
369                           mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
370         }
371     } else {
372         int i;
373
374         for (i = 0; i < n; i++) {
375             ds_put_format(ds, "lse%d=%#"PRIx32,
376                           i, ntohl(mpls_key[i].mpls_lse));
377             if (mpls_mask) {
378                 ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse));
379             }
380             ds_put_char(ds, ',');
381         }
382         ds_chomp(ds, ',');
383     }
384 }
385
386 static void
387 format_odp_action(struct ds *ds, const struct nlattr *a)
388 {
389     int expected_len;
390     enum ovs_action_attr type = nl_attr_type(a);
391     const struct ovs_action_push_vlan *vlan;
392
393     expected_len = odp_action_len(nl_attr_type(a));
394     if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
395         ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ",
396                       nl_attr_get_size(a), expected_len);
397         format_generic_odp_action(ds, a);
398         return;
399     }
400
401     switch (type) {
402     case OVS_ACTION_ATTR_OUTPUT:
403         ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
404         break;
405     case OVS_ACTION_ATTR_USERSPACE:
406         format_odp_userspace_action(ds, a);
407         break;
408     case OVS_ACTION_ATTR_SET:
409         ds_put_cstr(ds, "set(");
410         format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
411         ds_put_cstr(ds, ")");
412         break;
413     case OVS_ACTION_ATTR_PUSH_VLAN:
414         vlan = nl_attr_get(a);
415         ds_put_cstr(ds, "push_vlan(");
416         if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
417             ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
418         }
419         format_vlan_tci(ds, vlan->vlan_tci);
420         ds_put_char(ds, ')');
421         break;
422     case OVS_ACTION_ATTR_POP_VLAN:
423         ds_put_cstr(ds, "pop_vlan");
424         break;
425     case OVS_ACTION_ATTR_PUSH_MPLS: {
426         const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
427         ds_put_cstr(ds, "push_mpls(");
428         format_mpls_lse(ds, mpls->mpls_lse);
429         ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
430         break;
431     }
432     case OVS_ACTION_ATTR_POP_MPLS: {
433         ovs_be16 ethertype = nl_attr_get_be16(a);
434         ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
435         break;
436     }
437     case OVS_ACTION_ATTR_SAMPLE:
438         format_odp_sample_action(ds, a);
439         break;
440     case OVS_ACTION_ATTR_UNSPEC:
441     case __OVS_ACTION_ATTR_MAX:
442     default:
443         format_generic_odp_action(ds, a);
444         break;
445     }
446 }
447
448 void
449 format_odp_actions(struct ds *ds, const struct nlattr *actions,
450                    size_t actions_len)
451 {
452     if (actions_len) {
453         const struct nlattr *a;
454         unsigned int left;
455
456         NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
457             if (a != actions) {
458                 ds_put_char(ds, ',');
459             }
460             format_odp_action(ds, a);
461         }
462         if (left) {
463             int i;
464
465             if (left == actions_len) {
466                 ds_put_cstr(ds, "<empty>");
467             }
468             ds_put_format(ds, ",***%u leftover bytes*** (", left);
469             for (i = 0; i < left; i++) {
470                 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
471             }
472             ds_put_char(ds, ')');
473         }
474     } else {
475         ds_put_cstr(ds, "drop");
476     }
477 }
478
479 static int
480 parse_odp_action(const char *s, const struct simap *port_names,
481                  struct ofpbuf *actions)
482 {
483     {
484         uint32_t port;
485         int n;
486
487         if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
488             nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
489             return n;
490         }
491     }
492
493     if (port_names) {
494         int len = strcspn(s, delimiters);
495         struct simap_node *node;
496
497         node = simap_find_len(port_names, s, len);
498         if (node) {
499             nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
500             return len;
501         }
502     }
503
504     {
505         uint32_t pid;
506         uint32_t output;
507         uint32_t probability;
508         uint32_t collector_set_id;
509         uint32_t obs_domain_id;
510         uint32_t obs_point_id;
511         int vid, pcp;
512         int n = -1;
513
514         if (ovs_scan(s, "userspace(pid=%"SCNi32")%n", &pid, &n)) {
515             odp_put_userspace_action(pid, NULL, 0, actions);
516             return n;
517         } else if (ovs_scan(s, "userspace(pid=%"SCNi32",sFlow(vid=%i,"
518                             "pcp=%i,output=%"SCNi32"))%n",
519                             &pid, &vid, &pcp, &output, &n)) {
520             union user_action_cookie cookie;
521             uint16_t tci;
522
523             tci = vid | (pcp << VLAN_PCP_SHIFT);
524             if (tci) {
525                 tci |= VLAN_CFI;
526             }
527
528             cookie.type = USER_ACTION_COOKIE_SFLOW;
529             cookie.sflow.vlan_tci = htons(tci);
530             cookie.sflow.output = output;
531             odp_put_userspace_action(pid, &cookie, sizeof cookie.sflow,
532                                      actions);
533             return n;
534         } else if (ovs_scan(s, "userspace(pid=%"SCNi32",slow_path%n",
535                             &pid, &n)) {
536             union user_action_cookie cookie;
537             int res;
538
539             cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
540             cookie.slow_path.unused = 0;
541             cookie.slow_path.reason = 0;
542
543             res = parse_flags(&s[n], slow_path_reason_to_string,
544                               &cookie.slow_path.reason);
545             if (res < 0) {
546                 return res;
547             }
548             n += res;
549             if (s[n] != ')') {
550                 return -EINVAL;
551             }
552             n++;
553
554             odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path,
555                                      actions);
556             return n;
557         } else if (ovs_scan(s, "userspace(pid=%"SCNi32","
558                             "flow_sample(probability=%"SCNi32","
559                             "collector_set_id=%"SCNi32","
560                             "obs_domain_id=%"SCNi32","
561                             "obs_point_id=%"SCNi32"))%n",
562                             &pid, &probability, &collector_set_id,
563                             &obs_domain_id, &obs_point_id, &n)) {
564             union user_action_cookie cookie;
565
566             cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
567             cookie.flow_sample.probability = probability;
568             cookie.flow_sample.collector_set_id = collector_set_id;
569             cookie.flow_sample.obs_domain_id = obs_domain_id;
570             cookie.flow_sample.obs_point_id = obs_point_id;
571             odp_put_userspace_action(pid, &cookie, sizeof cookie.flow_sample,
572                                      actions);
573             return n;
574         } else if (ovs_scan(s, "userspace(pid=%"SCNi32",ipfix)%n", &pid, &n)) {
575             union user_action_cookie cookie;
576
577             cookie.type = USER_ACTION_COOKIE_IPFIX;
578             odp_put_userspace_action(pid, &cookie, sizeof cookie.ipfix,
579                                      actions);
580             return n;
581         } else if (ovs_scan(s, "userspace(pid=%"SCNi32",userdata(%n",
582                             &pid, &n)) {
583             struct ofpbuf buf;
584             char *end;
585
586             ofpbuf_init(&buf, 16);
587             end = ofpbuf_put_hex(&buf, &s[n], NULL);
588             if (end[0] == ')' && end[1] == ')') {
589                 odp_put_userspace_action(pid, buf.data, buf.size, actions);
590                 ofpbuf_uninit(&buf);
591                 return (end + 2) - s;
592             }
593         }
594     }
595
596     if (!strncmp(s, "set(", 4)) {
597         size_t start_ofs;
598         int retval;
599
600         start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
601         retval = parse_odp_key_mask_attr(s + 4, port_names, actions, NULL);
602         if (retval < 0) {
603             return retval;
604         }
605         if (s[retval + 4] != ')') {
606             return -EINVAL;
607         }
608         nl_msg_end_nested(actions, start_ofs);
609         return retval + 5;
610     }
611
612     {
613         struct ovs_action_push_vlan push;
614         int tpid = ETH_TYPE_VLAN;
615         int vid, pcp;
616         int cfi = 1;
617         int n = -1;
618
619         if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
620             || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
621                         &vid, &pcp, &cfi, &n)
622             || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
623                         &tpid, &vid, &pcp, &n)
624             || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
625                         &tpid, &vid, &pcp, &cfi, &n)) {
626             push.vlan_tpid = htons(tpid);
627             push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
628                                   | (pcp << VLAN_PCP_SHIFT)
629                                   | (cfi ? VLAN_CFI : 0));
630             nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
631                               &push, sizeof push);
632
633             return n;
634         }
635     }
636
637     if (!strncmp(s, "pop_vlan", 8)) {
638         nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
639         return 8;
640     }
641
642     {
643         double percentage;
644         int n = -1;
645
646         if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
647             && percentage >= 0. && percentage <= 100.0) {
648             size_t sample_ofs, actions_ofs;
649             double probability;
650
651             probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
652             sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
653             nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
654                            (probability <= 0 ? 0
655                             : probability >= UINT32_MAX ? UINT32_MAX
656                             : probability));
657
658             actions_ofs = nl_msg_start_nested(actions,
659                                               OVS_SAMPLE_ATTR_ACTIONS);
660             for (;;) {
661                 int retval;
662
663                 n += strspn(s + n, delimiters);
664                 if (s[n] == ')') {
665                     break;
666                 }
667
668                 retval = parse_odp_action(s + n, port_names, actions);
669                 if (retval < 0) {
670                     return retval;
671                 }
672                 n += retval;
673             }
674             nl_msg_end_nested(actions, actions_ofs);
675             nl_msg_end_nested(actions, sample_ofs);
676
677             return s[n + 1] == ')' ? n + 2 : -EINVAL;
678         }
679     }
680
681     return -EINVAL;
682 }
683
684 /* Parses the string representation of datapath actions, in the format output
685  * by format_odp_action().  Returns 0 if successful, otherwise a positive errno
686  * value.  On success, the ODP actions are appended to 'actions' as a series of
687  * Netlink attributes.  On failure, no data is appended to 'actions'.  Either
688  * way, 'actions''s data might be reallocated. */
689 int
690 odp_actions_from_string(const char *s, const struct simap *port_names,
691                         struct ofpbuf *actions)
692 {
693     size_t old_size;
694
695     if (!strcasecmp(s, "drop")) {
696         return 0;
697     }
698
699     old_size = actions->size;
700     for (;;) {
701         int retval;
702
703         s += strspn(s, delimiters);
704         if (!*s) {
705             return 0;
706         }
707
708         retval = parse_odp_action(s, port_names, actions);
709         if (retval < 0 || !strchr(delimiters, s[retval])) {
710             actions->size = old_size;
711             return -retval;
712         }
713         s += retval;
714     }
715
716     return 0;
717 }
718 \f
719 /* Returns the correct length of the payload for a flow key attribute of the
720  * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
721  * is variable length. */
722 static int
723 odp_flow_key_attr_len(uint16_t type)
724 {
725     if (type > OVS_KEY_ATTR_MAX) {
726         return -1;
727     }
728
729     switch ((enum ovs_key_attr) type) {
730     case OVS_KEY_ATTR_ENCAP: return -2;
731     case OVS_KEY_ATTR_PRIORITY: return 4;
732     case OVS_KEY_ATTR_SKB_MARK: return 4;
733     case OVS_KEY_ATTR_TUNNEL: return -2;
734     case OVS_KEY_ATTR_IN_PORT: return 4;
735     case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
736     case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
737     case OVS_KEY_ATTR_ETHERTYPE: return 2;
738     case OVS_KEY_ATTR_MPLS: return -2;
739     case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
740     case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
741     case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
742     case OVS_KEY_ATTR_TCP_FLAGS: return 2;
743     case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
744     case OVS_KEY_ATTR_SCTP: return sizeof(struct ovs_key_sctp);
745     case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
746     case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
747     case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
748     case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
749
750     case OVS_KEY_ATTR_UNSPEC:
751     case __OVS_KEY_ATTR_MAX:
752         return -1;
753     }
754
755     return -1;
756 }
757
758 static void
759 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
760 {
761     size_t len = nl_attr_get_size(a);
762     if (len) {
763         const uint8_t *unspec;
764         unsigned int i;
765
766         unspec = nl_attr_get(a);
767         for (i = 0; i < len; i++) {
768             if (i) {
769                 ds_put_char(ds, ' ');
770             }
771             ds_put_format(ds, "%02x", unspec[i]);
772         }
773     }
774 }
775
776 static const char *
777 ovs_frag_type_to_string(enum ovs_frag_type type)
778 {
779     switch (type) {
780     case OVS_FRAG_TYPE_NONE:
781         return "no";
782     case OVS_FRAG_TYPE_FIRST:
783         return "first";
784     case OVS_FRAG_TYPE_LATER:
785         return "later";
786     case __OVS_FRAG_TYPE_MAX:
787     default:
788         return "<error>";
789     }
790 }
791
792 static int
793 tunnel_key_attr_len(int type)
794 {
795     switch (type) {
796     case OVS_TUNNEL_KEY_ATTR_ID: return 8;
797     case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4;
798     case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4;
799     case OVS_TUNNEL_KEY_ATTR_TOS: return 1;
800     case OVS_TUNNEL_KEY_ATTR_TTL: return 1;
801     case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0;
802     case OVS_TUNNEL_KEY_ATTR_CSUM: return 0;
803     case __OVS_TUNNEL_KEY_ATTR_MAX:
804         return -1;
805     }
806     return -1;
807 }
808
809 enum odp_key_fitness
810 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
811 {
812     unsigned int left;
813     const struct nlattr *a;
814     bool ttl = false;
815     bool unknown = false;
816
817     NL_NESTED_FOR_EACH(a, left, attr) {
818         uint16_t type = nl_attr_type(a);
819         size_t len = nl_attr_get_size(a);
820         int expected_len = tunnel_key_attr_len(type);
821
822         if (len != expected_len && expected_len >= 0) {
823             return ODP_FIT_ERROR;
824         }
825
826         switch (type) {
827         case OVS_TUNNEL_KEY_ATTR_ID:
828             tun->tun_id = nl_attr_get_be64(a);
829             tun->flags |= FLOW_TNL_F_KEY;
830             break;
831         case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
832             tun->ip_src = nl_attr_get_be32(a);
833             break;
834         case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
835             tun->ip_dst = nl_attr_get_be32(a);
836             break;
837         case OVS_TUNNEL_KEY_ATTR_TOS:
838             tun->ip_tos = nl_attr_get_u8(a);
839             break;
840         case OVS_TUNNEL_KEY_ATTR_TTL:
841             tun->ip_ttl = nl_attr_get_u8(a);
842             ttl = true;
843             break;
844         case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
845             tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
846             break;
847         case OVS_TUNNEL_KEY_ATTR_CSUM:
848             tun->flags |= FLOW_TNL_F_CSUM;
849             break;
850         default:
851             /* Allow this to show up as unexpected, if there are unknown
852              * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
853             unknown = true;
854             break;
855         }
856     }
857
858     if (!ttl) {
859         return ODP_FIT_ERROR;
860     }
861     if (unknown) {
862         return ODP_FIT_TOO_MUCH;
863     }
864     return ODP_FIT_PERFECT;
865 }
866
867 static void
868 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
869 {
870     size_t tun_key_ofs;
871
872     tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
873
874     /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
875     if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
876         nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
877     }
878     if (tun_key->ip_src) {
879         nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
880     }
881     if (tun_key->ip_dst) {
882         nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
883     }
884     if (tun_key->ip_tos) {
885         nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
886     }
887     nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
888     if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
889         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
890     }
891     if (tun_key->flags & FLOW_TNL_F_CSUM) {
892         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
893     }
894
895     nl_msg_end_nested(a, tun_key_ofs);
896 }
897
898 static bool
899 odp_mask_attr_is_wildcard(const struct nlattr *ma)
900 {
901     return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
902 }
903
904 static bool
905 odp_mask_attr_is_exact(const struct nlattr *ma)
906 {
907     bool is_exact = false;
908     enum ovs_key_attr attr = nl_attr_type(ma);
909
910     if (attr == OVS_KEY_ATTR_TUNNEL) {
911         /* XXX this is a hack for now. Should change
912          * the exact match dection to per field
913          * instead of per attribute.
914          */
915         struct flow_tnl tun_mask;
916         memset(&tun_mask, 0, sizeof tun_mask);
917         odp_tun_key_from_attr(ma, &tun_mask);
918         if (tun_mask.flags == (FLOW_TNL_F_KEY
919                                | FLOW_TNL_F_DONT_FRAGMENT
920                                | FLOW_TNL_F_CSUM)) {
921             /* The flags are exact match, check the remaining fields. */
922             tun_mask.flags = 0xffff;
923             is_exact = is_all_ones((uint8_t *)&tun_mask,
924                                    offsetof(struct flow_tnl, ip_ttl));
925         }
926     } else {
927         is_exact = is_all_ones(nl_attr_get(ma), nl_attr_get_size(ma));
928     }
929
930     return is_exact;
931 }
932
933 void
934 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
935                      char *port_name)
936 {
937     struct odp_portno_names *odp_portno_names;
938
939     odp_portno_names = xmalloc(sizeof *odp_portno_names);
940     odp_portno_names->port_no = port_no;
941     odp_portno_names->name = xstrdup(port_name);
942     hmap_insert(portno_names, &odp_portno_names->hmap_node,
943                 hash_odp_port(port_no));
944 }
945
946 static char *
947 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
948 {
949     struct odp_portno_names *odp_portno_names;
950
951     HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
952                              hash_odp_port(port_no), portno_names) {
953         if (odp_portno_names->port_no == port_no) {
954             return odp_portno_names->name;
955         }
956     }
957     return NULL;
958 }
959
960 void
961 odp_portno_names_destroy(struct hmap *portno_names)
962 {
963     struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
964     HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
965                         hmap_node, portno_names) {
966         hmap_remove(portno_names, &odp_portno_names->hmap_node);
967         free(odp_portno_names->name);
968         free(odp_portno_names);
969     }
970 }
971
972 static void
973 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
974                     const struct hmap *portno_names, struct ds *ds,
975                     bool verbose)
976 {
977     struct flow_tnl tun_key;
978     enum ovs_key_attr attr = nl_attr_type(a);
979     char namebuf[OVS_KEY_ATTR_BUFSIZE];
980     int expected_len;
981     bool is_exact;
982
983     is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
984
985     ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
986
987     {
988         expected_len = odp_flow_key_attr_len(nl_attr_type(a));
989         if (expected_len != -2) {
990             bool bad_key_len = nl_attr_get_size(a) != expected_len;
991             bool bad_mask_len = ma && nl_attr_get_size(a) != expected_len;
992
993             if (bad_key_len || bad_mask_len) {
994                 if (bad_key_len) {
995                     ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
996                                   nl_attr_get_size(a),
997                                   odp_flow_key_attr_len(nl_attr_type(a)));
998                 }
999                 format_generic_odp_key(a, ds);
1000                 if (bad_mask_len) {
1001                     ds_put_char(ds, '/');
1002                     ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
1003                                   nl_attr_get_size(ma),
1004                                   odp_flow_key_attr_len(nl_attr_type(ma)));
1005                 }
1006                 format_generic_odp_key(ma, ds);
1007                 ds_put_char(ds, ')');
1008                 return;
1009             }
1010         }
1011     }
1012
1013     ds_put_char(ds, '(');
1014     switch (attr) {
1015     case OVS_KEY_ATTR_ENCAP:
1016         if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
1017             odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
1018                             nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
1019                             verbose);
1020         } else if (nl_attr_get_size(a)) {
1021             odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
1022                             ds, verbose);
1023         }
1024         break;
1025
1026     case OVS_KEY_ATTR_PRIORITY:
1027     case OVS_KEY_ATTR_SKB_MARK:
1028         ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
1029         if (!is_exact) {
1030             ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1031         }
1032         break;
1033
1034     case OVS_KEY_ATTR_TUNNEL:
1035         memset(&tun_key, 0, sizeof tun_key);
1036         if (odp_tun_key_from_attr(a, &tun_key) == ODP_FIT_ERROR) {
1037             ds_put_format(ds, "error");
1038         } else if (!is_exact) {
1039             struct flow_tnl tun_mask;
1040
1041             memset(&tun_mask, 0, sizeof tun_mask);
1042             odp_tun_key_from_attr(ma, &tun_mask);
1043             ds_put_format(ds, "tun_id=%#"PRIx64"/%#"PRIx64
1044                           ",src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT
1045                           ",tos=%#"PRIx8"/%#"PRIx8",ttl=%"PRIu8"/%#"PRIx8
1046                           ",flags(",
1047                           ntohll(tun_key.tun_id), ntohll(tun_mask.tun_id),
1048                           IP_ARGS(tun_key.ip_src), IP_ARGS(tun_mask.ip_src),
1049                           IP_ARGS(tun_key.ip_dst), IP_ARGS(tun_mask.ip_dst),
1050                           tun_key.ip_tos, tun_mask.ip_tos,
1051                           tun_key.ip_ttl, tun_mask.ip_ttl);
1052
1053             format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ',');
1054
1055             /* XXX This code is correct, but enabling it would break the unit
1056                test. Disable it for now until the input parser is fixed.
1057
1058                 ds_put_char(ds, '/');
1059                 format_flags(ds, flow_tun_flag_to_string, tun_mask.flags, ',');
1060             */
1061             ds_put_char(ds, ')');
1062         } else {
1063             ds_put_format(ds, "tun_id=0x%"PRIx64",src="IP_FMT",dst="IP_FMT","
1064                           "tos=0x%"PRIx8",ttl=%"PRIu8",flags(",
1065                           ntohll(tun_key.tun_id),
1066                           IP_ARGS(tun_key.ip_src),
1067                           IP_ARGS(tun_key.ip_dst),
1068                           tun_key.ip_tos, tun_key.ip_ttl);
1069
1070             format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ',');
1071             ds_put_char(ds, ')');
1072         }
1073         break;
1074
1075     case OVS_KEY_ATTR_IN_PORT:
1076         if (portno_names && verbose && is_exact) {
1077             char *name = odp_portno_names_get(portno_names,
1078                             u32_to_odp(nl_attr_get_u32(a)));
1079             if (name) {
1080                 ds_put_format(ds, "%s", name);
1081             } else {
1082                 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1083             }
1084         } else {
1085             ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1086             if (!is_exact) {
1087                 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1088             }
1089         }
1090         break;
1091
1092     case OVS_KEY_ATTR_ETHERNET:
1093         if (!is_exact) {
1094             const struct ovs_key_ethernet *eth_mask = nl_attr_get(ma);
1095             const struct ovs_key_ethernet *eth_key = nl_attr_get(a);
1096
1097             ds_put_format(ds, "src="ETH_ADDR_FMT"/"ETH_ADDR_FMT
1098                           ",dst="ETH_ADDR_FMT"/"ETH_ADDR_FMT,
1099                           ETH_ADDR_ARGS(eth_key->eth_src),
1100                           ETH_ADDR_ARGS(eth_mask->eth_src),
1101                           ETH_ADDR_ARGS(eth_key->eth_dst),
1102                           ETH_ADDR_ARGS(eth_mask->eth_dst));
1103         } else {
1104             const struct ovs_key_ethernet *eth_key = nl_attr_get(a);
1105
1106             ds_put_format(ds, "src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT,
1107                           ETH_ADDR_ARGS(eth_key->eth_src),
1108                           ETH_ADDR_ARGS(eth_key->eth_dst));
1109         }
1110         break;
1111
1112     case OVS_KEY_ATTR_VLAN:
1113         {
1114             ovs_be16 vlan_tci = nl_attr_get_be16(a);
1115             if (!is_exact) {
1116                 ovs_be16 mask = nl_attr_get_be16(ma);
1117                 ds_put_format(ds, "vid=%"PRIu16"/0x%"PRIx16",pcp=%d/0x%x,cfi=%d/%d",
1118                               vlan_tci_to_vid(vlan_tci),
1119                               vlan_tci_to_vid(mask),
1120                               vlan_tci_to_pcp(vlan_tci),
1121                               vlan_tci_to_pcp(mask),
1122                               vlan_tci_to_cfi(vlan_tci),
1123                               vlan_tci_to_cfi(mask));
1124             } else {
1125                 format_vlan_tci(ds, vlan_tci);
1126             }
1127         }
1128         break;
1129
1130     case OVS_KEY_ATTR_MPLS: {
1131         const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
1132         const struct ovs_key_mpls *mpls_mask = NULL;
1133         size_t size = nl_attr_get_size(a);
1134
1135         if (!size || size % sizeof *mpls_key) {
1136             ds_put_format(ds, "(bad key length %"PRIuSIZE")",
1137                           nl_attr_get_size(a));
1138             return;
1139         }
1140         if (!is_exact) {
1141             mpls_mask = nl_attr_get(ma);
1142             if (nl_attr_get_size(a) != nl_attr_get_size(ma)) {
1143                 ds_put_format(ds, "(key length %"PRIuSIZE" != "
1144                               "mask length %"PRIuSIZE")",
1145                               nl_attr_get_size(a), nl_attr_get_size(ma));
1146                 return;
1147             }
1148         }
1149         format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
1150         break;
1151     }
1152
1153     case OVS_KEY_ATTR_ETHERTYPE:
1154         ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
1155         if (!is_exact) {
1156             ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
1157         }
1158         break;
1159
1160     case OVS_KEY_ATTR_IPV4:
1161         if (!is_exact) {
1162             const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a);
1163             const struct ovs_key_ipv4 *ipv4_mask = nl_attr_get(ma);
1164
1165             ds_put_format(ds, "src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT
1166                           ",proto=%"PRIu8"/%#"PRIx8",tos=%#"PRIx8"/%#"PRIx8
1167                           ",ttl=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8,
1168                           IP_ARGS(ipv4_key->ipv4_src),
1169                           IP_ARGS(ipv4_mask->ipv4_src),
1170                           IP_ARGS(ipv4_key->ipv4_dst),
1171                           IP_ARGS(ipv4_mask->ipv4_dst),
1172                           ipv4_key->ipv4_proto, ipv4_mask->ipv4_proto,
1173                           ipv4_key->ipv4_tos, ipv4_mask->ipv4_tos,
1174                           ipv4_key->ipv4_ttl, ipv4_mask->ipv4_ttl,
1175                           ovs_frag_type_to_string(ipv4_key->ipv4_frag),
1176                           ipv4_mask->ipv4_frag);
1177         } else {
1178             const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a);
1179
1180             ds_put_format(ds, "src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
1181                           ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s",
1182                           IP_ARGS(ipv4_key->ipv4_src),
1183                           IP_ARGS(ipv4_key->ipv4_dst),
1184                           ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
1185                           ipv4_key->ipv4_ttl,
1186                           ovs_frag_type_to_string(ipv4_key->ipv4_frag));
1187         }
1188         break;
1189
1190     case OVS_KEY_ATTR_IPV6:
1191         if (!is_exact) {
1192             const struct ovs_key_ipv6 *ipv6_key, *ipv6_mask;
1193             char src_str[INET6_ADDRSTRLEN];
1194             char dst_str[INET6_ADDRSTRLEN];
1195             char src_mask[INET6_ADDRSTRLEN];
1196             char dst_mask[INET6_ADDRSTRLEN];
1197
1198             ipv6_key = nl_attr_get(a);
1199             inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
1200             inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
1201
1202             ipv6_mask = nl_attr_get(ma);
1203             inet_ntop(AF_INET6, ipv6_mask->ipv6_src, src_mask, sizeof src_mask);
1204             inet_ntop(AF_INET6, ipv6_mask->ipv6_dst, dst_mask, sizeof dst_mask);
1205
1206             ds_put_format(ds, "src=%s/%s,dst=%s/%s,label=%#"PRIx32"/%#"PRIx32
1207                           ",proto=%"PRIu8"/%#"PRIx8",tclass=%#"PRIx8"/%#"PRIx8
1208                           ",hlimit=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8,
1209                           src_str, src_mask, dst_str, dst_mask,
1210                           ntohl(ipv6_key->ipv6_label),
1211                           ntohl(ipv6_mask->ipv6_label),
1212                           ipv6_key->ipv6_proto, ipv6_mask->ipv6_proto,
1213                           ipv6_key->ipv6_tclass, ipv6_mask->ipv6_tclass,
1214                           ipv6_key->ipv6_hlimit, ipv6_mask->ipv6_hlimit,
1215                           ovs_frag_type_to_string(ipv6_key->ipv6_frag),
1216                           ipv6_mask->ipv6_frag);
1217         } else {
1218             const struct ovs_key_ipv6 *ipv6_key;
1219             char src_str[INET6_ADDRSTRLEN];
1220             char dst_str[INET6_ADDRSTRLEN];
1221
1222             ipv6_key = nl_attr_get(a);
1223             inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
1224             inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
1225
1226             ds_put_format(ds, "src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
1227                           ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s",
1228                           src_str, dst_str, ntohl(ipv6_key->ipv6_label),
1229                           ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
1230                           ipv6_key->ipv6_hlimit,
1231                           ovs_frag_type_to_string(ipv6_key->ipv6_frag));
1232         }
1233         break;
1234
1235     case OVS_KEY_ATTR_TCP:
1236         if (!is_exact) {
1237             const struct ovs_key_tcp *tcp_mask = nl_attr_get(ma);
1238             const struct ovs_key_tcp *tcp_key = nl_attr_get(a);
1239
1240             ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1241                           ",dst=%"PRIu16"/%#"PRIx16,
1242                           ntohs(tcp_key->tcp_src), ntohs(tcp_mask->tcp_src),
1243                           ntohs(tcp_key->tcp_dst), ntohs(tcp_mask->tcp_dst));
1244         } else {
1245             const struct ovs_key_tcp *tcp_key = nl_attr_get(a);
1246
1247             ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
1248                           ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
1249         }
1250         break;
1251
1252     case OVS_KEY_ATTR_TCP_FLAGS:
1253         ds_put_format(ds, "0x%03"PRIx16, ntohs(nl_attr_get_be16(a)));
1254         if (!is_exact) {
1255             ds_put_format(ds, "/0x%03"PRIx16, ntohs(nl_attr_get_be16(ma)));
1256         }
1257         break;
1258
1259     case OVS_KEY_ATTR_UDP:
1260         if (!is_exact) {
1261             const struct ovs_key_udp *udp_mask = nl_attr_get(ma);
1262             const struct ovs_key_udp *udp_key = nl_attr_get(a);
1263
1264             ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1265                           ",dst=%"PRIu16"/%#"PRIx16,
1266                           ntohs(udp_key->udp_src), ntohs(udp_mask->udp_src),
1267                           ntohs(udp_key->udp_dst), ntohs(udp_mask->udp_dst));
1268         } else {
1269             const struct ovs_key_udp *udp_key = nl_attr_get(a);
1270
1271             ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
1272                           ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
1273         }
1274         break;
1275
1276     case OVS_KEY_ATTR_SCTP:
1277         if (ma) {
1278             const struct ovs_key_sctp *sctp_mask = nl_attr_get(ma);
1279             const struct ovs_key_sctp *sctp_key = nl_attr_get(a);
1280
1281             ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1282                           ",dst=%"PRIu16"/%#"PRIx16,
1283                           ntohs(sctp_key->sctp_src), ntohs(sctp_mask->sctp_src),
1284                           ntohs(sctp_key->sctp_dst), ntohs(sctp_mask->sctp_dst));
1285         } else {
1286             const struct ovs_key_sctp *sctp_key = nl_attr_get(a);
1287
1288             ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
1289                           ntohs(sctp_key->sctp_src), ntohs(sctp_key->sctp_dst));
1290         }
1291         break;
1292
1293     case OVS_KEY_ATTR_ICMP:
1294         if (!is_exact) {
1295             const struct ovs_key_icmp *icmp_mask = nl_attr_get(ma);
1296             const struct ovs_key_icmp *icmp_key = nl_attr_get(a);
1297
1298             ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8,
1299                           icmp_key->icmp_type, icmp_mask->icmp_type,
1300                           icmp_key->icmp_code, icmp_mask->icmp_code);
1301         } else {
1302             const struct ovs_key_icmp *icmp_key = nl_attr_get(a);
1303
1304             ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8,
1305                           icmp_key->icmp_type, icmp_key->icmp_code);
1306         }
1307         break;
1308
1309     case OVS_KEY_ATTR_ICMPV6:
1310         if (!is_exact) {
1311             const struct ovs_key_icmpv6 *icmpv6_mask = nl_attr_get(ma);
1312             const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a);
1313
1314             ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8,
1315                           icmpv6_key->icmpv6_type, icmpv6_mask->icmpv6_type,
1316                           icmpv6_key->icmpv6_code, icmpv6_mask->icmpv6_code);
1317         } else {
1318             const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a);
1319
1320             ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8,
1321                           icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
1322         }
1323         break;
1324
1325     case OVS_KEY_ATTR_ARP:
1326         if (!is_exact) {
1327             const struct ovs_key_arp *arp_mask = nl_attr_get(ma);
1328             const struct ovs_key_arp *arp_key = nl_attr_get(a);
1329
1330             ds_put_format(ds, "sip="IP_FMT"/"IP_FMT",tip="IP_FMT"/"IP_FMT
1331                           ",op=%"PRIu16"/%#"PRIx16
1332                           ",sha="ETH_ADDR_FMT"/"ETH_ADDR_FMT
1333                           ",tha="ETH_ADDR_FMT"/"ETH_ADDR_FMT,
1334                           IP_ARGS(arp_key->arp_sip),
1335                           IP_ARGS(arp_mask->arp_sip),
1336                           IP_ARGS(arp_key->arp_tip),
1337                           IP_ARGS(arp_mask->arp_tip),
1338                           ntohs(arp_key->arp_op), ntohs(arp_mask->arp_op),
1339                           ETH_ADDR_ARGS(arp_key->arp_sha),
1340                           ETH_ADDR_ARGS(arp_mask->arp_sha),
1341                           ETH_ADDR_ARGS(arp_key->arp_tha),
1342                           ETH_ADDR_ARGS(arp_mask->arp_tha));
1343         } else {
1344             const struct ovs_key_arp *arp_key = nl_attr_get(a);
1345
1346             ds_put_format(ds, "sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
1347                           "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT,
1348                           IP_ARGS(arp_key->arp_sip), IP_ARGS(arp_key->arp_tip),
1349                           ntohs(arp_key->arp_op),
1350                           ETH_ADDR_ARGS(arp_key->arp_sha),
1351                           ETH_ADDR_ARGS(arp_key->arp_tha));
1352         }
1353         break;
1354
1355     case OVS_KEY_ATTR_ND: {
1356         const struct ovs_key_nd *nd_key, *nd_mask = NULL;
1357         char target[INET6_ADDRSTRLEN];
1358
1359         nd_key = nl_attr_get(a);
1360         if (!is_exact) {
1361             nd_mask = nl_attr_get(ma);
1362         }
1363
1364         inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
1365         ds_put_format(ds, "target=%s", target);
1366         if (!is_exact) {
1367             inet_ntop(AF_INET6, nd_mask->nd_target, target, sizeof target);
1368             ds_put_format(ds, "/%s", target);
1369         }
1370
1371         if (!eth_addr_is_zero(nd_key->nd_sll)) {
1372             ds_put_format(ds, ",sll="ETH_ADDR_FMT,
1373                           ETH_ADDR_ARGS(nd_key->nd_sll));
1374             if (!is_exact) {
1375                 ds_put_format(ds, "/"ETH_ADDR_FMT,
1376                               ETH_ADDR_ARGS(nd_mask->nd_sll));
1377             }
1378         }
1379         if (!eth_addr_is_zero(nd_key->nd_tll)) {
1380             ds_put_format(ds, ",tll="ETH_ADDR_FMT,
1381                           ETH_ADDR_ARGS(nd_key->nd_tll));
1382             if (!is_exact) {
1383                 ds_put_format(ds, "/"ETH_ADDR_FMT,
1384                               ETH_ADDR_ARGS(nd_mask->nd_tll));
1385             }
1386         }
1387         break;
1388     }
1389
1390     case OVS_KEY_ATTR_UNSPEC:
1391     case __OVS_KEY_ATTR_MAX:
1392     default:
1393         format_generic_odp_key(a, ds);
1394         if (!is_exact) {
1395             ds_put_char(ds, '/');
1396             format_generic_odp_key(ma, ds);
1397         }
1398         break;
1399     }
1400     ds_put_char(ds, ')');
1401 }
1402
1403 static struct nlattr *
1404 generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key)
1405 {
1406     const struct nlattr *a;
1407     unsigned int left;
1408     int type = nl_attr_type(key);
1409     int size = nl_attr_get_size(key);
1410
1411     if (odp_flow_key_attr_len(type) >=0) {
1412         nl_msg_put_unspec_zero(ofp, type, size);
1413     } else {
1414         size_t nested_mask;
1415
1416         nested_mask = nl_msg_start_nested(ofp, type);
1417         NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
1418             generate_all_wildcard_mask(ofp, nl_attr_get(a));
1419         }
1420         nl_msg_end_nested(ofp, nested_mask);
1421     }
1422
1423     return ofp->base;
1424 }
1425
1426 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1427  * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
1428  * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
1429  * non-null and 'verbose' is true, translates odp port number to its name. */
1430 void
1431 odp_flow_format(const struct nlattr *key, size_t key_len,
1432                 const struct nlattr *mask, size_t mask_len,
1433                 const struct hmap *portno_names, struct ds *ds, bool verbose)
1434 {
1435     if (key_len) {
1436         const struct nlattr *a;
1437         unsigned int left;
1438         bool has_ethtype_key = false;
1439         const struct nlattr *ma = NULL;
1440         struct ofpbuf ofp;
1441         bool first_field = true;
1442
1443         ofpbuf_init(&ofp, 100);
1444         NL_ATTR_FOR_EACH (a, left, key, key_len) {
1445             bool is_nested_attr;
1446             bool is_wildcard = false;
1447             int attr_type = nl_attr_type(a);
1448
1449             if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
1450                 has_ethtype_key = true;
1451             }
1452
1453             is_nested_attr = (odp_flow_key_attr_len(attr_type) == -2);
1454
1455             if (mask && mask_len) {
1456                 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
1457                 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
1458             }
1459
1460             if (verbose || !is_wildcard  || is_nested_attr) {
1461                 if (is_wildcard && !ma) {
1462                     ma = generate_all_wildcard_mask(&ofp, a);
1463                 }
1464                 if (!first_field) {
1465                     ds_put_char(ds, ',');
1466                 }
1467                 format_odp_key_attr(a, ma, portno_names, ds, verbose);
1468                 first_field = false;
1469             }
1470             ofpbuf_clear(&ofp);
1471         }
1472         ofpbuf_uninit(&ofp);
1473
1474         if (left) {
1475             int i;
1476
1477             if (left == key_len) {
1478                 ds_put_cstr(ds, "<empty>");
1479             }
1480             ds_put_format(ds, ",***%u leftover bytes*** (", left);
1481             for (i = 0; i < left; i++) {
1482                 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
1483             }
1484             ds_put_char(ds, ')');
1485         }
1486         if (!has_ethtype_key) {
1487             ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
1488             if (ma) {
1489                 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
1490                               ntohs(nl_attr_get_be16(ma)));
1491             }
1492         }
1493     } else {
1494         ds_put_cstr(ds, "<empty>");
1495     }
1496 }
1497
1498 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1499  * OVS_KEY_ATTR_* attributes in 'key'. */
1500 void
1501 odp_flow_key_format(const struct nlattr *key,
1502                     size_t key_len, struct ds *ds)
1503 {
1504     odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
1505 }
1506
1507 static void
1508 put_nd(struct ovs_key_nd* nd_key, const uint8_t *nd_sll,
1509        const uint8_t *nd_tll, struct ofpbuf *key)
1510 {
1511     if (nd_sll) {
1512         memcpy(nd_key->nd_sll, nd_sll, ETH_ADDR_LEN);
1513     }
1514
1515     if (nd_tll) {
1516         memcpy(nd_key->nd_tll, nd_tll, ETH_ADDR_LEN);
1517     }
1518
1519     nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, nd_key, sizeof *nd_key);
1520 }
1521
1522 static int
1523 put_nd_key(int n, const char *nd_target_s, const uint8_t *nd_sll,
1524            const uint8_t *nd_tll, struct ofpbuf *key)
1525 {
1526     struct ovs_key_nd nd_key;
1527
1528     memset(&nd_key, 0, sizeof nd_key);
1529
1530     if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
1531         return -EINVAL;
1532     }
1533
1534     put_nd(&nd_key, nd_sll, nd_tll, key);
1535     return n;
1536 }
1537
1538 static int
1539 put_nd_mask(int n, const char *nd_target_s,
1540            const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *mask)
1541 {
1542     struct ovs_key_nd nd_mask;
1543
1544     memset(&nd_mask, 0xff, sizeof nd_mask);
1545
1546     if (strlen(nd_target_s) != 0 &&
1547             inet_pton(AF_INET6, nd_target_s, nd_mask.nd_target) != 1) {
1548         return -EINVAL;
1549     }
1550
1551     put_nd(&nd_mask, nd_sll, nd_tll, mask);
1552     return n;
1553 }
1554
1555 static bool
1556 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
1557 {
1558     if (!strcasecmp(s, "no")) {
1559         *type = OVS_FRAG_TYPE_NONE;
1560     } else if (!strcasecmp(s, "first")) {
1561         *type = OVS_FRAG_TYPE_FIRST;
1562     } else if (!strcasecmp(s, "later")) {
1563         *type = OVS_FRAG_TYPE_LATER;
1564     } else {
1565         return false;
1566     }
1567     return true;
1568 }
1569
1570 static ovs_be32
1571 mpls_lse_from_components(int mpls_label, int mpls_tc, int mpls_ttl, int mpls_bos)
1572 {
1573     return (htonl((mpls_label << MPLS_LABEL_SHIFT) |
1574                   (mpls_tc << MPLS_TC_SHIFT)       |
1575                   (mpls_ttl << MPLS_TTL_SHIFT)     |
1576                   (mpls_bos << MPLS_BOS_SHIFT)));
1577 }
1578
1579 static int
1580 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
1581                         struct ofpbuf *key, struct ofpbuf *mask)
1582 {
1583     {
1584         uint32_t priority;
1585         uint32_t priority_mask;
1586         int n = -1;
1587
1588         if (mask && ovs_scan(s, "skb_priority(%"SCNi32"/%"SCNi32")%n",
1589                              &priority, &priority_mask, &n)) {
1590             nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1591             nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, priority_mask);
1592             return n;
1593         } else if (ovs_scan(s, "skb_priority(%"SCNi32")%n", &priority, &n)) {
1594             nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1595             if (mask) {
1596                 nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, UINT32_MAX);
1597             }
1598             return n;
1599         }
1600     }
1601
1602     {
1603         uint32_t mark;
1604         uint32_t mark_mask;
1605         int n = -1;
1606
1607         if (mask && ovs_scan(s, "skb_mark(%"SCNi32"/%"SCNi32")%n", &mark,
1608                              &mark_mask, &n)) {
1609             nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1610             nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, mark_mask);
1611             return n;
1612         } else if (ovs_scan(s, "skb_mark(%"SCNi32")%n", &mark, &n)) {
1613             nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1614             if (mask) {
1615                 nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, UINT32_MAX);
1616             }
1617             return n;
1618         }
1619     }
1620
1621     {
1622         uint64_t tun_id, tun_id_mask;
1623         struct flow_tnl tun_key, tun_key_mask;
1624         int n = -1;
1625
1626         if (mask && ovs_scan(s, "tunnel(tun_id=%"SCNi64"/%"SCNi64","
1627                              "src="IP_SCAN_FMT"/"IP_SCAN_FMT",dst="IP_SCAN_FMT
1628                              "/"IP_SCAN_FMT",tos=%"SCNi8"/%"SCNi8","
1629                              "ttl=%"SCNi8"/%"SCNi8",flags%n",
1630                              &tun_id, &tun_id_mask,
1631                              IP_SCAN_ARGS(&tun_key.ip_src),
1632                              IP_SCAN_ARGS(&tun_key_mask.ip_src),
1633                              IP_SCAN_ARGS(&tun_key.ip_dst),
1634                              IP_SCAN_ARGS(&tun_key_mask.ip_dst),
1635                              &tun_key.ip_tos, &tun_key_mask.ip_tos,
1636                              &tun_key.ip_ttl, &tun_key_mask.ip_ttl, &n)) {
1637             int res;
1638             uint32_t flags;
1639
1640             tun_key.tun_id = htonll(tun_id);
1641             tun_key_mask.tun_id = htonll(tun_id_mask);
1642             res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1643             tun_key.flags = flags;
1644             tun_key_mask.flags = UINT16_MAX;
1645
1646             if (res < 0) {
1647                 return res;
1648             }
1649             n += res;
1650             if (s[n] != ')') {
1651                 return -EINVAL;
1652             }
1653             n++;
1654             tun_key_to_attr(key, &tun_key);
1655             if (mask) {
1656                 tun_key_to_attr(mask, &tun_key_mask);
1657             }
1658             return n;
1659         } else if (ovs_scan(s, "tunnel(tun_id=%"SCNi64","
1660                             "src="IP_SCAN_FMT",dst="IP_SCAN_FMT
1661                             ",tos=%"SCNi8",ttl=%"SCNi8",flags%n", &tun_id,
1662                             IP_SCAN_ARGS(&tun_key.ip_src),
1663                             IP_SCAN_ARGS(&tun_key.ip_dst),
1664                             &tun_key.ip_tos, &tun_key.ip_ttl, &n)) {
1665             int res;
1666             uint32_t flags;
1667
1668             tun_key.tun_id = htonll(tun_id);
1669             res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1670             tun_key.flags = flags;
1671
1672             if (res < 0) {
1673                 return res;
1674             }
1675             n += res;
1676             if (s[n] != ')') {
1677                 return -EINVAL;
1678             }
1679             n++;
1680             tun_key_to_attr(key, &tun_key);
1681
1682             if (mask) {
1683                 memset(&tun_key, 0xff, sizeof tun_key);
1684                 tun_key_to_attr(mask, &tun_key);
1685             }
1686             return n;
1687         }
1688     }
1689
1690     {
1691         uint32_t in_port;
1692         uint32_t in_port_mask;
1693         int n = -1;
1694
1695         if (mask && ovs_scan(s, "in_port(%"SCNi32"/%"SCNi32")%n",
1696                              &in_port, &in_port_mask, &n)) {
1697             nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1698             nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, in_port_mask);
1699             return n;
1700         } else if (ovs_scan(s, "in_port(%"SCNi32")%n", &in_port, &n)) {
1701             nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1702             if (mask) {
1703                 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX);
1704             }
1705             return n;
1706         }
1707     }
1708
1709
1710     if (port_names && !strncmp(s, "in_port(", 8)) {
1711         const char *name;
1712         const struct simap_node *node;
1713         int name_len;
1714
1715         name = s + 8;
1716         name_len = strcspn(name, ")");
1717         node = simap_find_len(port_names, name, name_len);
1718         if (node) {
1719             nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, node->data);
1720
1721             if (mask) {
1722                 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX);
1723             }
1724             return 8 + name_len + 1;
1725         }
1726     }
1727
1728     {
1729         struct ovs_key_ethernet eth_key;
1730         struct ovs_key_ethernet eth_key_mask;
1731         int n = -1;
1732
1733         if (mask && ovs_scan(s,
1734                              "eth(src="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
1735                              "dst="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
1736                              ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1737                              ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_src),
1738                              ETH_ADDR_SCAN_ARGS(eth_key.eth_dst),
1739                              ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_dst), &n)) {
1740             nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1741                               &eth_key, sizeof eth_key);
1742             nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET,
1743                               &eth_key_mask, sizeof eth_key_mask);
1744             return n;
1745         } else if (ovs_scan(s, "eth(src="ETH_ADDR_SCAN_FMT","
1746                             "dst="ETH_ADDR_SCAN_FMT")%n",
1747                             ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1748                             ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n)) {
1749             nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1750                               &eth_key, sizeof eth_key);
1751
1752             if (mask) {
1753                 memset(&eth_key, 0xff, sizeof eth_key);
1754                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET,
1755                               &eth_key, sizeof eth_key);
1756             }
1757             return n;
1758         }
1759     }
1760
1761     {
1762         int vid, vid_mask;
1763         int pcp, pcp_mask;
1764         int cfi, cfi_mask;
1765         int n = -1;
1766
1767         if (mask && ovs_scan(s, "vlan(vid=%i/%i,pcp=%i/%i)%n",
1768                             &vid, &vid_mask, &pcp, &pcp_mask, &n)) {
1769             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1770                             htons((vid << VLAN_VID_SHIFT) |
1771                                   (pcp << VLAN_PCP_SHIFT) |
1772                                   VLAN_CFI));
1773             nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN,
1774                             htons((vid_mask << VLAN_VID_SHIFT) |
1775                                   (pcp_mask << VLAN_PCP_SHIFT) |
1776                                   (1 << VLAN_CFI_SHIFT)));
1777             return n;
1778         } else if (ovs_scan(s, "vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)) {
1779             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1780                             htons((vid << VLAN_VID_SHIFT) |
1781                                   (pcp << VLAN_PCP_SHIFT) |
1782                                   VLAN_CFI));
1783             if (mask) {
1784                 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, OVS_BE16_MAX);
1785             }
1786             return n;
1787         } else if (mask
1788                    && ovs_scan(s, "vlan(vid=%i/%i,pcp=%i/%i,cfi=%i/%i)%n",
1789                                &vid, &vid_mask, &pcp, &pcp_mask,
1790                                &cfi, &cfi_mask, &n)) {
1791             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1792                             htons((vid << VLAN_VID_SHIFT) |
1793                                   (pcp << VLAN_PCP_SHIFT) |
1794                                   (cfi ? VLAN_CFI : 0)));
1795             nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN,
1796                             htons((vid_mask << VLAN_VID_SHIFT) |
1797                                   (pcp_mask << VLAN_PCP_SHIFT) |
1798                                   (cfi_mask << VLAN_CFI_SHIFT)));
1799             return n;
1800         } else if (ovs_scan(s, "vlan(vid=%i,pcp=%i,cfi=%i)%n",
1801                             &vid, &pcp, &cfi, &n)) {
1802             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1803                             htons((vid << VLAN_VID_SHIFT) |
1804                                   (pcp << VLAN_PCP_SHIFT) |
1805                                   (cfi ? VLAN_CFI : 0)));
1806             if (mask) {
1807                 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, OVS_BE16_MAX);
1808             }
1809             return n;
1810         }
1811     }
1812
1813     {
1814         int eth_type;
1815         int eth_type_mask;
1816         int n = -1;
1817
1818         if (mask && ovs_scan(s, "eth_type(%i/%i)%n",
1819                              &eth_type, &eth_type_mask, &n)) {
1820             if (eth_type != 0) {
1821                 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1822             }
1823             nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type_mask));
1824             return n;
1825         } else if (ovs_scan(s, "eth_type(%i)%n", &eth_type, &n)) {
1826             nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1827             if (mask) {
1828                 nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
1829             }
1830             return n;
1831         }
1832     }
1833
1834     {
1835         int label, tc, ttl, bos;
1836         int label_mask, tc_mask, ttl_mask, bos_mask;
1837         int n = -1;
1838
1839         if (mask && ovs_scan(s, "mpls(label=%i/%i,tc=%i/%i,"
1840                              "ttl=%i/%i,bos=%i/%i)%n",
1841                              &label, &label_mask, &tc, &tc_mask,
1842                              &ttl, &ttl_mask, &bos, &bos_mask, &n)) {
1843             struct ovs_key_mpls *mpls, *mpls_mask;
1844
1845             mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1846                                             sizeof *mpls);
1847             mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos);
1848
1849             mpls_mask = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
1850                                             sizeof *mpls_mask);
1851             mpls_mask->mpls_lse = mpls_lse_from_components(
1852                                   label_mask, tc_mask, ttl_mask, bos_mask);
1853             return n;
1854         } else if (ovs_scan(s, "mpls(label=%i,tc=%i,ttl=%i,bos=%i)%n",
1855                             &label, &tc, &ttl, &bos, &n)) {
1856             struct ovs_key_mpls *mpls;
1857
1858             mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1859                                             sizeof *mpls);
1860             mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos);
1861             if (mask) {
1862                 mpls = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
1863                                             sizeof *mpls);
1864                 mpls->mpls_lse = OVS_BE32_MAX;
1865             }
1866             return n;
1867         }
1868     }
1869
1870
1871     {
1872         struct ovs_key_ipv4 ipv4_key;
1873         struct ovs_key_ipv4 ipv4_mask;
1874
1875         char frag[8];
1876         enum ovs_frag_type ipv4_frag;
1877         int n = -1;
1878
1879         if (mask
1880             && ovs_scan(s, "ipv4(src="IP_SCAN_FMT"/"IP_SCAN_FMT","
1881                         "dst="IP_SCAN_FMT"/"IP_SCAN_FMT","
1882                         "proto=%"SCNi8"/%"SCNi8","
1883                         "tos=%"SCNi8"/%"SCNi8","
1884                         "ttl=%"SCNi8"/%"SCNi8","
1885                         "frag=%7[a-z]/%"SCNi8")%n",
1886                         IP_SCAN_ARGS(&ipv4_key.ipv4_src),
1887                         IP_SCAN_ARGS(&ipv4_mask.ipv4_src),
1888                         IP_SCAN_ARGS(&ipv4_key.ipv4_dst),
1889                         IP_SCAN_ARGS(&ipv4_mask.ipv4_dst),
1890                         &ipv4_key.ipv4_proto, &ipv4_mask.ipv4_proto,
1891                         &ipv4_key.ipv4_tos, &ipv4_mask.ipv4_tos,
1892                         &ipv4_key.ipv4_ttl, &ipv4_mask.ipv4_ttl,
1893                         frag, &ipv4_mask.ipv4_frag, &n)
1894             && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1895             ipv4_key.ipv4_frag = ipv4_frag;
1896             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1897                               &ipv4_key, sizeof ipv4_key);
1898
1899             nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
1900                               &ipv4_mask, sizeof ipv4_mask);
1901             return n;
1902         } else if (ovs_scan(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
1903                             "proto=%"SCNi8",tos=%"SCNi8",ttl=%"SCNi8","
1904                             "frag=%7[a-z])%n",
1905                             IP_SCAN_ARGS(&ipv4_key.ipv4_src),
1906                             IP_SCAN_ARGS(&ipv4_key.ipv4_dst),
1907                             &ipv4_key.ipv4_proto,
1908                             &ipv4_key.ipv4_tos,
1909                             &ipv4_key.ipv4_ttl,
1910                             frag, &n) > 0
1911                    && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1912             ipv4_key.ipv4_frag = ipv4_frag;
1913             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1914                               &ipv4_key, sizeof ipv4_key);
1915
1916             if (mask) {
1917                 memset(&ipv4_key, 0xff, sizeof ipv4_key);
1918                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
1919                               &ipv4_key, sizeof ipv4_key);
1920             }
1921             return n;
1922         }
1923     }
1924
1925     {
1926         char ipv6_src_s[IPV6_SCAN_LEN + 1];
1927         char ipv6_src_mask_s[IPV6_SCAN_LEN + 1];
1928         char ipv6_dst_s[IPV6_SCAN_LEN + 1];
1929         char ipv6_dst_mask_s[IPV6_SCAN_LEN + 1];
1930         int ipv6_label, ipv6_label_mask;
1931         int ipv6_proto, ipv6_proto_mask;
1932         int ipv6_tclass, ipv6_tclass_mask;
1933         int ipv6_hlimit, ipv6_hlimit_mask;
1934         char frag[8];
1935         enum ovs_frag_type ipv6_frag;
1936         int ipv6_frag_mask;
1937         int n = -1;
1938
1939         if (mask && ovs_scan(s, "ipv6(src="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT",dst="
1940                              IPV6_SCAN_FMT"/"IPV6_SCAN_FMT","
1941                              "label=%i/%i,proto=%i/%i,tclass=%i/%i,"
1942                              "hlimit=%i/%i,frag=%7[a-z]/%i)%n",
1943                              ipv6_src_s, ipv6_src_mask_s,
1944                              ipv6_dst_s, ipv6_dst_mask_s,
1945                              &ipv6_label, &ipv6_label_mask, &ipv6_proto,
1946                              &ipv6_proto_mask, &ipv6_tclass, &ipv6_tclass_mask,
1947                              &ipv6_hlimit, &ipv6_hlimit_mask, frag,
1948                              &ipv6_frag_mask, &n)
1949             && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1950             struct ovs_key_ipv6 ipv6_key;
1951             struct ovs_key_ipv6 ipv6_mask;
1952
1953             if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1954                 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1 ||
1955                 inet_pton(AF_INET6, ipv6_src_mask_s, &ipv6_mask.ipv6_src) != 1 ||
1956                 inet_pton(AF_INET6, ipv6_dst_mask_s, &ipv6_mask.ipv6_dst) != 1) {
1957                 return -EINVAL;
1958             }
1959
1960             ipv6_key.ipv6_label = htonl(ipv6_label);
1961             ipv6_key.ipv6_proto = ipv6_proto;
1962             ipv6_key.ipv6_tclass = ipv6_tclass;
1963             ipv6_key.ipv6_hlimit = ipv6_hlimit;
1964             ipv6_key.ipv6_frag = ipv6_frag;
1965             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1966                               &ipv6_key, sizeof ipv6_key);
1967
1968             ipv6_mask.ipv6_label = htonl(ipv6_label_mask);
1969             ipv6_mask.ipv6_proto = ipv6_proto_mask;
1970             ipv6_mask.ipv6_tclass = ipv6_tclass_mask;
1971             ipv6_mask.ipv6_hlimit = ipv6_hlimit_mask;
1972             ipv6_mask.ipv6_frag = ipv6_frag_mask;
1973             nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
1974                               &ipv6_mask, sizeof ipv6_mask);
1975             return n;
1976         } else if (ovs_scan(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
1977                             "label=%i,proto=%i,tclass=%i,hlimit=%i,"
1978                             "frag=%7[a-z])%n",
1979                             ipv6_src_s, ipv6_dst_s, &ipv6_label,
1980                             &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n)
1981                    && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1982             struct ovs_key_ipv6 ipv6_key;
1983
1984             if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1985                 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
1986                 return -EINVAL;
1987             }
1988             ipv6_key.ipv6_label = htonl(ipv6_label);
1989             ipv6_key.ipv6_proto = ipv6_proto;
1990             ipv6_key.ipv6_tclass = ipv6_tclass;
1991             ipv6_key.ipv6_hlimit = ipv6_hlimit;
1992             ipv6_key.ipv6_frag = ipv6_frag;
1993             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1994                               &ipv6_key, sizeof ipv6_key);
1995
1996             if (mask) {
1997                 memset(&ipv6_key, 0xff, sizeof ipv6_key);
1998                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
1999                               &ipv6_key, sizeof ipv6_key);
2000             }
2001             return n;
2002         }
2003     }
2004
2005     {
2006         int tcp_src;
2007         int tcp_dst;
2008         int tcp_src_mask;
2009         int tcp_dst_mask;
2010         int n = -1;
2011
2012         if (mask && ovs_scan(s, "tcp(src=%i/%i,dst=%i/%i)%n",
2013                              &tcp_src, &tcp_src_mask, &tcp_dst,
2014                              &tcp_dst_mask, &n)) {
2015             struct ovs_key_tcp tcp_key;
2016             struct ovs_key_tcp tcp_mask;
2017
2018             tcp_key.tcp_src = htons(tcp_src);
2019             tcp_key.tcp_dst = htons(tcp_dst);
2020             nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
2021
2022             tcp_mask.tcp_src = htons(tcp_src_mask);
2023             tcp_mask.tcp_dst = htons(tcp_dst_mask);
2024             nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
2025                               &tcp_mask, sizeof tcp_mask);
2026             return n;
2027         } else if (ovs_scan(s, "tcp(src=%i,dst=%i)%n",
2028                             &tcp_src, &tcp_dst, &n)) {
2029             struct ovs_key_tcp tcp_key;
2030
2031             tcp_key.tcp_src = htons(tcp_src);
2032             tcp_key.tcp_dst = htons(tcp_dst);
2033             nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
2034
2035             if (mask) {
2036                 memset(&tcp_key, 0xff, sizeof tcp_key);
2037                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
2038                               &tcp_key, sizeof tcp_key);
2039             }
2040             return n;
2041         }
2042     }
2043
2044     {
2045         uint16_t tcp_flags, tcp_flags_mask;
2046         int n = -1;
2047
2048         if (mask && ovs_scan(s, "tcp_flags(%"SCNi16"/%"SCNi16")%n",
2049                              &tcp_flags, &tcp_flags_mask, &n) > 0 && n > 0) {
2050             nl_msg_put_be16(key, OVS_KEY_ATTR_TCP_FLAGS, htons(tcp_flags));
2051             nl_msg_put_be16(mask, OVS_KEY_ATTR_TCP_FLAGS, htons(tcp_flags_mask));
2052             return n;
2053         } else if (ovs_scan(s, "tcp_flags(%"SCNi16")%n", &tcp_flags, &n)) {
2054             nl_msg_put_be16(key, OVS_KEY_ATTR_TCP_FLAGS, htons(tcp_flags));
2055             if (mask) {
2056                 nl_msg_put_be16(mask, OVS_KEY_ATTR_TCP_FLAGS,
2057                                 htons(UINT16_MAX));
2058             }
2059             return n;
2060         }
2061     }
2062
2063     {
2064         int udp_src;
2065         int udp_dst;
2066         int udp_src_mask;
2067         int udp_dst_mask;
2068         int n = -1;
2069
2070         if (mask && ovs_scan(s, "udp(src=%i/%i,dst=%i/%i)%n",
2071                              &udp_src, &udp_src_mask,
2072                              &udp_dst, &udp_dst_mask, &n)) {
2073             struct ovs_key_udp udp_key;
2074             struct ovs_key_udp udp_mask;
2075
2076             udp_key.udp_src = htons(udp_src);
2077             udp_key.udp_dst = htons(udp_dst);
2078             nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2079
2080             udp_mask.udp_src = htons(udp_src_mask);
2081             udp_mask.udp_dst = htons(udp_dst_mask);
2082             nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP,
2083                               &udp_mask, sizeof udp_mask);
2084             return n;
2085         }
2086         if (ovs_scan(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n)) {
2087             struct ovs_key_udp udp_key;
2088
2089             udp_key.udp_src = htons(udp_src);
2090             udp_key.udp_dst = htons(udp_dst);
2091             nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2092
2093             if (mask) {
2094                 memset(&udp_key, 0xff, sizeof udp_key);
2095                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2096             }
2097             return n;
2098         }
2099     }
2100
2101     {
2102         int sctp_src;
2103         int sctp_dst;
2104         int sctp_src_mask;
2105         int sctp_dst_mask;
2106         int n = -1;
2107
2108         if (mask && ovs_scan(s, "sctp(src=%i/%i,dst=%i/%i)%n",
2109                              &sctp_src, &sctp_src_mask,
2110                              &sctp_dst, &sctp_dst_mask, &n)) {
2111             struct ovs_key_sctp sctp_key;
2112             struct ovs_key_sctp sctp_mask;
2113
2114             sctp_key.sctp_src = htons(sctp_src);
2115             sctp_key.sctp_dst = htons(sctp_dst);
2116             nl_msg_put_unspec(key, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key);
2117
2118             sctp_mask.sctp_src = htons(sctp_src_mask);
2119             sctp_mask.sctp_dst = htons(sctp_dst_mask);
2120             nl_msg_put_unspec(mask, OVS_KEY_ATTR_SCTP,
2121                               &sctp_mask, sizeof sctp_mask);
2122             return n;
2123         }
2124         if (ovs_scan(s, "sctp(src=%i,dst=%i)%n", &sctp_src, &sctp_dst, &n)) {
2125             struct ovs_key_sctp sctp_key;
2126
2127             sctp_key.sctp_src = htons(sctp_src);
2128             sctp_key.sctp_dst = htons(sctp_dst);
2129             nl_msg_put_unspec(key, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key);
2130
2131             if (mask) {
2132                 memset(&sctp_key, 0xff, sizeof sctp_key);
2133                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key);
2134             }
2135             return n;
2136         }
2137     }
2138
2139     {
2140         struct ovs_key_icmp icmp_key;
2141         struct ovs_key_icmp icmp_mask;
2142         int n = -1;
2143
2144         if (mask && ovs_scan(s, "icmp(type=%"SCNi8"/%"SCNi8","
2145                              "code=%"SCNi8"/%"SCNi8")%n",
2146                    &icmp_key.icmp_type, &icmp_mask.icmp_type,
2147                    &icmp_key.icmp_code, &icmp_mask.icmp_code, &n)) {
2148             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
2149                               &icmp_key, sizeof icmp_key);
2150             nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP,
2151                               &icmp_mask, sizeof icmp_mask);
2152             return n;
2153         } else if (ovs_scan(s, "icmp(type=%"SCNi8",code=%"SCNi8")%n",
2154                             &icmp_key.icmp_type, &icmp_key.icmp_code, &n)) {
2155             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
2156                               &icmp_key, sizeof icmp_key);
2157             if (mask) {
2158                 memset(&icmp_key, 0xff, sizeof icmp_key);
2159                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP, &icmp_key,
2160                               sizeof icmp_key);
2161             }
2162             return n;
2163         }
2164     }
2165
2166     {
2167         struct ovs_key_icmpv6 icmpv6_key;
2168         struct ovs_key_icmpv6 icmpv6_mask;
2169         int n = -1;
2170
2171         if (mask && ovs_scan(s, "icmpv6(type=%"SCNi8"/%"SCNi8","
2172                              "code=%"SCNi8"/%"SCNi8")%n",
2173                              &icmpv6_key.icmpv6_type, &icmpv6_mask.icmpv6_type,
2174                              &icmpv6_key.icmpv6_code, &icmpv6_mask.icmpv6_code,
2175                              &n)) {
2176             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
2177                               &icmpv6_key, sizeof icmpv6_key);
2178             nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_mask,
2179                               sizeof icmpv6_mask);
2180             return n;
2181         } else if (ovs_scan(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
2182                             &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,
2183                             &n)) {
2184             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
2185                               &icmpv6_key, sizeof icmpv6_key);
2186
2187             if (mask) {
2188                 memset(&icmpv6_key, 0xff, sizeof icmpv6_key);
2189                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_key,
2190                               sizeof icmpv6_key);
2191             }
2192             return n;
2193         }
2194     }
2195
2196     {
2197         struct ovs_key_arp arp_key;
2198         struct ovs_key_arp arp_mask;
2199         uint16_t arp_op, arp_op_mask;
2200         int n = -1;
2201
2202         if (mask && ovs_scan(s, "arp(sip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2203                              "tip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2204                              "op=%"SCNi16"/%"SCNi16","
2205                              "sha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2206                              "tha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2207                              IP_SCAN_ARGS(&arp_key.arp_sip),
2208                              IP_SCAN_ARGS(&arp_mask.arp_sip),
2209                              IP_SCAN_ARGS(&arp_key.arp_tip),
2210                              IP_SCAN_ARGS(&arp_mask.arp_tip),
2211                              &arp_op, &arp_op_mask,
2212                              ETH_ADDR_SCAN_ARGS(arp_key.arp_sha),
2213                              ETH_ADDR_SCAN_ARGS(arp_mask.arp_sha),
2214                              ETH_ADDR_SCAN_ARGS(arp_key.arp_tha),
2215                              ETH_ADDR_SCAN_ARGS(arp_mask.arp_tha), &n)) {
2216             arp_key.arp_op = htons(arp_op);
2217             nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2218             arp_mask.arp_op = htons(arp_op_mask);
2219             nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2220                               &arp_mask, sizeof arp_mask);
2221             return n;
2222         } else if (ovs_scan(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
2223                             "op=%"SCNi16",sha="ETH_ADDR_SCAN_FMT","
2224                             "tha="ETH_ADDR_SCAN_FMT")%n",
2225                             IP_SCAN_ARGS(&arp_key.arp_sip),
2226                             IP_SCAN_ARGS(&arp_key.arp_tip),
2227                             &arp_op,
2228                             ETH_ADDR_SCAN_ARGS(arp_key.arp_sha),
2229                             ETH_ADDR_SCAN_ARGS(arp_key.arp_tha), &n)) {
2230             arp_key.arp_op = htons(arp_op);
2231             nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2232
2233             if (mask) {
2234                 memset(&arp_key, 0xff, sizeof arp_key);
2235                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2236                                   &arp_key, sizeof arp_key);
2237             }
2238             return n;
2239         }
2240     }
2241
2242     {
2243         char nd_target_s[IPV6_SCAN_LEN + 1];
2244         char nd_target_mask_s[IPV6_SCAN_LEN + 1];
2245         uint8_t nd_sll[ETH_ADDR_LEN];
2246         uint8_t nd_sll_mask[ETH_ADDR_LEN];
2247         uint8_t nd_tll[ETH_ADDR_LEN];
2248         uint8_t nd_tll_mask[ETH_ADDR_LEN];
2249         int n = -1;
2250
2251         nd_target_mask_s[0] = 0;
2252         memset(nd_sll_mask, 0xff, sizeof nd_sll_mask);
2253         memset(nd_tll_mask, 0xff, sizeof nd_tll_mask);
2254
2255         if (mask && ovs_scan(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT")%n",
2256                              nd_target_s, nd_target_mask_s, &n)) {
2257                 put_nd_key(n, nd_target_s, NULL, NULL, key);
2258                 put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask);
2259         } else if (ovs_scan(s, "nd(target="IPV6_SCAN_FMT")%n",
2260                             nd_target_s, &n)) {
2261                 put_nd_key(n, nd_target_s, NULL, NULL, key);
2262                 if (mask) {
2263                     put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask);
2264                 }
2265         } else if (mask &&
2266                    ovs_scan(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2267                             ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2268                             nd_target_s, nd_target_mask_s,
2269                             ETH_ADDR_SCAN_ARGS(nd_sll),
2270                             ETH_ADDR_SCAN_ARGS(nd_sll_mask), &n)) {
2271             put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2272             put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2273         } else if (ovs_scan(s, "nd(target="IPV6_SCAN_FMT","
2274                             "sll="ETH_ADDR_SCAN_FMT")%n",
2275                             nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n)) {
2276             put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2277             if (mask) {
2278                 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2279             }
2280         } else if (mask &&
2281                    ovs_scan(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2282                             ",tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2283                             nd_target_s, nd_target_mask_s,
2284                             ETH_ADDR_SCAN_ARGS(nd_tll),
2285                             ETH_ADDR_SCAN_ARGS(nd_tll_mask), &n)) {
2286             put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2287             put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2288         } else if (ovs_scan(s, "nd(target="IPV6_SCAN_FMT","
2289                             "tll="ETH_ADDR_SCAN_FMT")%n",
2290                             nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n)) {
2291             put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2292             if (mask) {
2293                 put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2294             }
2295         } else if (mask &&
2296                    ovs_scan(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2297                             ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2298                             "tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2299                             nd_target_s, nd_target_mask_s,
2300                             ETH_ADDR_SCAN_ARGS(nd_sll),
2301                             ETH_ADDR_SCAN_ARGS(nd_sll_mask),
2302                             ETH_ADDR_SCAN_ARGS(nd_tll),
2303                             ETH_ADDR_SCAN_ARGS(nd_tll_mask),
2304                    &n)) {
2305             put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2306             put_nd_mask(n, nd_target_mask_s, nd_sll_mask, nd_tll_mask, mask);
2307         } else if (ovs_scan(s, "nd(target="IPV6_SCAN_FMT","
2308                             "sll="ETH_ADDR_SCAN_FMT","
2309                             "tll="ETH_ADDR_SCAN_FMT")%n",
2310                             nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
2311                             ETH_ADDR_SCAN_ARGS(nd_tll), &n)) {
2312             put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2313             if (mask) {
2314                 put_nd_mask(n, nd_target_mask_s,
2315                             nd_sll_mask, nd_tll_mask, mask);
2316             }
2317         }
2318
2319         if (n != -1)
2320             return n;
2321
2322     }
2323
2324     if (!strncmp(s, "encap(", 6)) {
2325         const char *start = s;
2326         size_t encap, encap_mask = 0;
2327
2328         encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
2329         if (mask) {
2330             encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
2331         }
2332
2333         s += 6;
2334         for (;;) {
2335             int retval;
2336
2337             s += strspn(s, ", \t\r\n");
2338             if (!*s) {
2339                 return -EINVAL;
2340             } else if (*s == ')') {
2341                 break;
2342             }
2343
2344             retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2345             if (retval < 0) {
2346                 return retval;
2347             }
2348             s += retval;
2349         }
2350         s++;
2351
2352         nl_msg_end_nested(key, encap);
2353         if (mask) {
2354             nl_msg_end_nested(mask, encap_mask);
2355         }
2356
2357         return s - start;
2358     }
2359
2360     return -EINVAL;
2361 }
2362
2363 /* Parses the string representation of a datapath flow key, in the
2364  * format output by odp_flow_key_format().  Returns 0 if successful,
2365  * otherwise a positive errno value.  On success, the flow key is
2366  * appended to 'key' as a series of Netlink attributes.  On failure, no
2367  * data is appended to 'key'.  Either way, 'key''s data might be
2368  * reallocated.
2369  *
2370  * If 'port_names' is nonnull, it points to an simap that maps from a port name
2371  * to a port number.  (Port names may be used instead of port numbers in
2372  * in_port.)
2373  *
2374  * On success, the attributes appended to 'key' are individually syntactically
2375  * valid, but they may not be valid as a sequence.  'key' might, for example,
2376  * have duplicated keys.  odp_flow_key_to_flow() will detect those errors. */
2377 int
2378 odp_flow_from_string(const char *s, const struct simap *port_names,
2379                      struct ofpbuf *key, struct ofpbuf *mask)
2380 {
2381     const size_t old_size = key->size;
2382     for (;;) {
2383         int retval;
2384
2385         s += strspn(s, delimiters);
2386         if (!*s) {
2387             return 0;
2388         }
2389
2390         retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2391         if (retval < 0) {
2392             key->size = old_size;
2393             return -retval;
2394         }
2395         s += retval;
2396     }
2397
2398     return 0;
2399 }
2400
2401 static uint8_t
2402 ovs_to_odp_frag(uint8_t nw_frag)
2403 {
2404     return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
2405           : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
2406           : OVS_FRAG_TYPE_LATER);
2407 }
2408
2409 static uint8_t
2410 ovs_to_odp_frag_mask(uint8_t nw_frag_mask)
2411 {
2412     uint8_t frag_mask = ~(OVS_FRAG_TYPE_FIRST | OVS_FRAG_TYPE_LATER);
2413
2414     frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_FIRST : 0;
2415     frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_LATER) ? OVS_FRAG_TYPE_LATER : 0;
2416
2417     return frag_mask;
2418 }
2419
2420 static void
2421 odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data,
2422                          const struct flow *flow, odp_port_t odp_in_port,
2423                          size_t max_mpls_depth)
2424 {
2425     bool is_mask;
2426     struct ovs_key_ethernet *eth_key;
2427     size_t encap;
2428
2429     /* We assume that if 'data' and 'flow' are not the same, we should
2430      * treat 'data' as a mask. */
2431     is_mask = (data != flow);
2432
2433     nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
2434
2435     if (flow->tunnel.ip_dst || is_mask) {
2436         tun_key_to_attr(buf, &data->tunnel);
2437     }
2438
2439     nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
2440
2441     /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2442      * is not the magical value "ODPP_NONE". */
2443     if (is_mask || odp_in_port != ODPP_NONE) {
2444         nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
2445     }
2446
2447     eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
2448                                        sizeof *eth_key);
2449     memcpy(eth_key->eth_src, data->dl_src, ETH_ADDR_LEN);
2450     memcpy(eth_key->eth_dst, data->dl_dst, ETH_ADDR_LEN);
2451
2452     if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
2453         if (is_mask) {
2454             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2455         } else {
2456             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
2457         }
2458         nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
2459         encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
2460         if (flow->vlan_tci == htons(0)) {
2461             goto unencap;
2462         }
2463     } else {
2464         encap = 0;
2465     }
2466
2467     if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2468         /* For backwards compatibility with kernels that don't support
2469          * wildcarding, the following convention is used to encode the
2470          * OVS_KEY_ATTR_ETHERTYPE for key and mask:
2471          *
2472          *   key      mask    matches
2473          * -------- --------  -------
2474          *  >0x5ff   0xffff   Specified Ethernet II Ethertype.
2475          *  >0x5ff      0     Any Ethernet II or non-Ethernet II frame.
2476          *  <none>   0xffff   Any non-Ethernet II frame (except valid
2477          *                    802.3 SNAP packet with valid eth_type).
2478          */
2479         if (is_mask) {
2480             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2481         }
2482         goto unencap;
2483     }
2484
2485     nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
2486
2487     if (flow->dl_type == htons(ETH_TYPE_IP)) {
2488         struct ovs_key_ipv4 *ipv4_key;
2489
2490         ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
2491                                             sizeof *ipv4_key);
2492         ipv4_key->ipv4_src = data->nw_src;
2493         ipv4_key->ipv4_dst = data->nw_dst;
2494         ipv4_key->ipv4_proto = data->nw_proto;
2495         ipv4_key->ipv4_tos = data->nw_tos;
2496         ipv4_key->ipv4_ttl = data->nw_ttl;
2497         ipv4_key->ipv4_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2498                                       : ovs_to_odp_frag(data->nw_frag);
2499     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2500         struct ovs_key_ipv6 *ipv6_key;
2501
2502         ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
2503                                             sizeof *ipv6_key);
2504         memcpy(ipv6_key->ipv6_src, &data->ipv6_src, sizeof ipv6_key->ipv6_src);
2505         memcpy(ipv6_key->ipv6_dst, &data->ipv6_dst, sizeof ipv6_key->ipv6_dst);
2506         ipv6_key->ipv6_label = data->ipv6_label;
2507         ipv6_key->ipv6_proto = data->nw_proto;
2508         ipv6_key->ipv6_tclass = data->nw_tos;
2509         ipv6_key->ipv6_hlimit = data->nw_ttl;
2510         ipv6_key->ipv6_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2511                                       : ovs_to_odp_frag(data->nw_frag);
2512     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2513                flow->dl_type == htons(ETH_TYPE_RARP)) {
2514         struct ovs_key_arp *arp_key;
2515
2516         arp_key = nl_msg_put_unspec_zero(buf, OVS_KEY_ATTR_ARP,
2517                                          sizeof *arp_key);
2518         arp_key->arp_sip = data->nw_src;
2519         arp_key->arp_tip = data->nw_dst;
2520         arp_key->arp_op = htons(data->nw_proto);
2521         memcpy(arp_key->arp_sha, data->arp_sha, ETH_ADDR_LEN);
2522         memcpy(arp_key->arp_tha, data->arp_tha, ETH_ADDR_LEN);
2523     } else if (eth_type_mpls(flow->dl_type)) {
2524         struct ovs_key_mpls *mpls_key;
2525         int i, n;
2526
2527         n = flow_count_mpls_labels(flow, NULL);
2528         n = MIN(n, max_mpls_depth);
2529         mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
2530                                             n * sizeof *mpls_key);
2531         for (i = 0; i < n; i++) {
2532             mpls_key[i].mpls_lse = data->mpls_lse[i];
2533         }
2534     }
2535
2536     if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2537         if (flow->nw_proto == IPPROTO_TCP) {
2538             struct ovs_key_tcp *tcp_key;
2539
2540             tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
2541                                                sizeof *tcp_key);
2542             tcp_key->tcp_src = data->tp_src;
2543             tcp_key->tcp_dst = data->tp_dst;
2544
2545             if (data->tcp_flags) {
2546                 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
2547             }
2548         } else if (flow->nw_proto == IPPROTO_UDP) {
2549             struct ovs_key_udp *udp_key;
2550
2551             udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
2552                                                sizeof *udp_key);
2553             udp_key->udp_src = data->tp_src;
2554             udp_key->udp_dst = data->tp_dst;
2555         } else if (flow->nw_proto == IPPROTO_SCTP) {
2556             struct ovs_key_sctp *sctp_key;
2557
2558             sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
2559                                                sizeof *sctp_key);
2560             sctp_key->sctp_src = data->tp_src;
2561             sctp_key->sctp_dst = data->tp_dst;
2562         } else if (flow->dl_type == htons(ETH_TYPE_IP)
2563                 && flow->nw_proto == IPPROTO_ICMP) {
2564             struct ovs_key_icmp *icmp_key;
2565
2566             icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
2567                                                 sizeof *icmp_key);
2568             icmp_key->icmp_type = ntohs(data->tp_src);
2569             icmp_key->icmp_code = ntohs(data->tp_dst);
2570         } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
2571                 && flow->nw_proto == IPPROTO_ICMPV6) {
2572             struct ovs_key_icmpv6 *icmpv6_key;
2573
2574             icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
2575                                                   sizeof *icmpv6_key);
2576             icmpv6_key->icmpv6_type = ntohs(data->tp_src);
2577             icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
2578
2579             if (flow->tp_dst == htons(0) &&
2580                 (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
2581                  flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) &&
2582                 (!is_mask || (data->tp_src == htons(0xffff) &&
2583                               data->tp_dst == htons(0xffff)))) {
2584
2585                 struct ovs_key_nd *nd_key;
2586
2587                 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
2588                                                     sizeof *nd_key);
2589                 memcpy(nd_key->nd_target, &data->nd_target,
2590                         sizeof nd_key->nd_target);
2591                 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
2592                 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
2593             }
2594         }
2595     }
2596
2597 unencap:
2598     if (encap) {
2599         nl_msg_end_nested(buf, encap);
2600     }
2601 }
2602
2603 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
2604  * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
2605  * number rather than a datapath port number).  Instead, if 'odp_in_port'
2606  * is anything other than ODPP_NONE, it is included in 'buf' as the input
2607  * port.
2608  *
2609  * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2610  * capable of being expanded to allow for that much space. */
2611 void
2612 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
2613                        odp_port_t odp_in_port)
2614 {
2615     odp_flow_key_from_flow__(buf, flow, flow, odp_in_port, SIZE_MAX);
2616 }
2617
2618 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
2619  * 'buf'.  'flow' is used as a template to determine how to interpret
2620  * 'mask'.  For example, the 'dl_type' of 'mask' describes the mask, but
2621  * it doesn't indicate whether the other fields should be interpreted as
2622  * ARP, IPv4, IPv6, etc.
2623  *
2624  * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2625  * capable of being expanded to allow for that much space. */
2626 void
2627 odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
2628                        const struct flow *flow, uint32_t odp_in_port_mask,
2629                        size_t max_mpls_depth)
2630 {
2631     odp_flow_key_from_flow__(buf, mask, flow, u32_to_odp(odp_in_port_mask),
2632                              max_mpls_depth);
2633 }
2634
2635 /* Generate ODP flow key from the given packet metadata */
2636 void
2637 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
2638 {
2639     nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
2640
2641     if (md->tunnel.ip_dst) {
2642         tun_key_to_attr(buf, &md->tunnel);
2643     }
2644
2645     nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
2646
2647     /* Add an ingress port attribute if 'odp_in_port' is not the magical
2648      * value "ODPP_NONE". */
2649     if (md->in_port.odp_port != ODPP_NONE) {
2650         nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
2651     }
2652 }
2653
2654 /* Generate packet metadata from the given ODP flow key. */
2655 void
2656 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
2657                         struct pkt_metadata *md)
2658 {
2659     const struct nlattr *nla;
2660     size_t left;
2661     uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
2662         1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
2663         1u << OVS_KEY_ATTR_IN_PORT;
2664
2665     *md = PKT_METADATA_INITIALIZER(ODPP_NONE);
2666
2667     NL_ATTR_FOR_EACH (nla, left, key, key_len) {
2668         uint16_t type = nl_attr_type(nla);
2669         size_t len = nl_attr_get_size(nla);
2670         int expected_len = odp_flow_key_attr_len(type);
2671
2672         if (len != expected_len && expected_len >= 0) {
2673             continue;
2674         }
2675
2676         if (type == OVS_KEY_ATTR_PRIORITY) {
2677             md->skb_priority = nl_attr_get_u32(nla);
2678             wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
2679         } else if (type == OVS_KEY_ATTR_SKB_MARK) {
2680             md->pkt_mark = nl_attr_get_u32(nla);
2681             wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
2682         } else if (type == OVS_KEY_ATTR_TUNNEL) {
2683             enum odp_key_fitness res;
2684
2685             res = odp_tun_key_from_attr(nla, &md->tunnel);
2686             if (res == ODP_FIT_ERROR) {
2687                 memset(&md->tunnel, 0, sizeof md->tunnel);
2688             } else if (res == ODP_FIT_PERFECT) {
2689                 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
2690             }
2691         } else if (type == OVS_KEY_ATTR_IN_PORT) {
2692             md->in_port.odp_port = nl_attr_get_odp_port(nla);
2693             wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
2694         }
2695
2696         if (!wanted_attrs) {
2697             return; /* Have everything. */
2698         }
2699     }
2700 }
2701
2702 uint32_t
2703 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
2704 {
2705     BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
2706     return hash_words(ALIGNED_CAST(const uint32_t *, key),
2707                       key_len / sizeof(uint32_t), 0);
2708 }
2709
2710 static void
2711 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
2712                        uint64_t attrs, int out_of_range_attr,
2713                        const struct nlattr *key, size_t key_len)
2714 {
2715     struct ds s;
2716     int i;
2717
2718     if (VLOG_DROP_DBG(rl)) {
2719         return;
2720     }
2721
2722     ds_init(&s);
2723     for (i = 0; i < 64; i++) {
2724         if (attrs & (UINT64_C(1) << i)) {
2725             char namebuf[OVS_KEY_ATTR_BUFSIZE];
2726
2727             ds_put_format(&s, " %s",
2728                           ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
2729         }
2730     }
2731     if (out_of_range_attr) {
2732         ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
2733     }
2734
2735     ds_put_cstr(&s, ": ");
2736     odp_flow_key_format(key, key_len, &s);
2737
2738     VLOG_DBG("%s:%s", title, ds_cstr(&s));
2739     ds_destroy(&s);
2740 }
2741
2742 static bool
2743 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
2744 {
2745     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2746
2747     if (odp_frag > OVS_FRAG_TYPE_LATER) {
2748         VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
2749         return false;
2750     }
2751
2752     if (odp_frag != OVS_FRAG_TYPE_NONE) {
2753         flow->nw_frag |= FLOW_NW_FRAG_ANY;
2754         if (odp_frag == OVS_FRAG_TYPE_LATER) {
2755             flow->nw_frag |= FLOW_NW_FRAG_LATER;
2756         }
2757     }
2758     return true;
2759 }
2760
2761 static bool
2762 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
2763                    const struct nlattr *attrs[], uint64_t *present_attrsp,
2764                    int *out_of_range_attrp)
2765 {
2766     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2767     const struct nlattr *nla;
2768     uint64_t present_attrs;
2769     size_t left;
2770
2771     BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
2772     present_attrs = 0;
2773     *out_of_range_attrp = 0;
2774     NL_ATTR_FOR_EACH (nla, left, key, key_len) {
2775         uint16_t type = nl_attr_type(nla);
2776         size_t len = nl_attr_get_size(nla);
2777         int expected_len = odp_flow_key_attr_len(type);
2778
2779         if (len != expected_len && expected_len >= 0) {
2780             char namebuf[OVS_KEY_ATTR_BUFSIZE];
2781
2782             VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
2783                         "length %d", ovs_key_attr_to_string(type, namebuf,
2784                                                             sizeof namebuf),
2785                         len, expected_len);
2786             return false;
2787         }
2788
2789         if (type > OVS_KEY_ATTR_MAX) {
2790             *out_of_range_attrp = type;
2791         } else {
2792             if (present_attrs & (UINT64_C(1) << type)) {
2793                 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2794
2795                 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
2796                             ovs_key_attr_to_string(type,
2797                                                    namebuf, sizeof namebuf));
2798                 return false;
2799             }
2800
2801             present_attrs |= UINT64_C(1) << type;
2802             attrs[type] = nla;
2803         }
2804     }
2805     if (left) {
2806         VLOG_ERR_RL(&rl, "trailing garbage in flow key");
2807         return false;
2808     }
2809
2810     *present_attrsp = present_attrs;
2811     return true;
2812 }
2813
2814 static enum odp_key_fitness
2815 check_expectations(uint64_t present_attrs, int out_of_range_attr,
2816                    uint64_t expected_attrs,
2817                    const struct nlattr *key, size_t key_len)
2818 {
2819     uint64_t missing_attrs;
2820     uint64_t extra_attrs;
2821
2822     missing_attrs = expected_attrs & ~present_attrs;
2823     if (missing_attrs) {
2824         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2825         log_odp_key_attributes(&rl, "expected but not present",
2826                                missing_attrs, 0, key, key_len);
2827         return ODP_FIT_TOO_LITTLE;
2828     }
2829
2830     extra_attrs = present_attrs & ~expected_attrs;
2831     if (extra_attrs || out_of_range_attr) {
2832         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2833         log_odp_key_attributes(&rl, "present but not expected",
2834                                extra_attrs, out_of_range_attr, key, key_len);
2835         return ODP_FIT_TOO_MUCH;
2836     }
2837
2838     return ODP_FIT_PERFECT;
2839 }
2840
2841 static bool
2842 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2843                 uint64_t present_attrs, uint64_t *expected_attrs,
2844                 struct flow *flow, const struct flow *src_flow)
2845 {
2846     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2847     bool is_mask = flow != src_flow;
2848
2849     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
2850         flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
2851         if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2852             VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
2853                         ntohs(flow->dl_type));
2854             return false;
2855         }
2856         if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
2857             flow->dl_type != htons(0xffff)) {
2858             return false;
2859         }
2860         *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
2861     } else {
2862         if (!is_mask) {
2863             flow->dl_type = htons(FLOW_DL_TYPE_NONE);
2864         } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
2865             /* See comments in odp_flow_key_from_flow__(). */
2866             VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
2867             return false;
2868         }
2869     }
2870     return true;
2871 }
2872
2873 static enum odp_key_fitness
2874 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2875                   uint64_t present_attrs, int out_of_range_attr,
2876                   uint64_t expected_attrs, struct flow *flow,
2877                   const struct nlattr *key, size_t key_len,
2878                   const struct flow *src_flow)
2879 {
2880     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2881     bool is_mask = src_flow != flow;
2882     const void *check_start = NULL;
2883     size_t check_len = 0;
2884     enum ovs_key_attr expected_bit = 0xff;
2885
2886     if (eth_type_mpls(src_flow->dl_type)) {
2887         size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
2888         const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
2889         int n = size / sizeof(ovs_be32);
2890         int i;
2891
2892         if (!size || size % sizeof(ovs_be32)) {
2893             return ODP_FIT_ERROR;
2894         }
2895
2896         if (!is_mask) {
2897             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
2898
2899             if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) {
2900                 return ODP_FIT_TOO_LITTLE;
2901             }
2902         } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
2903             if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
2904                 return ODP_FIT_ERROR;
2905             }
2906             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
2907         }
2908
2909         for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
2910             flow->mpls_lse[i] = mpls_lse[i];
2911         }
2912         if (n > FLOW_MAX_MPLS_LABELS) {
2913             return ODP_FIT_TOO_MUCH;
2914         }
2915
2916         if (!is_mask) {
2917             /* BOS may be set only in the innermost label. */
2918             for (i = 0; i < n - 1; i++) {
2919                 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
2920                     return ODP_FIT_ERROR;
2921                 }
2922             }
2923
2924             /* BOS must be set in the innermost label. */
2925             if (n < FLOW_MAX_MPLS_LABELS
2926                 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
2927                 return ODP_FIT_TOO_LITTLE;
2928             }
2929         }
2930
2931         goto done;
2932     } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
2933         if (!is_mask) {
2934             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
2935         }
2936         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
2937             const struct ovs_key_ipv4 *ipv4_key;
2938
2939             ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
2940             flow->nw_src = ipv4_key->ipv4_src;
2941             flow->nw_dst = ipv4_key->ipv4_dst;
2942             flow->nw_proto = ipv4_key->ipv4_proto;
2943             flow->nw_tos = ipv4_key->ipv4_tos;
2944             flow->nw_ttl = ipv4_key->ipv4_ttl;
2945             if (is_mask) {
2946                 flow->nw_frag = ipv4_key->ipv4_frag;
2947                 check_start = ipv4_key;
2948                 check_len = sizeof *ipv4_key;
2949                 expected_bit = OVS_KEY_ATTR_IPV4;
2950             } else if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
2951                 return ODP_FIT_ERROR;
2952             }
2953         }
2954     } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
2955         if (!is_mask) {
2956             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
2957         }
2958         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
2959             const struct ovs_key_ipv6 *ipv6_key;
2960
2961             ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
2962             memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
2963             memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
2964             flow->ipv6_label = ipv6_key->ipv6_label;
2965             flow->nw_proto = ipv6_key->ipv6_proto;
2966             flow->nw_tos = ipv6_key->ipv6_tclass;
2967             flow->nw_ttl = ipv6_key->ipv6_hlimit;
2968             if (is_mask) {
2969                 flow->nw_frag = ipv6_key->ipv6_frag;
2970                 check_start = ipv6_key;
2971                 check_len = sizeof *ipv6_key;
2972                 expected_bit = OVS_KEY_ATTR_IPV6;
2973             } else if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
2974                 return ODP_FIT_ERROR;
2975             }
2976         }
2977     } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
2978                src_flow->dl_type == htons(ETH_TYPE_RARP)) {
2979         if (!is_mask) {
2980             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
2981         }
2982         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
2983             const struct ovs_key_arp *arp_key;
2984
2985             arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
2986             flow->nw_src = arp_key->arp_sip;
2987             flow->nw_dst = arp_key->arp_tip;
2988             if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
2989                 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
2990                             "key", ntohs(arp_key->arp_op));
2991                 return ODP_FIT_ERROR;
2992             }
2993             flow->nw_proto = ntohs(arp_key->arp_op);
2994             memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
2995             memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
2996
2997             if (is_mask) {
2998                 check_start = arp_key;
2999                 check_len = sizeof *arp_key;
3000                 expected_bit = OVS_KEY_ATTR_ARP;
3001             }
3002         }
3003     } else {
3004         goto done;
3005     }
3006     if (check_len > 0) { /* Happens only when 'is_mask'. */
3007         if (!is_all_zeros(check_start, check_len) &&
3008             flow->dl_type != htons(0xffff)) {
3009             return ODP_FIT_ERROR;
3010         } else {
3011             expected_attrs |= UINT64_C(1) << expected_bit;
3012         }
3013     }
3014
3015     expected_bit = OVS_KEY_ATTR_UNSPEC;
3016     if (src_flow->nw_proto == IPPROTO_TCP
3017         && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3018             src_flow->dl_type == htons(ETH_TYPE_IPV6))
3019         && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3020         if (!is_mask) {
3021             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
3022         }
3023         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
3024             const struct ovs_key_tcp *tcp_key;
3025
3026             tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
3027             flow->tp_src = tcp_key->tcp_src;
3028             flow->tp_dst = tcp_key->tcp_dst;
3029             expected_bit = OVS_KEY_ATTR_TCP;
3030         }
3031         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
3032             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
3033             flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
3034         }
3035     } else if (src_flow->nw_proto == IPPROTO_UDP
3036                && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3037                    src_flow->dl_type == htons(ETH_TYPE_IPV6))
3038                && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3039         if (!is_mask) {
3040             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
3041         }
3042         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
3043             const struct ovs_key_udp *udp_key;
3044
3045             udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
3046             flow->tp_src = udp_key->udp_src;
3047             flow->tp_dst = udp_key->udp_dst;
3048             expected_bit = OVS_KEY_ATTR_UDP;
3049         }
3050     } else if (src_flow->nw_proto == IPPROTO_SCTP
3051                && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3052                    src_flow->dl_type == htons(ETH_TYPE_IPV6))
3053                && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3054         if (!is_mask) {
3055             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
3056         }
3057         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
3058             const struct ovs_key_sctp *sctp_key;
3059
3060             sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
3061             flow->tp_src = sctp_key->sctp_src;
3062             flow->tp_dst = sctp_key->sctp_dst;
3063             expected_bit = OVS_KEY_ATTR_SCTP;
3064         }
3065     } else if (src_flow->nw_proto == IPPROTO_ICMP
3066                && src_flow->dl_type == htons(ETH_TYPE_IP)
3067                && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3068         if (!is_mask) {
3069             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
3070         }
3071         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
3072             const struct ovs_key_icmp *icmp_key;
3073
3074             icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
3075             flow->tp_src = htons(icmp_key->icmp_type);
3076             flow->tp_dst = htons(icmp_key->icmp_code);
3077             expected_bit = OVS_KEY_ATTR_ICMP;
3078         }
3079     } else if (src_flow->nw_proto == IPPROTO_ICMPV6
3080                && src_flow->dl_type == htons(ETH_TYPE_IPV6)
3081                && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3082         if (!is_mask) {
3083             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
3084         }
3085         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
3086             const struct ovs_key_icmpv6 *icmpv6_key;
3087
3088             icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
3089             flow->tp_src = htons(icmpv6_key->icmpv6_type);
3090             flow->tp_dst = htons(icmpv6_key->icmpv6_code);
3091             expected_bit = OVS_KEY_ATTR_ICMPV6;
3092             if (src_flow->tp_dst == htons(0) &&
3093                 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
3094                  src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
3095                 if (!is_mask) {
3096                     expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3097                 }
3098                 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
3099                     const struct ovs_key_nd *nd_key;
3100
3101                     nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
3102                     memcpy(&flow->nd_target, nd_key->nd_target,
3103                            sizeof flow->nd_target);
3104                     memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
3105                     memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
3106                     if (is_mask) {
3107                         if (!is_all_zeros((const uint8_t *) nd_key,
3108                                           sizeof *nd_key) &&
3109                             (flow->tp_src != htons(0xffff) ||
3110                              flow->tp_dst != htons(0xffff))) {
3111                             return ODP_FIT_ERROR;
3112                         } else {
3113                             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3114                         }
3115                     }
3116                 }
3117             }
3118         }
3119     }
3120     if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
3121         if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
3122             return ODP_FIT_ERROR;
3123         } else {
3124             expected_attrs |= UINT64_C(1) << expected_bit;
3125         }
3126     }
3127
3128 done:
3129     return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
3130                               key, key_len);
3131 }
3132
3133 /* Parse 802.1Q header then encapsulated L3 attributes. */
3134 static enum odp_key_fitness
3135 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3136                    uint64_t present_attrs, int out_of_range_attr,
3137                    uint64_t expected_attrs, struct flow *flow,
3138                    const struct nlattr *key, size_t key_len,
3139                    const struct flow *src_flow)
3140 {
3141     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3142     bool is_mask = src_flow != flow;
3143
3144     const struct nlattr *encap
3145         = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
3146            ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
3147     enum odp_key_fitness encap_fitness;
3148     enum odp_key_fitness fitness;
3149     ovs_be16 tci;
3150
3151     /* Calculate fitness of outer attributes. */
3152     if (!is_mask) {
3153         expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
3154                           (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
3155     } else {
3156         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3157             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3158         }
3159         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
3160             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
3161         }
3162     }
3163     fitness = check_expectations(present_attrs, out_of_range_attr,
3164                                  expected_attrs, key, key_len);
3165
3166     /* Get the VLAN TCI value. */
3167     if (!is_mask && !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
3168         return ODP_FIT_TOO_LITTLE;
3169     } else {
3170         tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
3171                ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
3172                : htons(0));
3173         if (!is_mask) {
3174             if (tci == htons(0)) {
3175                 /* Corner case for a truncated 802.1Q header. */
3176                 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
3177                     return ODP_FIT_TOO_MUCH;
3178                 }
3179                 return fitness;
3180             } else if (!(tci & htons(VLAN_CFI))) {
3181                 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
3182                             "but CFI bit is not set", ntohs(tci));
3183                 return ODP_FIT_ERROR;
3184             }
3185         }
3186         /* Set vlan_tci.
3187          * Remove the TPID from dl_type since it's not the real Ethertype.  */
3188         flow->dl_type = htons(0);
3189         flow->vlan_tci = tci;
3190     }
3191
3192     if (is_mask && !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
3193         return fitness;
3194     }
3195     /* Now parse the encapsulated attributes. */
3196     if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
3197                             attrs, &present_attrs, &out_of_range_attr)) {
3198         return ODP_FIT_ERROR;
3199     }
3200     expected_attrs = 0;
3201
3202     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
3203         return ODP_FIT_ERROR;
3204     }
3205     encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3206                                       expected_attrs, flow, key, key_len,
3207                                       src_flow);
3208
3209     /* The overall fitness is the worse of the outer and inner attributes. */
3210     return MAX(fitness, encap_fitness);
3211 }
3212
3213 static enum odp_key_fitness
3214 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
3215                        struct flow *flow, const struct flow *src_flow)
3216 {
3217     const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
3218     uint64_t expected_attrs;
3219     uint64_t present_attrs;
3220     int out_of_range_attr;
3221     bool is_mask = src_flow != flow;
3222
3223     memset(flow, 0, sizeof *flow);
3224
3225     /* Parse attributes. */
3226     if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
3227                             &out_of_range_attr)) {
3228         return ODP_FIT_ERROR;
3229     }
3230     expected_attrs = 0;
3231
3232     /* Metadata. */
3233     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
3234         flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
3235         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
3236     }
3237
3238     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
3239         flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
3240         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
3241     }
3242
3243     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
3244         enum odp_key_fitness res;
3245
3246         res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
3247         if (res == ODP_FIT_ERROR) {
3248             return ODP_FIT_ERROR;
3249         } else if (res == ODP_FIT_PERFECT) {
3250             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
3251         }
3252     }
3253
3254     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
3255         flow->in_port.odp_port
3256             = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
3257         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
3258     } else if (!is_mask) {
3259         flow->in_port.odp_port = ODPP_NONE;
3260     }
3261
3262     /* Ethernet header. */
3263     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
3264         const struct ovs_key_ethernet *eth_key;
3265
3266         eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
3267         memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
3268         memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
3269         if (is_mask) {
3270             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3271         }
3272     }
3273     if (!is_mask) {
3274         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3275     }
3276
3277     /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
3278     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
3279         src_flow)) {
3280         return ODP_FIT_ERROR;
3281     }
3282
3283     if (is_mask
3284         ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
3285         : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
3286         return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
3287                                   expected_attrs, flow, key, key_len, src_flow);
3288     }
3289     if (is_mask) {
3290         flow->vlan_tci = htons(0xffff);
3291         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3292             flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
3293             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3294         }
3295     }
3296     return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3297                              expected_attrs, flow, key, key_len, src_flow);
3298 }
3299
3300 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
3301  * structure in 'flow'.  Returns an ODP_FIT_* value that indicates how well
3302  * 'key' fits our expectations for what a flow key should contain.
3303  *
3304  * The 'in_port' will be the datapath's understanding of the port.  The
3305  * caller will need to translate with odp_port_to_ofp_port() if the
3306  * OpenFlow port is needed.
3307  *
3308  * This function doesn't take the packet itself as an argument because none of
3309  * the currently understood OVS_KEY_ATTR_* attributes require it.  Currently,
3310  * it is always possible to infer which additional attribute(s) should appear
3311  * by looking at the attributes for lower-level protocols, e.g. if the network
3312  * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
3313  * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
3314  * must be absent. */
3315 enum odp_key_fitness
3316 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
3317                      struct flow *flow)
3318 {
3319    return odp_flow_key_to_flow__(key, key_len, flow, flow);
3320 }
3321
3322 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask
3323  * structure in 'mask'.  'flow' must be a previously translated flow
3324  * corresponding to 'mask'.  Returns an ODP_FIT_* value that indicates how well
3325  * 'key' fits our expectations for what a flow key should contain. */
3326 enum odp_key_fitness
3327 odp_flow_key_to_mask(const struct nlattr *key, size_t key_len,
3328                      struct flow *mask, const struct flow *flow)
3329 {
3330    return odp_flow_key_to_flow__(key, key_len, mask, flow);
3331 }
3332
3333 /* Returns 'fitness' as a string, for use in debug messages. */
3334 const char *
3335 odp_key_fitness_to_string(enum odp_key_fitness fitness)
3336 {
3337     switch (fitness) {
3338     case ODP_FIT_PERFECT:
3339         return "OK";
3340     case ODP_FIT_TOO_MUCH:
3341         return "too_much";
3342     case ODP_FIT_TOO_LITTLE:
3343         return "too_little";
3344     case ODP_FIT_ERROR:
3345         return "error";
3346     default:
3347         return "<unknown>";
3348     }
3349 }
3350
3351 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
3352  * Netlink PID 'pid'.  If 'userdata' is nonnull, adds a userdata attribute
3353  * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
3354  * offset within 'odp_actions' of the start of the cookie.  (If 'userdata' is
3355  * null, then the return value is not meaningful.) */
3356 size_t
3357 odp_put_userspace_action(uint32_t pid,
3358                          const void *userdata, size_t userdata_size,
3359                          struct ofpbuf *odp_actions)
3360 {
3361     size_t userdata_ofs;
3362     size_t offset;
3363
3364     offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3365     nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3366     if (userdata) {
3367         userdata_ofs = odp_actions->size + NLA_HDRLEN;
3368
3369         /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
3370          * module before Linux 3.10 required the userdata to be exactly 8 bytes
3371          * long:
3372          *
3373          *   - The kernel rejected shorter userdata with -ERANGE.
3374          *
3375          *   - The kernel silently dropped userdata beyond the first 8 bytes.
3376          *
3377          * Thus, for maximum compatibility, always put at least 8 bytes.  (We
3378          * separately disable features that required more than 8 bytes.) */
3379         memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3380                                       MAX(8, userdata_size)),
3381                userdata, userdata_size);
3382     } else {
3383         userdata_ofs = 0;
3384     }
3385     nl_msg_end_nested(odp_actions, offset);
3386
3387     return userdata_ofs;
3388 }
3389
3390 void
3391 odp_put_tunnel_action(const struct flow_tnl *tunnel,
3392                       struct ofpbuf *odp_actions)
3393 {
3394     size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3395     tun_key_to_attr(odp_actions, tunnel);
3396     nl_msg_end_nested(odp_actions, offset);
3397 }
3398 \f
3399 /* The commit_odp_actions() function and its helpers. */
3400
3401 static void
3402 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3403                   const void *key, size_t key_size)
3404 {
3405     size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3406     nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3407     nl_msg_end_nested(odp_actions, offset);
3408 }
3409
3410 void
3411 odp_put_pkt_mark_action(const uint32_t pkt_mark,
3412                         struct ofpbuf *odp_actions)
3413 {
3414     commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK, &pkt_mark,
3415                       sizeof(pkt_mark));
3416 }
3417
3418 /* If any of the flow key data that ODP actions can modify are different in
3419  * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
3420  * 'odp_actions' that change the flow tunneling information in key from
3421  * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
3422  * same way.  In other words, operates the same as commit_odp_actions(), but
3423  * only on tunneling information. */
3424 void
3425 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
3426                          struct ofpbuf *odp_actions)
3427 {
3428     /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
3429     if (flow->tunnel.ip_dst) {
3430         if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
3431             return;
3432         }
3433         memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
3434         odp_put_tunnel_action(&base->tunnel, odp_actions);
3435     }
3436 }
3437
3438 static void
3439 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3440                              struct ofpbuf *odp_actions,
3441                              struct flow_wildcards *wc)
3442 {
3443     struct ovs_key_ethernet eth_key;
3444
3445     if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3446         eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3447         return;
3448     }
3449
3450     memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3451     memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3452
3453     memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3454     memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3455
3456     memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3457     memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3458
3459     commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
3460                       &eth_key, sizeof(eth_key));
3461 }
3462
3463 static void
3464 pop_vlan(struct flow *base,
3465          struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3466 {
3467     memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3468
3469     if (base->vlan_tci & htons(VLAN_CFI)) {
3470         nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3471         base->vlan_tci = 0;
3472     }
3473 }
3474
3475 static void
3476 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
3477                    struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3478 {
3479     if (base->vlan_tci == vlan_tci) {
3480         return;
3481     }
3482
3483     pop_vlan(base, odp_actions, wc);
3484     if (vlan_tci & htons(VLAN_CFI)) {
3485         struct ovs_action_push_vlan vlan;
3486
3487         vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
3488         vlan.vlan_tci = vlan_tci;
3489         nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
3490                           &vlan, sizeof vlan);
3491     }
3492     base->vlan_tci = vlan_tci;
3493 }
3494
3495 static void
3496 commit_mpls_action(const struct flow *flow, struct flow *base,
3497                    struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3498 {
3499     int base_n = flow_count_mpls_labels(base, wc);
3500     int flow_n = flow_count_mpls_labels(flow, wc);
3501     int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
3502                                                  wc);
3503
3504     while (base_n > common_n) {
3505         if (base_n - 1 == common_n && flow_n > common_n) {
3506             /* If there is only one more LSE in base than there are common
3507              * between base and flow; and flow has at least one more LSE than
3508              * is common then the topmost LSE of base may be updated using
3509              * set */
3510             struct ovs_key_mpls mpls_key;
3511
3512             mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
3513             commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
3514                               &mpls_key, sizeof mpls_key);
3515             flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
3516             common_n++;
3517         } else {
3518             /* Otherwise, if there more LSEs in base than are common between
3519              * base and flow then pop the topmost one. */
3520             ovs_be16 dl_type;
3521             bool popped;
3522
3523             /* If all the LSEs are to be popped and this is not the outermost
3524              * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
3525              * POP_MPLS action instead of flow->dl_type.
3526              *
3527              * This is because the POP_MPLS action requires its ethertype
3528              * argument to be an MPLS ethernet type but in this case
3529              * flow->dl_type will be a non-MPLS ethernet type.
3530              *
3531              * When the final POP_MPLS action occurs it use flow->dl_type and
3532              * the and the resulting packet will have the desired dl_type. */
3533             if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
3534                 dl_type = htons(ETH_TYPE_MPLS);
3535             } else {
3536                 dl_type = flow->dl_type;
3537             }
3538             nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
3539             popped = flow_pop_mpls(base, base_n, flow->dl_type, wc);
3540             ovs_assert(popped);
3541             base_n--;
3542         }
3543     }
3544
3545     /* If, after the above popping and setting, there are more LSEs in flow
3546      * than base then some LSEs need to be pushed. */
3547     while (base_n < flow_n) {
3548         struct ovs_action_push_mpls *mpls;
3549
3550         mpls = nl_msg_put_unspec_zero(odp_actions,
3551                                       OVS_ACTION_ATTR_PUSH_MPLS,
3552                                       sizeof *mpls);
3553         mpls->mpls_ethertype = flow->dl_type;
3554         mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
3555         flow_push_mpls(base, base_n, mpls->mpls_ethertype, wc);
3556         flow_set_mpls_lse(base, 0, mpls->mpls_lse);
3557         base_n++;
3558     }
3559 }
3560
3561 static void
3562 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
3563                      struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3564 {
3565     struct ovs_key_ipv4 ipv4_key;
3566
3567     if (base->nw_src == flow->nw_src &&
3568         base->nw_dst == flow->nw_dst &&
3569         base->nw_tos == flow->nw_tos &&
3570         base->nw_ttl == flow->nw_ttl &&
3571         base->nw_frag == flow->nw_frag) {
3572         return;
3573     }
3574
3575     memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3576     memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3577     memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3578     memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3579     memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3580     memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3581
3582     ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3583     ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3584     ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
3585     ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
3586     ipv4_key.ipv4_proto = base->nw_proto;
3587     ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
3588
3589     commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
3590                       &ipv4_key, sizeof(ipv4_key));
3591 }
3592
3593 static void
3594 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
3595                        struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3596 {
3597     struct ovs_key_ipv6 ipv6_key;
3598
3599     if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
3600         ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
3601         base->ipv6_label == flow->ipv6_label &&
3602         base->nw_tos == flow->nw_tos &&
3603         base->nw_ttl == flow->nw_ttl &&
3604         base->nw_frag == flow->nw_frag) {
3605         return;
3606     }
3607
3608     memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
3609     memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
3610     memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
3611     memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3612     memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3613     memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3614     memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3615
3616     base->ipv6_src = flow->ipv6_src;
3617     memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
3618     base->ipv6_dst = flow->ipv6_dst;
3619     memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
3620
3621     ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
3622     ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
3623     ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
3624     ipv6_key.ipv6_proto = base->nw_proto;
3625     ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
3626
3627     commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
3628                       &ipv6_key, sizeof(ipv6_key));
3629 }
3630
3631 static enum slow_path_reason
3632 commit_set_arp_action(const struct flow *flow, struct flow *base,
3633                       struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3634 {
3635     struct ovs_key_arp arp_key;
3636
3637     if (base->nw_src == flow->nw_src &&
3638         base->nw_dst == flow->nw_dst &&
3639         base->nw_proto == flow->nw_proto &&
3640         eth_addr_equals(base->arp_sha, flow->arp_sha) &&
3641         eth_addr_equals(base->arp_tha, flow->arp_tha)) {
3642         return 0;
3643     }
3644
3645     memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3646     memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3647     memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3648     memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
3649     memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
3650
3651     base->nw_src = flow->nw_src;
3652     base->nw_dst = flow->nw_dst;
3653     base->nw_proto = flow->nw_proto;
3654     memcpy(base->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
3655     memcpy(base->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
3656
3657     arp_key.arp_sip = base->nw_src;
3658     arp_key.arp_tip = base->nw_dst;
3659     arp_key.arp_op = htons(base->nw_proto);
3660     memcpy(arp_key.arp_sha, flow->arp_sha, ETH_ADDR_LEN);
3661     memcpy(arp_key.arp_tha, flow->arp_tha, ETH_ADDR_LEN);
3662
3663     commit_set_action(odp_actions, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
3664
3665     return SLOW_ACTION;
3666 }
3667
3668 static enum slow_path_reason
3669 commit_set_nw_action(const struct flow *flow, struct flow *base,
3670                      struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3671 {
3672     /* Check if 'flow' really has an L3 header. */
3673     if (!flow->nw_proto) {
3674         return 0;
3675     }
3676
3677     switch (ntohs(base->dl_type)) {
3678     case ETH_TYPE_IP:
3679         commit_set_ipv4_action(flow, base, odp_actions, wc);
3680         break;
3681
3682     case ETH_TYPE_IPV6:
3683         commit_set_ipv6_action(flow, base, odp_actions, wc);
3684         break;
3685
3686     case ETH_TYPE_ARP:
3687         return commit_set_arp_action(flow, base, odp_actions, wc);
3688     }
3689
3690     return 0;
3691 }
3692
3693 static void
3694 commit_set_port_action(const struct flow *flow, struct flow *base,
3695                        struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3696 {
3697     if (!is_ip_any(base) || (!base->tp_src && !base->tp_dst)) {
3698         return;
3699     }
3700
3701     if (base->tp_src == flow->tp_src &&
3702         base->tp_dst == flow->tp_dst) {
3703         return;
3704     }
3705
3706     memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3707     memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3708
3709     if (flow->nw_proto == IPPROTO_TCP) {
3710         struct ovs_key_tcp port_key;
3711
3712         port_key.tcp_src = base->tp_src = flow->tp_src;
3713         port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3714
3715         commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
3716                           &port_key, sizeof(port_key));
3717
3718     } else if (flow->nw_proto == IPPROTO_UDP) {
3719         struct ovs_key_udp port_key;
3720
3721         port_key.udp_src = base->tp_src = flow->tp_src;
3722         port_key.udp_dst = base->tp_dst = flow->tp_dst;
3723
3724         commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
3725                           &port_key, sizeof(port_key));
3726     } else if (flow->nw_proto == IPPROTO_SCTP) {
3727         struct ovs_key_sctp port_key;
3728
3729         port_key.sctp_src = base->tp_src = flow->tp_src;
3730         port_key.sctp_dst = base->tp_dst = flow->tp_dst;
3731
3732         commit_set_action(odp_actions, OVS_KEY_ATTR_SCTP,
3733                           &port_key, sizeof(port_key));
3734     }
3735 }
3736
3737 static void
3738 commit_set_priority_action(const struct flow *flow, struct flow *base,
3739                            struct ofpbuf *odp_actions,
3740                            struct flow_wildcards *wc)
3741 {
3742     if (base->skb_priority == flow->skb_priority) {
3743         return;
3744     }
3745
3746     memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3747     base->skb_priority = flow->skb_priority;
3748
3749     commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
3750                       &base->skb_priority, sizeof(base->skb_priority));
3751 }
3752
3753 static void
3754 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base,
3755                            struct ofpbuf *odp_actions,
3756                            struct flow_wildcards *wc)
3757 {
3758     if (base->pkt_mark == flow->pkt_mark) {
3759         return;
3760     }
3761
3762     memset(&wc->masks.pkt_mark, 0xff, sizeof wc->masks.pkt_mark);
3763     base->pkt_mark = flow->pkt_mark;
3764
3765     odp_put_pkt_mark_action(base->pkt_mark, odp_actions);
3766 }
3767
3768 /* If any of the flow key data that ODP actions can modify are different in
3769  * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
3770  * key from 'base' into 'flow', and then changes 'base' the same way.  Does not
3771  * commit set_tunnel actions.  Users should call commit_odp_tunnel_action()
3772  * in addition to this function if needed.  Sets fields in 'wc' that are
3773  * used as part of the action.
3774  *
3775  * Returns a reason to force processing the flow's packets into the userspace
3776  * slow path, if there is one, otherwise 0. */
3777 enum slow_path_reason
3778 commit_odp_actions(const struct flow *flow, struct flow *base,
3779                    struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3780 {
3781     enum slow_path_reason slow;
3782
3783     commit_set_ether_addr_action(flow, base, odp_actions, wc);
3784     slow = commit_set_nw_action(flow, base, odp_actions, wc);
3785     commit_set_port_action(flow, base, odp_actions, wc);
3786     commit_mpls_action(flow, base, odp_actions, wc);
3787     commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
3788     commit_set_priority_action(flow, base, odp_actions, wc);
3789     commit_set_pkt_mark_action(flow, base, odp_actions, wc);
3790
3791     return slow;
3792 }