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