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