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