]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
d40533fc | 2 | * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019 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> | |
b2befd5b BP |
18 | #include <sys/types.h> |
19 | #include <netinet/in.h> | |
bed69b3e | 20 | #include <arpa/inet.h> |
064af421 | 21 | #include "odp-util.h" |
36956a7d | 22 | #include <errno.h> |
064af421 | 23 | #include <inttypes.h> |
7202cbe5 | 24 | #include <math.h> |
685a51a5 | 25 | #include <netinet/icmp6.h> |
370e373b | 26 | #include <netinet/ip6.h> |
064af421 BP |
27 | #include <stdlib.h> |
28 | #include <string.h> | |
a36de779 | 29 | |
cdee00fd | 30 | #include "byte-order.h" |
064af421 | 31 | #include "coverage.h" |
758c456d | 32 | #include "dpif.h" |
3e8a2ad1 | 33 | #include "openvswitch/dynamic-string.h" |
064af421 | 34 | #include "flow.h" |
cdee00fd | 35 | #include "netlink.h" |
64c96779 | 36 | #include "openvswitch/ofpbuf.h" |
064af421 | 37 | #include "packets.h" |
44bac24b | 38 | #include "simap.h" |
064af421 | 39 | #include "timeval.h" |
9558d2a5 | 40 | #include "tun-metadata.h" |
a36de779 | 41 | #include "unaligned.h" |
064af421 | 42 | #include "util.h" |
10e92b4f | 43 | #include "uuid.h" |
e6211adc | 44 | #include "openvswitch/vlog.h" |
8b668ee3 | 45 | #include "openvswitch/match.h" |
dbf4a928 | 46 | #include "odp-netlink-macros.h" |
285de6b2 | 47 | #include "csum.h" |
34118cae BP |
48 | |
49 | VLOG_DEFINE_THIS_MODULE(odp_util); | |
064af421 | 50 | |
df2c07f4 JP |
51 | /* The interface between userspace and kernel uses an "OVS_*" prefix. |
52 | * Since this is fairly non-specific for the OVS userspace components, | |
53 | * "ODP_*" (Open vSwitch Datapath) is used as the prefix for | |
54 | * interactions with the datapath. | |
55 | */ | |
56 | ||
7202cbe5 BP |
57 | /* The set of characters that may separate one action or one key attribute |
58 | * from another. */ | |
59 | static const char *delimiters = ", \t\r\n"; | |
d787ad39 | 60 | static const char *delimiters_end = ", \t\r\n)"; |
7202cbe5 | 61 | |
ac20d3fe YS |
62 | #define MAX_ODP_NESTED 32 |
63 | ||
64 | struct parse_odp_context { | |
65 | const struct simap *port_names; | |
66 | int depth; /* Current nested depth of odp string. */ | |
67 | }; | |
68 | ||
69 | static int parse_odp_key_mask_attr(struct parse_odp_context *, const char *, | |
70 | struct ofpbuf *, struct ofpbuf *); | |
5fbe9d15 TA |
71 | |
72 | static int parse_odp_key_mask_attr__(struct parse_odp_context *, const char *, | |
73 | struct ofpbuf *, struct ofpbuf *); | |
74 | ||
e6cc0bab | 75 | static void format_odp_key_attr(const struct nlattr *a, |
0a37839c GS |
76 | const struct nlattr *ma, |
77 | const struct hmap *portno_names, struct ds *ds, | |
041e7168 | 78 | bool verbose); |
a052b51b | 79 | |
5bb08b0e JG |
80 | struct geneve_scan { |
81 | struct geneve_opt d[63]; | |
82 | int len; | |
83 | }; | |
84 | ||
85 | static int scan_geneve(const char *s, struct geneve_scan *key, | |
86 | struct geneve_scan *mask); | |
87 | static void format_geneve_opts(const struct geneve_opt *opt, | |
88 | const struct geneve_opt *mask, int opts_len, | |
89 | struct ds *, bool verbose); | |
90 | ||
65da723b JG |
91 | static struct nlattr *generate_all_wildcard_mask(const struct attr_len_tbl tbl[], |
92 | int max, struct ofpbuf *, | |
93 | const struct nlattr *key); | |
89cf41ec BP |
94 | static void format_u128(struct ds *d, const ovs_32aligned_u128 *key, |
95 | const ovs_32aligned_u128 *mask, bool verbose); | |
9daf2348 JS |
96 | static int scan_u128(const char *s, ovs_u128 *value, ovs_u128 *mask); |
97 | ||
5fbe9d15 TA |
98 | static int parse_odp_action(struct parse_odp_context *context, const char *s, |
99 | struct ofpbuf *actions); | |
100 | ||
101 | static int parse_odp_action__(struct parse_odp_context *context, const char *s, | |
c37f7135 AZ |
102 | struct ofpbuf *actions); |
103 | ||
98403001 BP |
104 | /* Returns one the following for the action with the given OVS_ACTION_ATTR_* |
105 | * 'type': | |
106 | * | |
107 | * - For an action whose argument has a fixed length, returned that | |
108 | * nonnegative length in bytes. | |
109 | * | |
6b8da9e9 | 110 | * - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE. |
98403001 | 111 | * |
6b8da9e9 | 112 | * - For an invalid 'type', returns ATTR_LEN_INVALID. */ |
4edb9ae9 | 113 | static int |
cdee00fd BP |
114 | odp_action_len(uint16_t type) |
115 | { | |
df2c07f4 | 116 | if (type > OVS_ACTION_ATTR_MAX) { |
cdee00fd BP |
117 | return -1; |
118 | } | |
119 | ||
09ded0ad | 120 | switch ((enum ovs_action_attr) type) { |
fea393b1 | 121 | case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t); |
9df65060 | 122 | case OVS_ACTION_ATTR_LB_OUTPUT: return sizeof(uint32_t); |
aaca4fe0 | 123 | case OVS_ACTION_ATTR_TRUNC: return sizeof(struct ovs_action_trunc); |
6b8da9e9 | 124 | case OVS_ACTION_ATTR_TUNNEL_PUSH: return ATTR_LEN_VARIABLE; |
a36de779 | 125 | case OVS_ACTION_ATTR_TUNNEL_POP: return sizeof(uint32_t); |
5dddf960 | 126 | case OVS_ACTION_ATTR_METER: return sizeof(uint32_t); |
6b8da9e9 | 127 | case OVS_ACTION_ATTR_USERSPACE: return ATTR_LEN_VARIABLE; |
fea393b1 BP |
128 | case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan); |
129 | case OVS_ACTION_ATTR_POP_VLAN: return 0; | |
b02475c5 SH |
130 | case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls); |
131 | case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16); | |
347bf289 AZ |
132 | case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t); |
133 | case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash); | |
6b8da9e9 JG |
134 | case OVS_ACTION_ATTR_SET: return ATTR_LEN_VARIABLE; |
135 | case OVS_ACTION_ATTR_SET_MASKED: return ATTR_LEN_VARIABLE; | |
136 | case OVS_ACTION_ATTR_SAMPLE: return ATTR_LEN_VARIABLE; | |
07659514 | 137 | case OVS_ACTION_ATTR_CT: return ATTR_LEN_VARIABLE; |
1fe178d2 | 138 | case OVS_ACTION_ATTR_CT_CLEAR: return 0; |
6fcecb85 YY |
139 | case OVS_ACTION_ATTR_PUSH_ETH: return sizeof(struct ovs_action_push_eth); |
140 | case OVS_ACTION_ATTR_POP_ETH: return 0; | |
535e3acf | 141 | case OVS_ACTION_ATTR_CLONE: return ATTR_LEN_VARIABLE; |
f59cb331 YY |
142 | case OVS_ACTION_ATTR_PUSH_NSH: return ATTR_LEN_VARIABLE; |
143 | case OVS_ACTION_ATTR_POP_NSH: return 0; | |
5b34f8fc | 144 | case OVS_ACTION_ATTR_CHECK_PKT_LEN: return ATTR_LEN_VARIABLE; |
a13a0209 | 145 | case OVS_ACTION_ATTR_DROP: return sizeof(uint32_t); |
df2c07f4 JP |
146 | |
147 | case OVS_ACTION_ATTR_UNSPEC: | |
148 | case __OVS_ACTION_ATTR_MAX: | |
6b8da9e9 | 149 | return ATTR_LEN_INVALID; |
cdee00fd BP |
150 | } |
151 | ||
6b8da9e9 | 152 | return ATTR_LEN_INVALID; |
cdee00fd BP |
153 | } |
154 | ||
e6603631 BP |
155 | /* Returns a string form of 'attr'. The return value is either a statically |
156 | * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize' | |
157 | * should be at least OVS_KEY_ATTR_BUFSIZE. */ | |
158 | enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 }; | |
744791b5 | 159 | static const char * |
e6603631 | 160 | ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize) |
744791b5 | 161 | { |
744791b5 BP |
162 | switch (attr) { |
163 | case OVS_KEY_ATTR_UNSPEC: return "unspec"; | |
fea393b1 | 164 | case OVS_KEY_ATTR_ENCAP: return "encap"; |
1b567fb9 | 165 | case OVS_KEY_ATTR_PRIORITY: return "skb_priority"; |
72e8bf28 | 166 | case OVS_KEY_ATTR_SKB_MARK: return "skb_mark"; |
07659514 JS |
167 | case OVS_KEY_ATTR_CT_STATE: return "ct_state"; |
168 | case OVS_KEY_ATTR_CT_ZONE: return "ct_zone"; | |
8e53fe8c | 169 | case OVS_KEY_ATTR_CT_MARK: return "ct_mark"; |
9daf2348 | 170 | case OVS_KEY_ATTR_CT_LABELS: return "ct_label"; |
c30b4cea JR |
171 | case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: return "ct_tuple4"; |
172 | case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: return "ct_tuple6"; | |
9b405f1a | 173 | case OVS_KEY_ATTR_TUNNEL: return "tunnel"; |
744791b5 BP |
174 | case OVS_KEY_ATTR_IN_PORT: return "in_port"; |
175 | case OVS_KEY_ATTR_ETHERNET: return "eth"; | |
fea393b1 | 176 | case OVS_KEY_ATTR_VLAN: return "vlan"; |
744791b5 BP |
177 | case OVS_KEY_ATTR_ETHERTYPE: return "eth_type"; |
178 | case OVS_KEY_ATTR_IPV4: return "ipv4"; | |
179 | case OVS_KEY_ATTR_IPV6: return "ipv6"; | |
180 | case OVS_KEY_ATTR_TCP: return "tcp"; | |
dc235f7f | 181 | case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags"; |
744791b5 | 182 | case OVS_KEY_ATTR_UDP: return "udp"; |
c6bcb685 | 183 | case OVS_KEY_ATTR_SCTP: return "sctp"; |
744791b5 BP |
184 | case OVS_KEY_ATTR_ICMP: return "icmp"; |
185 | case OVS_KEY_ATTR_ICMPV6: return "icmpv6"; | |
186 | case OVS_KEY_ATTR_ARP: return "arp"; | |
187 | case OVS_KEY_ATTR_ND: return "nd"; | |
9b2b8497 | 188 | case OVS_KEY_ATTR_ND_EXTENSIONS: return "nd_ext"; |
b02475c5 | 189 | case OVS_KEY_ATTR_MPLS: return "mpls"; |
572f732a AZ |
190 | case OVS_KEY_ATTR_DP_HASH: return "dp_hash"; |
191 | case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id"; | |
beb75a40 | 192 | case OVS_KEY_ATTR_PACKET_TYPE: return "packet_type"; |
3d2fbd70 | 193 | case OVS_KEY_ATTR_NSH: return "nsh"; |
744791b5 BP |
194 | |
195 | case __OVS_KEY_ATTR_MAX: | |
196 | default: | |
e6603631 BP |
197 | snprintf(namebuf, bufsize, "key%u", (unsigned int) attr); |
198 | return namebuf; | |
744791b5 BP |
199 | } |
200 | } | |
201 | ||
cdee00fd BP |
202 | static void |
203 | format_generic_odp_action(struct ds *ds, const struct nlattr *a) | |
204 | { | |
8ba43fbd BP |
205 | size_t len = nl_attr_get_size(a); |
206 | ||
fd13c6b5 | 207 | ds_put_format(ds, "action%d", nl_attr_type(a)); |
8ba43fbd | 208 | if (len) { |
cdee00fd BP |
209 | const uint8_t *unspec; |
210 | unsigned int i; | |
211 | ||
212 | unspec = nl_attr_get(a); | |
8ba43fbd | 213 | for (i = 0; i < len; i++) { |
cdee00fd BP |
214 | ds_put_char(ds, i ? ' ': '('); |
215 | ds_put_format(ds, "%02x", unspec[i]); | |
216 | } | |
217 | ds_put_char(ds, ')'); | |
218 | } | |
219 | } | |
220 | ||
6ff686f2 | 221 | static void |
0722f341 BP |
222 | format_odp_sample_action(struct ds *ds, const struct nlattr *attr, |
223 | const struct hmap *portno_names) | |
6ff686f2 PS |
224 | { |
225 | static const struct nl_policy ovs_sample_policy[] = { | |
3548d242 BP |
226 | [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 }, |
227 | [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED } | |
6ff686f2 PS |
228 | }; |
229 | struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)]; | |
6ff686f2 PS |
230 | double percentage; |
231 | const struct nlattr *nla_acts; | |
232 | int len; | |
233 | ||
234 | ds_put_cstr(ds, "sample"); | |
235 | ||
5621afff | 236 | if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) { |
6ff686f2 PS |
237 | ds_put_cstr(ds, "(error)"); |
238 | return; | |
239 | } | |
240 | ||
241 | percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) / | |
242 | UINT32_MAX; | |
243 | ||
244 | ds_put_format(ds, "(sample=%.1f%%,", percentage); | |
245 | ||
246 | ds_put_cstr(ds, "actions("); | |
247 | nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]); | |
248 | len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]); | |
0722f341 | 249 | format_odp_actions(ds, nla_acts, len, portno_names); |
6ff686f2 PS |
250 | ds_put_format(ds, "))"); |
251 | } | |
252 | ||
535e3acf | 253 | static void |
0722f341 BP |
254 | format_odp_clone_action(struct ds *ds, const struct nlattr *attr, |
255 | const struct hmap *portno_names) | |
535e3acf AZ |
256 | { |
257 | const struct nlattr *nla_acts = nl_attr_get(attr); | |
258 | int len = nl_attr_get_size(attr); | |
259 | ||
260 | ds_put_cstr(ds, "clone"); | |
261 | ds_put_format(ds, "("); | |
0722f341 | 262 | format_odp_actions(ds, nla_acts, len, portno_names); |
535e3acf AZ |
263 | ds_put_format(ds, ")"); |
264 | } | |
265 | ||
3d2fbd70 JS |
266 | static void |
267 | format_nsh_key(struct ds *ds, const struct ovs_key_nsh *key) | |
268 | { | |
269 | ds_put_format(ds, "flags=%d", key->flags); | |
569c3df2 | 270 | ds_put_format(ds, ",ttl=%d", key->ttl); |
3d2fbd70 JS |
271 | ds_put_format(ds, ",mdtype=%d", key->mdtype); |
272 | ds_put_format(ds, ",np=%d", key->np); | |
273 | ds_put_format(ds, ",spi=0x%x", | |
17553f27 | 274 | nsh_path_hdr_to_spi_uint32(key->path_hdr)); |
3d2fbd70 | 275 | ds_put_format(ds, ",si=%d", |
17553f27 | 276 | nsh_path_hdr_to_si(key->path_hdr)); |
3d2fbd70 JS |
277 | |
278 | switch (key->mdtype) { | |
279 | case NSH_M_TYPE1: | |
280 | for (int i = 0; i < 4; i++) { | |
f59cb331 | 281 | ds_put_format(ds, ",c%d=0x%x", i + 1, ntohl(key->context[i])); |
3d2fbd70 JS |
282 | } |
283 | break; | |
284 | case NSH_M_TYPE2: | |
3d2fbd70 | 285 | default: |
1fc11c59 JS |
286 | /* No support for matching other metadata formats yet. */ |
287 | break; | |
3d2fbd70 JS |
288 | } |
289 | } | |
290 | ||
291 | static void | |
292 | format_uint8_masked(struct ds *s, bool *first, const char *name, | |
293 | uint8_t value, uint8_t mask) | |
294 | { | |
295 | if (mask != 0) { | |
296 | if (!*first) { | |
297 | ds_put_char(s, ','); | |
298 | } | |
299 | ds_put_format(s, "%s=", name); | |
300 | if (mask == UINT8_MAX) { | |
301 | ds_put_format(s, "%"PRIu8, value); | |
302 | } else { | |
303 | ds_put_format(s, "0x%02"PRIx8"/0x%02"PRIx8, value, mask); | |
304 | } | |
305 | *first = false; | |
306 | } | |
307 | } | |
308 | ||
309 | static void | |
310 | format_be32_masked(struct ds *s, bool *first, const char *name, | |
311 | ovs_be32 value, ovs_be32 mask) | |
312 | { | |
313 | if (mask != htonl(0)) { | |
314 | if (!*first) { | |
315 | ds_put_char(s, ','); | |
316 | } | |
317 | ds_put_format(s, "%s=", name); | |
318 | if (mask == OVS_BE32_MAX) { | |
319 | ds_put_format(s, "0x%"PRIx32, ntohl(value)); | |
320 | } else { | |
321 | ds_put_format(s, "0x%"PRIx32"/0x%08"PRIx32, | |
322 | ntohl(value), ntohl(mask)); | |
323 | } | |
324 | *first = false; | |
325 | } | |
326 | } | |
327 | ||
328 | static void | |
329 | format_nsh_key_mask(struct ds *ds, const struct ovs_key_nsh *key, | |
330 | const struct ovs_key_nsh *mask) | |
331 | { | |
332 | if (!mask) { | |
333 | format_nsh_key(ds, key); | |
334 | } else { | |
335 | bool first = true; | |
17553f27 YY |
336 | uint32_t spi = nsh_path_hdr_to_spi_uint32(key->path_hdr); |
337 | uint32_t spi_mask = nsh_path_hdr_to_spi_uint32(mask->path_hdr); | |
338 | if (spi_mask == (NSH_SPI_MASK >> NSH_SPI_SHIFT)) { | |
3d2fbd70 JS |
339 | spi_mask = UINT32_MAX; |
340 | } | |
17553f27 YY |
341 | uint8_t si = nsh_path_hdr_to_si(key->path_hdr); |
342 | uint8_t si_mask = nsh_path_hdr_to_si(mask->path_hdr); | |
3d2fbd70 JS |
343 | |
344 | format_uint8_masked(ds, &first, "flags", key->flags, mask->flags); | |
17553f27 | 345 | format_uint8_masked(ds, &first, "ttl", key->ttl, mask->ttl); |
3d2fbd70 JS |
346 | format_uint8_masked(ds, &first, "mdtype", key->mdtype, mask->mdtype); |
347 | format_uint8_masked(ds, &first, "np", key->np, mask->np); | |
348 | format_be32_masked(ds, &first, "spi", htonl(spi), htonl(spi_mask)); | |
349 | format_uint8_masked(ds, &first, "si", si, si_mask); | |
f59cb331 YY |
350 | format_be32_masked(ds, &first, "c1", key->context[0], |
351 | mask->context[0]); | |
352 | format_be32_masked(ds, &first, "c2", key->context[1], | |
353 | mask->context[1]); | |
354 | format_be32_masked(ds, &first, "c3", key->context[2], | |
355 | mask->context[2]); | |
356 | format_be32_masked(ds, &first, "c4", key->context[3], | |
357 | mask->context[3]); | |
3d2fbd70 JS |
358 | } |
359 | } | |
360 | ||
1fc11c59 | 361 | static void |
f59cb331 YY |
362 | format_odp_push_nsh_action(struct ds *ds, |
363 | const struct nsh_hdr *nsh_hdr) | |
1fc11c59 | 364 | { |
f59cb331 | 365 | size_t mdlen = nsh_hdr_len(nsh_hdr) - NSH_BASE_HDR_LEN; |
17553f27 YY |
366 | uint32_t spi = ntohl(nsh_get_spi(nsh_hdr)); |
367 | uint8_t si = nsh_get_si(nsh_hdr); | |
f59cb331 | 368 | uint8_t flags = nsh_get_flags(nsh_hdr); |
17553f27 | 369 | uint8_t ttl = nsh_get_ttl(nsh_hdr); |
1fc11c59 | 370 | |
f59cb331 YY |
371 | ds_put_cstr(ds, "push_nsh("); |
372 | ds_put_format(ds, "flags=%d", flags); | |
17553f27 | 373 | ds_put_format(ds, ",ttl=%d", ttl); |
f59cb331 YY |
374 | ds_put_format(ds, ",mdtype=%d", nsh_hdr->md_type); |
375 | ds_put_format(ds, ",np=%d", nsh_hdr->next_proto); | |
1fc11c59 JS |
376 | ds_put_format(ds, ",spi=0x%x", spi); |
377 | ds_put_format(ds, ",si=%d", si); | |
f59cb331 | 378 | switch (nsh_hdr->md_type) { |
1fc11c59 | 379 | case NSH_M_TYPE1: { |
f59cb331 | 380 | const struct nsh_md1_ctx *md1_ctx = &nsh_hdr->md1; |
1fc11c59 JS |
381 | for (int i = 0; i < 4; i++) { |
382 | ds_put_format(ds, ",c%d=0x%x", i + 1, | |
f59cb331 | 383 | ntohl(get_16aligned_be32(&md1_ctx->context[i]))); |
1fc11c59 JS |
384 | } |
385 | break; | |
386 | } | |
f59cb331 YY |
387 | case NSH_M_TYPE2: { |
388 | const struct nsh_md2_tlv *md2_ctx = &nsh_hdr->md2; | |
1fc11c59 | 389 | ds_put_cstr(ds, ",md2="); |
f59cb331 | 390 | ds_put_hex(ds, md2_ctx, mdlen); |
1fc11c59 | 391 | break; |
f59cb331 | 392 | } |
1fc11c59 JS |
393 | default: |
394 | OVS_NOT_REACHED(); | |
395 | } | |
396 | ds_put_format(ds, ")"); | |
397 | } | |
398 | ||
6a7e895f | 399 | static const char * |
04594cd5 | 400 | slow_path_reason_to_string(uint32_t reason) |
6a7e895f | 401 | { |
04594cd5 BP |
402 | switch ((enum slow_path_reason) reason) { |
403 | #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING; | |
404 | SLOW_PATH_REASONS | |
405 | #undef SPR | |
6a7e895f | 406 | } |
04594cd5 BP |
407 | |
408 | return NULL; | |
6a7e895f BP |
409 | } |
410 | ||
04594cd5 BP |
411 | const char * |
412 | slow_path_reason_to_explanation(enum slow_path_reason reason) | |
98f0520f | 413 | { |
04594cd5 BP |
414 | switch (reason) { |
415 | #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION; | |
416 | SLOW_PATH_REASONS | |
417 | #undef SPR | |
98f0520f EJ |
418 | } |
419 | ||
04594cd5 | 420 | return "<unknown>"; |
98f0520f EJ |
421 | } |
422 | ||
ec956fc7 | 423 | static int |
8e4c1621 JG |
424 | parse_odp_flags(const char *s, const char *(*bit_to_string)(uint32_t), |
425 | uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask) | |
ec956fc7 | 426 | { |
8e4c1621 JG |
427 | return parse_flags(s, bit_to_string, ')', NULL, NULL, |
428 | res_flags, allowed, res_mask); | |
6a7e895f BP |
429 | } |
430 | ||
98403001 | 431 | static void |
0722f341 BP |
432 | format_odp_userspace_action(struct ds *ds, const struct nlattr *attr, |
433 | const struct hmap *portno_names) | |
98403001 BP |
434 | { |
435 | static const struct nl_policy ovs_userspace_policy[] = { | |
3548d242 BP |
436 | [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 }, |
437 | [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC, | |
438 | .optional = true }, | |
8b7ea2d4 WZ |
439 | [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32, |
440 | .optional = true }, | |
7321bda3 NM |
441 | [OVS_USERSPACE_ATTR_ACTIONS] = { .type = NL_A_UNSPEC, |
442 | .optional = true }, | |
98403001 BP |
443 | }; |
444 | struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)]; | |
e995e3df | 445 | const struct nlattr *userdata_attr; |
8b7ea2d4 | 446 | const struct nlattr *tunnel_out_port_attr; |
98403001 BP |
447 | |
448 | if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) { | |
449 | ds_put_cstr(ds, "userspace(error)"); | |
450 | return; | |
451 | } | |
452 | ||
453 | ds_put_format(ds, "userspace(pid=%"PRIu32, | |
454 | nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID])); | |
455 | ||
e995e3df | 456 | userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA]; |
29089a54 RL |
457 | |
458 | if (userdata_attr) { | |
459 | const uint8_t *userdata = nl_attr_get(userdata_attr); | |
460 | size_t userdata_len = nl_attr_get_size(userdata_attr); | |
461 | bool userdata_unspec = true; | |
8de6ff3e | 462 | struct user_action_cookie cookie; |
98403001 | 463 | |
8de6ff3e JP |
464 | if (userdata_len == sizeof cookie) { |
465 | memcpy(&cookie, userdata, sizeof cookie); | |
29089a54 RL |
466 | |
467 | userdata_unspec = false; | |
468 | ||
8de6ff3e | 469 | if (cookie.type == USER_ACTION_COOKIE_SFLOW) { |
29089a54 | 470 | ds_put_format(ds, ",sFlow(" |
fd13c6b5 | 471 | "vid=%"PRIu16",pcp=%d,output=%"PRIu32")", |
29089a54 RL |
472 | vlan_tci_to_vid(cookie.sflow.vlan_tci), |
473 | vlan_tci_to_pcp(cookie.sflow.vlan_tci), | |
474 | cookie.sflow.output); | |
8de6ff3e | 475 | } else if (cookie.type == USER_ACTION_COOKIE_SLOW_PATH) { |
04594cd5 BP |
476 | ds_put_cstr(ds, ",slow_path("); |
477 | format_flags(ds, slow_path_reason_to_string, | |
478 | cookie.slow_path.reason, ','); | |
479 | ds_put_format(ds, ")"); | |
8de6ff3e | 480 | } else if (cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) { |
29089a54 RL |
481 | ds_put_format(ds, ",flow_sample(probability=%"PRIu16 |
482 | ",collector_set_id=%"PRIu32 | |
483 | ",obs_domain_id=%"PRIu32 | |
f69f713b | 484 | ",obs_point_id=%"PRIu32 |
0722f341 | 485 | ",output_port=", |
29089a54 RL |
486 | cookie.flow_sample.probability, |
487 | cookie.flow_sample.collector_set_id, | |
488 | cookie.flow_sample.obs_domain_id, | |
0722f341 BP |
489 | cookie.flow_sample.obs_point_id); |
490 | odp_portno_name_format(portno_names, | |
491 | cookie.flow_sample.output_odp_port, ds); | |
4930ea56 BP |
492 | if (cookie.flow_sample.direction == NX_ACTION_SAMPLE_INGRESS) { |
493 | ds_put_cstr(ds, ",ingress"); | |
494 | } else if (cookie.flow_sample.direction == NX_ACTION_SAMPLE_EGRESS) { | |
495 | ds_put_cstr(ds, ",egress"); | |
496 | } | |
497 | ds_put_char(ds, ')'); | |
8de6ff3e | 498 | } else if (cookie.type == USER_ACTION_COOKIE_IPFIX) { |
0722f341 BP |
499 | ds_put_format(ds, ",ipfix(output_port="); |
500 | odp_portno_name_format(portno_names, | |
501 | cookie.ipfix.output_odp_port, ds); | |
502 | ds_put_char(ds, ')'); | |
d39ec23d JP |
503 | } else if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) { |
504 | ds_put_format(ds, ",controller(reason=%"PRIu16 | |
68c5c73b AC |
505 | ",dont_send=%d" |
506 | ",continuation=%d" | |
d39ec23d JP |
507 | ",recirc_id=%"PRIu32 |
508 | ",rule_cookie=%#"PRIx64 | |
509 | ",controller_id=%"PRIu16 | |
510 | ",max_len=%"PRIu16, | |
511 | cookie.controller.reason, | |
abaf6dcf YS |
512 | !!cookie.controller.dont_send, |
513 | !!cookie.controller.continuation, | |
d39ec23d JP |
514 | cookie.controller.recirc_id, |
515 | ntohll(get_32aligned_be64( | |
516 | &cookie.controller.rule_cookie)), | |
517 | cookie.controller.controller_id, | |
518 | cookie.controller.max_len); | |
519 | ds_put_char(ds, ')'); | |
29089a54 RL |
520 | } else { |
521 | userdata_unspec = true; | |
522 | } | |
98403001 | 523 | } |
e995e3df | 524 | |
29089a54 RL |
525 | if (userdata_unspec) { |
526 | size_t i; | |
527 | ds_put_format(ds, ",userdata("); | |
528 | for (i = 0; i < userdata_len; i++) { | |
529 | ds_put_format(ds, "%02x", userdata[i]); | |
530 | } | |
531 | ds_put_char(ds, ')'); | |
e995e3df | 532 | } |
98403001 BP |
533 | } |
534 | ||
7321bda3 NM |
535 | if (a[OVS_USERSPACE_ATTR_ACTIONS]) { |
536 | ds_put_cstr(ds, ",actions"); | |
537 | } | |
538 | ||
8b7ea2d4 WZ |
539 | tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT]; |
540 | if (tunnel_out_port_attr) { | |
0722f341 BP |
541 | ds_put_format(ds, ",tunnel_out_port="); |
542 | odp_portno_name_format(portno_names, | |
543 | nl_attr_get_odp_port(tunnel_out_port_attr), ds); | |
8b7ea2d4 WZ |
544 | } |
545 | ||
98403001 BP |
546 | ds_put_char(ds, ')'); |
547 | } | |
548 | ||
8ddc056d | 549 | static void |
2d18eae8 | 550 | format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose) |
8ddc056d | 551 | { |
2d18eae8 JR |
552 | if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) { |
553 | ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci)); | |
554 | if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */ | |
555 | ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask)); | |
556 | }; | |
557 | ds_put_char(ds, ','); | |
558 | } | |
559 | if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) { | |
560 | ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci)); | |
561 | if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) { | |
562 | ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask)); | |
563 | } | |
564 | ds_put_char(ds, ','); | |
565 | } | |
566 | if (!(tci & htons(VLAN_CFI))) { | |
567 | ds_put_cstr(ds, "cfi=0"); | |
568 | ds_put_char(ds, ','); | |
8ddc056d | 569 | } |
2d18eae8 | 570 | ds_chomp(ds, ','); |
8ddc056d BP |
571 | } |
572 | ||
b02475c5 SH |
573 | static void |
574 | format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse) | |
575 | { | |
576 | ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d", | |
577 | mpls_lse_to_label(mpls_lse), | |
578 | mpls_lse_to_tc(mpls_lse), | |
579 | mpls_lse_to_ttl(mpls_lse), | |
580 | mpls_lse_to_bos(mpls_lse)); | |
581 | } | |
582 | ||
e6cc0bab AZ |
583 | static void |
584 | format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key, | |
8bfd0fda | 585 | const struct ovs_key_mpls *mpls_mask, int n) |
e6cc0bab | 586 | { |
8f79bb4d JR |
587 | for (int i = 0; i < n; i++) { |
588 | ovs_be32 key = mpls_key[i].mpls_lse; | |
e6cc0bab | 589 | |
8bfd0fda BP |
590 | if (mpls_mask == NULL) { |
591 | format_mpls_lse(ds, key); | |
592 | } else { | |
8f79bb4d | 593 | ovs_be32 mask = mpls_mask[i].mpls_lse; |
8bfd0fda BP |
594 | |
595 | ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x", | |
596 | mpls_lse_to_label(key), mpls_lse_to_label(mask), | |
597 | mpls_lse_to_tc(key), mpls_lse_to_tc(mask), | |
598 | mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask), | |
599 | mpls_lse_to_bos(key), mpls_lse_to_bos(mask)); | |
600 | } | |
8f79bb4d | 601 | ds_put_char(ds, ','); |
e6cc0bab | 602 | } |
8f79bb4d | 603 | ds_chomp(ds, ','); |
e6cc0bab AZ |
604 | } |
605 | ||
572f732a | 606 | static void |
347bf289 | 607 | format_odp_recirc_action(struct ds *ds, uint32_t recirc_id) |
572f732a | 608 | { |
8f19f0a7 | 609 | ds_put_format(ds, "recirc(%#"PRIx32")", recirc_id); |
347bf289 | 610 | } |
572f732a | 611 | |
347bf289 AZ |
612 | static void |
613 | format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act) | |
614 | { | |
615 | ds_put_format(ds, "hash("); | |
572f732a | 616 | |
347bf289 | 617 | if (hash_act->hash_alg == OVS_HASH_ALG_L4) { |
2e3fd24c JS |
618 | ds_put_format(ds, "l4(%"PRIu32")", hash_act->hash_basis); |
619 | } else if (hash_act->hash_alg == OVS_HASH_ALG_SYM_L4) { | |
620 | ds_put_format(ds, "sym_l4(%"PRIu32")", hash_act->hash_basis); | |
347bf289 AZ |
621 | } else { |
622 | ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")", | |
623 | hash_act->hash_alg); | |
624 | } | |
625 | ds_put_format(ds, ")"); | |
572f732a AZ |
626 | } |
627 | ||
e066f78f | 628 | static const void * |
370e373b | 629 | format_udp_tnl_push_header(struct ds *ds, const struct udp_header *udp) |
e066f78f | 630 | { |
8e45fe7c JG |
631 | ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16",csum=0x%"PRIx16"),", |
632 | ntohs(udp->udp_src), ntohs(udp->udp_dst), | |
633 | ntohs(udp->udp_csum)); | |
e066f78f JG |
634 | |
635 | return udp + 1; | |
636 | } | |
637 | ||
a36de779 PS |
638 | static void |
639 | format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data) | |
640 | { | |
641 | const struct eth_header *eth; | |
a36de779 | 642 | const void *l3; |
370e373b TLSC |
643 | const void *l4; |
644 | const struct udp_header *udp; | |
a36de779 PS |
645 | |
646 | eth = (const struct eth_header *)data->header; | |
647 | ||
648 | l3 = eth + 1; | |
a36de779 PS |
649 | |
650 | /* Ethernet */ | |
fd13c6b5 | 651 | ds_put_format(ds, "header(size=%"PRIu32",type=%"PRIu32",eth(dst=", |
a36de779 PS |
652 | data->header_len, data->tnl_type); |
653 | ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_dst)); | |
654 | ds_put_format(ds, ",src="); | |
655 | ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src)); | |
656 | ds_put_format(ds, ",dl_type=0x%04"PRIx16"),", ntohs(eth->eth_type)); | |
657 | ||
370e373b TLSC |
658 | if (eth->eth_type == htons(ETH_TYPE_IP)) { |
659 | /* IPv4 */ | |
8568c7a5 | 660 | const struct ip_header *ip = l3; |
370e373b TLSC |
661 | ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8 |
662 | ",tos=%#"PRIx8",ttl=%"PRIu8",frag=0x%"PRIx16"),", | |
663 | IP_ARGS(get_16aligned_be32(&ip->ip_src)), | |
664 | IP_ARGS(get_16aligned_be32(&ip->ip_dst)), | |
665 | ip->ip_proto, ip->ip_tos, | |
666 | ip->ip_ttl, | |
fa47c114 | 667 | ntohs(ip->ip_frag_off)); |
370e373b TLSC |
668 | l4 = (ip + 1); |
669 | } else { | |
8568c7a5 BP |
670 | const struct ovs_16aligned_ip6_hdr *ip6 = l3; |
671 | struct in6_addr src, dst; | |
672 | memcpy(&src, &ip6->ip6_src, sizeof src); | |
673 | memcpy(&dst, &ip6->ip6_dst, sizeof dst); | |
674 | uint32_t ipv6_flow = ntohl(get_16aligned_be32(&ip6->ip6_flow)); | |
675 | ||
370e373b | 676 | ds_put_format(ds, "ipv6(src="); |
8568c7a5 | 677 | ipv6_format_addr(&src, ds); |
370e373b | 678 | ds_put_format(ds, ",dst="); |
8568c7a5 | 679 | ipv6_format_addr(&dst, ds); |
fd13c6b5 | 680 | ds_put_format(ds, ",label=%i,proto=%"PRIu8",tclass=0x%"PRIx32 |
370e373b | 681 | ",hlimit=%"PRIu8"),", |
8568c7a5 BP |
682 | ipv6_flow & IPV6_LABEL_MASK, ip6->ip6_nxt, |
683 | (ipv6_flow >> 20) & 0xff, ip6->ip6_hlim); | |
370e373b TLSC |
684 | l4 = (ip6 + 1); |
685 | } | |
686 | ||
687 | udp = (const struct udp_header *) l4; | |
a36de779 PS |
688 | |
689 | if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) { | |
690 | const struct vxlanhdr *vxh; | |
a36de779 | 691 | |
370e373b | 692 | vxh = format_udp_tnl_push_header(ds, udp); |
a36de779 | 693 | |
a36de779 PS |
694 | ds_put_format(ds, "vxlan(flags=0x%"PRIx32",vni=0x%"PRIx32")", |
695 | ntohl(get_16aligned_be32(&vxh->vx_flags)), | |
a92a3aae | 696 | ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8); |
e5a1caee JG |
697 | } else if (data->tnl_type == OVS_VPORT_TYPE_GENEVE) { |
698 | const struct genevehdr *gnh; | |
699 | ||
370e373b | 700 | gnh = format_udp_tnl_push_header(ds, udp); |
e5a1caee | 701 | |
5bb08b0e | 702 | ds_put_format(ds, "geneve(%s%svni=0x%"PRIx32, |
e5a1caee | 703 | gnh->oam ? "oam," : "", |
5bb08b0e | 704 | gnh->critical ? "crit," : "", |
e5a1caee | 705 | ntohl(get_16aligned_be32(&gnh->vni)) >> 8); |
1fc11c59 | 706 | |
5bb08b0e JG |
707 | if (gnh->opt_len) { |
708 | ds_put_cstr(ds, ",options("); | |
709 | format_geneve_opts(gnh->options, NULL, gnh->opt_len * 4, | |
710 | ds, false); | |
711 | ds_put_char(ds, ')'); | |
712 | } | |
713 | ||
714 | ds_put_char(ds, ')'); | |
3b10ceee GR |
715 | } else if (data->tnl_type == OVS_VPORT_TYPE_GRE || |
716 | data->tnl_type == OVS_VPORT_TYPE_IP6GRE) { | |
a36de779 PS |
717 | const struct gre_base_hdr *greh; |
718 | ovs_16aligned_be32 *options; | |
a36de779 | 719 | |
a36de779 PS |
720 | greh = (const struct gre_base_hdr *) l4; |
721 | ||
722 | ds_put_format(ds, "gre((flags=0x%"PRIx16",proto=0x%"PRIx16")", | |
e8fe6ad0 | 723 | ntohs(greh->flags), ntohs(greh->protocol)); |
a36de779 PS |
724 | options = (ovs_16aligned_be32 *)(greh + 1); |
725 | if (greh->flags & htons(GRE_CSUM)) { | |
d804d31e | 726 | ds_put_format(ds, ",csum=0x%"PRIx16, ntohs(*((ovs_be16 *)options))); |
a36de779 PS |
727 | options++; |
728 | } | |
729 | if (greh->flags & htons(GRE_KEY)) { | |
730 | ds_put_format(ds, ",key=0x%"PRIx32, ntohl(get_16aligned_be32(options))); | |
731 | options++; | |
732 | } | |
733 | if (greh->flags & htons(GRE_SEQ)) { | |
734 | ds_put_format(ds, ",seq=0x%"PRIx32, ntohl(get_16aligned_be32(options))); | |
735 | options++; | |
736 | } | |
737 | ds_put_format(ds, ")"); | |
7dc18ae9 WT |
738 | } else if (data->tnl_type == OVS_VPORT_TYPE_ERSPAN || |
739 | data->tnl_type == OVS_VPORT_TYPE_IP6ERSPAN) { | |
740 | const struct gre_base_hdr *greh; | |
741 | const struct erspan_base_hdr *ersh; | |
742 | ||
743 | greh = (const struct gre_base_hdr *) l4; | |
744 | ersh = ERSPAN_HDR(greh); | |
745 | ||
746 | if (ersh->ver == 1) { | |
747 | ovs_16aligned_be32 *index = ALIGNED_CAST(ovs_16aligned_be32 *, | |
748 | ersh + 1); | |
749 | ds_put_format(ds, "erspan(ver=1,sid=0x%"PRIx16",idx=0x%"PRIx32")", | |
750 | get_sid(ersh), ntohl(get_16aligned_be32(index))); | |
751 | } else if (ersh->ver == 2) { | |
752 | struct erspan_md2 *md2 = ALIGNED_CAST(struct erspan_md2 *, | |
753 | ersh + 1); | |
754 | ds_put_format(ds, "erspan(ver=2,sid=0x%"PRIx16 | |
755 | ",dir=%"PRIu8",hwid=0x%"PRIx8")", | |
756 | get_sid(ersh), md2->dir, get_hwid(md2)); | |
757 | } else { | |
758 | VLOG_WARN("%s Invalid ERSPAN version %d\n", __func__, ersh->ver); | |
759 | } | |
3c6d05a0 WT |
760 | } else if (data->tnl_type == OVS_VPORT_TYPE_GTPU) { |
761 | const struct gtpuhdr *gtph; | |
762 | ||
763 | gtph = format_udp_tnl_push_header(ds, udp); | |
764 | ||
765 | ds_put_format(ds, "gtpu(flags=0x%"PRIx8 | |
766 | ",msgtype=%"PRIu8",teid=0x%"PRIx32")", | |
767 | gtph->md.flags, gtph->md.msgtype, | |
768 | ntohl(get_16aligned_be32(>ph->teid))); | |
a36de779 | 769 | } |
3c6d05a0 | 770 | |
a36de779 PS |
771 | ds_put_format(ds, ")"); |
772 | } | |
773 | ||
774 | static void | |
0722f341 BP |
775 | format_odp_tnl_push_action(struct ds *ds, const struct nlattr *attr, |
776 | const struct hmap *portno_names) | |
a36de779 PS |
777 | { |
778 | struct ovs_action_push_tnl *data; | |
779 | ||
780 | data = (struct ovs_action_push_tnl *) nl_attr_get(attr); | |
781 | ||
0722f341 BP |
782 | ds_put_cstr(ds, "tnl_push(tnl_port("); |
783 | odp_portno_name_format(portno_names, data->tnl_port, ds); | |
784 | ds_put_cstr(ds, "),"); | |
a36de779 | 785 | format_odp_tnl_push_header(ds, data); |
0722f341 BP |
786 | ds_put_format(ds, ",out_port("); |
787 | odp_portno_name_format(portno_names, data->out_port, ds); | |
788 | ds_put_cstr(ds, "))"); | |
a36de779 PS |
789 | } |
790 | ||
9ac0aada JR |
791 | static const struct nl_policy ovs_nat_policy[] = { |
792 | [OVS_NAT_ATTR_SRC] = { .type = NL_A_FLAG, .optional = true, }, | |
793 | [OVS_NAT_ATTR_DST] = { .type = NL_A_FLAG, .optional = true, }, | |
794 | [OVS_NAT_ATTR_IP_MIN] = { .type = NL_A_UNSPEC, .optional = true, | |
795 | .min_len = sizeof(struct in_addr), | |
796 | .max_len = sizeof(struct in6_addr)}, | |
797 | [OVS_NAT_ATTR_IP_MAX] = { .type = NL_A_UNSPEC, .optional = true, | |
798 | .min_len = sizeof(struct in_addr), | |
799 | .max_len = sizeof(struct in6_addr)}, | |
800 | [OVS_NAT_ATTR_PROTO_MIN] = { .type = NL_A_U16, .optional = true, }, | |
801 | [OVS_NAT_ATTR_PROTO_MAX] = { .type = NL_A_U16, .optional = true, }, | |
802 | [OVS_NAT_ATTR_PERSISTENT] = { .type = NL_A_FLAG, .optional = true, }, | |
803 | [OVS_NAT_ATTR_PROTO_HASH] = { .type = NL_A_FLAG, .optional = true, }, | |
804 | [OVS_NAT_ATTR_PROTO_RANDOM] = { .type = NL_A_FLAG, .optional = true, }, | |
805 | }; | |
806 | ||
807 | static void | |
808 | format_odp_ct_nat(struct ds *ds, const struct nlattr *attr) | |
809 | { | |
810 | struct nlattr *a[ARRAY_SIZE(ovs_nat_policy)]; | |
811 | size_t addr_len; | |
812 | ovs_be32 ip_min, ip_max; | |
813 | struct in6_addr ip6_min, ip6_max; | |
814 | uint16_t proto_min, proto_max; | |
815 | ||
816 | if (!nl_parse_nested(attr, ovs_nat_policy, a, ARRAY_SIZE(a))) { | |
817 | ds_put_cstr(ds, "nat(error: nl_parse_nested() failed.)"); | |
818 | return; | |
819 | } | |
820 | /* If no type, then nothing else either. */ | |
821 | if (!(a[OVS_NAT_ATTR_SRC] || a[OVS_NAT_ATTR_DST]) | |
822 | && (a[OVS_NAT_ATTR_IP_MIN] || a[OVS_NAT_ATTR_IP_MAX] | |
823 | || a[OVS_NAT_ATTR_PROTO_MIN] || a[OVS_NAT_ATTR_PROTO_MAX] | |
824 | || a[OVS_NAT_ATTR_PERSISTENT] || a[OVS_NAT_ATTR_PROTO_HASH] | |
825 | || a[OVS_NAT_ATTR_PROTO_RANDOM])) { | |
826 | ds_put_cstr(ds, "nat(error: options allowed only with \"src\" or \"dst\")"); | |
827 | return; | |
828 | } | |
829 | /* Both SNAT & DNAT may not be specified. */ | |
830 | if (a[OVS_NAT_ATTR_SRC] && a[OVS_NAT_ATTR_DST]) { | |
831 | ds_put_cstr(ds, "nat(error: Only one of \"src\" or \"dst\" may be present.)"); | |
832 | return; | |
833 | } | |
834 | /* proto may not appear without ip. */ | |
835 | if (!a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_PROTO_MIN]) { | |
836 | ds_put_cstr(ds, "nat(error: proto but no IP.)"); | |
837 | return; | |
838 | } | |
839 | /* MAX may not appear without MIN. */ | |
840 | if ((!a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_IP_MAX]) | |
841 | || (!a[OVS_NAT_ATTR_PROTO_MIN] && a[OVS_NAT_ATTR_PROTO_MAX])) { | |
842 | ds_put_cstr(ds, "nat(error: range max without min.)"); | |
843 | return; | |
844 | } | |
845 | /* Address sizes must match. */ | |
846 | if ((a[OVS_NAT_ATTR_IP_MIN] | |
847 | && (nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) != sizeof(ovs_be32) && | |
848 | nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) != sizeof(struct in6_addr))) | |
849 | || (a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_IP_MAX] | |
850 | && (nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) | |
851 | != nl_attr_get_size(a[OVS_NAT_ATTR_IP_MAX])))) { | |
852 | ds_put_cstr(ds, "nat(error: IP address sizes do not match)"); | |
853 | return; | |
854 | } | |
855 | ||
856 | addr_len = a[OVS_NAT_ATTR_IP_MIN] | |
857 | ? nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) : 0; | |
858 | ip_min = addr_len == sizeof(ovs_be32) && a[OVS_NAT_ATTR_IP_MIN] | |
859 | ? nl_attr_get_be32(a[OVS_NAT_ATTR_IP_MIN]) : 0; | |
860 | ip_max = addr_len == sizeof(ovs_be32) && a[OVS_NAT_ATTR_IP_MAX] | |
861 | ? nl_attr_get_be32(a[OVS_NAT_ATTR_IP_MAX]) : 0; | |
862 | if (addr_len == sizeof ip6_min) { | |
863 | ip6_min = a[OVS_NAT_ATTR_IP_MIN] | |
864 | ? *(struct in6_addr *)nl_attr_get(a[OVS_NAT_ATTR_IP_MIN]) | |
865 | : in6addr_any; | |
866 | ip6_max = a[OVS_NAT_ATTR_IP_MAX] | |
867 | ? *(struct in6_addr *)nl_attr_get(a[OVS_NAT_ATTR_IP_MAX]) | |
868 | : in6addr_any; | |
869 | } | |
870 | proto_min = a[OVS_NAT_ATTR_PROTO_MIN] | |
871 | ? nl_attr_get_u16(a[OVS_NAT_ATTR_PROTO_MIN]) : 0; | |
872 | proto_max = a[OVS_NAT_ATTR_PROTO_MAX] | |
873 | ? nl_attr_get_u16(a[OVS_NAT_ATTR_PROTO_MAX]) : 0; | |
874 | ||
875 | if ((addr_len == sizeof(ovs_be32) | |
876 | && ip_max && ntohl(ip_min) > ntohl(ip_max)) | |
877 | || (addr_len == sizeof(struct in6_addr) | |
878 | && !ipv6_mask_is_any(&ip6_max) | |
879 | && memcmp(&ip6_min, &ip6_max, sizeof ip6_min) > 0) | |
880 | || (proto_max && proto_min > proto_max)) { | |
881 | ds_put_cstr(ds, "nat(range error)"); | |
882 | return; | |
883 | } | |
884 | ||
885 | ds_put_cstr(ds, "nat"); | |
886 | if (a[OVS_NAT_ATTR_SRC] || a[OVS_NAT_ATTR_DST]) { | |
887 | ds_put_char(ds, '('); | |
888 | if (a[OVS_NAT_ATTR_SRC]) { | |
889 | ds_put_cstr(ds, "src"); | |
890 | } else if (a[OVS_NAT_ATTR_DST]) { | |
891 | ds_put_cstr(ds, "dst"); | |
892 | } | |
893 | ||
894 | if (addr_len > 0) { | |
895 | ds_put_cstr(ds, "="); | |
896 | ||
897 | if (addr_len == sizeof ip_min) { | |
898 | ds_put_format(ds, IP_FMT, IP_ARGS(ip_min)); | |
899 | ||
900 | if (ip_max && ip_max != ip_min) { | |
901 | ds_put_format(ds, "-"IP_FMT, IP_ARGS(ip_max)); | |
902 | } | |
903 | } else if (addr_len == sizeof ip6_min) { | |
904 | ipv6_format_addr_bracket(&ip6_min, ds, proto_min); | |
905 | ||
906 | if (!ipv6_mask_is_any(&ip6_max) && | |
907 | memcmp(&ip6_max, &ip6_min, sizeof ip6_max) != 0) { | |
908 | ds_put_char(ds, '-'); | |
909 | ipv6_format_addr_bracket(&ip6_max, ds, proto_min); | |
910 | } | |
911 | } | |
912 | if (proto_min) { | |
913 | ds_put_format(ds, ":%"PRIu16, proto_min); | |
914 | ||
915 | if (proto_max && proto_max != proto_min) { | |
916 | ds_put_format(ds, "-%"PRIu16, proto_max); | |
917 | } | |
918 | } | |
919 | } | |
920 | ds_put_char(ds, ','); | |
921 | if (a[OVS_NAT_ATTR_PERSISTENT]) { | |
922 | ds_put_cstr(ds, "persistent,"); | |
923 | } | |
924 | if (a[OVS_NAT_ATTR_PROTO_HASH]) { | |
925 | ds_put_cstr(ds, "hash,"); | |
926 | } | |
927 | if (a[OVS_NAT_ATTR_PROTO_RANDOM]) { | |
928 | ds_put_cstr(ds, "random,"); | |
929 | } | |
930 | ds_chomp(ds, ','); | |
931 | ds_put_char(ds, ')'); | |
932 | } | |
933 | } | |
934 | ||
07659514 JS |
935 | static const struct nl_policy ovs_conntrack_policy[] = { |
936 | [OVS_CT_ATTR_COMMIT] = { .type = NL_A_FLAG, .optional = true, }, | |
a76a37ef | 937 | [OVS_CT_ATTR_FORCE_COMMIT] = { .type = NL_A_FLAG, .optional = true, }, |
07659514 | 938 | [OVS_CT_ATTR_ZONE] = { .type = NL_A_U16, .optional = true, }, |
8e53fe8c JS |
939 | [OVS_CT_ATTR_MARK] = { .type = NL_A_UNSPEC, .optional = true, |
940 | .min_len = sizeof(uint32_t) * 2 }, | |
9daf2348 JS |
941 | [OVS_CT_ATTR_LABELS] = { .type = NL_A_UNSPEC, .optional = true, |
942 | .min_len = sizeof(struct ovs_key_ct_labels) * 2 }, | |
d787ad39 JS |
943 | [OVS_CT_ATTR_HELPER] = { .type = NL_A_STRING, .optional = true, |
944 | .min_len = 1, .max_len = 16 }, | |
9ac0aada | 945 | [OVS_CT_ATTR_NAT] = { .type = NL_A_UNSPEC, .optional = true }, |
ebe62ec1 YHW |
946 | [OVS_CT_ATTR_TIMEOUT] = { .type = NL_A_STRING, .optional = true, |
947 | .min_len = 1, .max_len = 32 }, | |
07659514 JS |
948 | }; |
949 | ||
950 | static void | |
951 | format_odp_conntrack_action(struct ds *ds, const struct nlattr *attr) | |
952 | { | |
953 | struct nlattr *a[ARRAY_SIZE(ovs_conntrack_policy)]; | |
89cf41ec BP |
954 | const struct { |
955 | ovs_32aligned_u128 value; | |
956 | ovs_32aligned_u128 mask; | |
957 | } *label; | |
8e53fe8c | 958 | const uint32_t *mark; |
ebe62ec1 | 959 | const char *helper, *timeout; |
07659514 | 960 | uint16_t zone; |
a76a37ef | 961 | bool commit, force; |
9ac0aada | 962 | const struct nlattr *nat; |
07659514 JS |
963 | |
964 | if (!nl_parse_nested(attr, ovs_conntrack_policy, a, ARRAY_SIZE(a))) { | |
965 | ds_put_cstr(ds, "ct(error)"); | |
966 | return; | |
967 | } | |
968 | ||
969 | commit = a[OVS_CT_ATTR_COMMIT] ? true : false; | |
a76a37ef | 970 | force = a[OVS_CT_ATTR_FORCE_COMMIT] ? true : false; |
07659514 | 971 | zone = a[OVS_CT_ATTR_ZONE] ? nl_attr_get_u16(a[OVS_CT_ATTR_ZONE]) : 0; |
8e53fe8c | 972 | mark = a[OVS_CT_ATTR_MARK] ? nl_attr_get(a[OVS_CT_ATTR_MARK]) : NULL; |
9daf2348 | 973 | label = a[OVS_CT_ATTR_LABELS] ? nl_attr_get(a[OVS_CT_ATTR_LABELS]): NULL; |
d787ad39 | 974 | helper = a[OVS_CT_ATTR_HELPER] ? nl_attr_get(a[OVS_CT_ATTR_HELPER]) : NULL; |
ebe62ec1 YHW |
975 | timeout = a[OVS_CT_ATTR_TIMEOUT] ? |
976 | nl_attr_get(a[OVS_CT_ATTR_TIMEOUT]) : NULL; | |
9ac0aada | 977 | nat = a[OVS_CT_ATTR_NAT]; |
07659514 JS |
978 | |
979 | ds_put_format(ds, "ct"); | |
ebe62ec1 | 980 | if (commit || force || zone || mark || label || helper || timeout || nat) { |
07659514 JS |
981 | ds_put_cstr(ds, "("); |
982 | if (commit) { | |
983 | ds_put_format(ds, "commit,"); | |
984 | } | |
a76a37ef JR |
985 | if (force) { |
986 | ds_put_format(ds, "force_commit,"); | |
987 | } | |
07659514 JS |
988 | if (zone) { |
989 | ds_put_format(ds, "zone=%"PRIu16",", zone); | |
990 | } | |
8e53fe8c JS |
991 | if (mark) { |
992 | ds_put_format(ds, "mark=%#"PRIx32"/%#"PRIx32",", *mark, | |
993 | *(mark + 1)); | |
994 | } | |
9daf2348 JS |
995 | if (label) { |
996 | ds_put_format(ds, "label="); | |
89cf41ec | 997 | format_u128(ds, &label->value, &label->mask, true); |
121cf327 | 998 | ds_put_char(ds, ','); |
9daf2348 | 999 | } |
d787ad39 JS |
1000 | if (helper) { |
1001 | ds_put_format(ds, "helper=%s,", helper); | |
1002 | } | |
ebe62ec1 YHW |
1003 | if (timeout) { |
1004 | ds_put_format(ds, "timeout=%s", timeout); | |
1005 | } | |
9ac0aada JR |
1006 | if (nat) { |
1007 | format_odp_ct_nat(ds, nat); | |
1008 | } | |
07659514 JS |
1009 | ds_chomp(ds, ','); |
1010 | ds_put_cstr(ds, ")"); | |
1011 | } | |
1012 | } | |
1013 | ||
81fdabb9 YY |
1014 | static const struct attr_len_tbl |
1015 | ovs_nsh_key_attr_lens[OVS_NSH_KEY_ATTR_MAX + 1] = { | |
1016 | [OVS_NSH_KEY_ATTR_BASE] = { .len = 8 }, | |
1017 | [OVS_NSH_KEY_ATTR_MD1] = { .len = 16 }, | |
1018 | [OVS_NSH_KEY_ATTR_MD2] = { .len = ATTR_LEN_VARIABLE }, | |
1019 | }; | |
1020 | ||
1021 | static void | |
1022 | format_odp_set_nsh(struct ds *ds, const struct nlattr *attr) | |
1023 | { | |
1024 | unsigned int left; | |
1025 | const struct nlattr *a; | |
1026 | struct ovs_key_nsh nsh; | |
1027 | struct ovs_key_nsh nsh_mask; | |
1028 | ||
1029 | memset(&nsh, 0, sizeof nsh); | |
1030 | memset(&nsh_mask, 0xff, sizeof nsh_mask); | |
1031 | ||
1032 | NL_NESTED_FOR_EACH (a, left, attr) { | |
1033 | enum ovs_nsh_key_attr type = nl_attr_type(a); | |
1034 | size_t len = nl_attr_get_size(a); | |
1035 | ||
1036 | if (type >= OVS_NSH_KEY_ATTR_MAX) { | |
1037 | return; | |
1038 | } | |
1039 | ||
1040 | int expected_len = ovs_nsh_key_attr_lens[type].len; | |
1041 | if ((expected_len != ATTR_LEN_VARIABLE) && (len != 2 * expected_len)) { | |
1042 | return; | |
1043 | } | |
1044 | ||
1045 | switch (type) { | |
1046 | case OVS_NSH_KEY_ATTR_UNSPEC: | |
1047 | break; | |
1048 | case OVS_NSH_KEY_ATTR_BASE: { | |
1049 | const struct ovs_nsh_key_base *base = nl_attr_get(a); | |
1050 | const struct ovs_nsh_key_base *base_mask = base + 1; | |
1051 | memcpy(&nsh, base, sizeof(*base)); | |
1052 | memcpy(&nsh_mask, base_mask, sizeof(*base_mask)); | |
1053 | break; | |
1054 | } | |
1055 | case OVS_NSH_KEY_ATTR_MD1: { | |
1056 | const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a); | |
1057 | const struct ovs_nsh_key_md1 *md1_mask = md1 + 1; | |
1058 | memcpy(&nsh.context, &md1->context, sizeof(*md1)); | |
1059 | memcpy(&nsh_mask.context, &md1_mask->context, sizeof(*md1_mask)); | |
1060 | break; | |
1061 | } | |
1062 | case OVS_NSH_KEY_ATTR_MD2: | |
1063 | case __OVS_NSH_KEY_ATTR_MAX: | |
1064 | default: | |
1065 | /* No support for matching other metadata formats yet. */ | |
1066 | break; | |
1067 | } | |
1068 | } | |
1069 | ||
1070 | ds_put_cstr(ds, "set(nsh("); | |
1071 | format_nsh_key_mask(ds, &nsh, &nsh_mask); | |
1072 | ds_put_cstr(ds, "))"); | |
1073 | } | |
1074 | ||
5b34f8fc NS |
1075 | static void |
1076 | format_odp_check_pkt_len_action(struct ds *ds, const struct nlattr *attr, | |
1077 | const struct hmap *portno_names OVS_UNUSED) | |
1078 | { | |
1079 | static const struct nl_policy ovs_cpl_policy[] = { | |
1080 | [OVS_CHECK_PKT_LEN_ATTR_PKT_LEN] = { .type = NL_A_U16 }, | |
1081 | [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER] = { .type = NL_A_NESTED }, | |
1082 | [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL] | |
1083 | = { .type = NL_A_NESTED }, | |
1084 | }; | |
1085 | struct nlattr *a[ARRAY_SIZE(ovs_cpl_policy)]; | |
1086 | ds_put_cstr(ds, "check_pkt_len"); | |
1087 | if (!nl_parse_nested(attr, ovs_cpl_policy, a, ARRAY_SIZE(a))) { | |
1088 | ds_put_cstr(ds, "(error)"); | |
1089 | return; | |
1090 | } | |
1091 | ||
1092 | if (!a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER] || | |
1093 | !a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL]) { | |
1094 | ds_put_cstr(ds, "(error)"); | |
1095 | return; | |
1096 | } | |
1097 | ||
1098 | uint16_t pkt_len = nl_attr_get_u16(a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN]); | |
1099 | ds_put_format(ds, "(size=%u,gt(", pkt_len); | |
1100 | const struct nlattr *acts; | |
1101 | acts = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER]; | |
1102 | format_odp_actions(ds, nl_attr_get(acts), nl_attr_get_size(acts), | |
1103 | portno_names); | |
1104 | ||
1105 | ds_put_cstr(ds, "),le("); | |
1106 | acts = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL]; | |
1107 | format_odp_actions(ds, nl_attr_get(acts), nl_attr_get_size(acts), | |
1108 | portno_names); | |
1109 | ds_put_cstr(ds, "))"); | |
1110 | } | |
81fdabb9 | 1111 | |
4edb9ae9 | 1112 | static void |
0722f341 BP |
1113 | format_odp_action(struct ds *ds, const struct nlattr *a, |
1114 | const struct hmap *portno_names) | |
064af421 | 1115 | { |
98403001 | 1116 | int expected_len; |
4edb9ae9 | 1117 | enum ovs_action_attr type = nl_attr_type(a); |
6d670e7f | 1118 | size_t size; |
cdee00fd | 1119 | |
98403001 | 1120 | expected_len = odp_action_len(nl_attr_type(a)); |
6b8da9e9 JG |
1121 | if (expected_len != ATTR_LEN_VARIABLE && |
1122 | nl_attr_get_size(a) != expected_len) { | |
34582733 | 1123 | ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ", |
98403001 | 1124 | nl_attr_get_size(a), expected_len); |
cdee00fd BP |
1125 | format_generic_odp_action(ds, a); |
1126 | return; | |
1127 | } | |
1128 | ||
4edb9ae9 | 1129 | switch (type) { |
5dddf960 JR |
1130 | case OVS_ACTION_ATTR_METER: |
1131 | ds_put_format(ds, "meter(%"PRIu32")", nl_attr_get_u32(a)); | |
1132 | break; | |
df2c07f4 | 1133 | case OVS_ACTION_ATTR_OUTPUT: |
0722f341 | 1134 | odp_portno_name_format(portno_names, nl_attr_get_odp_port(a), ds); |
064af421 | 1135 | break; |
9df65060 VDA |
1136 | case OVS_ACTION_ATTR_LB_OUTPUT: |
1137 | ds_put_format(ds, "lb_output(%"PRIu32")", nl_attr_get_u32(a)); | |
1138 | break; | |
aaca4fe0 WT |
1139 | case OVS_ACTION_ATTR_TRUNC: { |
1140 | const struct ovs_action_trunc *trunc = | |
1141 | nl_attr_get_unspec(a, sizeof *trunc); | |
1142 | ||
1143 | ds_put_format(ds, "trunc(%"PRIu32")", trunc->max_len); | |
1144 | break; | |
1145 | } | |
a36de779 | 1146 | case OVS_ACTION_ATTR_TUNNEL_POP: |
0722f341 BP |
1147 | ds_put_cstr(ds, "tnl_pop("); |
1148 | odp_portno_name_format(portno_names, nl_attr_get_odp_port(a), ds); | |
1149 | ds_put_char(ds, ')'); | |
a36de779 PS |
1150 | break; |
1151 | case OVS_ACTION_ATTR_TUNNEL_PUSH: | |
0722f341 | 1152 | format_odp_tnl_push_action(ds, a, portno_names); |
a36de779 | 1153 | break; |
df2c07f4 | 1154 | case OVS_ACTION_ATTR_USERSPACE: |
0722f341 | 1155 | format_odp_userspace_action(ds, a, portno_names); |
064af421 | 1156 | break; |
572f732a | 1157 | case OVS_ACTION_ATTR_RECIRC: |
347bf289 AZ |
1158 | format_odp_recirc_action(ds, nl_attr_get_u32(a)); |
1159 | break; | |
1160 | case OVS_ACTION_ATTR_HASH: | |
1161 | format_odp_hash_action(ds, nl_attr_get(a)); | |
572f732a | 1162 | break; |
6d670e7f JR |
1163 | case OVS_ACTION_ATTR_SET_MASKED: |
1164 | a = nl_attr_get(a); | |
81fdabb9 YY |
1165 | /* OVS_KEY_ATTR_NSH is nested attribute, so it needs special process */ |
1166 | if (nl_attr_type(a) == OVS_KEY_ATTR_NSH) { | |
1167 | format_odp_set_nsh(ds, a); | |
1168 | break; | |
1169 | } | |
6d670e7f JR |
1170 | size = nl_attr_get_size(a) / 2; |
1171 | ds_put_cstr(ds, "set("); | |
1172 | ||
1173 | /* Masked set action not supported for tunnel key, which is bigger. */ | |
1174 | if (size <= sizeof(struct ovs_key_ipv6)) { | |
1175 | struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6), | |
1176 | sizeof(struct nlattr))]; | |
1177 | struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6), | |
1178 | sizeof(struct nlattr))]; | |
1179 | ||
1180 | mask->nla_type = attr->nla_type = nl_attr_type(a); | |
1181 | mask->nla_len = attr->nla_len = NLA_HDRLEN + size; | |
1182 | memcpy(attr + 1, (char *)(a + 1), size); | |
1183 | memcpy(mask + 1, (char *)(a + 1) + size, size); | |
2d18eae8 | 1184 | format_odp_key_attr(attr, mask, NULL, ds, false); |
6d670e7f | 1185 | } else { |
2d18eae8 | 1186 | format_odp_key_attr(a, NULL, NULL, ds, false); |
6d670e7f JR |
1187 | } |
1188 | ds_put_cstr(ds, ")"); | |
1189 | break; | |
4edb9ae9 PS |
1190 | case OVS_ACTION_ATTR_SET: |
1191 | ds_put_cstr(ds, "set("); | |
0a37839c | 1192 | format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true); |
4edb9ae9 | 1193 | ds_put_cstr(ds, ")"); |
6fcecb85 YY |
1194 | break; |
1195 | case OVS_ACTION_ATTR_PUSH_ETH: { | |
1196 | const struct ovs_action_push_eth *eth = nl_attr_get(a); | |
1197 | ds_put_format(ds, "push_eth(src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT")", | |
1198 | ETH_ADDR_ARGS(eth->addresses.eth_src), | |
1199 | ETH_ADDR_ARGS(eth->addresses.eth_dst)); | |
1200 | break; | |
1201 | } | |
1202 | case OVS_ACTION_ATTR_POP_ETH: | |
1203 | ds_put_cstr(ds, "pop_eth"); | |
064af421 | 1204 | break; |
66026437 SH |
1205 | case OVS_ACTION_ATTR_PUSH_VLAN: { |
1206 | const struct ovs_action_push_vlan *vlan = nl_attr_get(a); | |
fea393b1 BP |
1207 | ds_put_cstr(ds, "push_vlan("); |
1208 | if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) { | |
1209 | ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid)); | |
1210 | } | |
2d18eae8 | 1211 | format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false); |
8ddc056d | 1212 | ds_put_char(ds, ')'); |
064af421 | 1213 | break; |
66026437 | 1214 | } |
fea393b1 BP |
1215 | case OVS_ACTION_ATTR_POP_VLAN: |
1216 | ds_put_cstr(ds, "pop_vlan"); | |
064af421 | 1217 | break; |
b02475c5 SH |
1218 | case OVS_ACTION_ATTR_PUSH_MPLS: { |
1219 | const struct ovs_action_push_mpls *mpls = nl_attr_get(a); | |
1220 | ds_put_cstr(ds, "push_mpls("); | |
1221 | format_mpls_lse(ds, mpls->mpls_lse); | |
1222 | ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype)); | |
1223 | break; | |
1224 | } | |
1225 | case OVS_ACTION_ATTR_POP_MPLS: { | |
1226 | ovs_be16 ethertype = nl_attr_get_be16(a); | |
1227 | ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype)); | |
1228 | break; | |
1229 | } | |
6ff686f2 | 1230 | case OVS_ACTION_ATTR_SAMPLE: |
0722f341 | 1231 | format_odp_sample_action(ds, a, portno_names); |
6ff686f2 | 1232 | break; |
07659514 JS |
1233 | case OVS_ACTION_ATTR_CT: |
1234 | format_odp_conntrack_action(ds, a); | |
1235 | break; | |
1fe178d2 EG |
1236 | case OVS_ACTION_ATTR_CT_CLEAR: |
1237 | ds_put_cstr(ds, "ct_clear"); | |
1238 | break; | |
535e3acf | 1239 | case OVS_ACTION_ATTR_CLONE: |
0722f341 | 1240 | format_odp_clone_action(ds, a, portno_names); |
535e3acf | 1241 | break; |
f59cb331 YY |
1242 | case OVS_ACTION_ATTR_PUSH_NSH: { |
1243 | uint32_t buffer[NSH_HDR_MAX_LEN / 4]; | |
1244 | struct nsh_hdr *nsh_hdr = ALIGNED_CAST(struct nsh_hdr *, buffer); | |
1245 | nsh_reset_ver_flags_ttl_len(nsh_hdr); | |
1246 | odp_nsh_hdr_from_attr(nl_attr_get(a), nsh_hdr, NSH_HDR_MAX_LEN); | |
1247 | format_odp_push_nsh_action(ds, nsh_hdr); | |
1fc11c59 | 1248 | break; |
f59cb331 YY |
1249 | } |
1250 | case OVS_ACTION_ATTR_POP_NSH: | |
1251 | ds_put_cstr(ds, "pop_nsh()"); | |
1fc11c59 | 1252 | break; |
5b34f8fc NS |
1253 | case OVS_ACTION_ATTR_CHECK_PKT_LEN: |
1254 | format_odp_check_pkt_len_action(ds, a, portno_names); | |
1255 | break; | |
a13a0209 AT |
1256 | case OVS_ACTION_ATTR_DROP: |
1257 | ds_put_cstr(ds, "drop"); | |
1258 | break; | |
4edb9ae9 PS |
1259 | case OVS_ACTION_ATTR_UNSPEC: |
1260 | case __OVS_ACTION_ATTR_MAX: | |
064af421 | 1261 | default: |
cdee00fd | 1262 | format_generic_odp_action(ds, a); |
064af421 BP |
1263 | break; |
1264 | } | |
1265 | } | |
1266 | ||
1267 | void | |
cdee00fd | 1268 | format_odp_actions(struct ds *ds, const struct nlattr *actions, |
0722f341 | 1269 | size_t actions_len, const struct hmap *portno_names) |
064af421 | 1270 | { |
cdee00fd BP |
1271 | if (actions_len) { |
1272 | const struct nlattr *a; | |
1273 | unsigned int left; | |
1274 | ||
1275 | NL_ATTR_FOR_EACH (a, left, actions, actions_len) { | |
1276 | if (a != actions) { | |
1277 | ds_put_char(ds, ','); | |
1278 | } | |
0722f341 | 1279 | format_odp_action(ds, a, portno_names); |
064af421 | 1280 | } |
cdee00fd | 1281 | if (left) { |
3a8cfc50 BP |
1282 | int i; |
1283 | ||
3476fce3 BP |
1284 | if (left == actions_len) { |
1285 | ds_put_cstr(ds, "<empty>"); | |
1286 | } | |
3a8cfc50 BP |
1287 | ds_put_format(ds, ",***%u leftover bytes*** (", left); |
1288 | for (i = 0; i < left; i++) { | |
1289 | ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]); | |
1290 | } | |
1291 | ds_put_char(ds, ')'); | |
cdee00fd BP |
1292 | } |
1293 | } else { | |
064af421 BP |
1294 | ds_put_cstr(ds, "drop"); |
1295 | } | |
1296 | } | |
7202cbe5 | 1297 | |
8b7ea2d4 | 1298 | /* Separate out parse_odp_userspace_action() function. */ |
7202cbe5 | 1299 | static int |
8b7ea2d4 | 1300 | parse_odp_userspace_action(const char *s, struct ofpbuf *actions) |
7202cbe5 | 1301 | { |
8b7ea2d4 | 1302 | uint32_t pid; |
8de6ff3e | 1303 | struct user_action_cookie cookie; |
8b7ea2d4 WZ |
1304 | struct ofpbuf buf; |
1305 | odp_port_t tunnel_out_port; | |
1306 | int n = -1; | |
1307 | void *user_data = NULL; | |
1308 | size_t user_data_size = 0; | |
7321bda3 | 1309 | bool include_actions = false; |
9d4e54c6 | 1310 | int res; |
8b7ea2d4 WZ |
1311 | |
1312 | if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) { | |
1313 | return -EINVAL; | |
7202cbe5 BP |
1314 | } |
1315 | ||
9d4e54c6 | 1316 | ofpbuf_init(&buf, 16); |
8de6ff3e | 1317 | memset(&cookie, 0, sizeof cookie); |
9d4e54c6 | 1318 | |
8de6ff3e JP |
1319 | user_data = &cookie; |
1320 | user_data_size = sizeof cookie; | |
7202cbe5 | 1321 | { |
c2c28dfd BP |
1322 | uint32_t output; |
1323 | uint32_t probability; | |
1324 | uint32_t collector_set_id; | |
1325 | uint32_t obs_domain_id; | |
1326 | uint32_t obs_point_id; | |
d39ec23d JP |
1327 | |
1328 | /* USER_ACTION_COOKIE_CONTROLLER. */ | |
1329 | uint8_t dont_send; | |
74c4530d | 1330 | uint8_t continuation; |
d39ec23d JP |
1331 | uint16_t reason; |
1332 | uint32_t recirc_id; | |
ab53fe8d | 1333 | uint64_t rule_cookie; |
d39ec23d JP |
1334 | uint16_t controller_id; |
1335 | uint16_t max_len; | |
1336 | ||
7202cbe5 | 1337 | int vid, pcp; |
8b7ea2d4 WZ |
1338 | int n1 = -1; |
1339 | if (ovs_scan(&s[n], ",sFlow(vid=%i," | |
1340 | "pcp=%i,output=%"SCNi32")%n", | |
1341 | &vid, &pcp, &output, &n1)) { | |
7202cbe5 BP |
1342 | uint16_t tci; |
1343 | ||
8b7ea2d4 | 1344 | n += n1; |
7202cbe5 BP |
1345 | tci = vid | (pcp << VLAN_PCP_SHIFT); |
1346 | if (tci) { | |
1347 | tci |= VLAN_CFI; | |
1348 | } | |
1349 | ||
1350 | cookie.type = USER_ACTION_COOKIE_SFLOW; | |
fcb9579b JP |
1351 | cookie.ofp_in_port = OFPP_NONE; |
1352 | cookie.ofproto_uuid = UUID_ZERO; | |
1673e0e4 BP |
1353 | cookie.sflow.vlan_tci = htons(tci); |
1354 | cookie.sflow.output = output; | |
2d18eae8 | 1355 | } else if (ovs_scan(&s[n], ",slow_path(%n", |
8b7ea2d4 | 1356 | &n1)) { |
8b7ea2d4 | 1357 | n += n1; |
6a7e895f | 1358 | cookie.type = USER_ACTION_COOKIE_SLOW_PATH; |
fcb9579b JP |
1359 | cookie.ofp_in_port = OFPP_NONE; |
1360 | cookie.ofproto_uuid = UUID_ZERO; | |
04594cd5 | 1361 | cookie.slow_path.reason = 0; |
6a7e895f | 1362 | |
8e4c1621 JG |
1363 | res = parse_odp_flags(&s[n], slow_path_reason_to_string, |
1364 | &cookie.slow_path.reason, | |
1365 | SLOW_PATH_REASON_MASK, NULL); | |
2d18eae8 | 1366 | if (res < 0 || s[n + res] != ')') { |
9d4e54c6 | 1367 | goto out; |
04594cd5 | 1368 | } |
2d18eae8 | 1369 | n += res + 1; |
8b7ea2d4 | 1370 | } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32"," |
c2c28dfd BP |
1371 | "collector_set_id=%"SCNi32"," |
1372 | "obs_domain_id=%"SCNi32"," | |
f69f713b | 1373 | "obs_point_id=%"SCNi32"," |
4930ea56 | 1374 | "output_port=%"SCNi32"%n", |
8b7ea2d4 | 1375 | &probability, &collector_set_id, |
f69f713b BY |
1376 | &obs_domain_id, &obs_point_id, |
1377 | &output, &n1)) { | |
8b7ea2d4 | 1378 | n += n1; |
29089a54 RL |
1379 | |
1380 | cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE; | |
fcb9579b JP |
1381 | cookie.ofp_in_port = OFPP_NONE; |
1382 | cookie.ofproto_uuid = UUID_ZERO; | |
29089a54 RL |
1383 | cookie.flow_sample.probability = probability; |
1384 | cookie.flow_sample.collector_set_id = collector_set_id; | |
1385 | cookie.flow_sample.obs_domain_id = obs_domain_id; | |
1386 | cookie.flow_sample.obs_point_id = obs_point_id; | |
f69f713b | 1387 | cookie.flow_sample.output_odp_port = u32_to_odp(output); |
4930ea56 BP |
1388 | |
1389 | if (ovs_scan(&s[n], ",ingress%n", &n1)) { | |
1390 | cookie.flow_sample.direction = NX_ACTION_SAMPLE_INGRESS; | |
1391 | n += n1; | |
1392 | } else if (ovs_scan(&s[n], ",egress%n", &n1)) { | |
1393 | cookie.flow_sample.direction = NX_ACTION_SAMPLE_EGRESS; | |
1394 | n += n1; | |
1395 | } else { | |
1396 | cookie.flow_sample.direction = NX_ACTION_SAMPLE_DEFAULT; | |
1397 | } | |
1398 | if (s[n] != ')') { | |
1399 | res = -EINVAL; | |
1400 | goto out; | |
1401 | } | |
1402 | n++; | |
8b7ea2d4 WZ |
1403 | } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n", |
1404 | &output, &n1) ) { | |
1405 | n += n1; | |
29089a54 | 1406 | cookie.type = USER_ACTION_COOKIE_IPFIX; |
fcb9579b JP |
1407 | cookie.ofp_in_port = OFPP_NONE; |
1408 | cookie.ofproto_uuid = UUID_ZERO; | |
8b7ea2d4 | 1409 | cookie.ipfix.output_odp_port = u32_to_odp(output); |
d39ec23d JP |
1410 | } else if (ovs_scan(&s[n], ",controller(reason=%"SCNu16 |
1411 | ",dont_send=%"SCNu8 | |
74c4530d | 1412 | ",continuation=%"SCNu8 |
d39ec23d JP |
1413 | ",recirc_id=%"SCNu32 |
1414 | ",rule_cookie=%"SCNx64 | |
1415 | ",controller_id=%"SCNu16 | |
1416 | ",max_len=%"SCNu16")%n", | |
74c4530d JP |
1417 | &reason, &dont_send, &continuation, &recirc_id, |
1418 | &rule_cookie, &controller_id, &max_len, &n1)) { | |
d39ec23d JP |
1419 | n += n1; |
1420 | cookie.type = USER_ACTION_COOKIE_CONTROLLER; | |
1421 | cookie.ofp_in_port = OFPP_NONE; | |
1422 | cookie.ofproto_uuid = UUID_ZERO; | |
1423 | cookie.controller.dont_send = dont_send ? true : false; | |
74c4530d | 1424 | cookie.controller.continuation = continuation ? true : false; |
d39ec23d JP |
1425 | cookie.controller.reason = reason; |
1426 | cookie.controller.recirc_id = recirc_id; | |
1427 | put_32aligned_be64(&cookie.controller.rule_cookie, | |
1428 | htonll(rule_cookie)); | |
1429 | cookie.controller.controller_id = controller_id; | |
1430 | cookie.controller.max_len = max_len; | |
1431 | } else if (ovs_scan(&s[n], ",userdata(%n", &n1)) { | |
e995e3df BP |
1432 | char *end; |
1433 | ||
8b7ea2d4 | 1434 | n += n1; |
e995e3df | 1435 | end = ofpbuf_put_hex(&buf, &s[n], NULL); |
8b7ea2d4 | 1436 | if (end[0] != ')') { |
9d4e54c6 WT |
1437 | res = -EINVAL; |
1438 | goto out; | |
e995e3df | 1439 | } |
6fd6ed71 PS |
1440 | user_data = buf.data; |
1441 | user_data_size = buf.size; | |
8b7ea2d4 WZ |
1442 | n = (end + 1) - s; |
1443 | } | |
1444 | } | |
1445 | ||
7321bda3 NM |
1446 | { |
1447 | int n1 = -1; | |
1448 | if (ovs_scan(&s[n], ",actions%n", &n1)) { | |
1449 | n += n1; | |
1450 | include_actions = true; | |
1451 | } | |
1452 | } | |
1453 | ||
8b7ea2d4 WZ |
1454 | { |
1455 | int n1 = -1; | |
1456 | if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n", | |
1457 | &tunnel_out_port, &n1)) { | |
7321bda3 NM |
1458 | odp_put_userspace_action(pid, user_data, user_data_size, |
1459 | tunnel_out_port, include_actions, actions); | |
9d4e54c6 | 1460 | res = n + n1; |
88fc5281 | 1461 | goto out; |
8b7ea2d4 | 1462 | } else if (s[n] == ')') { |
7321bda3 NM |
1463 | odp_put_userspace_action(pid, user_data, user_data_size, |
1464 | ODPP_NONE, include_actions, actions); | |
9d4e54c6 | 1465 | res = n + 1; |
88fc5281 JS |
1466 | goto out; |
1467 | } | |
1468 | } | |
1469 | ||
1470 | { | |
1471 | struct ovs_action_push_eth push; | |
1472 | int eth_type = 0; | |
1473 | int n1 = -1; | |
1474 | ||
1475 | if (ovs_scan(&s[n], "push_eth(src="ETH_ADDR_SCAN_FMT"," | |
1476 | "dst="ETH_ADDR_SCAN_FMT",type=%i)%n", | |
1477 | ETH_ADDR_SCAN_ARGS(push.addresses.eth_src), | |
1478 | ETH_ADDR_SCAN_ARGS(push.addresses.eth_dst), | |
1479 | ð_type, &n1)) { | |
1480 | ||
1481 | nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_ETH, | |
1482 | &push, sizeof push); | |
1483 | ||
1484 | res = n + n1; | |
1485 | goto out; | |
8b7ea2d4 WZ |
1486 | } |
1487 | } | |
88fc5281 JS |
1488 | |
1489 | if (!strncmp(&s[n], "pop_eth", 7)) { | |
1490 | nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_ETH); | |
1491 | res = 7; | |
1492 | goto out; | |
1493 | } | |
1494 | ||
1495 | res = -EINVAL; | |
9d4e54c6 WT |
1496 | out: |
1497 | ofpbuf_uninit(&buf); | |
1498 | return res; | |
8b7ea2d4 WZ |
1499 | } |
1500 | ||
a36de779 PS |
1501 | static int |
1502 | ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data) | |
1503 | { | |
1504 | struct eth_header *eth; | |
1505 | struct ip_header *ip; | |
370e373b | 1506 | struct ovs_16aligned_ip6_hdr *ip6; |
a36de779 PS |
1507 | struct udp_header *udp; |
1508 | struct gre_base_hdr *greh; | |
7dc18ae9 WT |
1509 | struct erspan_base_hdr *ersh; |
1510 | struct erspan_md2 *md2; | |
285de6b2 | 1511 | uint16_t gre_proto, gre_flags, dl_type, udp_src, udp_dst, udp_csum, sid; |
a36de779 | 1512 | ovs_be32 sip, dip; |
7dc18ae9 | 1513 | uint32_t tnl_type = 0, header_len = 0, ip_len = 0, erspan_idx = 0; |
a36de779 PS |
1514 | void *l3, *l4; |
1515 | int n = 0; | |
7dc18ae9 | 1516 | uint8_t hwid, dir; |
3c6d05a0 WT |
1517 | uint32_t teid; |
1518 | uint8_t gtpu_flags, gtpu_msgtype; | |
a36de779 PS |
1519 | |
1520 | if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) { | |
1521 | return -EINVAL; | |
1522 | } | |
1523 | eth = (struct eth_header *) data->header; | |
1620b7ea | 1524 | l3 = (struct ip_header *) (eth + 1); |
a36de779 | 1525 | ip = (struct ip_header *) l3; |
370e373b | 1526 | ip6 = (struct ovs_16aligned_ip6_hdr *) l3; |
a36de779 | 1527 | if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32"," |
9ac0aada JR |
1528 | "eth(dst="ETH_ADDR_SCAN_FMT",", |
1529 | &data->header_len, | |
1530 | &data->tnl_type, | |
1531 | ETH_ADDR_SCAN_ARGS(eth->eth_dst))) { | |
a36de779 PS |
1532 | return -EINVAL; |
1533 | } | |
1534 | ||
1535 | if (!ovs_scan_len(s, &n, "src="ETH_ADDR_SCAN_FMT",", | |
9ac0aada | 1536 | ETH_ADDR_SCAN_ARGS(eth->eth_src))) { |
a36de779 PS |
1537 | return -EINVAL; |
1538 | } | |
1539 | if (!ovs_scan_len(s, &n, "dl_type=0x%"SCNx16"),", &dl_type)) { | |
1540 | return -EINVAL; | |
1541 | } | |
1542 | eth->eth_type = htons(dl_type); | |
1543 | ||
370e373b TLSC |
1544 | if (eth->eth_type == htons(ETH_TYPE_IP)) { |
1545 | /* IPv4 */ | |
fa47c114 | 1546 | uint16_t ip_frag_off; |
285de6b2 | 1547 | memset(ip, 0, sizeof(*ip)); |
370e373b TLSC |
1548 | if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8 |
1549 | ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),", | |
1550 | IP_SCAN_ARGS(&sip), | |
1551 | IP_SCAN_ARGS(&dip), | |
1552 | &ip->ip_proto, &ip->ip_tos, | |
fa47c114 | 1553 | &ip->ip_ttl, &ip_frag_off)) { |
370e373b TLSC |
1554 | return -EINVAL; |
1555 | } | |
1556 | put_16aligned_be32(&ip->ip_src, sip); | |
1557 | put_16aligned_be32(&ip->ip_dst, dip); | |
fa47c114 | 1558 | ip->ip_frag_off = htons(ip_frag_off); |
f210ce43 | 1559 | ip->ip_ihl_ver = IP_IHL_VER(5, 4); |
370e373b | 1560 | ip_len = sizeof *ip; |
285de6b2 | 1561 | ip->ip_csum = csum(ip, ip_len); |
370e373b TLSC |
1562 | } else { |
1563 | char sip6_s[IPV6_SCAN_LEN + 1]; | |
1564 | char dip6_s[IPV6_SCAN_LEN + 1]; | |
1565 | struct in6_addr sip6, dip6; | |
1566 | uint8_t tclass; | |
1567 | uint32_t label; | |
1568 | if (!ovs_scan_len(s, &n, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT | |
1569 | ",label=%i,proto=%"SCNi8",tclass=0x%"SCNx8 | |
1570 | ",hlimit=%"SCNi8"),", | |
1571 | sip6_s, dip6_s, &label, &ip6->ip6_nxt, | |
1572 | &tclass, &ip6->ip6_hlim) | |
1573 | || (label & ~IPV6_LABEL_MASK) != 0 | |
1574 | || inet_pton(AF_INET6, sip6_s, &sip6) != 1 | |
1575 | || inet_pton(AF_INET6, dip6_s, &dip6) != 1) { | |
1576 | return -EINVAL; | |
1577 | } | |
1578 | put_16aligned_be32(&ip6->ip6_flow, htonl(6 << 28) | | |
1579 | htonl(tclass << 20) | htonl(label)); | |
1580 | memcpy(&ip6->ip6_src, &sip6, sizeof(ip6->ip6_src)); | |
1581 | memcpy(&ip6->ip6_dst, &dip6, sizeof(ip6->ip6_dst)); | |
1582 | ip_len = sizeof *ip6; | |
a36de779 | 1583 | } |
a36de779 PS |
1584 | |
1585 | /* Tunnel header */ | |
370e373b | 1586 | l4 = ((uint8_t *) l3 + ip_len); |
a36de779 PS |
1587 | udp = (struct udp_header *) l4; |
1588 | greh = (struct gre_base_hdr *) l4; | |
8e45fe7c | 1589 | if (ovs_scan_len(s, &n, "udp(src=%"SCNi16",dst=%"SCNi16",csum=0x%"SCNx16"),", |
285de6b2 | 1590 | &udp_src, &udp_dst, &udp_csum)) { |
e5a1caee | 1591 | uint32_t vx_flags, vni; |
a36de779 PS |
1592 | |
1593 | udp->udp_src = htons(udp_src); | |
1594 | udp->udp_dst = htons(udp_dst); | |
1595 | udp->udp_len = 0; | |
285de6b2 | 1596 | udp->udp_csum = htons(udp_csum); |
a36de779 | 1597 | |
e066f78f | 1598 | if (ovs_scan_len(s, &n, "vxlan(flags=0x%"SCNx32",vni=0x%"SCNx32"))", |
9ac0aada | 1599 | &vx_flags, &vni)) { |
e066f78f JG |
1600 | struct vxlanhdr *vxh = (struct vxlanhdr *) (udp + 1); |
1601 | ||
1602 | put_16aligned_be32(&vxh->vx_flags, htonl(vx_flags)); | |
e5a1caee | 1603 | put_16aligned_be32(&vxh->vx_vni, htonl(vni << 8)); |
e066f78f | 1604 | tnl_type = OVS_VPORT_TYPE_VXLAN; |
370e373b | 1605 | header_len = sizeof *eth + ip_len + |
e066f78f | 1606 | sizeof *udp + sizeof *vxh; |
e5a1caee JG |
1607 | } else if (ovs_scan_len(s, &n, "geneve(")) { |
1608 | struct genevehdr *gnh = (struct genevehdr *) (udp + 1); | |
1609 | ||
46e7137c | 1610 | memset(gnh, 0, sizeof *gnh); |
370e373b | 1611 | header_len = sizeof *eth + ip_len + |
5bb08b0e JG |
1612 | sizeof *udp + sizeof *gnh; |
1613 | ||
e5a1caee JG |
1614 | if (ovs_scan_len(s, &n, "oam,")) { |
1615 | gnh->oam = 1; | |
1616 | } | |
5bb08b0e JG |
1617 | if (ovs_scan_len(s, &n, "crit,")) { |
1618 | gnh->critical = 1; | |
1619 | } | |
1620 | if (!ovs_scan_len(s, &n, "vni=%"SCNi32, &vni)) { | |
e5a1caee JG |
1621 | return -EINVAL; |
1622 | } | |
5bb08b0e JG |
1623 | if (ovs_scan_len(s, &n, ",options(")) { |
1624 | struct geneve_scan options; | |
1625 | int len; | |
1626 | ||
1627 | memset(&options, 0, sizeof options); | |
1628 | len = scan_geneve(s + n, &options, NULL); | |
1629 | if (!len) { | |
1630 | return -EINVAL; | |
1631 | } | |
1632 | ||
1633 | memcpy(gnh->options, options.d, options.len); | |
1634 | gnh->opt_len = options.len / 4; | |
1635 | header_len += options.len; | |
1636 | ||
1637 | n += len; | |
1638 | } | |
1639 | if (!ovs_scan_len(s, &n, "))")) { | |
1640 | return -EINVAL; | |
1641 | } | |
1642 | ||
e5a1caee JG |
1643 | gnh->proto_type = htons(ETH_TYPE_TEB); |
1644 | put_16aligned_be32(&gnh->vni, htonl(vni << 8)); | |
1645 | tnl_type = OVS_VPORT_TYPE_GENEVE; | |
e066f78f | 1646 | } else { |
a36de779 PS |
1647 | return -EINVAL; |
1648 | } | |
a36de779 | 1649 | } else if (ovs_scan_len(s, &n, "gre((flags=0x%"SCNx16",proto=0x%"SCNx16")", |
9ac0aada | 1650 | &gre_flags, &gre_proto)){ |
a36de779 | 1651 | |
3b10ceee GR |
1652 | if (eth->eth_type == htons(ETH_TYPE_IP)) { |
1653 | tnl_type = OVS_VPORT_TYPE_GRE; | |
1654 | } else { | |
1655 | tnl_type = OVS_VPORT_TYPE_IP6GRE; | |
1656 | } | |
e8fe6ad0 | 1657 | greh->flags = htons(gre_flags); |
a36de779 PS |
1658 | greh->protocol = htons(gre_proto); |
1659 | ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1); | |
1660 | ||
1661 | if (greh->flags & htons(GRE_CSUM)) { | |
285de6b2 | 1662 | uint16_t csum; |
d804d31e | 1663 | if (!ovs_scan_len(s, &n, ",csum=0x%"SCNx16, &csum)) { |
a36de779 PS |
1664 | return -EINVAL; |
1665 | } | |
d804d31e JG |
1666 | |
1667 | memset(options, 0, sizeof *options); | |
1668 | *((ovs_be16 *)options) = htons(csum); | |
a36de779 PS |
1669 | options++; |
1670 | } | |
1671 | if (greh->flags & htons(GRE_KEY)) { | |
1672 | uint32_t key; | |
1673 | ||
1674 | if (!ovs_scan_len(s, &n, ",key=0x%"SCNx32, &key)) { | |
1675 | return -EINVAL; | |
1676 | } | |
1677 | ||
1678 | put_16aligned_be32(options, htonl(key)); | |
1679 | options++; | |
1680 | } | |
1681 | if (greh->flags & htons(GRE_SEQ)) { | |
1682 | uint32_t seq; | |
1683 | ||
1684 | if (!ovs_scan_len(s, &n, ",seq=0x%"SCNx32, &seq)) { | |
1685 | return -EINVAL; | |
1686 | } | |
1687 | put_16aligned_be32(options, htonl(seq)); | |
1688 | options++; | |
1689 | } | |
1690 | ||
1691 | if (!ovs_scan_len(s, &n, "))")) { | |
1692 | return -EINVAL; | |
1693 | } | |
1694 | ||
370e373b | 1695 | header_len = sizeof *eth + ip_len + |
a36de779 | 1696 | ((uint8_t *) options - (uint8_t *) greh); |
7dc18ae9 WT |
1697 | } else if (ovs_scan_len(s, &n, "erspan(ver=1,sid="SCNx16",idx=0x"SCNx32")", |
1698 | &sid, &erspan_idx)) { | |
1699 | ersh = ERSPAN_HDR(greh); | |
1700 | ovs_16aligned_be32 *index = ALIGNED_CAST(ovs_16aligned_be32 *, | |
1701 | ersh + 1); | |
1702 | ||
1703 | if (eth->eth_type == htons(ETH_TYPE_IP)) { | |
1704 | tnl_type = OVS_VPORT_TYPE_ERSPAN; | |
1705 | } else { | |
1706 | tnl_type = OVS_VPORT_TYPE_IP6ERSPAN; | |
1707 | } | |
1708 | ||
1709 | greh->flags = htons(GRE_SEQ); | |
1710 | greh->protocol = htons(ETH_TYPE_ERSPAN1); | |
1711 | ||
1712 | ersh->ver = 1; | |
1713 | set_sid(ersh, sid); | |
1714 | put_16aligned_be32(index, htonl(erspan_idx)); | |
1715 | ||
1716 | if (!ovs_scan_len(s, &n, ")")) { | |
1717 | return -EINVAL; | |
1718 | } | |
1719 | header_len = sizeof *eth + ip_len + ERSPAN_GREHDR_LEN + | |
1720 | sizeof *ersh + ERSPAN_V1_MDSIZE; | |
1721 | ||
1722 | } else if (ovs_scan_len(s, &n, "erspan(ver=2,sid="SCNx16"dir="SCNu8 | |
1723 | ",hwid=0x"SCNx8")", &sid, &dir, &hwid)) { | |
1724 | ||
1725 | ersh = ERSPAN_HDR(greh); | |
1726 | md2 = ALIGNED_CAST(struct erspan_md2 *, ersh + 1); | |
1727 | ||
1728 | if (eth->eth_type == htons(ETH_TYPE_IP)) { | |
1729 | tnl_type = OVS_VPORT_TYPE_ERSPAN; | |
1730 | } else { | |
1731 | tnl_type = OVS_VPORT_TYPE_IP6ERSPAN; | |
1732 | } | |
1733 | ||
1734 | greh->flags = htons(GRE_SEQ); | |
1735 | greh->protocol = htons(ETH_TYPE_ERSPAN2); | |
1736 | ||
1737 | ersh->ver = 2; | |
1738 | set_sid(ersh, sid); | |
1739 | set_hwid(md2, hwid); | |
1740 | md2->dir = dir; | |
1741 | ||
1742 | if (!ovs_scan_len(s, &n, ")")) { | |
1743 | return -EINVAL; | |
1744 | } | |
1745 | ||
1746 | header_len = sizeof *eth + ip_len + ERSPAN_GREHDR_LEN + | |
1747 | sizeof *ersh + ERSPAN_V2_MDSIZE; | |
3c6d05a0 WT |
1748 | |
1749 | } else if (ovs_scan_len(s, &n, "gtpu(flags=%"SCNi8",msgtype=%" | |
1750 | SCNu8",teid=0x%"SCNx32"))", | |
1751 | >pu_flags, >pu_msgtype, &teid)) { | |
1752 | struct gtpuhdr *gtph = (struct gtpuhdr *) (udp + 1); | |
1753 | ||
1754 | gtph->md.flags = gtpu_flags; | |
1755 | gtph->md.msgtype = gtpu_msgtype; | |
1756 | put_16aligned_be32(>ph->teid, htonl(teid)); | |
1757 | tnl_type = OVS_VPORT_TYPE_GTPU; | |
1758 | header_len = sizeof *eth + ip_len + | |
1759 | sizeof *udp + sizeof *gtph; | |
a36de779 PS |
1760 | } else { |
1761 | return -EINVAL; | |
1762 | } | |
1763 | ||
1764 | /* check tunnel meta data. */ | |
1765 | if (data->tnl_type != tnl_type) { | |
1766 | return -EINVAL; | |
1767 | } | |
1768 | if (data->header_len != header_len) { | |
1769 | return -EINVAL; | |
1770 | } | |
1771 | ||
1772 | /* Out port */ | |
1773 | if (!ovs_scan_len(s, &n, ",out_port(%"SCNi32"))", &data->out_port)) { | |
1774 | return -EINVAL; | |
1775 | } | |
1776 | ||
1777 | return n; | |
1778 | } | |
1779 | ||
9ac0aada JR |
1780 | struct ct_nat_params { |
1781 | bool snat; | |
1782 | bool dnat; | |
1783 | size_t addr_len; | |
1784 | union { | |
1785 | ovs_be32 ip; | |
1786 | struct in6_addr ip6; | |
1787 | } addr_min; | |
1788 | union { | |
1789 | ovs_be32 ip; | |
1790 | struct in6_addr ip6; | |
1791 | } addr_max; | |
1792 | uint16_t proto_min; | |
1793 | uint16_t proto_max; | |
1794 | bool persistent; | |
1795 | bool proto_hash; | |
1796 | bool proto_random; | |
1797 | }; | |
1798 | ||
1799 | static int | |
1800 | scan_ct_nat_range(const char *s, int *n, struct ct_nat_params *p) | |
1801 | { | |
1802 | if (ovs_scan_len(s, n, "=")) { | |
1803 | char ipv6_s[IPV6_SCAN_LEN + 1]; | |
1804 | struct in6_addr ipv6; | |
1805 | ||
1806 | if (ovs_scan_len(s, n, IP_SCAN_FMT, IP_SCAN_ARGS(&p->addr_min.ip))) { | |
1807 | p->addr_len = sizeof p->addr_min.ip; | |
1808 | if (ovs_scan_len(s, n, "-")) { | |
1809 | if (!ovs_scan_len(s, n, IP_SCAN_FMT, | |
1810 | IP_SCAN_ARGS(&p->addr_max.ip))) { | |
1811 | return -EINVAL; | |
1812 | } | |
1813 | } | |
1814 | } else if ((ovs_scan_len(s, n, IPV6_SCAN_FMT, ipv6_s) | |
1815 | || ovs_scan_len(s, n, "["IPV6_SCAN_FMT"]", ipv6_s)) | |
1816 | && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) { | |
1817 | p->addr_len = sizeof p->addr_min.ip6; | |
1818 | p->addr_min.ip6 = ipv6; | |
1819 | if (ovs_scan_len(s, n, "-")) { | |
1820 | if ((ovs_scan_len(s, n, IPV6_SCAN_FMT, ipv6_s) | |
1821 | || ovs_scan_len(s, n, "["IPV6_SCAN_FMT"]", ipv6_s)) | |
1822 | && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) { | |
1823 | p->addr_max.ip6 = ipv6; | |
1824 | } else { | |
1825 | return -EINVAL; | |
1826 | } | |
1827 | } | |
1828 | } else { | |
1829 | return -EINVAL; | |
1830 | } | |
1831 | if (ovs_scan_len(s, n, ":%"SCNu16, &p->proto_min)) { | |
1832 | if (ovs_scan_len(s, n, "-")) { | |
1833 | if (!ovs_scan_len(s, n, "%"SCNu16, &p->proto_max)) { | |
1834 | return -EINVAL; | |
1835 | } | |
1836 | } | |
1837 | } | |
1838 | } | |
1839 | return 0; | |
1840 | } | |
1841 | ||
1842 | static int | |
1843 | scan_ct_nat(const char *s, struct ct_nat_params *p) | |
1844 | { | |
1845 | int n = 0; | |
1846 | ||
1847 | if (ovs_scan_len(s, &n, "nat")) { | |
1848 | memset(p, 0, sizeof *p); | |
1849 | ||
1850 | if (ovs_scan_len(s, &n, "(")) { | |
1851 | char *end; | |
1852 | int end_n; | |
1853 | ||
1854 | end = strchr(s + n, ')'); | |
1855 | if (!end) { | |
1856 | return -EINVAL; | |
1857 | } | |
1858 | end_n = end - s; | |
1859 | ||
1860 | while (n < end_n) { | |
1861 | n += strspn(s + n, delimiters); | |
1862 | if (ovs_scan_len(s, &n, "src")) { | |
1863 | int err = scan_ct_nat_range(s, &n, p); | |
1864 | if (err) { | |
1865 | return err; | |
1866 | } | |
1867 | p->snat = true; | |
1868 | continue; | |
1869 | } | |
1870 | if (ovs_scan_len(s, &n, "dst")) { | |
1871 | int err = scan_ct_nat_range(s, &n, p); | |
1872 | if (err) { | |
1873 | return err; | |
1874 | } | |
1875 | p->dnat = true; | |
1876 | continue; | |
1877 | } | |
1878 | if (ovs_scan_len(s, &n, "persistent")) { | |
1879 | p->persistent = true; | |
1880 | continue; | |
1881 | } | |
1882 | if (ovs_scan_len(s, &n, "hash")) { | |
1883 | p->proto_hash = true; | |
1884 | continue; | |
1885 | } | |
1886 | if (ovs_scan_len(s, &n, "random")) { | |
1887 | p->proto_random = true; | |
1888 | continue; | |
1889 | } | |
1890 | return -EINVAL; | |
1891 | } | |
1892 | ||
1893 | if (p->snat && p->dnat) { | |
1894 | return -EINVAL; | |
1895 | } | |
1896 | if ((p->addr_len != 0 && | |
1897 | memcmp(&p->addr_max, &in6addr_any, p->addr_len) && | |
1898 | memcmp(&p->addr_max, &p->addr_min, p->addr_len) < 0) || | |
1899 | (p->proto_max && p->proto_max < p->proto_min)) { | |
1900 | return -EINVAL; | |
1901 | } | |
1902 | if (p->proto_hash && p->proto_random) { | |
1903 | return -EINVAL; | |
1904 | } | |
1905 | n++; | |
1906 | } | |
1907 | } | |
1908 | return n; | |
1909 | } | |
1910 | ||
1911 | static void | |
1912 | nl_msg_put_ct_nat(struct ct_nat_params *p, struct ofpbuf *actions) | |
1913 | { | |
1914 | size_t start = nl_msg_start_nested(actions, OVS_CT_ATTR_NAT); | |
1915 | ||
1916 | if (p->snat) { | |
1917 | nl_msg_put_flag(actions, OVS_NAT_ATTR_SRC); | |
1918 | } else if (p->dnat) { | |
1919 | nl_msg_put_flag(actions, OVS_NAT_ATTR_DST); | |
1920 | } else { | |
1921 | goto out; | |
1922 | } | |
1923 | if (p->addr_len != 0) { | |
1924 | nl_msg_put_unspec(actions, OVS_NAT_ATTR_IP_MIN, &p->addr_min, | |
1925 | p->addr_len); | |
1926 | if (memcmp(&p->addr_max, &p->addr_min, p->addr_len) > 0) { | |
1927 | nl_msg_put_unspec(actions, OVS_NAT_ATTR_IP_MAX, &p->addr_max, | |
1928 | p->addr_len); | |
1929 | } | |
1930 | if (p->proto_min) { | |
1931 | nl_msg_put_u16(actions, OVS_NAT_ATTR_PROTO_MIN, p->proto_min); | |
1932 | if (p->proto_max && p->proto_max > p->proto_min) { | |
1933 | nl_msg_put_u16(actions, OVS_NAT_ATTR_PROTO_MAX, p->proto_max); | |
1934 | } | |
1935 | } | |
1936 | if (p->persistent) { | |
1937 | nl_msg_put_flag(actions, OVS_NAT_ATTR_PERSISTENT); | |
1938 | } | |
1939 | if (p->proto_hash) { | |
1940 | nl_msg_put_flag(actions, OVS_NAT_ATTR_PROTO_HASH); | |
1941 | } | |
1942 | if (p->proto_random) { | |
1943 | nl_msg_put_flag(actions, OVS_NAT_ATTR_PROTO_RANDOM); | |
1944 | } | |
1945 | } | |
1946 | out: | |
1947 | nl_msg_end_nested(actions, start); | |
1948 | } | |
1949 | ||
07659514 JS |
1950 | static int |
1951 | parse_conntrack_action(const char *s_, struct ofpbuf *actions) | |
1952 | { | |
1953 | const char *s = s_; | |
1954 | ||
1955 | if (ovs_scan(s, "ct")) { | |
ebe62ec1 YHW |
1956 | const char *helper = NULL, *timeout = NULL; |
1957 | size_t helper_len = 0, timeout_len = 0; | |
07659514 | 1958 | bool commit = false; |
a76a37ef | 1959 | bool force_commit = false; |
07659514 | 1960 | uint16_t zone = 0; |
8e53fe8c JS |
1961 | struct { |
1962 | uint32_t value; | |
1963 | uint32_t mask; | |
1964 | } ct_mark = { 0, 0 }; | |
9daf2348 JS |
1965 | struct { |
1966 | ovs_u128 value; | |
1967 | ovs_u128 mask; | |
1968 | } ct_label; | |
9ac0aada JR |
1969 | struct ct_nat_params nat_params; |
1970 | bool have_nat = false; | |
07659514 JS |
1971 | size_t start; |
1972 | char *end; | |
1973 | ||
9daf2348 JS |
1974 | memset(&ct_label, 0, sizeof(ct_label)); |
1975 | ||
07659514 JS |
1976 | s += 2; |
1977 | if (ovs_scan(s, "(")) { | |
1978 | s++; | |
9ac0aada | 1979 | find_end: |
07659514 JS |
1980 | end = strchr(s, ')'); |
1981 | if (!end) { | |
1982 | return -EINVAL; | |
1983 | } | |
1984 | ||
1985 | while (s != end) { | |
9ac0aada | 1986 | int n; |
07659514 JS |
1987 | |
1988 | s += strspn(s, delimiters); | |
1989 | if (ovs_scan(s, "commit%n", &n)) { | |
1990 | commit = true; | |
1991 | s += n; | |
1992 | continue; | |
1993 | } | |
a76a37ef JR |
1994 | if (ovs_scan(s, "force_commit%n", &n)) { |
1995 | force_commit = true; | |
1996 | s += n; | |
1997 | continue; | |
1998 | } | |
07659514 JS |
1999 | if (ovs_scan(s, "zone=%"SCNu16"%n", &zone, &n)) { |
2000 | s += n; | |
2001 | continue; | |
2002 | } | |
8e53fe8c JS |
2003 | if (ovs_scan(s, "mark=%"SCNx32"%n", &ct_mark.value, &n)) { |
2004 | s += n; | |
2005 | n = -1; | |
2006 | if (ovs_scan(s, "/%"SCNx32"%n", &ct_mark.mask, &n)) { | |
2007 | s += n; | |
2008 | } else { | |
2009 | ct_mark.mask = UINT32_MAX; | |
2010 | } | |
2011 | continue; | |
2012 | } | |
9daf2348 JS |
2013 | if (ovs_scan(s, "label=%n", &n)) { |
2014 | int retval; | |
2015 | ||
2016 | s += n; | |
2017 | retval = scan_u128(s, &ct_label.value, &ct_label.mask); | |
4e9c96f0 YS |
2018 | if (retval == 0) { |
2019 | return -EINVAL; | |
9daf2348 JS |
2020 | } |
2021 | s += retval; | |
2022 | continue; | |
2023 | } | |
d787ad39 JS |
2024 | if (ovs_scan(s, "helper=%n", &n)) { |
2025 | s += n; | |
2026 | helper_len = strcspn(s, delimiters_end); | |
2027 | if (!helper_len || helper_len > 15) { | |
2028 | return -EINVAL; | |
2029 | } | |
2030 | helper = s; | |
2031 | s += helper_len; | |
2032 | continue; | |
2033 | } | |
ebe62ec1 YHW |
2034 | if (ovs_scan(s, "timeout=%n", &n)) { |
2035 | s += n; | |
2036 | timeout_len = strcspn(s, delimiters_end); | |
2037 | if (!timeout_len || timeout_len > 31) { | |
2038 | return -EINVAL; | |
2039 | } | |
2040 | timeout = s; | |
2041 | s += timeout_len; | |
2042 | continue; | |
2043 | } | |
07659514 | 2044 | |
9ac0aada JR |
2045 | n = scan_ct_nat(s, &nat_params); |
2046 | if (n > 0) { | |
2047 | s += n; | |
2048 | have_nat = true; | |
2049 | ||
2050 | /* end points to the end of the nested, nat action. | |
2051 | * find the real end. */ | |
2052 | goto find_end; | |
2053 | } | |
2054 | /* Nothing matched. */ | |
07659514 JS |
2055 | return -EINVAL; |
2056 | } | |
2057 | s++; | |
2058 | } | |
a76a37ef JR |
2059 | if (commit && force_commit) { |
2060 | return -EINVAL; | |
2061 | } | |
07659514 JS |
2062 | |
2063 | start = nl_msg_start_nested(actions, OVS_ACTION_ATTR_CT); | |
2064 | if (commit) { | |
2065 | nl_msg_put_flag(actions, OVS_CT_ATTR_COMMIT); | |
a76a37ef JR |
2066 | } else if (force_commit) { |
2067 | nl_msg_put_flag(actions, OVS_CT_ATTR_FORCE_COMMIT); | |
07659514 JS |
2068 | } |
2069 | if (zone) { | |
2070 | nl_msg_put_u16(actions, OVS_CT_ATTR_ZONE, zone); | |
2071 | } | |
8e53fe8c JS |
2072 | if (ct_mark.mask) { |
2073 | nl_msg_put_unspec(actions, OVS_CT_ATTR_MARK, &ct_mark, | |
2074 | sizeof(ct_mark)); | |
2075 | } | |
2ff8484b | 2076 | if (!ovs_u128_is_zero(ct_label.mask)) { |
9daf2348 JS |
2077 | nl_msg_put_unspec(actions, OVS_CT_ATTR_LABELS, &ct_label, |
2078 | sizeof ct_label); | |
2079 | } | |
d787ad39 JS |
2080 | if (helper) { |
2081 | nl_msg_put_string__(actions, OVS_CT_ATTR_HELPER, helper, | |
2082 | helper_len); | |
2083 | } | |
ebe62ec1 YHW |
2084 | if (timeout) { |
2085 | nl_msg_put_string__(actions, OVS_CT_ATTR_TIMEOUT, timeout, | |
2086 | timeout_len); | |
2087 | } | |
9ac0aada JR |
2088 | if (have_nat) { |
2089 | nl_msg_put_ct_nat(&nat_params, actions); | |
2090 | } | |
07659514 JS |
2091 | nl_msg_end_nested(actions, start); |
2092 | } | |
2093 | ||
2094 | return s - s_; | |
2095 | } | |
2096 | ||
f59cb331 | 2097 | static void |
17553f27 | 2098 | nsh_key_to_attr(struct ofpbuf *buf, const struct ovs_key_nsh *nsh, |
f59cb331 YY |
2099 | uint8_t * metadata, size_t md_size, |
2100 | bool is_mask) | |
2101 | { | |
2102 | size_t nsh_key_ofs; | |
2103 | struct ovs_nsh_key_base base; | |
2104 | ||
2105 | base.flags = nsh->flags; | |
17553f27 | 2106 | base.ttl = nsh->ttl; |
f59cb331 YY |
2107 | base.mdtype = nsh->mdtype; |
2108 | base.np = nsh->np; | |
17553f27 | 2109 | base.path_hdr = nsh->path_hdr; |
f59cb331 YY |
2110 | |
2111 | nsh_key_ofs = nl_msg_start_nested(buf, OVS_KEY_ATTR_NSH); | |
2112 | nl_msg_put_unspec(buf, OVS_NSH_KEY_ATTR_BASE, &base, sizeof base); | |
2113 | ||
2114 | if (is_mask) { | |
2115 | nl_msg_put_unspec(buf, OVS_NSH_KEY_ATTR_MD1, nsh->context, | |
2116 | sizeof nsh->context); | |
2117 | } else { | |
2118 | switch (nsh->mdtype) { | |
2119 | case NSH_M_TYPE1: | |
2120 | nl_msg_put_unspec(buf, OVS_NSH_KEY_ATTR_MD1, nsh->context, | |
2121 | sizeof nsh->context); | |
2122 | break; | |
2123 | case NSH_M_TYPE2: | |
2124 | if (metadata && md_size > 0) { | |
2125 | nl_msg_put_unspec(buf, OVS_NSH_KEY_ATTR_MD2, metadata, | |
2126 | md_size); | |
2127 | } | |
2128 | break; | |
2129 | default: | |
2130 | /* No match support for other MD formats yet. */ | |
2131 | break; | |
2132 | } | |
2133 | } | |
2134 | nl_msg_end_nested(buf, nsh_key_ofs); | |
2135 | } | |
2136 | ||
2137 | ||
1fc11c59 | 2138 | static int |
f59cb331 | 2139 | parse_odp_push_nsh_action(const char *s, struct ofpbuf *actions) |
1fc11c59 JS |
2140 | { |
2141 | int n = 0; | |
2142 | int ret = 0; | |
f59cb331 | 2143 | uint32_t spi = 0; |
17553f27 | 2144 | uint8_t si = 255; |
1fc11c59 | 2145 | uint32_t cd; |
17553f27 | 2146 | struct ovs_key_nsh nsh; |
f59cb331 YY |
2147 | uint8_t metadata[NSH_CTX_HDRS_MAX_LEN]; |
2148 | uint8_t md_size = 0; | |
1fc11c59 | 2149 | |
f59cb331 | 2150 | if (!ovs_scan_len(s, &n, "push_nsh(")) { |
1fc11c59 JS |
2151 | ret = -EINVAL; |
2152 | goto out; | |
2153 | } | |
2154 | ||
2155 | /* The default is NSH_M_TYPE1 */ | |
f59cb331 | 2156 | nsh.flags = 0; |
17553f27 | 2157 | nsh.ttl = 63; |
f59cb331 YY |
2158 | nsh.mdtype = NSH_M_TYPE1; |
2159 | nsh.np = NSH_P_ETHERNET; | |
17553f27 | 2160 | nsh.path_hdr = nsh_spi_si_to_path_hdr(0, 255); |
f59cb331 | 2161 | memset(nsh.context, 0, NSH_M_TYPE1_MDLEN); |
1fc11c59 JS |
2162 | |
2163 | for (;;) { | |
2164 | n += strspn(s + n, delimiters); | |
2165 | if (s[n] == ')') { | |
2166 | break; | |
2167 | } | |
2168 | ||
f59cb331 | 2169 | if (ovs_scan_len(s, &n, "flags=%"SCNi8, &nsh.flags)) { |
1fc11c59 JS |
2170 | continue; |
2171 | } | |
17553f27 YY |
2172 | if (ovs_scan_len(s, &n, "ttl=%"SCNi8, &nsh.ttl)) { |
2173 | continue; | |
2174 | } | |
f59cb331 YY |
2175 | if (ovs_scan_len(s, &n, "mdtype=%"SCNi8, &nsh.mdtype)) { |
2176 | switch (nsh.mdtype) { | |
1fc11c59 JS |
2177 | case NSH_M_TYPE1: |
2178 | /* This is the default format. */; | |
2179 | break; | |
2180 | case NSH_M_TYPE2: | |
2181 | /* Length will be updated later. */ | |
f59cb331 | 2182 | md_size = 0; |
1fc11c59 JS |
2183 | break; |
2184 | default: | |
2185 | ret = -EINVAL; | |
2186 | goto out; | |
2187 | } | |
2188 | continue; | |
2189 | } | |
f59cb331 | 2190 | if (ovs_scan_len(s, &n, "np=%"SCNi8, &nsh.np)) { |
1fc11c59 JS |
2191 | continue; |
2192 | } | |
2193 | if (ovs_scan_len(s, &n, "spi=0x%"SCNx32, &spi)) { | |
1fc11c59 JS |
2194 | continue; |
2195 | } | |
17553f27 | 2196 | if (ovs_scan_len(s, &n, "si=%"SCNi8, &si)) { |
1fc11c59 JS |
2197 | continue; |
2198 | } | |
f59cb331 | 2199 | if (nsh.mdtype == NSH_M_TYPE1) { |
1fc11c59 | 2200 | if (ovs_scan_len(s, &n, "c1=0x%"SCNx32, &cd)) { |
f59cb331 | 2201 | nsh.context[0] = htonl(cd); |
1fc11c59 JS |
2202 | continue; |
2203 | } | |
2204 | if (ovs_scan_len(s, &n, "c2=0x%"SCNx32, &cd)) { | |
f59cb331 | 2205 | nsh.context[1] = htonl(cd); |
1fc11c59 JS |
2206 | continue; |
2207 | } | |
2208 | if (ovs_scan_len(s, &n, "c3=0x%"SCNx32, &cd)) { | |
f59cb331 | 2209 | nsh.context[2] = htonl(cd); |
1fc11c59 JS |
2210 | continue; |
2211 | } | |
2212 | if (ovs_scan_len(s, &n, "c4=0x%"SCNx32, &cd)) { | |
f59cb331 | 2213 | nsh.context[3] = htonl(cd); |
1fc11c59 JS |
2214 | continue; |
2215 | } | |
2216 | } | |
f59cb331 | 2217 | else if (nsh.mdtype == NSH_M_TYPE2) { |
1fc11c59 JS |
2218 | struct ofpbuf b; |
2219 | char buf[512]; | |
7edef47b | 2220 | size_t mdlen, padding; |
33a3ee6b YS |
2221 | if (ovs_scan_len(s, &n, "md2=0x%511[0-9a-fA-F]", buf) |
2222 | && n/2 <= sizeof metadata) { | |
2223 | ofpbuf_use_stub(&b, metadata, sizeof metadata); | |
1fc11c59 | 2224 | ofpbuf_put_hex(&b, buf, &mdlen); |
7edef47b JS |
2225 | /* Pad metadata to 4 bytes. */ |
2226 | padding = PAD_SIZE(mdlen, 4); | |
2227 | if (padding > 0) { | |
2e38f13d | 2228 | ofpbuf_put_zeros(&b, padding); |
7edef47b | 2229 | } |
f59cb331 | 2230 | md_size = mdlen + padding; |
1fc11c59 | 2231 | ofpbuf_uninit(&b); |
99cf9959 | 2232 | continue; |
1fc11c59 | 2233 | } |
1fc11c59 | 2234 | } |
0884990e BP |
2235 | |
2236 | ret = -EINVAL; | |
2237 | goto out; | |
1fc11c59 JS |
2238 | } |
2239 | out: | |
f59cb331 | 2240 | if (ret >= 0) { |
17553f27 | 2241 | nsh.path_hdr = nsh_spi_si_to_path_hdr(spi, si); |
f59cb331 YY |
2242 | size_t offset = nl_msg_start_nested(actions, OVS_ACTION_ATTR_PUSH_NSH); |
2243 | nsh_key_to_attr(actions, &nsh, metadata, md_size, false); | |
2244 | nl_msg_end_nested(actions, offset); | |
2245 | ret = n; | |
1fc11c59 | 2246 | } |
f59cb331 | 2247 | return ret; |
1fc11c59 JS |
2248 | } |
2249 | ||
c37f7135 | 2250 | static int |
5fbe9d15 | 2251 | parse_action_list(struct parse_odp_context *context, const char *s, |
c37f7135 AZ |
2252 | struct ofpbuf *actions) |
2253 | { | |
2254 | int n = 0; | |
2255 | ||
2256 | for (;;) { | |
2257 | int retval; | |
2258 | ||
2259 | n += strspn(s + n, delimiters); | |
2260 | if (s[n] == ')') { | |
2261 | break; | |
2262 | } | |
5fbe9d15 | 2263 | retval = parse_odp_action(context, s + n, actions); |
c37f7135 AZ |
2264 | if (retval < 0) { |
2265 | return retval; | |
2266 | } | |
2267 | n += retval; | |
2268 | } | |
2269 | ||
692fc656 TA |
2270 | if (actions->size > UINT16_MAX) { |
2271 | return -EFBIG; | |
2272 | } | |
2273 | ||
c37f7135 AZ |
2274 | return n; |
2275 | } | |
2276 | ||
5fbe9d15 | 2277 | |
8b7ea2d4 | 2278 | static int |
5fbe9d15 | 2279 | parse_odp_action(struct parse_odp_context *context, const char *s, |
8b7ea2d4 | 2280 | struct ofpbuf *actions) |
5fbe9d15 TA |
2281 | { |
2282 | int retval; | |
2283 | ||
2284 | context->depth++; | |
2285 | ||
2286 | if (context->depth == MAX_ODP_NESTED) { | |
2287 | retval = -EINVAL; | |
2288 | } else { | |
2289 | retval = parse_odp_action__(context, s, actions); | |
2290 | } | |
2291 | ||
2292 | context->depth--; | |
2293 | ||
2294 | return retval; | |
2295 | } | |
2296 | ||
2297 | ||
2298 | static int | |
2299 | parse_odp_action__(struct parse_odp_context *context, const char *s, | |
2300 | struct ofpbuf *actions) | |
8b7ea2d4 WZ |
2301 | { |
2302 | { | |
2303 | uint32_t port; | |
2304 | int n; | |
2305 | ||
2306 | if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) { | |
2307 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port); | |
2308 | return n; | |
7202cbe5 BP |
2309 | } |
2310 | } | |
2311 | ||
9df65060 VDA |
2312 | { |
2313 | uint32_t bond_id; | |
2314 | int n; | |
2315 | ||
2316 | if (ovs_scan(s, "lb_output(%"PRIu32")%n", &bond_id, &n)) { | |
2317 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_LB_OUTPUT, bond_id); | |
2318 | return n; | |
2319 | } | |
2320 | } | |
2321 | ||
aaca4fe0 WT |
2322 | { |
2323 | uint32_t max_len; | |
2324 | int n; | |
2325 | ||
2326 | if (ovs_scan(s, "trunc(%"SCNi32")%n", &max_len, &n)) { | |
2327 | struct ovs_action_trunc *trunc; | |
2328 | ||
2329 | trunc = nl_msg_put_unspec_uninit(actions, | |
2330 | OVS_ACTION_ATTR_TRUNC, sizeof *trunc); | |
2331 | trunc->max_len = max_len; | |
2332 | return n; | |
2333 | } | |
2334 | } | |
2335 | ||
5fbe9d15 | 2336 | if (context->port_names) { |
8b7ea2d4 WZ |
2337 | int len = strcspn(s, delimiters); |
2338 | struct simap_node *node; | |
2339 | ||
5fbe9d15 | 2340 | node = simap_find_len(context->port_names, s, len); |
8b7ea2d4 WZ |
2341 | if (node) { |
2342 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data); | |
2343 | return len; | |
2344 | } | |
2345 | } | |
2346 | ||
d73803ca DDP |
2347 | { |
2348 | uint32_t recirc_id; | |
2349 | int n = -1; | |
2350 | ||
2351 | if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) { | |
2352 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id); | |
2353 | return n; | |
2354 | } | |
2355 | } | |
2356 | ||
8b7ea2d4 WZ |
2357 | if (!strncmp(s, "userspace(", 10)) { |
2358 | return parse_odp_userspace_action(s, actions); | |
2359 | } | |
2360 | ||
7202cbe5 BP |
2361 | if (!strncmp(s, "set(", 4)) { |
2362 | size_t start_ofs; | |
2363 | int retval; | |
c34a5515 BP |
2364 | struct nlattr mask[1024 / sizeof(struct nlattr)]; |
2365 | struct ofpbuf maskbuf = OFPBUF_STUB_INITIALIZER(mask); | |
6d670e7f JR |
2366 | struct nlattr *nested, *key; |
2367 | size_t size; | |
2368 | ||
7202cbe5 | 2369 | start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET); |
5fbe9d15 | 2370 | retval = parse_odp_key_mask_attr(context, s + 4, actions, &maskbuf); |
7202cbe5 | 2371 | if (retval < 0) { |
c34a5515 | 2372 | ofpbuf_uninit(&maskbuf); |
7202cbe5 BP |
2373 | return retval; |
2374 | } | |
2375 | if (s[retval + 4] != ')') { | |
c34a5515 | 2376 | ofpbuf_uninit(&maskbuf); |
7202cbe5 BP |
2377 | return -EINVAL; |
2378 | } | |
6d670e7f JR |
2379 | |
2380 | nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested); | |
2381 | key = nested + 1; | |
2382 | ||
2383 | size = nl_attr_get_size(mask); | |
2384 | if (size == nl_attr_get_size(key)) { | |
2385 | /* Change to masked set action if not fully masked. */ | |
53cb9c3e | 2386 | if (!is_all_ones(mask + 1, size)) { |
34ed68a6 PB |
2387 | /* Remove padding of eariler key payload */ |
2388 | actions->size -= NLA_ALIGN(key->nla_len) - key->nla_len; | |
2389 | ||
2390 | /* Put mask payload right after key payload */ | |
6d670e7f JR |
2391 | key->nla_len += size; |
2392 | ofpbuf_put(actions, mask + 1, size); | |
34ed68a6 | 2393 | |
6d670e7f JR |
2394 | /* 'actions' may have been reallocated by ofpbuf_put(). */ |
2395 | nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested); | |
2396 | nested->nla_type = OVS_ACTION_ATTR_SET_MASKED; | |
2c022949 YS |
2397 | |
2398 | key = nested + 1; | |
2399 | /* Add new padding as needed */ | |
2400 | ofpbuf_put_zeros(actions, NLA_ALIGN(key->nla_len) - | |
2401 | key->nla_len); | |
6d670e7f JR |
2402 | } |
2403 | } | |
c34a5515 | 2404 | ofpbuf_uninit(&maskbuf); |
6d670e7f | 2405 | |
7202cbe5 BP |
2406 | nl_msg_end_nested(actions, start_ofs); |
2407 | return retval + 5; | |
2408 | } | |
2409 | ||
becffb86 BP |
2410 | { |
2411 | struct ovs_action_push_vlan push; | |
2412 | int tpid = ETH_TYPE_VLAN; | |
2413 | int vid, pcp; | |
2414 | int cfi = 1; | |
2415 | int n = -1; | |
7202cbe5 | 2416 | |
c2c28dfd BP |
2417 | if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) |
2418 | || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n", | |
2419 | &vid, &pcp, &cfi, &n) | |
2420 | || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n", | |
2421 | &tpid, &vid, &pcp, &n) | |
2422 | || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n", | |
2423 | &tpid, &vid, &pcp, &cfi, &n)) { | |
1a47405b YS |
2424 | if ((vid & ~(VLAN_VID_MASK >> VLAN_VID_SHIFT)) != 0 |
2425 | || (pcp & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) != 0) { | |
2426 | return -EINVAL; | |
2427 | } | |
becffb86 BP |
2428 | push.vlan_tpid = htons(tpid); |
2429 | push.vlan_tci = htons((vid << VLAN_VID_SHIFT) | |
2430 | | (pcp << VLAN_PCP_SHIFT) | |
2431 | | (cfi ? VLAN_CFI : 0)); | |
2432 | nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN, | |
2433 | &push, sizeof push); | |
2434 | ||
2435 | return n; | |
7202cbe5 | 2436 | } |
7202cbe5 BP |
2437 | } |
2438 | ||
becffb86 BP |
2439 | if (!strncmp(s, "pop_vlan", 8)) { |
2440 | nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN); | |
2441 | return 8; | |
7202cbe5 BP |
2442 | } |
2443 | ||
5dddf960 JR |
2444 | { |
2445 | unsigned long long int meter_id; | |
2446 | int n = -1; | |
2447 | ||
2448 | if (sscanf(s, "meter(%lli)%n", &meter_id, &n) > 0 && n > 0) { | |
2449 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_METER, meter_id); | |
2450 | return n; | |
2451 | } | |
2452 | } | |
2453 | ||
7202cbe5 BP |
2454 | { |
2455 | double percentage; | |
2456 | int n = -1; | |
2457 | ||
c2c28dfd BP |
2458 | if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) |
2459 | && percentage >= 0. && percentage <= 100.0) { | |
7202cbe5 BP |
2460 | size_t sample_ofs, actions_ofs; |
2461 | double probability; | |
2462 | ||
2463 | probability = floor(UINT32_MAX * (percentage / 100.0) + .5); | |
2464 | sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE); | |
2465 | nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY, | |
2466 | (probability <= 0 ? 0 | |
2467 | : probability >= UINT32_MAX ? UINT32_MAX | |
2468 | : probability)); | |
2469 | ||
2470 | actions_ofs = nl_msg_start_nested(actions, | |
2471 | OVS_SAMPLE_ATTR_ACTIONS); | |
5fbe9d15 TA |
2472 | int retval = parse_action_list(context, s + n, actions); |
2473 | if (retval < 0) { | |
c37f7135 | 2474 | return retval; |
5fbe9d15 TA |
2475 | } |
2476 | ||
7202cbe5 | 2477 | |
c37f7135 | 2478 | n += retval; |
7202cbe5 BP |
2479 | nl_msg_end_nested(actions, actions_ofs); |
2480 | nl_msg_end_nested(actions, sample_ofs); | |
2481 | ||
2482 | return s[n + 1] == ')' ? n + 2 : -EINVAL; | |
2483 | } | |
2484 | } | |
2485 | ||
c37f7135 AZ |
2486 | { |
2487 | if (!strncmp(s, "clone(", 6)) { | |
2488 | size_t actions_ofs; | |
2489 | int n = 6; | |
2490 | ||
2491 | actions_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_CLONE); | |
5fbe9d15 | 2492 | int retval = parse_action_list(context, s + n, actions); |
c37f7135 AZ |
2493 | if (retval < 0) { |
2494 | return retval; | |
2495 | } | |
2496 | n += retval; | |
2497 | nl_msg_end_nested(actions, actions_ofs); | |
2498 | return n + 1; | |
2499 | } | |
2500 | } | |
2501 | ||
1fc11c59 | 2502 | { |
f59cb331 YY |
2503 | if (!strncmp(s, "push_nsh(", 9)) { |
2504 | int retval = parse_odp_push_nsh_action(s, actions); | |
1fc11c59 JS |
2505 | if (retval < 0) { |
2506 | return retval; | |
2507 | } | |
2508 | return retval + 1; | |
2509 | } | |
2510 | } | |
2511 | ||
2512 | { | |
2513 | int n; | |
f59cb331 YY |
2514 | if (ovs_scan(s, "pop_nsh()%n", &n)) { |
2515 | nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_NSH); | |
1fc11c59 JS |
2516 | return n; |
2517 | } | |
2518 | } | |
2519 | ||
a36de779 PS |
2520 | { |
2521 | uint32_t port; | |
2522 | int n; | |
2523 | ||
2524 | if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) { | |
2525 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port); | |
2526 | return n; | |
2527 | } | |
2528 | } | |
2529 | ||
1fe178d2 EG |
2530 | { |
2531 | if (!strncmp(s, "ct_clear", 8)) { | |
2532 | nl_msg_put_flag(actions, OVS_ACTION_ATTR_CT_CLEAR); | |
2533 | return 8; | |
2534 | } | |
2535 | } | |
2536 | ||
5b34f8fc NS |
2537 | { |
2538 | uint16_t pkt_len; | |
2539 | int n = -1; | |
2540 | if (ovs_scan(s, "check_pkt_len(size=%"SCNi16",gt(%n", &pkt_len, &n)) { | |
2541 | size_t cpl_ofs, actions_ofs; | |
2542 | cpl_ofs = nl_msg_start_nested(actions, | |
2543 | OVS_ACTION_ATTR_CHECK_PKT_LEN); | |
2544 | nl_msg_put_u16(actions, OVS_CHECK_PKT_LEN_ATTR_PKT_LEN, pkt_len); | |
2545 | actions_ofs = nl_msg_start_nested( | |
2546 | actions, OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER); | |
2547 | ||
2548 | int retval; | |
2549 | if (!strncasecmp(s + n, "drop", 4)) { | |
2550 | n += 4; | |
2551 | } else { | |
5fbe9d15 | 2552 | retval = parse_action_list(context, s + n, actions); |
5b34f8fc NS |
2553 | if (retval < 0) { |
2554 | return retval; | |
2555 | } | |
2556 | ||
2557 | n += retval; | |
2558 | } | |
2559 | nl_msg_end_nested(actions, actions_ofs); | |
2560 | retval = -1; | |
2561 | if (!ovs_scan(s + n, "),le(%n", &retval)) { | |
2562 | return -EINVAL; | |
2563 | } | |
2564 | n += retval; | |
2565 | ||
2566 | actions_ofs = nl_msg_start_nested( | |
2567 | actions, OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL); | |
2568 | if (!strncasecmp(s + n, "drop", 4)) { | |
2569 | n += 4; | |
2570 | } else { | |
5fbe9d15 | 2571 | retval = parse_action_list(context, s + n, actions); |
5b34f8fc NS |
2572 | if (retval < 0) { |
2573 | return retval; | |
2574 | } | |
2575 | n += retval; | |
2576 | } | |
2577 | nl_msg_end_nested(actions, actions_ofs); | |
2578 | nl_msg_end_nested(actions, cpl_ofs); | |
2579 | return s[n + 1] == ')' ? n + 2 : -EINVAL; | |
2580 | } | |
2581 | } | |
2582 | ||
07659514 JS |
2583 | { |
2584 | int retval; | |
2585 | ||
2586 | retval = parse_conntrack_action(s, actions); | |
2587 | if (retval) { | |
2588 | return retval; | |
2589 | } | |
2590 | } | |
2591 | ||
a36de779 PS |
2592 | { |
2593 | struct ovs_action_push_tnl data; | |
2594 | int n; | |
2595 | ||
2596 | n = ovs_parse_tnl_push(s, &data); | |
2597 | if (n > 0) { | |
2598 | odp_put_tnl_push_action(actions, &data); | |
2599 | return n; | |
2600 | } else if (n < 0) { | |
2601 | return n; | |
2602 | } | |
2603 | } | |
5fbe9d15 | 2604 | |
7202cbe5 BP |
2605 | return -EINVAL; |
2606 | } | |
2607 | ||
2608 | /* Parses the string representation of datapath actions, in the format output | |
2609 | * by format_odp_action(). Returns 0 if successful, otherwise a positive errno | |
2610 | * value. On success, the ODP actions are appended to 'actions' as a series of | |
2611 | * Netlink attributes. On failure, no data is appended to 'actions'. Either | |
2612 | * way, 'actions''s data might be reallocated. */ | |
2613 | int | |
44bac24b | 2614 | odp_actions_from_string(const char *s, const struct simap *port_names, |
7202cbe5 BP |
2615 | struct ofpbuf *actions) |
2616 | { | |
2617 | size_t old_size; | |
2618 | ||
2619 | if (!strcasecmp(s, "drop")) { | |
a13a0209 | 2620 | nl_msg_put_u32(actions, OVS_ACTION_ATTR_DROP, XLATE_OK); |
7202cbe5 BP |
2621 | return 0; |
2622 | } | |
2623 | ||
5fbe9d15 TA |
2624 | struct parse_odp_context context = (struct parse_odp_context) { |
2625 | .port_names = port_names, | |
2626 | }; | |
2627 | ||
6fd6ed71 | 2628 | old_size = actions->size; |
7202cbe5 BP |
2629 | for (;;) { |
2630 | int retval; | |
2631 | ||
2632 | s += strspn(s, delimiters); | |
2633 | if (!*s) { | |
2634 | return 0; | |
2635 | } | |
2636 | ||
5fbe9d15 TA |
2637 | retval = parse_odp_action(&context, s, actions); |
2638 | ||
7202cbe5 | 2639 | if (retval < 0 || !strchr(delimiters, s[retval])) { |
6fd6ed71 | 2640 | actions->size = old_size; |
7202cbe5 BP |
2641 | return -retval; |
2642 | } | |
2643 | s += retval; | |
2644 | } | |
2645 | ||
2646 | return 0; | |
2647 | } | |
14608a15 | 2648 | \f |
6b8da9e9 JG |
2649 | static const struct attr_len_tbl ovs_vxlan_ext_attr_lens[OVS_VXLAN_EXT_MAX + 1] = { |
2650 | [OVS_VXLAN_EXT_GBP] = { .len = 4 }, | |
2651 | }; | |
2652 | ||
2653 | static const struct attr_len_tbl ovs_tun_key_attr_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = { | |
2654 | [OVS_TUNNEL_KEY_ATTR_ID] = { .len = 8 }, | |
2655 | [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = 4 }, | |
2656 | [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = 4 }, | |
2657 | [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 }, | |
2658 | [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 }, | |
2659 | [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 }, | |
2660 | [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 }, | |
2661 | [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = 2 }, | |
2662 | [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = 2 }, | |
2663 | [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 }, | |
2664 | [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = ATTR_LEN_VARIABLE }, | |
2665 | [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = ATTR_LEN_NESTED, | |
2666 | .next = ovs_vxlan_ext_attr_lens , | |
2667 | .next_max = OVS_VXLAN_EXT_MAX}, | |
ffe4c74f JB |
2668 | [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = 16 }, |
2669 | [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = 16 }, | |
98514eea | 2670 | [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS] = { .len = ATTR_LEN_VARIABLE }, |
3c6d05a0 | 2671 | [OVS_TUNNEL_KEY_ATTR_GTPU_OPTS] = { .len = ATTR_LEN_VARIABLE }, |
6b8da9e9 JG |
2672 | }; |
2673 | ||
0aee3827 | 2674 | const struct attr_len_tbl ovs_flow_key_attr_lens[OVS_KEY_ATTR_MAX + 1] = { |
6b8da9e9 JG |
2675 | [OVS_KEY_ATTR_ENCAP] = { .len = ATTR_LEN_NESTED }, |
2676 | [OVS_KEY_ATTR_PRIORITY] = { .len = 4 }, | |
2677 | [OVS_KEY_ATTR_SKB_MARK] = { .len = 4 }, | |
2678 | [OVS_KEY_ATTR_DP_HASH] = { .len = 4 }, | |
2679 | [OVS_KEY_ATTR_RECIRC_ID] = { .len = 4 }, | |
2680 | [OVS_KEY_ATTR_TUNNEL] = { .len = ATTR_LEN_NESTED, | |
2681 | .next = ovs_tun_key_attr_lens, | |
2682 | .next_max = OVS_TUNNEL_KEY_ATTR_MAX }, | |
2683 | [OVS_KEY_ATTR_IN_PORT] = { .len = 4 }, | |
2684 | [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) }, | |
2685 | [OVS_KEY_ATTR_VLAN] = { .len = 2 }, | |
2686 | [OVS_KEY_ATTR_ETHERTYPE] = { .len = 2 }, | |
2687 | [OVS_KEY_ATTR_MPLS] = { .len = ATTR_LEN_VARIABLE }, | |
2688 | [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) }, | |
2689 | [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) }, | |
2690 | [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) }, | |
2691 | [OVS_KEY_ATTR_TCP_FLAGS] = { .len = 2 }, | |
2692 | [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) }, | |
2693 | [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) }, | |
2694 | [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) }, | |
2695 | [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) }, | |
2696 | [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) }, | |
2697 | [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) }, | |
9b2b8497 | 2698 | [OVS_KEY_ATTR_ND_EXTENSIONS] = { .len = sizeof(struct ovs_key_nd_extensions) }, |
07659514 JS |
2699 | [OVS_KEY_ATTR_CT_STATE] = { .len = 4 }, |
2700 | [OVS_KEY_ATTR_CT_ZONE] = { .len = 2 }, | |
8e53fe8c | 2701 | [OVS_KEY_ATTR_CT_MARK] = { .len = 4 }, |
9daf2348 | 2702 | [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) }, |
c30b4cea JR |
2703 | [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = { .len = sizeof(struct ovs_key_ct_tuple_ipv4) }, |
2704 | [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = { .len = sizeof(struct ovs_key_ct_tuple_ipv6) }, | |
beb75a40 | 2705 | [OVS_KEY_ATTR_PACKET_TYPE] = { .len = 4 }, |
f59cb331 YY |
2706 | [OVS_KEY_ATTR_NSH] = { .len = ATTR_LEN_NESTED, |
2707 | .next = ovs_nsh_key_attr_lens, | |
2708 | .next_max = OVS_NSH_KEY_ATTR_MAX }, | |
6b8da9e9 JG |
2709 | }; |
2710 | ||
36956a7d | 2711 | /* Returns the correct length of the payload for a flow key attribute of the |
6b8da9e9 JG |
2712 | * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE |
2713 | * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the | |
2714 | * payload is a nested type. */ | |
36956a7d | 2715 | static int |
ce9c9b8b | 2716 | odp_key_attr_len(const struct attr_len_tbl tbl[], int max_type, uint16_t type) |
36956a7d | 2717 | { |
ce9c9b8b | 2718 | if (type > max_type) { |
6b8da9e9 | 2719 | return ATTR_LEN_INVALID; |
36956a7d BP |
2720 | } |
2721 | ||
6b8da9e9 | 2722 | return tbl[type].len; |
36956a7d BP |
2723 | } |
2724 | ||
36956a7d BP |
2725 | static void |
2726 | format_generic_odp_key(const struct nlattr *a, struct ds *ds) | |
2727 | { | |
2728 | size_t len = nl_attr_get_size(a); | |
36956a7d BP |
2729 | if (len) { |
2730 | const uint8_t *unspec; | |
2731 | unsigned int i; | |
2732 | ||
2733 | unspec = nl_attr_get(a); | |
2734 | for (i = 0; i < len; i++) { | |
e6cc0bab AZ |
2735 | if (i) { |
2736 | ds_put_char(ds, ' '); | |
2737 | } | |
36956a7d BP |
2738 | ds_put_format(ds, "%02x", unspec[i]); |
2739 | } | |
36956a7d BP |
2740 | } |
2741 | } | |
2742 | ||
7257b535 BP |
2743 | static const char * |
2744 | ovs_frag_type_to_string(enum ovs_frag_type type) | |
2745 | { | |
2746 | switch (type) { | |
2747 | case OVS_FRAG_TYPE_NONE: | |
2748 | return "no"; | |
2749 | case OVS_FRAG_TYPE_FIRST: | |
2750 | return "first"; | |
2751 | case OVS_FRAG_TYPE_LATER: | |
2752 | return "later"; | |
2753 | case __OVS_FRAG_TYPE_MAX: | |
2754 | default: | |
2755 | return "<error>"; | |
2756 | } | |
2757 | } | |
2758 | ||
f59cb331 YY |
2759 | enum odp_key_fitness |
2760 | odp_nsh_hdr_from_attr(const struct nlattr *attr, | |
2761 | struct nsh_hdr *nsh_hdr, size_t size) | |
2762 | { | |
2763 | unsigned int left; | |
2764 | const struct nlattr *a; | |
2765 | bool unknown = false; | |
2766 | uint8_t flags = 0; | |
17553f27 | 2767 | uint8_t ttl = 63; |
f59cb331 YY |
2768 | size_t mdlen = 0; |
2769 | bool has_md1 = false; | |
2770 | bool has_md2 = false; | |
2771 | ||
7587e1da YS |
2772 | memset(nsh_hdr, 0, size); |
2773 | ||
f59cb331 YY |
2774 | NL_NESTED_FOR_EACH (a, left, attr) { |
2775 | uint16_t type = nl_attr_type(a); | |
2776 | size_t len = nl_attr_get_size(a); | |
2777 | int expected_len = odp_key_attr_len(ovs_nsh_key_attr_lens, | |
2778 | OVS_NSH_KEY_ATTR_MAX, type); | |
2779 | ||
2780 | if (len != expected_len && expected_len >= 0) { | |
2781 | return ODP_FIT_ERROR; | |
2782 | } | |
2783 | ||
2784 | switch (type) { | |
2785 | case OVS_NSH_KEY_ATTR_BASE: { | |
2786 | const struct ovs_nsh_key_base *base = nl_attr_get(a); | |
2787 | nsh_hdr->next_proto = base->np; | |
2788 | nsh_hdr->md_type = base->mdtype; | |
2789 | put_16aligned_be32(&nsh_hdr->path_hdr, base->path_hdr); | |
2790 | flags = base->flags; | |
17553f27 | 2791 | ttl = base->ttl; |
f59cb331 YY |
2792 | break; |
2793 | } | |
2794 | case OVS_NSH_KEY_ATTR_MD1: { | |
2795 | const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a); | |
2796 | struct nsh_md1_ctx *md1_dst = &nsh_hdr->md1; | |
2797 | has_md1 = true; | |
2798 | mdlen = nl_attr_get_size(a); | |
2799 | if ((mdlen + NSH_BASE_HDR_LEN != NSH_M_TYPE1_LEN) || | |
2800 | (mdlen + NSH_BASE_HDR_LEN > size)) { | |
2801 | return ODP_FIT_ERROR; | |
2802 | } | |
2803 | memcpy(md1_dst, md1, mdlen); | |
2804 | break; | |
2805 | } | |
2806 | case OVS_NSH_KEY_ATTR_MD2: { | |
2807 | struct nsh_md2_tlv *md2_dst = &nsh_hdr->md2; | |
2808 | const uint8_t *md2 = nl_attr_get(a); | |
2809 | has_md2 = true; | |
2810 | mdlen = nl_attr_get_size(a); | |
2811 | if (mdlen + NSH_BASE_HDR_LEN > size) { | |
2812 | return ODP_FIT_ERROR; | |
2813 | } | |
2814 | memcpy(md2_dst, md2, mdlen); | |
2815 | break; | |
2816 | } | |
2817 | default: | |
2818 | /* Allow this to show up as unexpected, if there are unknown | |
2819 | * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */ | |
2820 | unknown = true; | |
2821 | break; | |
2822 | } | |
2823 | } | |
2824 | ||
2825 | if (unknown) { | |
2826 | return ODP_FIT_TOO_MUCH; | |
2827 | } | |
2828 | ||
2829 | if ((has_md1 && nsh_hdr->md_type != NSH_M_TYPE1) | |
2830 | || (has_md2 && nsh_hdr->md_type != NSH_M_TYPE2)) { | |
2831 | return ODP_FIT_ERROR; | |
2832 | } | |
2833 | ||
2834 | /* nsh header length = NSH_BASE_HDR_LEN + mdlen */ | |
17553f27 | 2835 | nsh_set_flags_ttl_len(nsh_hdr, flags, ttl, NSH_BASE_HDR_LEN + mdlen); |
f59cb331 YY |
2836 | |
2837 | return ODP_FIT_PERFECT; | |
2838 | } | |
2839 | ||
d40533fc BP |
2840 | /* Reports the error 'msg', which is formatted as with printf(). |
2841 | * | |
2842 | * If 'errorp' is nonnull, then some the wants the error report to come | |
2843 | * directly back to it, so the function stores the error message into '*errorp' | |
2844 | * (after first freeing it in case there's something there already). | |
2845 | * | |
2846 | * Otherwise, logs the message at WARN level, rate-limited. */ | |
2847 | static void OVS_PRINTF_FORMAT(3, 4) | |
2848 | odp_parse_error(struct vlog_rate_limit *rl, char **errorp, | |
2849 | const char *msg, ...) | |
2850 | { | |
2851 | if (OVS_UNLIKELY(errorp)) { | |
2852 | free(*errorp); | |
2853 | ||
2854 | va_list args; | |
2855 | va_start(args, msg); | |
2856 | *errorp = xvasprintf(msg, args); | |
2857 | va_end(args); | |
2858 | } else if (!VLOG_DROP_WARN(rl)) { | |
2859 | va_list args; | |
2860 | va_start(args, msg); | |
2861 | char *error = xvasprintf(msg, args); | |
2862 | va_end(args); | |
2863 | ||
2864 | VLOG_WARN("%s", error); | |
2865 | ||
2866 | free(error); | |
2867 | } | |
2868 | } | |
2869 | ||
2870 | /* Parses OVS_KEY_ATTR_NSH attribute 'attr' into 'nsh' and 'nsh_mask' and | |
569c3df2 IM |
2871 | * returns fitness. If the attribute is a key, 'is_mask' should be false; |
2872 | * if it is a mask, 'is_mask' should be true. If 'errorp' is nonnull and the | |
2873 | * function returns ODP_FIT_ERROR, stores a malloc()'d error message in | |
2874 | * '*errorp'. */ | |
2875 | static enum odp_key_fitness | |
2876 | odp_nsh_key_from_attr__(const struct nlattr *attr, bool is_mask, | |
2877 | struct ovs_key_nsh *nsh, struct ovs_key_nsh *nsh_mask, | |
2878 | char **errorp) | |
f59cb331 | 2879 | { |
d40533fc BP |
2880 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
2881 | if (errorp) { | |
2882 | *errorp = NULL; | |
2883 | } | |
2884 | ||
f59cb331 YY |
2885 | unsigned int left; |
2886 | const struct nlattr *a; | |
2887 | bool unknown = false; | |
2888 | bool has_md1 = false; | |
2889 | ||
2890 | NL_NESTED_FOR_EACH (a, left, attr) { | |
2891 | uint16_t type = nl_attr_type(a); | |
2892 | size_t len = nl_attr_get_size(a); | |
2893 | int expected_len = odp_key_attr_len(ovs_nsh_key_attr_lens, | |
2894 | OVS_NSH_KEY_ATTR_MAX, type); | |
d40533fc BP |
2895 | if (expected_len) { |
2896 | if (nsh_mask) { | |
2897 | expected_len *= 2; | |
2898 | } | |
2899 | if (len != expected_len) { | |
2900 | odp_parse_error(&rl, errorp, "NSH %s attribute %"PRIu16" " | |
2901 | "should have length %d but actually has " | |
2902 | "%"PRIuSIZE, | |
2903 | nsh_mask ? "mask" : "key", | |
2904 | type, expected_len, len); | |
2905 | return ODP_FIT_ERROR; | |
2906 | } | |
f59cb331 YY |
2907 | } |
2908 | ||
2909 | switch (type) { | |
81fdabb9 YY |
2910 | case OVS_NSH_KEY_ATTR_UNSPEC: |
2911 | break; | |
f59cb331 YY |
2912 | case OVS_NSH_KEY_ATTR_BASE: { |
2913 | const struct ovs_nsh_key_base *base = nl_attr_get(a); | |
2914 | nsh->flags = base->flags; | |
17553f27 | 2915 | nsh->ttl = base->ttl; |
f59cb331 YY |
2916 | nsh->mdtype = base->mdtype; |
2917 | nsh->np = base->np; | |
17553f27 | 2918 | nsh->path_hdr = base->path_hdr; |
81fdabb9 YY |
2919 | if (nsh_mask && (len == 2 * sizeof(*base))) { |
2920 | const struct ovs_nsh_key_base *base_mask = base + 1; | |
2921 | nsh_mask->flags = base_mask->flags; | |
2922 | nsh_mask->ttl = base_mask->ttl; | |
2923 | nsh_mask->mdtype = base_mask->mdtype; | |
2924 | nsh_mask->np = base_mask->np; | |
2925 | nsh_mask->path_hdr = base_mask->path_hdr; | |
2926 | } | |
f59cb331 YY |
2927 | break; |
2928 | } | |
2929 | case OVS_NSH_KEY_ATTR_MD1: { | |
2930 | const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a); | |
2931 | has_md1 = true; | |
2932 | memcpy(nsh->context, md1->context, sizeof md1->context); | |
81fdabb9 YY |
2933 | if (len == 2 * sizeof(*md1)) { |
2934 | const struct ovs_nsh_key_md1 *md1_mask = md1 + 1; | |
2935 | memcpy(nsh_mask->context, md1_mask->context, | |
2936 | sizeof(*md1_mask)); | |
2937 | } | |
f59cb331 YY |
2938 | break; |
2939 | } | |
2940 | case OVS_NSH_KEY_ATTR_MD2: | |
2941 | default: | |
2942 | /* Allow this to show up as unexpected, if there are unknown | |
2943 | * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */ | |
2944 | unknown = true; | |
2945 | break; | |
2946 | } | |
2947 | } | |
2948 | ||
2949 | if (unknown) { | |
2950 | return ODP_FIT_TOO_MUCH; | |
2951 | } | |
2952 | ||
569c3df2 | 2953 | if (!is_mask && has_md1 && nsh->mdtype != NSH_M_TYPE1 && !nsh_mask) { |
d40533fc BP |
2954 | odp_parse_error(&rl, errorp, "OVS_NSH_KEY_ATTR_MD1 present but " |
2955 | "declared mdtype %"PRIu8" is not %d (NSH_M_TYPE1)", | |
2956 | nsh->mdtype, NSH_M_TYPE1); | |
f59cb331 YY |
2957 | return ODP_FIT_ERROR; |
2958 | } | |
2959 | ||
2960 | return ODP_FIT_PERFECT; | |
2961 | } | |
2962 | ||
569c3df2 IM |
2963 | /* Parses OVS_KEY_ATTR_NSH attribute 'attr' into 'nsh' and 'nsh_mask' and |
2964 | * returns fitness. The attribute should be a key (not a mask). If 'errorp' | |
2965 | * is nonnull and the function returns ODP_FIT_ERROR, stores a malloc()'d error | |
2966 | * message in '*errorp'. */ | |
2967 | enum odp_key_fitness | |
2968 | odp_nsh_key_from_attr(const struct nlattr *attr, struct ovs_key_nsh *nsh, | |
2969 | struct ovs_key_nsh *nsh_mask, char **errorp) | |
2970 | { | |
2971 | return odp_nsh_key_from_attr__(attr, false, nsh, nsh_mask, errorp); | |
2972 | } | |
2973 | ||
d40533fc BP |
2974 | /* Parses OVS_KEY_ATTR_TUNNEL attribute 'attr' into 'tun' and returns fitness. |
2975 | * If the attribute is a key, 'is_mask' should be false; if it is a mask, | |
2976 | * 'is_mask' should be true. If 'errorp' is nonnull and the function returns | |
2977 | * ODP_FIT_ERROR, stores a malloc()'d error message in '*errorp'. */ | |
ec1f6f32 | 2978 | static enum odp_key_fitness |
8d8ab6c2 | 2979 | odp_tun_key_from_attr__(const struct nlattr *attr, bool is_mask, |
d40533fc | 2980 | struct flow_tnl *tun, char **errorp) |
9b405f1a | 2981 | { |
d40533fc | 2982 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
9b405f1a PS |
2983 | unsigned int left; |
2984 | const struct nlattr *a; | |
2985 | bool ttl = false; | |
2986 | bool unknown = false; | |
2987 | ||
2988 | NL_NESTED_FOR_EACH(a, left, attr) { | |
2989 | uint16_t type = nl_attr_type(a); | |
2990 | size_t len = nl_attr_get_size(a); | |
6b8da9e9 JG |
2991 | int expected_len = odp_key_attr_len(ovs_tun_key_attr_lens, |
2992 | OVS_TUNNEL_ATTR_MAX, type); | |
9b405f1a PS |
2993 | |
2994 | if (len != expected_len && expected_len >= 0) { | |
d40533fc BP |
2995 | odp_parse_error(&rl, errorp, "tunnel key attribute %"PRIu16" " |
2996 | "should have length %d but actually has %"PRIuSIZE, | |
2997 | type, expected_len, len); | |
9b405f1a PS |
2998 | return ODP_FIT_ERROR; |
2999 | } | |
3000 | ||
3001 | switch (type) { | |
3002 | case OVS_TUNNEL_KEY_ATTR_ID: | |
3003 | tun->tun_id = nl_attr_get_be64(a); | |
3004 | tun->flags |= FLOW_TNL_F_KEY; | |
3005 | break; | |
3006 | case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: | |
3007 | tun->ip_src = nl_attr_get_be32(a); | |
3008 | break; | |
3009 | case OVS_TUNNEL_KEY_ATTR_IPV4_DST: | |
3010 | tun->ip_dst = nl_attr_get_be32(a); | |
3011 | break; | |
ffe4c74f JB |
3012 | case OVS_TUNNEL_KEY_ATTR_IPV6_SRC: |
3013 | tun->ipv6_src = nl_attr_get_in6_addr(a); | |
3014 | break; | |
3015 | case OVS_TUNNEL_KEY_ATTR_IPV6_DST: | |
3016 | tun->ipv6_dst = nl_attr_get_in6_addr(a); | |
3017 | break; | |
9b405f1a PS |
3018 | case OVS_TUNNEL_KEY_ATTR_TOS: |
3019 | tun->ip_tos = nl_attr_get_u8(a); | |
3020 | break; | |
3021 | case OVS_TUNNEL_KEY_ATTR_TTL: | |
3022 | tun->ip_ttl = nl_attr_get_u8(a); | |
3023 | ttl = true; | |
3024 | break; | |
3025 | case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: | |
3026 | tun->flags |= FLOW_TNL_F_DONT_FRAGMENT; | |
3027 | break; | |
3028 | case OVS_TUNNEL_KEY_ATTR_CSUM: | |
3029 | tun->flags |= FLOW_TNL_F_CSUM; | |
3030 | break; | |
8b7ea2d4 WZ |
3031 | case OVS_TUNNEL_KEY_ATTR_TP_SRC: |
3032 | tun->tp_src = nl_attr_get_be16(a); | |
3033 | break; | |
3034 | case OVS_TUNNEL_KEY_ATTR_TP_DST: | |
3035 | tun->tp_dst = nl_attr_get_be16(a); | |
3036 | break; | |
94872594 JG |
3037 | case OVS_TUNNEL_KEY_ATTR_OAM: |
3038 | tun->flags |= FLOW_TNL_F_OAM; | |
3039 | break; | |
ac6073e3 MC |
3040 | case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: { |
3041 | static const struct nl_policy vxlan_opts_policy[] = { | |
3042 | [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 }, | |
3043 | }; | |
3044 | struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)]; | |
3045 | ||
3046 | if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) { | |
d40533fc | 3047 | odp_parse_error(&rl, errorp, "error parsing VXLAN options"); |
ac6073e3 MC |
3048 | return ODP_FIT_ERROR; |
3049 | } | |
3050 | ||
3051 | if (ext[OVS_VXLAN_EXT_GBP]) { | |
3052 | uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]); | |
3053 | ||
3054 | tun->gbp_id = htons(gbp & 0xFFFF); | |
3055 | tun->gbp_flags = (gbp >> 16) & 0xFF; | |
3056 | } | |
3057 | ||
3058 | break; | |
3059 | } | |
9558d2a5 | 3060 | case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: |
8d8ab6c2 | 3061 | tun_metadata_from_geneve_nlattr(a, is_mask, tun); |
c1fc1411 | 3062 | break; |
98514eea | 3063 | case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS: { |
5062a040 | 3064 | const struct erspan_metadata *opts = nl_attr_get(a); |
98514eea | 3065 | |
5062a040 | 3066 | tun->erspan_ver = opts->version; |
98514eea | 3067 | if (tun->erspan_ver == 1) { |
5062a040 | 3068 | tun->erspan_idx = ntohl(opts->u.index); |
98514eea | 3069 | } else if (tun->erspan_ver == 2) { |
5062a040 WT |
3070 | tun->erspan_dir = opts->u.md2.dir; |
3071 | tun->erspan_hwid = get_hwid(&opts->u.md2); | |
98514eea WT |
3072 | } else { |
3073 | VLOG_WARN("%s invalid erspan version\n", __func__); | |
3074 | } | |
3075 | break; | |
3076 | } | |
3c6d05a0 WT |
3077 | case OVS_TUNNEL_KEY_ATTR_GTPU_OPTS: { |
3078 | const struct gtpu_metadata *opts = nl_attr_get(a); | |
3079 | ||
3080 | tun->gtpu_flags = opts->flags; | |
3081 | tun->gtpu_msgtype = opts->msgtype; | |
3082 | break; | |
3083 | } | |
9558d2a5 | 3084 | |
9b405f1a PS |
3085 | default: |
3086 | /* Allow this to show up as unexpected, if there are unknown | |
3087 | * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */ | |
3088 | unknown = true; | |
3089 | break; | |
3090 | } | |
3091 | } | |
3092 | ||
3093 | if (!ttl) { | |
d40533fc | 3094 | odp_parse_error(&rl, errorp, "tunnel options missing TTL"); |
9b405f1a PS |
3095 | return ODP_FIT_ERROR; |
3096 | } | |
3097 | if (unknown) { | |
758c456d | 3098 | return ODP_FIT_TOO_MUCH; |
9b405f1a PS |
3099 | } |
3100 | return ODP_FIT_PERFECT; | |
3101 | } | |
3102 | ||
d40533fc BP |
3103 | /* Parses OVS_KEY_ATTR_TUNNEL key attribute 'attr' into 'tun' and returns |
3104 | * fitness. The attribute should be a key (not a mask). If 'errorp' is | |
3105 | * nonnull, stores NULL into '*errorp' on success, otherwise a malloc()'d error | |
3106 | * message. */ | |
ec1f6f32 | 3107 | enum odp_key_fitness |
d40533fc BP |
3108 | odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun, |
3109 | char **errorp) | |
ec1f6f32 | 3110 | { |
d40533fc BP |
3111 | if (errorp) { |
3112 | *errorp = NULL; | |
3113 | } | |
35303d71 | 3114 | memset(tun, 0, sizeof *tun); |
d40533fc | 3115 | return odp_tun_key_from_attr__(attr, false, tun, errorp); |
ec1f6f32 JG |
3116 | } |
3117 | ||
9b405f1a | 3118 | static void |
ec1f6f32 | 3119 | tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key, |
9558d2a5 | 3120 | const struct flow_tnl *tun_flow_key, |
c6d87201 | 3121 | const struct ofpbuf *key_buf, const char *tnl_type) |
9b405f1a PS |
3122 | { |
3123 | size_t tun_key_ofs; | |
3124 | ||
3125 | tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL); | |
3126 | ||
27a8cbbc BP |
3127 | /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */ |
3128 | if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) { | |
9b405f1a PS |
3129 | nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id); |
3130 | } | |
3131 | if (tun_key->ip_src) { | |
3132 | nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src); | |
3133 | } | |
3134 | if (tun_key->ip_dst) { | |
3135 | nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst); | |
3136 | } | |
ffe4c74f JB |
3137 | if (ipv6_addr_is_set(&tun_key->ipv6_src)) { |
3138 | nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_SRC, &tun_key->ipv6_src); | |
3139 | } | |
3140 | if (ipv6_addr_is_set(&tun_key->ipv6_dst)) { | |
3141 | nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_DST, &tun_key->ipv6_dst); | |
3142 | } | |
9b405f1a PS |
3143 | if (tun_key->ip_tos) { |
3144 | nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos); | |
3145 | } | |
3146 | nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl); | |
3147 | if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) { | |
3148 | nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT); | |
3149 | } | |
3150 | if (tun_key->flags & FLOW_TNL_F_CSUM) { | |
3151 | nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM); | |
3152 | } | |
8b7ea2d4 WZ |
3153 | if (tun_key->tp_src) { |
3154 | nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src); | |
3155 | } | |
3156 | if (tun_key->tp_dst) { | |
3157 | nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst); | |
3158 | } | |
94872594 JG |
3159 | if (tun_key->flags & FLOW_TNL_F_OAM) { |
3160 | nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM); | |
3161 | } | |
c6d87201 WT |
3162 | |
3163 | /* If tnl_type is set to a particular type of output tunnel, | |
3164 | * only put its relevant tunnel metadata to the nlattr. | |
3165 | * If tnl_type is NULL, put tunnel metadata according to the | |
3166 | * 'tun_key'. | |
3167 | */ | |
3168 | if ((!tnl_type || !strcmp(tnl_type, "vxlan")) && | |
3169 | (tun_key->gbp_flags || tun_key->gbp_id)) { | |
ac6073e3 MC |
3170 | size_t vxlan_opts_ofs; |
3171 | ||
3172 | vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS); | |
3173 | nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, | |
3174 | (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id)); | |
3175 | nl_msg_end_nested(a, vxlan_opts_ofs); | |
3176 | } | |
c6d87201 WT |
3177 | |
3178 | if (!tnl_type || !strcmp(tnl_type, "geneve")) { | |
3179 | tun_metadata_to_geneve_nlattr(tun_key, tun_flow_key, key_buf, a); | |
3180 | } | |
9558d2a5 | 3181 | |
98514eea WT |
3182 | if ((!tnl_type || !strcmp(tnl_type, "erspan") || |
3183 | !strcmp(tnl_type, "ip6erspan")) && | |
3184 | (tun_key->erspan_ver == 1 || tun_key->erspan_ver == 2)) { | |
3185 | struct erspan_metadata opts; | |
3186 | ||
3187 | opts.version = tun_key->erspan_ver; | |
3188 | if (opts.version == 1) { | |
3189 | opts.u.index = htonl(tun_key->erspan_idx); | |
3190 | } else { | |
3191 | opts.u.md2.dir = tun_key->erspan_dir; | |
3192 | set_hwid(&opts.u.md2, tun_key->erspan_hwid); | |
3193 | } | |
3194 | nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS, | |
3195 | &opts, sizeof(opts)); | |
3196 | } | |
3197 | ||
3c6d05a0 WT |
3198 | if ((!tnl_type || !strcmp(tnl_type, "gtpu")) && |
3199 | (tun_key->gtpu_flags && tun_key->gtpu_msgtype)) { | |
3200 | struct gtpu_metadata opts; | |
3201 | ||
3202 | opts.flags = tun_key->gtpu_flags; | |
3203 | opts.msgtype = tun_key->gtpu_msgtype; | |
3204 | nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, | |
3205 | &opts, sizeof(opts)); | |
3206 | } | |
9b405f1a | 3207 | nl_msg_end_nested(a, tun_key_ofs); |
29c7a2b2 PS |
3208 | } |
3209 | ||
b17298ae BP |
3210 | static bool |
3211 | odp_mask_is_constant__(enum ovs_key_attr attr, const void *mask, size_t size, | |
3212 | int constant) | |
3213 | { | |
3214 | /* Convert 'constant' to all the widths we need. C conversion rules ensure | |
3215 | * that -1 becomes all-1-bits and 0 does not change. */ | |
3216 | ovs_be16 be16 = (OVS_FORCE ovs_be16) constant; | |
3217 | uint32_t u32 = constant; | |
3218 | uint8_t u8 = constant; | |
3219 | const struct in6_addr *in6 = constant ? &in6addr_exact : &in6addr_any; | |
3220 | ||
3221 | switch (attr) { | |
3222 | case OVS_KEY_ATTR_UNSPEC: | |
3223 | case OVS_KEY_ATTR_ENCAP: | |
3224 | case __OVS_KEY_ATTR_MAX: | |
3225 | default: | |
3226 | return false; | |
3227 | ||
3228 | case OVS_KEY_ATTR_PRIORITY: | |
3229 | case OVS_KEY_ATTR_IN_PORT: | |
3230 | case OVS_KEY_ATTR_ETHERNET: | |
3231 | case OVS_KEY_ATTR_VLAN: | |
3232 | case OVS_KEY_ATTR_ETHERTYPE: | |
3233 | case OVS_KEY_ATTR_IPV4: | |
3234 | case OVS_KEY_ATTR_TCP: | |
3235 | case OVS_KEY_ATTR_UDP: | |
3236 | case OVS_KEY_ATTR_ICMP: | |
3237 | case OVS_KEY_ATTR_ICMPV6: | |
3238 | case OVS_KEY_ATTR_ND: | |
9b2b8497 | 3239 | case OVS_KEY_ATTR_ND_EXTENSIONS: |
b17298ae BP |
3240 | case OVS_KEY_ATTR_SKB_MARK: |
3241 | case OVS_KEY_ATTR_TUNNEL: | |
3242 | case OVS_KEY_ATTR_SCTP: | |
3243 | case OVS_KEY_ATTR_DP_HASH: | |
3244 | case OVS_KEY_ATTR_RECIRC_ID: | |
3245 | case OVS_KEY_ATTR_MPLS: | |
3246 | case OVS_KEY_ATTR_CT_STATE: | |
3247 | case OVS_KEY_ATTR_CT_ZONE: | |
3248 | case OVS_KEY_ATTR_CT_MARK: | |
3249 | case OVS_KEY_ATTR_CT_LABELS: | |
3250 | case OVS_KEY_ATTR_PACKET_TYPE: | |
3d2fbd70 | 3251 | case OVS_KEY_ATTR_NSH: |
b17298ae BP |
3252 | return is_all_byte(mask, size, u8); |
3253 | ||
3254 | case OVS_KEY_ATTR_TCP_FLAGS: | |
3255 | return TCP_FLAGS(*(ovs_be16 *) mask) == TCP_FLAGS(be16); | |
3256 | ||
3257 | case OVS_KEY_ATTR_IPV6: { | |
3258 | const struct ovs_key_ipv6 *ipv6_mask = mask; | |
3259 | return ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK)) | |
3260 | == htonl(IPV6_LABEL_MASK & u32) | |
3261 | && ipv6_mask->ipv6_proto == u8 | |
3262 | && ipv6_mask->ipv6_tclass == u8 | |
3263 | && ipv6_mask->ipv6_hlimit == u8 | |
3264 | && ipv6_mask->ipv6_frag == u8 | |
3265 | && ipv6_addr_equals(&ipv6_mask->ipv6_src, in6) | |
3266 | && ipv6_addr_equals(&ipv6_mask->ipv6_dst, in6)); | |
3267 | } | |
3268 | ||
3269 | case OVS_KEY_ATTR_ARP: | |
3270 | return is_all_byte(mask, OFFSETOFEND(struct ovs_key_arp, arp_tha), u8); | |
3271 | ||
3272 | case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: | |
3273 | return is_all_byte(mask, OFFSETOFEND(struct ovs_key_ct_tuple_ipv4, | |
3274 | ipv4_proto), u8); | |
3275 | ||
3276 | case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: | |
3277 | return is_all_byte(mask, OFFSETOFEND(struct ovs_key_ct_tuple_ipv6, | |
3278 | ipv6_proto), u8); | |
3279 | } | |
3280 | } | |
3281 | ||
3282 | /* The caller must already have verified that 'ma' has a correct length. | |
3283 | * | |
3284 | * The main purpose of this function is formatting, to allow code to figure out | |
3285 | * whether the mask can be omitted. It doesn't try hard for attributes that | |
3286 | * contain sub-attributes, etc., because normally those would be broken down | |
3287 | * further for formatting. */ | |
041e7168 AZ |
3288 | static bool |
3289 | odp_mask_attr_is_wildcard(const struct nlattr *ma) | |
3290 | { | |
b17298ae BP |
3291 | return odp_mask_is_constant__(nl_attr_type(ma), |
3292 | nl_attr_get(ma), nl_attr_get_size(ma), 0); | |
041e7168 AZ |
3293 | } |
3294 | ||
2f73024d | 3295 | /* The caller must already have verified that 'size' is a correct length for |
b17298ae BP |
3296 | * 'attr'. |
3297 | * | |
3298 | * The main purpose of this function is formatting, to allow code to figure out | |
3299 | * whether the mask can be omitted. It doesn't try hard for attributes that | |
3300 | * contain sub-attributes, etc., because normally those would be broken down | |
3301 | * further for formatting. */ | |
e6cc0bab | 3302 | static bool |
d23df9a8 | 3303 | odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size) |
e6cc0bab | 3304 | { |
b17298ae | 3305 | return odp_mask_is_constant__(attr, mask, size, -1); |
d23df9a8 JR |
3306 | } |
3307 | ||
2f73024d | 3308 | /* The caller must already have verified that 'ma' has a correct length. */ |
d23df9a8 JR |
3309 | static bool |
3310 | odp_mask_attr_is_exact(const struct nlattr *ma) | |
3311 | { | |
d23df9a8 | 3312 | enum ovs_key_attr attr = nl_attr_type(ma); |
a4e8ed0c | 3313 | return odp_mask_is_exact(attr, nl_attr_get(ma), nl_attr_get_size(ma)); |
e6cc0bab AZ |
3314 | } |
3315 | ||
0a37839c GS |
3316 | void |
3317 | odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no, | |
3318 | char *port_name) | |
3319 | { | |
3320 | struct odp_portno_names *odp_portno_names; | |
3321 | ||
3322 | odp_portno_names = xmalloc(sizeof *odp_portno_names); | |
3323 | odp_portno_names->port_no = port_no; | |
3324 | odp_portno_names->name = xstrdup(port_name); | |
3325 | hmap_insert(portno_names, &odp_portno_names->hmap_node, | |
3326 | hash_odp_port(port_no)); | |
3327 | } | |
3328 | ||
3329 | static char * | |
3330 | odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no) | |
3331 | { | |
0722f341 BP |
3332 | if (portno_names) { |
3333 | struct odp_portno_names *odp_portno_names; | |
0a37839c | 3334 | |
0722f341 BP |
3335 | HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node, |
3336 | hash_odp_port(port_no), portno_names) { | |
3337 | if (odp_portno_names->port_no == port_no) { | |
3338 | return odp_portno_names->name; | |
3339 | } | |
0a37839c GS |
3340 | } |
3341 | } | |
3342 | return NULL; | |
3343 | } | |
3344 | ||
3345 | void | |
3346 | odp_portno_names_destroy(struct hmap *portno_names) | |
3347 | { | |
4ec3d7c7 DDP |
3348 | struct odp_portno_names *odp_portno_names; |
3349 | ||
3350 | HMAP_FOR_EACH_POP (odp_portno_names, hmap_node, portno_names) { | |
0a37839c GS |
3351 | free(odp_portno_names->name); |
3352 | free(odp_portno_names); | |
3353 | } | |
3354 | } | |
e6cc0bab | 3355 | |
0722f341 BP |
3356 | void |
3357 | odp_portno_name_format(const struct hmap *portno_names, odp_port_t port_no, | |
3358 | struct ds *s) | |
3359 | { | |
3360 | const char *name = odp_portno_names_get(portno_names, port_no); | |
3361 | if (name) { | |
3362 | ds_put_cstr(s, name); | |
3363 | } else { | |
3364 | ds_put_format(s, "%"PRIu32, port_no); | |
3365 | } | |
3366 | } | |
3367 | ||
2d18eae8 JR |
3368 | /* Format helpers. */ |
3369 | ||
3370 | static void | |
74ff3298 JR |
3371 | format_eth(struct ds *ds, const char *name, const struct eth_addr key, |
3372 | const struct eth_addr *mask, bool verbose) | |
2d18eae8 JR |
3373 | { |
3374 | bool mask_empty = mask && eth_addr_is_zero(*mask); | |
3375 | ||
3376 | if (verbose || !mask_empty) { | |
3377 | bool mask_full = !mask || eth_mask_is_exact(*mask); | |
3378 | ||
3379 | if (mask_full) { | |
3380 | ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key)); | |
3381 | } else { | |
3382 | ds_put_format(ds, "%s=", name); | |
74ff3298 | 3383 | eth_format_masked(key, mask, ds); |
2d18eae8 JR |
3384 | ds_put_char(ds, ','); |
3385 | } | |
3386 | } | |
3387 | } | |
3388 | ||
1fc11c59 | 3389 | |
2d18eae8 JR |
3390 | static void |
3391 | format_be64(struct ds *ds, const char *name, ovs_be64 key, | |
3392 | const ovs_be64 *mask, bool verbose) | |
3393 | { | |
3394 | bool mask_empty = mask && !*mask; | |
3395 | ||
3396 | if (verbose || !mask_empty) { | |
3397 | bool mask_full = !mask || *mask == OVS_BE64_MAX; | |
3398 | ||
3399 | ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key)); | |
3400 | if (!mask_full) { /* Partially masked. */ | |
3401 | ds_put_format(ds, "/%#"PRIx64, ntohll(*mask)); | |
3402 | } | |
3403 | ds_put_char(ds, ','); | |
3404 | } | |
3405 | } | |
3406 | ||
3407 | static void | |
3408 | format_ipv4(struct ds *ds, const char *name, ovs_be32 key, | |
3409 | const ovs_be32 *mask, bool verbose) | |
3410 | { | |
3411 | bool mask_empty = mask && !*mask; | |
3412 | ||
3413 | if (verbose || !mask_empty) { | |
3414 | bool mask_full = !mask || *mask == OVS_BE32_MAX; | |
3415 | ||
3416 | ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key)); | |
3417 | if (!mask_full) { /* Partially masked. */ | |
3418 | ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask)); | |
3419 | } | |
3420 | ds_put_char(ds, ','); | |
3421 | } | |
3422 | } | |
3423 | ||
3424 | static void | |
d15aef48 JB |
3425 | format_in6_addr(struct ds *ds, const char *name, |
3426 | const struct in6_addr *key, | |
3427 | const struct in6_addr *mask, | |
3428 | bool verbose) | |
2d18eae8 JR |
3429 | { |
3430 | char buf[INET6_ADDRSTRLEN]; | |
2d18eae8 JR |
3431 | bool mask_empty = mask && ipv6_mask_is_any(mask); |
3432 | ||
3433 | if (verbose || !mask_empty) { | |
3434 | bool mask_full = !mask || ipv6_mask_is_exact(mask); | |
3435 | ||
3436 | inet_ntop(AF_INET6, key, buf, sizeof buf); | |
3437 | ds_put_format(ds, "%s=%s", name, buf); | |
3438 | if (!mask_full) { /* Partially masked. */ | |
3439 | inet_ntop(AF_INET6, mask, buf, sizeof buf); | |
3440 | ds_put_format(ds, "/%s", buf); | |
3441 | } | |
3442 | ds_put_char(ds, ','); | |
3443 | } | |
3444 | } | |
3445 | ||
3446 | static void | |
3447 | format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key, | |
3448 | const ovs_be32 *mask, bool verbose) | |
3449 | { | |
3450 | bool mask_empty = mask && !*mask; | |
3451 | ||
3452 | if (verbose || !mask_empty) { | |
3453 | bool mask_full = !mask | |
3454 | || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK); | |
3455 | ||
3456 | ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key)); | |
3457 | if (!mask_full) { /* Partially masked. */ | |
3458 | ds_put_format(ds, "/%#"PRIx32, ntohl(*mask)); | |
3459 | } | |
3460 | ds_put_char(ds, ','); | |
3461 | } | |
3462 | } | |
3463 | ||
3464 | static void | |
3465 | format_u8x(struct ds *ds, const char *name, uint8_t key, | |
3466 | const uint8_t *mask, bool verbose) | |
3467 | { | |
3468 | bool mask_empty = mask && !*mask; | |
3469 | ||
3470 | if (verbose || !mask_empty) { | |
3471 | bool mask_full = !mask || *mask == UINT8_MAX; | |
3472 | ||
3473 | ds_put_format(ds, "%s=%#"PRIx8, name, key); | |
3474 | if (!mask_full) { /* Partially masked. */ | |
3475 | ds_put_format(ds, "/%#"PRIx8, *mask); | |
3476 | } | |
3477 | ds_put_char(ds, ','); | |
3478 | } | |
3479 | } | |
3480 | ||
3481 | static void | |
3482 | format_u8u(struct ds *ds, const char *name, uint8_t key, | |
3483 | const uint8_t *mask, bool verbose) | |
3484 | { | |
3485 | bool mask_empty = mask && !*mask; | |
3486 | ||
3487 | if (verbose || !mask_empty) { | |
3488 | bool mask_full = !mask || *mask == UINT8_MAX; | |
3489 | ||
3490 | ds_put_format(ds, "%s=%"PRIu8, name, key); | |
3491 | if (!mask_full) { /* Partially masked. */ | |
3492 | ds_put_format(ds, "/%#"PRIx8, *mask); | |
3493 | } | |
3494 | ds_put_char(ds, ','); | |
3495 | } | |
3496 | } | |
3497 | ||
3498 | static void | |
3499 | format_be16(struct ds *ds, const char *name, ovs_be16 key, | |
3500 | const ovs_be16 *mask, bool verbose) | |
3501 | { | |
3502 | bool mask_empty = mask && !*mask; | |
3503 | ||
3504 | if (verbose || !mask_empty) { | |
3505 | bool mask_full = !mask || *mask == OVS_BE16_MAX; | |
3506 | ||
3507 | ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key)); | |
3508 | if (!mask_full) { /* Partially masked. */ | |
3509 | ds_put_format(ds, "/%#"PRIx16, ntohs(*mask)); | |
3510 | } | |
3511 | ds_put_char(ds, ','); | |
3512 | } | |
3513 | } | |
3514 | ||
622a0a8e JG |
3515 | static void |
3516 | format_be16x(struct ds *ds, const char *name, ovs_be16 key, | |
3517 | const ovs_be16 *mask, bool verbose) | |
3518 | { | |
3519 | bool mask_empty = mask && !*mask; | |
3520 | ||
3521 | if (verbose || !mask_empty) { | |
3522 | bool mask_full = !mask || *mask == OVS_BE16_MAX; | |
3523 | ||
3524 | ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key)); | |
3525 | if (!mask_full) { /* Partially masked. */ | |
3526 | ds_put_format(ds, "/%#"PRIx16, ntohs(*mask)); | |
3527 | } | |
3528 | ds_put_char(ds, ','); | |
3529 | } | |
3530 | } | |
3531 | ||
2d18eae8 JR |
3532 | static void |
3533 | format_tun_flags(struct ds *ds, const char *name, uint16_t key, | |
3534 | const uint16_t *mask, bool verbose) | |
3535 | { | |
3536 | bool mask_empty = mask && !*mask; | |
3537 | ||
3538 | if (verbose || !mask_empty) { | |
2d18eae8 JR |
3539 | ds_put_cstr(ds, name); |
3540 | ds_put_char(ds, '('); | |
8e4c1621 JG |
3541 | if (mask) { |
3542 | format_flags_masked(ds, NULL, flow_tun_flag_to_string, key, | |
3543 | *mask & FLOW_TNL_F_MASK, FLOW_TNL_F_MASK); | |
2d18eae8 | 3544 | } else { /* Fully masked. */ |
8e4c1621 | 3545 | format_flags(ds, flow_tun_flag_to_string, key, '|'); |
2d18eae8 JR |
3546 | } |
3547 | ds_put_cstr(ds, "),"); | |
3548 | } | |
3549 | } | |
3550 | ||
65da723b JG |
3551 | static bool |
3552 | check_attr_len(struct ds *ds, const struct nlattr *a, const struct nlattr *ma, | |
ce9c9b8b | 3553 | const struct attr_len_tbl tbl[], int max_type, bool need_key) |
65da723b JG |
3554 | { |
3555 | int expected_len; | |
3556 | ||
ce9c9b8b | 3557 | expected_len = odp_key_attr_len(tbl, max_type, nl_attr_type(a)); |
65da723b JG |
3558 | if (expected_len != ATTR_LEN_VARIABLE && |
3559 | expected_len != ATTR_LEN_NESTED) { | |
3560 | ||
3561 | bool bad_key_len = nl_attr_get_size(a) != expected_len; | |
3562 | bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len; | |
3563 | ||
3564 | if (bad_key_len || bad_mask_len) { | |
3565 | if (need_key) { | |
3566 | ds_put_format(ds, "key%u", nl_attr_type(a)); | |
3567 | } | |
3568 | if (bad_key_len) { | |
3569 | ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(", | |
3570 | nl_attr_get_size(a), expected_len); | |
3571 | } | |
3572 | format_generic_odp_key(a, ds); | |
3573 | if (ma) { | |
3574 | ds_put_char(ds, '/'); | |
3575 | if (bad_mask_len) { | |
3576 | ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(", | |
3577 | nl_attr_get_size(ma), expected_len); | |
3578 | } | |
3579 | format_generic_odp_key(ma, ds); | |
3580 | } | |
3581 | ds_put_char(ds, ')'); | |
3582 | return false; | |
3583 | } | |
3584 | } | |
3585 | ||
3586 | return true; | |
3587 | } | |
3588 | ||
3589 | static void | |
3590 | format_unknown_key(struct ds *ds, const struct nlattr *a, | |
3591 | const struct nlattr *ma) | |
3592 | { | |
3593 | ds_put_format(ds, "key%u(", nl_attr_type(a)); | |
3594 | format_generic_odp_key(a, ds); | |
3595 | if (ma && !odp_mask_attr_is_exact(ma)) { | |
3596 | ds_put_char(ds, '/'); | |
3597 | format_generic_odp_key(ma, ds); | |
3598 | } | |
3599 | ds_put_cstr(ds, "),"); | |
3600 | } | |
3601 | ||
3602 | static void | |
3603 | format_odp_tun_vxlan_opt(const struct nlattr *attr, | |
3604 | const struct nlattr *mask_attr, struct ds *ds, | |
3605 | bool verbose) | |
3606 | { | |
3607 | unsigned int left; | |
3608 | const struct nlattr *a; | |
3609 | struct ofpbuf ofp; | |
3610 | ||
3611 | ofpbuf_init(&ofp, 100); | |
3612 | NL_NESTED_FOR_EACH(a, left, attr) { | |
3613 | uint16_t type = nl_attr_type(a); | |
3614 | const struct nlattr *ma = NULL; | |
3615 | ||
3616 | if (mask_attr) { | |
3617 | ma = nl_attr_find__(nl_attr_get(mask_attr), | |
3618 | nl_attr_get_size(mask_attr), type); | |
3619 | if (!ma) { | |
3620 | ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens, | |
3621 | OVS_VXLAN_EXT_MAX, | |
3622 | &ofp, a); | |
3623 | } | |
3624 | } | |
3625 | ||
3626 | if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens, | |
3627 | OVS_VXLAN_EXT_MAX, true)) { | |
3628 | continue; | |
3629 | } | |
3630 | ||
3631 | switch (type) { | |
3632 | case OVS_VXLAN_EXT_GBP: { | |
3633 | uint32_t key = nl_attr_get_u32(a); | |
3634 | ovs_be16 id, id_mask; | |
498535c3 | 3635 | uint8_t flags, flags_mask = 0; |
65da723b JG |
3636 | |
3637 | id = htons(key & 0xFFFF); | |
3638 | flags = (key >> 16) & 0xFF; | |
3639 | if (ma) { | |
3640 | uint32_t mask = nl_attr_get_u32(ma); | |
3641 | id_mask = htons(mask & 0xFFFF); | |
3642 | flags_mask = (mask >> 16) & 0xFF; | |
3643 | } | |
3644 | ||
3645 | ds_put_cstr(ds, "gbp("); | |
3646 | format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose); | |
3647 | format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose); | |
3648 | ds_chomp(ds, ','); | |
3649 | ds_put_cstr(ds, "),"); | |
3650 | break; | |
3651 | } | |
3652 | ||
3653 | default: | |
3654 | format_unknown_key(ds, a, ma); | |
3655 | } | |
3656 | ofpbuf_clear(&ofp); | |
3657 | } | |
3658 | ||
3659 | ds_chomp(ds, ','); | |
3660 | ofpbuf_uninit(&ofp); | |
3661 | } | |
3662 | ||
98514eea WT |
3663 | static void |
3664 | format_odp_tun_erspan_opt(const struct nlattr *attr, | |
9bf871c4 WT |
3665 | const struct nlattr *mask_attr, struct ds *ds, |
3666 | bool verbose) | |
98514eea WT |
3667 | { |
3668 | const struct erspan_metadata *opts, *mask; | |
3669 | uint8_t ver, ver_ma, dir, dir_ma, hwid, hwid_ma; | |
3670 | ||
3671 | opts = nl_attr_get(attr); | |
3672 | mask = mask_attr ? nl_attr_get(mask_attr) : NULL; | |
3673 | ||
3674 | ver = (uint8_t)opts->version; | |
3675 | if (mask) { | |
3676 | ver_ma = (uint8_t)mask->version; | |
3677 | } | |
3678 | ||
3679 | format_u8u(ds, "ver", ver, mask ? &ver_ma : NULL, verbose); | |
3680 | ||
3681 | if (opts->version == 1) { | |
3682 | if (mask) { | |
3683 | ds_put_format(ds, "idx=%#"PRIx32"/%#"PRIx32",", | |
3684 | ntohl(opts->u.index), | |
3685 | ntohl(mask->u.index)); | |
3686 | } else { | |
3687 | ds_put_format(ds, "idx=%#"PRIx32",", ntohl(opts->u.index)); | |
3688 | } | |
3689 | } else if (opts->version == 2) { | |
3690 | dir = opts->u.md2.dir; | |
3691 | hwid = opts->u.md2.hwid; | |
3692 | if (mask) { | |
3693 | dir_ma = mask->u.md2.dir; | |
3694 | hwid_ma = mask->u.md2.hwid; | |
3695 | } | |
3696 | ||
3697 | format_u8u(ds, "dir", dir, mask ? &dir_ma : NULL, verbose); | |
3698 | format_u8x(ds, "hwid", hwid, mask ? &hwid_ma : NULL, verbose); | |
3699 | } | |
3700 | ds_chomp(ds, ','); | |
3701 | } | |
3702 | ||
3c6d05a0 WT |
3703 | static void |
3704 | format_odp_tun_gtpu_opt(const struct nlattr *attr, | |
3705 | const struct nlattr *mask_attr, struct ds *ds, | |
3706 | bool verbose) | |
3707 | { | |
3708 | const struct gtpu_metadata *opts, *mask; | |
3709 | ||
3710 | opts = nl_attr_get(attr); | |
3711 | mask = mask_attr ? nl_attr_get(mask_attr) : NULL; | |
3712 | ||
3713 | format_u8x(ds, "flags", opts->flags, mask ? &mask->flags : NULL, verbose); | |
3714 | format_u8u(ds, "msgtype", opts->msgtype, mask ? &mask->msgtype : NULL, | |
3715 | verbose); | |
3716 | ds_chomp(ds, ','); | |
3717 | } | |
3718 | ||
622a0a8e JG |
3719 | #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL |
3720 | ||
3721 | static void | |
5bb08b0e JG |
3722 | format_geneve_opts(const struct geneve_opt *opt, |
3723 | const struct geneve_opt *mask, int opts_len, | |
3724 | struct ds *ds, bool verbose) | |
622a0a8e | 3725 | { |
622a0a8e JG |
3726 | while (opts_len > 0) { |
3727 | unsigned int len; | |
3728 | uint8_t data_len, data_len_mask; | |
3729 | ||
3730 | if (opts_len < sizeof *opt) { | |
3731 | ds_put_format(ds, "opt len %u less than minimum %"PRIuSIZE, | |
3732 | opts_len, sizeof *opt); | |
3733 | return; | |
3734 | } | |
3735 | ||
3736 | data_len = opt->length * 4; | |
3737 | if (mask) { | |
3738 | if (mask->length == 0x1f) { | |
3739 | data_len_mask = UINT8_MAX; | |
3740 | } else { | |
3741 | data_len_mask = mask->length; | |
3742 | } | |
3743 | } | |
3744 | len = sizeof *opt + data_len; | |
3745 | if (len > opts_len) { | |
3746 | ds_put_format(ds, "opt len %u greater than remaining %u", | |
3747 | len, opts_len); | |
3748 | return; | |
3749 | } | |
3750 | ||
3751 | ds_put_char(ds, '{'); | |
3752 | format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class), | |
3753 | verbose); | |
3754 | format_u8x(ds, "type", opt->type, MASK(mask, type), verbose); | |
3755 | format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose); | |
1cb20095 JG |
3756 | if (data_len && |
3757 | (verbose || !mask || !is_all_zeros(mask + 1, data_len))) { | |
622a0a8e JG |
3758 | ds_put_hex(ds, opt + 1, data_len); |
3759 | if (mask && !is_all_ones(mask + 1, data_len)) { | |
3760 | ds_put_char(ds, '/'); | |
3761 | ds_put_hex(ds, mask + 1, data_len); | |
3762 | } | |
3763 | } else { | |
3764 | ds_chomp(ds, ','); | |
3765 | } | |
3766 | ds_put_char(ds, '}'); | |
3767 | ||
3768 | opt += len / sizeof(*opt); | |
3769 | if (mask) { | |
3770 | mask += len / sizeof(*opt); | |
3771 | } | |
3772 | opts_len -= len; | |
3773 | }; | |
3774 | } | |
3775 | ||
5bb08b0e JG |
3776 | static void |
3777 | format_odp_tun_geneve(const struct nlattr *attr, | |
3778 | const struct nlattr *mask_attr, struct ds *ds, | |
3779 | bool verbose) | |
3780 | { | |
3781 | int opts_len = nl_attr_get_size(attr); | |
3782 | const struct geneve_opt *opt = nl_attr_get(attr); | |
3783 | const struct geneve_opt *mask = mask_attr ? | |
3784 | nl_attr_get(mask_attr) : NULL; | |
3785 | ||
3786 | if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) { | |
3787 | ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE, | |
3788 | nl_attr_get_size(attr), nl_attr_get_size(mask_attr)); | |
3789 | return; | |
3790 | } | |
3791 | ||
3792 | format_geneve_opts(opt, mask, opts_len, ds, verbose); | |
3793 | } | |
3794 | ||
f59cb331 YY |
3795 | static void |
3796 | format_odp_nsh_attr(const struct nlattr *attr, const struct nlattr *mask_attr, | |
3797 | struct ds *ds) | |
3798 | { | |
3799 | unsigned int left; | |
3800 | const struct nlattr *a; | |
3801 | struct ovs_key_nsh nsh; | |
3802 | struct ovs_key_nsh nsh_mask; | |
3803 | ||
3804 | memset(&nsh, 0, sizeof nsh); | |
3805 | memset(&nsh_mask, 0xff, sizeof nsh_mask); | |
3806 | ||
3807 | NL_NESTED_FOR_EACH (a, left, attr) { | |
3808 | enum ovs_nsh_key_attr type = nl_attr_type(a); | |
3809 | const struct nlattr *ma = NULL; | |
3810 | ||
3811 | if (mask_attr) { | |
3812 | ma = nl_attr_find__(nl_attr_get(mask_attr), | |
3813 | nl_attr_get_size(mask_attr), type); | |
3814 | } | |
3815 | ||
3816 | if (!check_attr_len(ds, a, ma, ovs_nsh_key_attr_lens, | |
3817 | OVS_NSH_KEY_ATTR_MAX, true)) { | |
3818 | continue; | |
3819 | } | |
3820 | ||
3821 | switch (type) { | |
3822 | case OVS_NSH_KEY_ATTR_UNSPEC: | |
3823 | break; | |
3824 | case OVS_NSH_KEY_ATTR_BASE: { | |
17553f27 YY |
3825 | const struct ovs_nsh_key_base *base = nl_attr_get(a); |
3826 | const struct ovs_nsh_key_base *base_mask | |
f59cb331 YY |
3827 | = ma ? nl_attr_get(ma) : NULL; |
3828 | nsh.flags = base->flags; | |
17553f27 | 3829 | nsh.ttl = base->ttl; |
f59cb331 YY |
3830 | nsh.mdtype = base->mdtype; |
3831 | nsh.np = base->np; | |
3832 | nsh.path_hdr = base->path_hdr; | |
3833 | if (base_mask) { | |
3834 | nsh_mask.flags = base_mask->flags; | |
17553f27 | 3835 | nsh_mask.ttl = base_mask->ttl; |
f59cb331 YY |
3836 | nsh_mask.mdtype = base_mask->mdtype; |
3837 | nsh_mask.np = base_mask->np; | |
3838 | nsh_mask.path_hdr = base_mask->path_hdr; | |
3839 | } | |
3840 | break; | |
3841 | } | |
3842 | case OVS_NSH_KEY_ATTR_MD1: { | |
17553f27 YY |
3843 | const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a); |
3844 | const struct ovs_nsh_key_md1 *md1_mask | |
f59cb331 YY |
3845 | = ma ? nl_attr_get(ma) : NULL; |
3846 | memcpy(nsh.context, md1->context, sizeof md1->context); | |
3847 | if (md1_mask) { | |
3848 | memcpy(nsh_mask.context, md1_mask->context, | |
3849 | sizeof md1_mask->context); | |
3850 | } | |
3851 | break; | |
3852 | } | |
3853 | case OVS_NSH_KEY_ATTR_MD2: | |
3854 | case __OVS_NSH_KEY_ATTR_MAX: | |
3855 | default: | |
3856 | /* No support for matching other metadata formats yet. */ | |
3857 | break; | |
3858 | } | |
3859 | } | |
3860 | ||
3861 | if (mask_attr) { | |
3862 | format_nsh_key_mask(ds, &nsh, &nsh_mask); | |
3863 | } else { | |
3864 | format_nsh_key(ds, &nsh); | |
3865 | } | |
3866 | } | |
3867 | ||
65da723b JG |
3868 | static void |
3869 | format_odp_tun_attr(const struct nlattr *attr, const struct nlattr *mask_attr, | |
3870 | struct ds *ds, bool verbose) | |
3871 | { | |
3872 | unsigned int left; | |
3873 | const struct nlattr *a; | |
3874 | uint16_t flags = 0; | |
3875 | uint16_t mask_flags = 0; | |
3876 | struct ofpbuf ofp; | |
3877 | ||
3878 | ofpbuf_init(&ofp, 100); | |
3879 | NL_NESTED_FOR_EACH(a, left, attr) { | |
3880 | enum ovs_tunnel_key_attr type = nl_attr_type(a); | |
3881 | const struct nlattr *ma = NULL; | |
3882 | ||
3883 | if (mask_attr) { | |
3884 | ma = nl_attr_find__(nl_attr_get(mask_attr), | |
3885 | nl_attr_get_size(mask_attr), type); | |
3886 | if (!ma) { | |
3887 | ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens, | |
3888 | OVS_TUNNEL_KEY_ATTR_MAX, | |
3889 | &ofp, a); | |
3890 | } | |
3891 | } | |
3892 | ||
3893 | if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens, | |
3894 | OVS_TUNNEL_KEY_ATTR_MAX, true)) { | |
3895 | continue; | |
3896 | } | |
3897 | ||
3898 | switch (type) { | |
3899 | case OVS_TUNNEL_KEY_ATTR_ID: | |
3900 | format_be64(ds, "tun_id", nl_attr_get_be64(a), | |
3901 | ma ? nl_attr_get(ma) : NULL, verbose); | |
d39ec23d | 3902 | flags |= FLOW_TNL_F_KEY; |
65da723b JG |
3903 | if (ma) { |
3904 | mask_flags |= FLOW_TNL_F_KEY; | |
3905 | } | |
3906 | break; | |
3907 | case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: | |
3908 | format_ipv4(ds, "src", nl_attr_get_be32(a), | |
3909 | ma ? nl_attr_get(ma) : NULL, verbose); | |
3910 | break; | |
3911 | case OVS_TUNNEL_KEY_ATTR_IPV4_DST: | |
3912 | format_ipv4(ds, "dst", nl_attr_get_be32(a), | |
3913 | ma ? nl_attr_get(ma) : NULL, verbose); | |
3914 | break; | |
ffe4c74f JB |
3915 | case OVS_TUNNEL_KEY_ATTR_IPV6_SRC: { |
3916 | struct in6_addr ipv6_src; | |
3917 | ipv6_src = nl_attr_get_in6_addr(a); | |
3918 | format_in6_addr(ds, "ipv6_src", &ipv6_src, | |
3919 | ma ? nl_attr_get(ma) : NULL, verbose); | |
3920 | break; | |
3921 | } | |
3922 | case OVS_TUNNEL_KEY_ATTR_IPV6_DST: { | |
3923 | struct in6_addr ipv6_dst; | |
3924 | ipv6_dst = nl_attr_get_in6_addr(a); | |
3925 | format_in6_addr(ds, "ipv6_dst", &ipv6_dst, | |
3926 | ma ? nl_attr_get(ma) : NULL, verbose); | |
3927 | break; | |
3928 | } | |
65da723b JG |
3929 | case OVS_TUNNEL_KEY_ATTR_TOS: |
3930 | format_u8x(ds, "tos", nl_attr_get_u8(a), | |
3931 | ma ? nl_attr_get(ma) : NULL, verbose); | |
3932 | break; | |
3933 | case OVS_TUNNEL_KEY_ATTR_TTL: | |
3934 | format_u8u(ds, "ttl", nl_attr_get_u8(a), | |
3935 | ma ? nl_attr_get(ma) : NULL, verbose); | |
3936 | break; | |
3937 | case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: | |
d39ec23d | 3938 | flags |= FLOW_TNL_F_DONT_FRAGMENT; |
65da723b JG |
3939 | break; |
3940 | case OVS_TUNNEL_KEY_ATTR_CSUM: | |
d39ec23d | 3941 | flags |= FLOW_TNL_F_CSUM; |
65da723b JG |
3942 | break; |
3943 | case OVS_TUNNEL_KEY_ATTR_TP_SRC: | |
3944 | format_be16(ds, "tp_src", nl_attr_get_be16(a), | |
3945 | ma ? nl_attr_get(ma) : NULL, verbose); | |
3946 | break; | |
3947 | case OVS_TUNNEL_KEY_ATTR_TP_DST: | |
3948 | format_be16(ds, "tp_dst", nl_attr_get_be16(a), | |
3949 | ma ? nl_attr_get(ma) : NULL, verbose); | |
3950 | break; | |
3951 | case OVS_TUNNEL_KEY_ATTR_OAM: | |
d39ec23d | 3952 | flags |= FLOW_TNL_F_OAM; |
65da723b JG |
3953 | break; |
3954 | case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: | |
3955 | ds_put_cstr(ds, "vxlan("); | |
3956 | format_odp_tun_vxlan_opt(a, ma, ds, verbose); | |
3957 | ds_put_cstr(ds, "),"); | |
3958 | break; | |
3959 | case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: | |
622a0a8e JG |
3960 | ds_put_cstr(ds, "geneve("); |
3961 | format_odp_tun_geneve(a, ma, ds, verbose); | |
3962 | ds_put_cstr(ds, "),"); | |
3963 | break; | |
f3464818 PS |
3964 | case OVS_TUNNEL_KEY_ATTR_PAD: |
3965 | break; | |
320326e9 | 3966 | case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS: |
98514eea WT |
3967 | ds_put_cstr(ds, "erspan("); |
3968 | format_odp_tun_erspan_opt(a, ma, ds, verbose); | |
3969 | ds_put_cstr(ds, "),"); | |
320326e9 | 3970 | break; |
3c6d05a0 WT |
3971 | case OVS_TUNNEL_KEY_ATTR_GTPU_OPTS: |
3972 | ds_put_cstr(ds, "gtpu("); | |
3973 | format_odp_tun_gtpu_opt(a, ma, ds, verbose); | |
2c5a48c9 | 3974 | ds_put_cstr(ds, "),"); |
3c6d05a0 | 3975 | break; |
65da723b JG |
3976 | case __OVS_TUNNEL_KEY_ATTR_MAX: |
3977 | default: | |
3978 | format_unknown_key(ds, a, ma); | |
3979 | } | |
3980 | ofpbuf_clear(&ofp); | |
3981 | } | |
3982 | ||
3983 | /* Flags can have a valid mask even if the attribute is not set, so | |
3984 | * we need to collect these separately. */ | |
3985 | if (mask_attr) { | |
3986 | NL_NESTED_FOR_EACH(a, left, mask_attr) { | |
3987 | switch (nl_attr_type(a)) { | |
3988 | case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: | |
3989 | mask_flags |= FLOW_TNL_F_DONT_FRAGMENT; | |
3990 | break; | |
3991 | case OVS_TUNNEL_KEY_ATTR_CSUM: | |
3992 | mask_flags |= FLOW_TNL_F_CSUM; | |
3993 | break; | |
3994 | case OVS_TUNNEL_KEY_ATTR_OAM: | |
3995 | mask_flags |= FLOW_TNL_F_OAM; | |
3996 | break; | |
3997 | } | |
3998 | } | |
3999 | } | |
4000 | ||
4001 | format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL, | |
4002 | verbose); | |
4003 | ds_chomp(ds, ','); | |
4004 | ofpbuf_uninit(&ofp); | |
4005 | } | |
4006 | ||
07659514 JS |
4007 | static const char * |
4008 | odp_ct_state_to_string(uint32_t flag) | |
4009 | { | |
4010 | switch (flag) { | |
4011 | case OVS_CS_F_REPLY_DIR: | |
4012 | return "rpl"; | |
4013 | case OVS_CS_F_TRACKED: | |
4014 | return "trk"; | |
4015 | case OVS_CS_F_NEW: | |
4016 | return "new"; | |
4017 | case OVS_CS_F_ESTABLISHED: | |
4018 | return "est"; | |
4019 | case OVS_CS_F_RELATED: | |
4020 | return "rel"; | |
4021 | case OVS_CS_F_INVALID: | |
4022 | return "inv"; | |
819571b5 DDP |
4023 | case OVS_CS_F_SRC_NAT: |
4024 | return "snat"; | |
4025 | case OVS_CS_F_DST_NAT: | |
4026 | return "dnat"; | |
07659514 JS |
4027 | default: |
4028 | return NULL; | |
4029 | } | |
4030 | } | |
4031 | ||
2d18eae8 JR |
4032 | static void |
4033 | format_frag(struct ds *ds, const char *name, uint8_t key, | |
08fa0266 | 4034 | const uint8_t *mask, bool verbose OVS_UNUSED) |
2d18eae8 JR |
4035 | { |
4036 | bool mask_empty = mask && !*mask; | |
08fa0266 | 4037 | bool mask_full = !mask || *mask == UINT8_MAX; |
2d18eae8 JR |
4038 | |
4039 | /* ODP frag is an enumeration field; partial masks are not meaningful. */ | |
08fa0266 PB |
4040 | if (!mask_empty && !mask_full) { |
4041 | ds_put_format(ds, "error: partial mask not supported for frag (%#" | |
4042 | PRIx8"),", *mask); | |
4043 | } else if (!mask_empty) { | |
4044 | ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key)); | |
2d18eae8 JR |
4045 | } |
4046 | } | |
4047 | ||
07659514 JS |
4048 | static bool |
4049 | mask_empty(const struct nlattr *ma) | |
4050 | { | |
4051 | const void *mask; | |
4052 | size_t n; | |
4053 | ||
4054 | if (!ma) { | |
4055 | return true; | |
4056 | } | |
4057 | mask = nl_attr_get(ma); | |
4058 | n = nl_attr_get_size(ma); | |
4059 | ||
4060 | return is_all_zeros(mask, n); | |
4061 | } | |
4062 | ||
2f73024d BP |
4063 | /* The caller must have already verified that 'a' and 'ma' have correct |
4064 | * lengths. */ | |
36956a7d | 4065 | static void |
2f73024d BP |
4066 | format_odp_key_attr__(const struct nlattr *a, const struct nlattr *ma, |
4067 | const struct hmap *portno_names, struct ds *ds, | |
4068 | bool verbose) | |
36956a7d | 4069 | { |
7a8e9ed2 | 4070 | enum ovs_key_attr attr = nl_attr_type(a); |
e6603631 | 4071 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
dc8c5408 | 4072 | bool is_exact; |
36956a7d | 4073 | |
dc8c5408 | 4074 | is_exact = ma ? odp_mask_attr_is_exact(ma) : true; |
e6cc0bab | 4075 | |
e6603631 | 4076 | ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf)); |
e6cc0bab | 4077 | |
e6cc0bab | 4078 | ds_put_char(ds, '('); |
7a8e9ed2 | 4079 | switch (attr) { |
fea393b1 | 4080 | case OVS_KEY_ATTR_ENCAP: |
e6cc0bab AZ |
4081 | if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) { |
4082 | odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), | |
0a37839c | 4083 | nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds, |
041e7168 | 4084 | verbose); |
0a37839c GS |
4085 | } else if (nl_attr_get_size(a)) { |
4086 | odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL, | |
4087 | ds, verbose); | |
fea393b1 | 4088 | } |
fea393b1 BP |
4089 | break; |
4090 | ||
abff858b | 4091 | case OVS_KEY_ATTR_PRIORITY: |
72e8bf28 | 4092 | case OVS_KEY_ATTR_SKB_MARK: |
572f732a AZ |
4093 | case OVS_KEY_ATTR_DP_HASH: |
4094 | case OVS_KEY_ATTR_RECIRC_ID: | |
e6cc0bab | 4095 | ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a)); |
dc8c5408 | 4096 | if (!is_exact) { |
e6cc0bab AZ |
4097 | ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma)); |
4098 | } | |
72e8bf28 AA |
4099 | break; |
4100 | ||
8e53fe8c JS |
4101 | case OVS_KEY_ATTR_CT_MARK: |
4102 | if (verbose || !mask_empty(ma)) { | |
4103 | ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a)); | |
4104 | if (!is_exact) { | |
4105 | ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma)); | |
4106 | } | |
4107 | } | |
4108 | break; | |
4109 | ||
07659514 JS |
4110 | case OVS_KEY_ATTR_CT_STATE: |
4111 | if (verbose) { | |
4112 | ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a)); | |
4113 | if (!is_exact) { | |
4114 | ds_put_format(ds, "/%#"PRIx32, | |
4115 | mask_empty(ma) ? 0 : nl_attr_get_u32(ma)); | |
4116 | } | |
4117 | } else if (!is_exact) { | |
4118 | format_flags_masked(ds, NULL, odp_ct_state_to_string, | |
4119 | nl_attr_get_u32(a), | |
4120 | mask_empty(ma) ? 0 : nl_attr_get_u32(ma), | |
4121 | UINT32_MAX); | |
4122 | } else { | |
4123 | format_flags(ds, odp_ct_state_to_string, nl_attr_get_u32(a), '|'); | |
4124 | } | |
4125 | break; | |
4126 | ||
4127 | case OVS_KEY_ATTR_CT_ZONE: | |
4128 | if (verbose || !mask_empty(ma)) { | |
4129 | ds_put_format(ds, "%#"PRIx16, nl_attr_get_u16(a)); | |
4130 | if (!is_exact) { | |
4131 | ds_put_format(ds, "/%#"PRIx16, nl_attr_get_u16(ma)); | |
4132 | } | |
4133 | } | |
4134 | break; | |
4135 | ||
9daf2348 | 4136 | case OVS_KEY_ATTR_CT_LABELS: { |
ab79d262 BP |
4137 | const ovs_32aligned_u128 *value = nl_attr_get(a); |
4138 | const ovs_32aligned_u128 *mask = ma ? nl_attr_get(ma) : NULL; | |
9daf2348 JS |
4139 | |
4140 | format_u128(ds, value, mask, verbose); | |
4141 | break; | |
4142 | } | |
07659514 | 4143 | |
c30b4cea JR |
4144 | case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: { |
4145 | const struct ovs_key_ct_tuple_ipv4 *key = nl_attr_get(a); | |
4146 | const struct ovs_key_ct_tuple_ipv4 *mask = ma ? nl_attr_get(ma) : NULL; | |
4147 | ||
4148 | format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose); | |
4149 | format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose); | |
4150 | format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto), | |
4151 | verbose); | |
4152 | format_be16(ds, "tp_src", key->src_port, MASK(mask, src_port), | |
4153 | verbose); | |
4154 | format_be16(ds, "tp_dst", key->dst_port, MASK(mask, dst_port), | |
4155 | verbose); | |
4156 | ds_chomp(ds, ','); | |
4157 | break; | |
4158 | } | |
4159 | ||
4160 | case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: { | |
4161 | const struct ovs_key_ct_tuple_ipv6 *key = nl_attr_get(a); | |
4162 | const struct ovs_key_ct_tuple_ipv6 *mask = ma ? nl_attr_get(ma) : NULL; | |
4163 | ||
4164 | format_in6_addr(ds, "src", &key->ipv6_src, MASK(mask, ipv6_src), | |
4165 | verbose); | |
4166 | format_in6_addr(ds, "dst", &key->ipv6_dst, MASK(mask, ipv6_dst), | |
4167 | verbose); | |
4168 | format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto), | |
4169 | verbose); | |
4170 | format_be16(ds, "src_port", key->src_port, MASK(mask, src_port), | |
4171 | verbose); | |
4172 | format_be16(ds, "dst_port", key->dst_port, MASK(mask, dst_port), | |
4173 | verbose); | |
4174 | ds_chomp(ds, ','); | |
4175 | break; | |
4176 | } | |
4177 | ||
65da723b JG |
4178 | case OVS_KEY_ATTR_TUNNEL: |
4179 | format_odp_tun_attr(a, ma, ds, verbose); | |
356af50b | 4180 | break; |
65da723b | 4181 | |
df2c07f4 | 4182 | case OVS_KEY_ATTR_IN_PORT: |
0722f341 BP |
4183 | if (is_exact) { |
4184 | odp_portno_name_format(portno_names, nl_attr_get_odp_port(a), ds); | |
0a37839c GS |
4185 | } else { |
4186 | ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a)); | |
4187 | if (!is_exact) { | |
4188 | ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma)); | |
4189 | } | |
e6cc0bab | 4190 | } |
36956a7d BP |
4191 | break; |
4192 | ||
beb75a40 | 4193 | case OVS_KEY_ATTR_PACKET_TYPE: { |
3d4b2e6e JS |
4194 | ovs_be32 value = nl_attr_get_be32(a); |
4195 | ovs_be32 mask = ma ? nl_attr_get_be32(ma) : OVS_BE32_MAX; | |
beb75a40 | 4196 | |
3d4b2e6e JS |
4197 | ovs_be16 ns = htons(pt_ns(value)); |
4198 | ovs_be16 ns_mask = htons(pt_ns(mask)); | |
4199 | format_be16(ds, "ns", ns, &ns_mask, verbose); | |
beb75a40 | 4200 | |
3d4b2e6e JS |
4201 | ovs_be16 ns_type = pt_ns_type_be(value); |
4202 | ovs_be16 ns_type_mask = pt_ns_type_be(mask); | |
4203 | format_be16x(ds, "id", ns_type, &ns_type_mask, verbose); | |
4204 | ||
4205 | ds_chomp(ds, ','); | |
beb75a40 JS |
4206 | break; |
4207 | } | |
4208 | ||
2d18eae8 JR |
4209 | case OVS_KEY_ATTR_ETHERNET: { |
4210 | const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL; | |
4211 | const struct ovs_key_ethernet *key = nl_attr_get(a); | |
e6cc0bab | 4212 | |
2d18eae8 JR |
4213 | format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose); |
4214 | format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose); | |
4215 | ds_chomp(ds, ','); | |
36956a7d | 4216 | break; |
2d18eae8 | 4217 | } |
fea393b1 | 4218 | case OVS_KEY_ATTR_VLAN: |
2d18eae8 JR |
4219 | format_vlan_tci(ds, nl_attr_get_be16(a), |
4220 | ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose); | |
36956a7d BP |
4221 | break; |
4222 | ||
b02475c5 SH |
4223 | case OVS_KEY_ATTR_MPLS: { |
4224 | const struct ovs_key_mpls *mpls_key = nl_attr_get(a); | |
e6cc0bab | 4225 | const struct ovs_key_mpls *mpls_mask = NULL; |
8bfd0fda BP |
4226 | size_t size = nl_attr_get_size(a); |
4227 | ||
4228 | if (!size || size % sizeof *mpls_key) { | |
6d670e7f | 4229 | ds_put_format(ds, "(bad key length %"PRIuSIZE")", size); |
8bfd0fda BP |
4230 | return; |
4231 | } | |
dc8c5408 | 4232 | if (!is_exact) { |
e6cc0bab | 4233 | mpls_mask = nl_attr_get(ma); |
6d670e7f | 4234 | if (size != nl_attr_get_size(ma)) { |
8bfd0fda BP |
4235 | ds_put_format(ds, "(key length %"PRIuSIZE" != " |
4236 | "mask length %"PRIuSIZE")", | |
6d670e7f | 4237 | size, nl_attr_get_size(ma)); |
8bfd0fda BP |
4238 | return; |
4239 | } | |
e6cc0bab | 4240 | } |
8bfd0fda | 4241 | format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key); |
b02475c5 SH |
4242 | break; |
4243 | } | |
df2c07f4 | 4244 | case OVS_KEY_ATTR_ETHERTYPE: |
e6cc0bab | 4245 | ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a))); |
dc8c5408 | 4246 | if (!is_exact) { |
e6cc0bab AZ |
4247 | ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma))); |
4248 | } | |
36956a7d BP |
4249 | break; |
4250 | ||
2d18eae8 JR |
4251 | case OVS_KEY_ATTR_IPV4: { |
4252 | const struct ovs_key_ipv4 *key = nl_attr_get(a); | |
4253 | const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL; | |
4254 | ||
4255 | format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose); | |
4256 | format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose); | |
4257 | format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto), | |
4258 | verbose); | |
4259 | format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose); | |
4260 | format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose); | |
4261 | format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag), | |
4262 | verbose); | |
4263 | ds_chomp(ds, ','); | |
36956a7d | 4264 | break; |
2d18eae8 JR |
4265 | } |
4266 | case OVS_KEY_ATTR_IPV6: { | |
4267 | const struct ovs_key_ipv6 *key = nl_attr_get(a); | |
4268 | const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL; | |
4269 | ||
932c96b7 JR |
4270 | format_in6_addr(ds, "src", &key->ipv6_src, MASK(mask, ipv6_src), |
4271 | verbose); | |
4272 | format_in6_addr(ds, "dst", &key->ipv6_dst, MASK(mask, ipv6_dst), | |
4273 | verbose); | |
2d18eae8 JR |
4274 | format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label), |
4275 | verbose); | |
4276 | format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto), | |
932c96b7 | 4277 | verbose); |
2d18eae8 | 4278 | format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass), |
932c96b7 | 4279 | verbose); |
2d18eae8 | 4280 | format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit), |
932c96b7 | 4281 | verbose); |
2d18eae8 JR |
4282 | format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag), |
4283 | verbose); | |
4284 | ds_chomp(ds, ','); | |
d31f1109 | 4285 | break; |
2d18eae8 JR |
4286 | } |
4287 | /* These have the same structure and format. */ | |
df2c07f4 | 4288 | case OVS_KEY_ATTR_TCP: |
2d18eae8 JR |
4289 | case OVS_KEY_ATTR_UDP: |
4290 | case OVS_KEY_ATTR_SCTP: { | |
4291 | const struct ovs_key_tcp *key = nl_attr_get(a); | |
4292 | const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL; | |
e6cc0bab | 4293 | |
2d18eae8 JR |
4294 | format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose); |
4295 | format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose); | |
4296 | ds_chomp(ds, ','); | |
36956a7d | 4297 | break; |
2d18eae8 | 4298 | } |
dc235f7f | 4299 | case OVS_KEY_ATTR_TCP_FLAGS: |
dc235f7f | 4300 | if (!is_exact) { |
ea2735d3 JR |
4301 | format_flags_masked(ds, NULL, packet_tcp_flag_to_string, |
4302 | ntohs(nl_attr_get_be16(a)), | |
8e4c1621 JG |
4303 | TCP_FLAGS(nl_attr_get_be16(ma)), |
4304 | TCP_FLAGS(OVS_BE16_MAX)); | |
ea2735d3 JR |
4305 | } else { |
4306 | format_flags(ds, packet_tcp_flag_to_string, | |
8e4c1621 | 4307 | ntohs(nl_attr_get_be16(a)), '|'); |
dc235f7f JR |
4308 | } |
4309 | break; | |
4310 | ||
2d18eae8 JR |
4311 | case OVS_KEY_ATTR_ICMP: { |
4312 | const struct ovs_key_icmp *key = nl_attr_get(a); | |
4313 | const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL; | |
e6cc0bab | 4314 | |
2d18eae8 JR |
4315 | format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose); |
4316 | format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose); | |
4317 | ds_chomp(ds, ','); | |
36956a7d | 4318 | break; |
2d18eae8 JR |
4319 | } |
4320 | case OVS_KEY_ATTR_ICMPV6: { | |
4321 | const struct ovs_key_icmpv6 *key = nl_attr_get(a); | |
4322 | const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL; | |
36956a7d | 4323 | |
2d18eae8 JR |
4324 | format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type), |
4325 | verbose); | |
4326 | format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code), | |
4327 | verbose); | |
4328 | ds_chomp(ds, ','); | |
d31f1109 | 4329 | break; |
2d18eae8 JR |
4330 | } |
4331 | case OVS_KEY_ATTR_ARP: { | |
4332 | const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL; | |
4333 | const struct ovs_key_arp *key = nl_attr_get(a); | |
d31f1109 | 4334 | |
2d18eae8 JR |
4335 | format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose); |
4336 | format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose); | |
4337 | format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose); | |
4338 | format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose); | |
4339 | format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose); | |
4340 | ds_chomp(ds, ','); | |
36956a7d | 4341 | break; |
2d18eae8 | 4342 | } |
df2c07f4 | 4343 | case OVS_KEY_ATTR_ND: { |
2d18eae8 JR |
4344 | const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL; |
4345 | const struct ovs_key_nd *key = nl_attr_get(a); | |
e6cc0bab | 4346 | |
932c96b7 JR |
4347 | format_in6_addr(ds, "target", &key->nd_target, MASK(mask, nd_target), |
4348 | verbose); | |
2d18eae8 JR |
4349 | format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose); |
4350 | format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose); | |
685a51a5 | 4351 | |
2d18eae8 | 4352 | ds_chomp(ds, ','); |
685a51a5 JP |
4353 | break; |
4354 | } | |
9b2b8497 VDA |
4355 | case OVS_KEY_ATTR_ND_EXTENSIONS: { |
4356 | const struct ovs_key_nd_extensions *mask = ma ? nl_attr_get(ma) : NULL; | |
4357 | const struct ovs_key_nd_extensions *key = nl_attr_get(a); | |
4358 | ||
4359 | bool first = true; | |
4360 | format_be32_masked(ds, &first, "nd_reserved", key->nd_reserved, | |
4361 | OVS_BE32_MAX); | |
4362 | ds_put_char(ds, ','); | |
4363 | ||
4364 | format_u8u(ds, "nd_options_type", key->nd_options_type, | |
4365 | MASK(mask, nd_options_type), verbose); | |
4366 | ||
4367 | ds_chomp(ds, ','); | |
4368 | break; | |
4369 | } | |
3d2fbd70 | 4370 | case OVS_KEY_ATTR_NSH: { |
f59cb331 | 4371 | format_odp_nsh_attr(a, ma, ds); |
3d2fbd70 JS |
4372 | break; |
4373 | } | |
7a8e9ed2 BP |
4374 | case OVS_KEY_ATTR_UNSPEC: |
4375 | case __OVS_KEY_ATTR_MAX: | |
36956a7d BP |
4376 | default: |
4377 | format_generic_odp_key(a, ds); | |
dc8c5408 | 4378 | if (!is_exact) { |
e6cc0bab AZ |
4379 | ds_put_char(ds, '/'); |
4380 | format_generic_odp_key(ma, ds); | |
4381 | } | |
36956a7d BP |
4382 | break; |
4383 | } | |
e6cc0bab | 4384 | ds_put_char(ds, ')'); |
36956a7d BP |
4385 | } |
4386 | ||
2f73024d BP |
4387 | static void |
4388 | format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma, | |
4389 | const struct hmap *portno_names, struct ds *ds, | |
4390 | bool verbose) | |
4391 | { | |
4392 | if (check_attr_len(ds, a, ma, ovs_flow_key_attr_lens, | |
4393 | OVS_KEY_ATTR_MAX, false)) { | |
4394 | format_odp_key_attr__(a, ma, portno_names, ds, verbose); | |
4395 | } | |
4396 | } | |
4397 | ||
dc8c5408 | 4398 | static struct nlattr * |
6b8da9e9 JG |
4399 | generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max, |
4400 | struct ofpbuf *ofp, const struct nlattr *key) | |
dc8c5408 AZ |
4401 | { |
4402 | const struct nlattr *a; | |
4403 | unsigned int left; | |
4404 | int type = nl_attr_type(key); | |
4405 | int size = nl_attr_get_size(key); | |
4406 | ||
6b8da9e9 | 4407 | if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) { |
9ddf12cc | 4408 | nl_msg_put_unspec_zero(ofp, type, size); |
dc8c5408 AZ |
4409 | } else { |
4410 | size_t nested_mask; | |
4411 | ||
6b8da9e9 | 4412 | if (tbl[type].next) { |
464cc3ee BP |
4413 | const struct attr_len_tbl *entry = &tbl[type]; |
4414 | tbl = entry->next; | |
4415 | max = entry->next_max; | |
6b8da9e9 JG |
4416 | } |
4417 | ||
dc8c5408 AZ |
4418 | nested_mask = nl_msg_start_nested(ofp, type); |
4419 | NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) { | |
6b8da9e9 | 4420 | generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a)); |
dc8c5408 AZ |
4421 | } |
4422 | nl_msg_end_nested(ofp, nested_mask); | |
4423 | } | |
4424 | ||
6fd6ed71 | 4425 | return ofp->base; |
dc8c5408 AZ |
4426 | } |
4427 | ||
9daf2348 | 4428 | static void |
ab79d262 BP |
4429 | format_u128(struct ds *ds, const ovs_32aligned_u128 *key, |
4430 | const ovs_32aligned_u128 *mask, bool verbose) | |
9daf2348 | 4431 | { |
ab79d262 BP |
4432 | if (verbose || (mask && !ovs_u128_is_zero(get_32aligned_u128(mask)))) { |
4433 | ovs_be128 value = hton128(get_32aligned_u128(key)); | |
9daf2348 | 4434 | ds_put_hex(ds, &value, sizeof value); |
ab79d262 BP |
4435 | if (mask && !(ovs_u128_is_ones(get_32aligned_u128(mask)))) { |
4436 | value = hton128(get_32aligned_u128(mask)); | |
9daf2348 JS |
4437 | ds_put_char(ds, '/'); |
4438 | ds_put_hex(ds, &value, sizeof value); | |
4439 | } | |
4440 | } | |
4441 | } | |
4442 | ||
b23ada8e JP |
4443 | /* Read the string from 's_' as a 128-bit value. If the string contains |
4444 | * a "/", the rest of the string will be treated as a 128-bit mask. | |
4445 | * | |
4446 | * If either the value or mask is larger than 64 bits, the string must | |
4447 | * be in hexadecimal. | |
4448 | */ | |
9daf2348 JS |
4449 | static int |
4450 | scan_u128(const char *s_, ovs_u128 *value, ovs_u128 *mask) | |
4451 | { | |
4452 | char *s = CONST_CAST(char *, s_); | |
4453 | ovs_be128 be_value; | |
4454 | ovs_be128 be_mask; | |
4455 | ||
4456 | if (!parse_int_string(s, (uint8_t *)&be_value, sizeof be_value, &s)) { | |
32ea15f6 | 4457 | *value = ntoh128(be_value); |
9daf2348 JS |
4458 | |
4459 | if (mask) { | |
4460 | int n; | |
4461 | ||
4462 | if (ovs_scan(s, "/%n", &n)) { | |
4463 | int error; | |
4464 | ||
4465 | s += n; | |
4466 | error = parse_int_string(s, (uint8_t *)&be_mask, | |
4467 | sizeof be_mask, &s); | |
4468 | if (error) { | |
2c539492 | 4469 | return 0; |
9daf2348 | 4470 | } |
32ea15f6 | 4471 | *mask = ntoh128(be_mask); |
9daf2348 JS |
4472 | } else { |
4473 | *mask = OVS_U128_MAX; | |
4474 | } | |
4475 | } | |
4476 | return s - s_; | |
4477 | } | |
4478 | ||
4479 | return 0; | |
4480 | } | |
4481 | ||
534a19b9 JS |
4482 | int |
4483 | odp_ufid_from_string(const char *s_, ovs_u128 *ufid) | |
4484 | { | |
4485 | const char *s = s_; | |
4486 | ||
4487 | if (ovs_scan(s, "ufid:")) { | |
534a19b9 | 4488 | s += 5; |
534a19b9 | 4489 | |
10e92b4f | 4490 | if (!uuid_from_string_prefix((struct uuid *)ufid, s)) { |
534a19b9 JS |
4491 | return -EINVAL; |
4492 | } | |
10e92b4f | 4493 | s += UUID_LEN; |
534a19b9 JS |
4494 | |
4495 | return s - s_; | |
4496 | } | |
4497 | ||
4498 | return 0; | |
4499 | } | |
4500 | ||
70e5ed6f JS |
4501 | void |
4502 | odp_format_ufid(const ovs_u128 *ufid, struct ds *ds) | |
4503 | { | |
10e92b4f | 4504 | ds_put_format(ds, "ufid:"UUID_FMT, UUID_ARGS((struct uuid *)ufid)); |
70e5ed6f JS |
4505 | } |
4506 | ||
36956a7d | 4507 | /* Appends to 'ds' a string representation of the 'key_len' bytes of |
e6cc0bab | 4508 | * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the |
73580638 | 4509 | * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is |
d1fd1ea9 | 4510 | * non-null, translates odp port number to its name. */ |
14608a15 | 4511 | void |
e6cc0bab AZ |
4512 | odp_flow_format(const struct nlattr *key, size_t key_len, |
4513 | const struct nlattr *mask, size_t mask_len, | |
0a37839c | 4514 | const struct hmap *portno_names, struct ds *ds, bool verbose) |
14608a15 | 4515 | { |
36956a7d BP |
4516 | if (key_len) { |
4517 | const struct nlattr *a; | |
4518 | unsigned int left; | |
e6cc0bab | 4519 | bool has_ethtype_key = false; |
efde1886 | 4520 | bool has_packet_type_key = false; |
dc8c5408 | 4521 | struct ofpbuf ofp; |
041e7168 | 4522 | bool first_field = true; |
36956a7d | 4523 | |
dc8c5408 | 4524 | ofpbuf_init(&ofp, 100); |
36956a7d | 4525 | NL_ATTR_FOR_EACH (a, left, key, key_len) { |
2f73024d BP |
4526 | int attr_type = nl_attr_type(a); |
4527 | const struct nlattr *ma = (mask && mask_len | |
4528 | ? nl_attr_find__(mask, mask_len, | |
4529 | attr_type) | |
4530 | : NULL); | |
4531 | if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens, | |
4532 | OVS_KEY_ATTR_MAX, false)) { | |
4533 | continue; | |
4534 | } | |
4535 | ||
041e7168 AZ |
4536 | bool is_nested_attr; |
4537 | bool is_wildcard = false; | |
041e7168 AZ |
4538 | |
4539 | if (attr_type == OVS_KEY_ATTR_ETHERTYPE) { | |
e6cc0bab | 4540 | has_ethtype_key = true; |
efde1886 BP |
4541 | } else if (attr_type == OVS_KEY_ATTR_PACKET_TYPE) { |
4542 | has_packet_type_key = true; | |
e6cc0bab | 4543 | } |
041e7168 | 4544 | |
6b8da9e9 JG |
4545 | is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens, |
4546 | OVS_KEY_ATTR_MAX, attr_type) == | |
4547 | ATTR_LEN_NESTED; | |
041e7168 | 4548 | |
e6cc0bab AZ |
4549 | if (mask && mask_len) { |
4550 | ma = nl_attr_find__(mask, mask_len, nl_attr_type(a)); | |
041e7168 AZ |
4551 | is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true; |
4552 | } | |
4553 | ||
4554 | if (verbose || !is_wildcard || is_nested_attr) { | |
4555 | if (is_wildcard && !ma) { | |
6b8da9e9 JG |
4556 | ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens, |
4557 | OVS_KEY_ATTR_MAX, | |
4558 | &ofp, a); | |
dc8c5408 | 4559 | } |
041e7168 AZ |
4560 | if (!first_field) { |
4561 | ds_put_char(ds, ','); | |
4562 | } | |
2f73024d | 4563 | format_odp_key_attr__(a, ma, portno_names, ds, verbose); |
041e7168 | 4564 | first_field = false; |
efde1886 BP |
4565 | } else if (attr_type == OVS_KEY_ATTR_ETHERNET |
4566 | && !has_packet_type_key) { | |
4567 | /* This special case reflects differences between the kernel | |
4568 | * and userspace datapaths regarding the root type of the | |
4569 | * packet being matched (typically Ethernet but some tunnels | |
4570 | * can encapsulate IPv4 etc.). The kernel datapath does not | |
4571 | * have an explicit way to indicate packet type; instead: | |
4572 | * | |
4573 | * - If OVS_KEY_ATTR_ETHERNET is present, the packet is an | |
4574 | * Ethernet packet and OVS_KEY_ATTR_ETHERTYPE is the | |
4575 | * Ethertype encoded in the Ethernet header. | |
4576 | * | |
4577 | * - If OVS_KEY_ATTR_ETHERNET is absent, then the packet's | |
4578 | * root type is that encoded in OVS_KEY_ATTR_ETHERTYPE | |
4579 | * (i.e. if OVS_KEY_ATTR_ETHERTYPE is 0x0800 then the | |
4580 | * packet is an IPv4 packet). | |
4581 | * | |
4582 | * Thus, if OVS_KEY_ATTR_ETHERNET is present, even if it is | |
4583 | * all-wildcarded, it is important to print it. | |
4584 | * | |
4585 | * On the other hand, the userspace datapath supports | |
4586 | * OVS_KEY_ATTR_PACKET_TYPE and uses it to indicate the packet | |
4587 | * type. Thus, if OVS_KEY_ATTR_PACKET_TYPE is present, we need | |
4588 | * not print an all-wildcarded OVS_KEY_ATTR_ETHERNET. */ | |
4589 | if (!first_field) { | |
4590 | ds_put_char(ds, ','); | |
4591 | } | |
4592 | ds_put_cstr(ds, "eth()"); | |
e6cc0bab | 4593 | } |
dc8c5408 | 4594 | ofpbuf_clear(&ofp); |
36956a7d | 4595 | } |
dc8c5408 AZ |
4596 | ofpbuf_uninit(&ofp); |
4597 | ||
36956a7d | 4598 | if (left) { |
3a8cfc50 | 4599 | int i; |
1394054e | 4600 | |
36956a7d BP |
4601 | if (left == key_len) { |
4602 | ds_put_cstr(ds, "<empty>"); | |
4603 | } | |
3a8cfc50 BP |
4604 | ds_put_format(ds, ",***%u leftover bytes*** (", left); |
4605 | for (i = 0; i < left; i++) { | |
4606 | ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]); | |
4607 | } | |
4608 | ds_put_char(ds, ')'); | |
36956a7d | 4609 | } |
e6cc0bab | 4610 | if (!has_ethtype_key) { |
2f73024d BP |
4611 | const struct nlattr *ma = nl_attr_find__(mask, mask_len, |
4612 | OVS_KEY_ATTR_ETHERTYPE); | |
e6cc0bab AZ |
4613 | if (ma) { |
4614 | ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")", | |
4615 | ntohs(nl_attr_get_be16(ma))); | |
4616 | } | |
4617 | } | |
36956a7d BP |
4618 | } else { |
4619 | ds_put_cstr(ds, "<empty>"); | |
4620 | } | |
14608a15 | 4621 | } |
064af421 | 4622 | |
e6cc0bab AZ |
4623 | /* Appends to 'ds' a string representation of the 'key_len' bytes of |
4624 | * OVS_KEY_ATTR_* attributes in 'key'. */ | |
4625 | void | |
4626 | odp_flow_key_format(const struct nlattr *key, | |
4627 | size_t key_len, struct ds *ds) | |
4628 | { | |
0a37839c | 4629 | odp_flow_format(key, key_len, NULL, 0, NULL, ds, true); |
e6cc0bab AZ |
4630 | } |
4631 | ||
7257b535 BP |
4632 | static bool |
4633 | ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type) | |
4634 | { | |
4635 | if (!strcasecmp(s, "no")) { | |
4636 | *type = OVS_FRAG_TYPE_NONE; | |
4637 | } else if (!strcasecmp(s, "first")) { | |
4638 | *type = OVS_FRAG_TYPE_FIRST; | |
4639 | } else if (!strcasecmp(s, "later")) { | |
4640 | *type = OVS_FRAG_TYPE_LATER; | |
4641 | } else { | |
4642 | return false; | |
4643 | } | |
4644 | return true; | |
4645 | } | |
4646 | ||
2d18eae8 | 4647 | /* Parsing. */ |
b02475c5 | 4648 | |
3bffc610 | 4649 | static int |
74ff3298 | 4650 | scan_eth(const char *s, struct eth_addr *key, struct eth_addr *mask) |
3bffc610 | 4651 | { |
2d18eae8 | 4652 | int n; |
abff858b | 4653 | |
74ff3298 JR |
4654 | if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", |
4655 | ETH_ADDR_SCAN_ARGS(*key), &n)) { | |
2d18eae8 JR |
4656 | int len = n; |
4657 | ||
4658 | if (mask) { | |
4659 | if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n", | |
4660 | ETH_ADDR_SCAN_ARGS(*mask), &n)) { | |
4661 | len += n; | |
4662 | } else { | |
4663 | memset(mask, 0xff, sizeof *mask); | |
e6cc0bab | 4664 | } |
abff858b | 4665 | } |
2d18eae8 | 4666 | return len; |
abff858b | 4667 | } |
2d18eae8 JR |
4668 | return 0; |
4669 | } | |
abff858b | 4670 | |
2d18eae8 JR |
4671 | static int |
4672 | scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
4673 | { | |
4674 | int n; | |
72e8bf28 | 4675 | |
2d18eae8 JR |
4676 | if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) { |
4677 | int len = n; | |
4678 | ||
4679 | if (mask) { | |
4680 | if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n", | |
4681 | IP_SCAN_ARGS(mask), &n)) { | |
4682 | len += n; | |
4683 | } else { | |
4684 | *mask = OVS_BE32_MAX; | |
e6cc0bab | 4685 | } |
72e8bf28 | 4686 | } |
2d18eae8 | 4687 | return len; |
72e8bf28 | 4688 | } |
2d18eae8 JR |
4689 | return 0; |
4690 | } | |
72e8bf28 | 4691 | |
2d18eae8 | 4692 | static int |
d15aef48 | 4693 | scan_in6_addr(const char *s, struct in6_addr *key, struct in6_addr *mask) |
2d18eae8 JR |
4694 | { |
4695 | int n; | |
4696 | char ipv6_s[IPV6_SCAN_LEN + 1]; | |
572f732a | 4697 | |
2d18eae8 JR |
4698 | if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n) |
4699 | && inet_pton(AF_INET6, ipv6_s, key) == 1) { | |
4700 | int len = n; | |
4701 | ||
4702 | if (mask) { | |
4703 | if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n) | |
4704 | && inet_pton(AF_INET6, ipv6_s, mask) == 1) { | |
4705 | len += n; | |
4706 | } else { | |
4707 | memset(mask, 0xff, sizeof *mask); | |
78669073 | 4708 | } |
572f732a | 4709 | } |
2d18eae8 | 4710 | return len; |
572f732a | 4711 | } |
2d18eae8 JR |
4712 | return 0; |
4713 | } | |
572f732a | 4714 | |
2d18eae8 JR |
4715 | static int |
4716 | scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
4717 | { | |
4718 | int key_, mask_; | |
4719 | int n; | |
572f732a | 4720 | |
2d18eae8 JR |
4721 | if (ovs_scan(s, "%i%n", &key_, &n) |
4722 | && (key_ & ~IPV6_LABEL_MASK) == 0) { | |
4723 | int len = n; | |
29c7a2b2 | 4724 | |
2d18eae8 JR |
4725 | *key = htonl(key_); |
4726 | if (mask) { | |
4727 | if (ovs_scan(s + len, "/%i%n", &mask_, &n) | |
4728 | && (mask_ & ~IPV6_LABEL_MASK) == 0) { | |
4729 | len += n; | |
4730 | *mask = htonl(mask_); | |
4731 | } else { | |
4732 | *mask = htonl(IPV6_LABEL_MASK); | |
e6cc0bab | 4733 | } |
35651d6a | 4734 | } |
2d18eae8 | 4735 | return len; |
35651d6a | 4736 | } |
2d18eae8 JR |
4737 | return 0; |
4738 | } | |
35651d6a | 4739 | |
2d18eae8 JR |
4740 | static int |
4741 | scan_u8(const char *s, uint8_t *key, uint8_t *mask) | |
4742 | { | |
4743 | int n; | |
3bffc610 | 4744 | |
2d18eae8 JR |
4745 | if (ovs_scan(s, "%"SCNi8"%n", key, &n)) { |
4746 | int len = n; | |
4747 | ||
4748 | if (mask) { | |
4749 | if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) { | |
4750 | len += n; | |
4751 | } else { | |
4752 | *mask = UINT8_MAX; | |
e6cc0bab | 4753 | } |
3bffc610 | 4754 | } |
2d18eae8 | 4755 | return len; |
3bffc610 | 4756 | } |
2d18eae8 JR |
4757 | return 0; |
4758 | } | |
3bffc610 | 4759 | |
07659514 JS |
4760 | static int |
4761 | scan_u16(const char *s, uint16_t *key, uint16_t *mask) | |
4762 | { | |
4763 | int n; | |
4764 | ||
4765 | if (ovs_scan(s, "%"SCNi16"%n", key, &n)) { | |
4766 | int len = n; | |
4767 | ||
4768 | if (mask) { | |
4769 | if (ovs_scan(s + len, "/%"SCNi16"%n", mask, &n)) { | |
4770 | len += n; | |
4771 | } else { | |
4772 | *mask = UINT16_MAX; | |
4773 | } | |
4774 | } | |
4775 | return len; | |
4776 | } | |
4777 | return 0; | |
4778 | } | |
4779 | ||
2d18eae8 JR |
4780 | static int |
4781 | scan_u32(const char *s, uint32_t *key, uint32_t *mask) | |
4782 | { | |
4783 | int n; | |
e6cc0bab | 4784 | |
2d18eae8 JR |
4785 | if (ovs_scan(s, "%"SCNi32"%n", key, &n)) { |
4786 | int len = n; | |
e6cc0bab | 4787 | |
2d18eae8 JR |
4788 | if (mask) { |
4789 | if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) { | |
4790 | len += n; | |
4791 | } else { | |
4792 | *mask = UINT32_MAX; | |
e6cc0bab | 4793 | } |
b2a60db8 | 4794 | } |
2d18eae8 | 4795 | return len; |
b2a60db8 | 4796 | } |
2d18eae8 JR |
4797 | return 0; |
4798 | } | |
b2a60db8 | 4799 | |
2d18eae8 JR |
4800 | static int |
4801 | scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask) | |
4802 | { | |
4803 | uint16_t key_, mask_; | |
4804 | int n; | |
3bffc610 | 4805 | |
2d18eae8 JR |
4806 | if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) { |
4807 | int len = n; | |
e6cc0bab | 4808 | |
2d18eae8 JR |
4809 | *key = htons(key_); |
4810 | if (mask) { | |
4811 | if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) { | |
4812 | len += n; | |
4813 | *mask = htons(mask_); | |
4814 | } else { | |
4815 | *mask = OVS_BE16_MAX; | |
e6cc0bab | 4816 | } |
3bffc610 | 4817 | } |
2d18eae8 | 4818 | return len; |
3bffc610 | 4819 | } |
2d18eae8 JR |
4820 | return 0; |
4821 | } | |
3bffc610 | 4822 | |
3d2fbd70 JS |
4823 | static int |
4824 | scan_be32(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
4825 | { | |
4826 | uint32_t key_, mask_; | |
4827 | int n; | |
4828 | ||
4829 | if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) { | |
4830 | int len = n; | |
4831 | ||
4832 | *key = htonl(key_); | |
4833 | if (mask) { | |
4834 | if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) { | |
4835 | len += n; | |
4836 | *mask = htonl(mask_); | |
4837 | } else { | |
4838 | *mask = OVS_BE32_MAX; | |
4839 | } | |
4840 | } | |
4841 | return len; | |
4842 | } | |
4843 | return 0; | |
4844 | } | |
4845 | ||
2d18eae8 JR |
4846 | static int |
4847 | scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask) | |
4848 | { | |
4849 | uint64_t key_, mask_; | |
4850 | int n; | |
3bffc610 | 4851 | |
2d18eae8 JR |
4852 | if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) { |
4853 | int len = n; | |
4854 | ||
4855 | *key = htonll(key_); | |
4856 | if (mask) { | |
4857 | if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) { | |
4858 | len += n; | |
4859 | *mask = htonll(mask_); | |
4860 | } else { | |
4861 | *mask = OVS_BE64_MAX; | |
e6cc0bab | 4862 | } |
3bffc610 | 4863 | } |
2d18eae8 | 4864 | return len; |
3bffc610 | 4865 | } |
2d18eae8 JR |
4866 | return 0; |
4867 | } | |
3bffc610 | 4868 | |
2d18eae8 JR |
4869 | static int |
4870 | scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask) | |
4871 | { | |
4872 | uint32_t flags, fmask; | |
4873 | int n; | |
3bffc610 | 4874 | |
8e4c1621 JG |
4875 | n = parse_odp_flags(s, flow_tun_flag_to_string, &flags, |
4876 | FLOW_TNL_F_MASK, mask ? &fmask : NULL); | |
2d18eae8 JR |
4877 | if (n >= 0 && s[n] == ')') { |
4878 | *key = flags; | |
4879 | if (mask) { | |
4880 | *mask = fmask; | |
3bffc610 | 4881 | } |
2d18eae8 | 4882 | return n + 1; |
3bffc610 | 4883 | } |
2d18eae8 JR |
4884 | return 0; |
4885 | } | |
3bffc610 | 4886 | |
2d18eae8 JR |
4887 | static int |
4888 | scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask) | |
4889 | { | |
4890 | uint32_t flags, fmask; | |
4891 | int n; | |
b02475c5 | 4892 | |
8e4c1621 JG |
4893 | n = parse_odp_flags(s, packet_tcp_flag_to_string, &flags, |
4894 | TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL); | |
2d18eae8 JR |
4895 | if (n >= 0) { |
4896 | *key = htons(flags); | |
4897 | if (mask) { | |
4898 | *mask = htons(fmask); | |
b02475c5 | 4899 | } |
2d18eae8 | 4900 | return n; |
b02475c5 | 4901 | } |
2d18eae8 JR |
4902 | return 0; |
4903 | } | |
b02475c5 | 4904 | |
07659514 JS |
4905 | static uint32_t |
4906 | ovs_to_odp_ct_state(uint8_t state) | |
4907 | { | |
4908 | uint32_t odp = 0; | |
4909 | ||
fd6cd1bf BP |
4910 | #define CS_STATE(ENUM, INDEX, NAME) \ |
4911 | if (state & CS_##ENUM) { \ | |
4912 | odp |= OVS_CS_F_##ENUM; \ | |
819571b5 | 4913 | } |
fd6cd1bf BP |
4914 | CS_STATES |
4915 | #undef CS_STATE | |
07659514 JS |
4916 | |
4917 | return odp; | |
4918 | } | |
4919 | ||
4920 | static uint8_t | |
4921 | odp_to_ovs_ct_state(uint32_t flags) | |
4922 | { | |
4923 | uint32_t state = 0; | |
4924 | ||
fd6cd1bf BP |
4925 | #define CS_STATE(ENUM, INDEX, NAME) \ |
4926 | if (flags & OVS_CS_F_##ENUM) { \ | |
4927 | state |= CS_##ENUM; \ | |
819571b5 | 4928 | } |
fd6cd1bf BP |
4929 | CS_STATES |
4930 | #undef CS_STATE | |
07659514 JS |
4931 | |
4932 | return state; | |
4933 | } | |
4934 | ||
4935 | static int | |
4936 | scan_ct_state(const char *s, uint32_t *key, uint32_t *mask) | |
4937 | { | |
4938 | uint32_t flags, fmask; | |
4939 | int n; | |
4940 | ||
4941 | n = parse_flags(s, odp_ct_state_to_string, ')', NULL, NULL, &flags, | |
4942 | ovs_to_odp_ct_state(CS_SUPPORTED_MASK), | |
4943 | mask ? &fmask : NULL); | |
4944 | ||
4945 | if (n >= 0) { | |
4946 | *key = flags; | |
4947 | if (mask) { | |
4948 | *mask = fmask; | |
4949 | } | |
4950 | return n; | |
4951 | } | |
4952 | return 0; | |
4953 | } | |
4954 | ||
2d18eae8 JR |
4955 | static int |
4956 | scan_frag(const char *s, uint8_t *key, uint8_t *mask) | |
4957 | { | |
4958 | int n; | |
4959 | char frag[8]; | |
4960 | enum ovs_frag_type frag_type; | |
e6cc0bab | 4961 | |
2d18eae8 JR |
4962 | if (ovs_scan(s, "%7[a-z]%n", frag, &n) |
4963 | && ovs_frag_type_from_string(frag, &frag_type)) { | |
4964 | int len = n; | |
e6cc0bab | 4965 | |
2d18eae8 JR |
4966 | *key = frag_type; |
4967 | if (mask) { | |
4968 | *mask = UINT8_MAX; | |
3bffc610 | 4969 | } |
2d18eae8 | 4970 | return len; |
3bffc610 | 4971 | } |
2d18eae8 JR |
4972 | return 0; |
4973 | } | |
3bffc610 | 4974 | |
2d18eae8 JR |
4975 | static int |
4976 | scan_port(const char *s, uint32_t *key, uint32_t *mask, | |
4977 | const struct simap *port_names) | |
4978 | { | |
4979 | int n; | |
3bffc610 | 4980 | |
2d18eae8 JR |
4981 | if (ovs_scan(s, "%"SCNi32"%n", key, &n)) { |
4982 | int len = n; | |
e6cc0bab | 4983 | |
2d18eae8 JR |
4984 | if (mask) { |
4985 | if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) { | |
4986 | len += n; | |
4987 | } else { | |
4988 | *mask = UINT32_MAX; | |
e6cc0bab | 4989 | } |
3bffc610 | 4990 | } |
2d18eae8 JR |
4991 | return len; |
4992 | } else if (port_names) { | |
4993 | const struct simap_node *node; | |
4994 | int len; | |
e6cc0bab | 4995 | |
2d18eae8 JR |
4996 | len = strcspn(s, ")"); |
4997 | node = simap_find_len(port_names, s, len); | |
4998 | if (node) { | |
4999 | *key = node->data; | |
e6cc0bab AZ |
5000 | |
5001 | if (mask) { | |
2d18eae8 | 5002 | *mask = UINT32_MAX; |
e6cc0bab | 5003 | } |
2d18eae8 | 5004 | return len; |
3bffc610 BP |
5005 | } |
5006 | } | |
2d18eae8 JR |
5007 | return 0; |
5008 | } | |
3bffc610 | 5009 | |
2d18eae8 JR |
5010 | /* Helper for vlan parsing. */ |
5011 | struct ovs_key_vlan__ { | |
5012 | ovs_be16 tci; | |
5013 | }; | |
dc235f7f | 5014 | |
2d18eae8 JR |
5015 | static bool |
5016 | set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value) | |
5017 | { | |
5018 | const uint16_t mask = ((1U << bits) - 1) << offset; | |
ea2735d3 | 5019 | |
2d18eae8 JR |
5020 | if (value >> bits) { |
5021 | return false; | |
dc235f7f JR |
5022 | } |
5023 | ||
2d18eae8 JR |
5024 | *bf = htons((ntohs(*bf) & ~mask) | (value << offset)); |
5025 | return true; | |
5026 | } | |
e6cc0bab | 5027 | |
2d18eae8 JR |
5028 | static int |
5029 | scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits, | |
5030 | uint8_t offset) | |
5031 | { | |
5032 | uint16_t key_, mask_; | |
5033 | int n; | |
3bffc610 | 5034 | |
2d18eae8 JR |
5035 | if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) { |
5036 | int len = n; | |
e6cc0bab | 5037 | |
2d18eae8 | 5038 | if (set_be16_bf(key, bits, offset, key_)) { |
e6cc0bab | 5039 | if (mask) { |
2d18eae8 JR |
5040 | if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) { |
5041 | len += n; | |
5042 | ||
5043 | if (!set_be16_bf(mask, bits, offset, mask_)) { | |
5044 | return 0; | |
5045 | } | |
5046 | } else { | |
5047 | *mask |= htons(((1U << bits) - 1) << offset); | |
5048 | } | |
e6cc0bab | 5049 | } |
2d18eae8 | 5050 | return len; |
3bffc610 BP |
5051 | } |
5052 | } | |
2d18eae8 JR |
5053 | return 0; |
5054 | } | |
3bffc610 | 5055 | |
2d18eae8 JR |
5056 | static int |
5057 | scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask) | |
5058 | { | |
5059 | return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT); | |
5060 | } | |
c6bcb685 | 5061 | |
2d18eae8 JR |
5062 | static int |
5063 | scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask) | |
5064 | { | |
5065 | return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT); | |
5066 | } | |
c6bcb685 | 5067 | |
2d18eae8 JR |
5068 | static int |
5069 | scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask) | |
5070 | { | |
5071 | return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT); | |
5072 | } | |
c6bcb685 | 5073 | |
2d18eae8 JR |
5074 | /* For MPLS. */ |
5075 | static bool | |
5076 | set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value) | |
5077 | { | |
5078 | const uint32_t mask = ((1U << bits) - 1) << offset; | |
c6bcb685 | 5079 | |
2d18eae8 JR |
5080 | if (value >> bits) { |
5081 | return false; | |
c6bcb685 JS |
5082 | } |
5083 | ||
2d18eae8 JR |
5084 | *bf = htonl((ntohl(*bf) & ~mask) | (value << offset)); |
5085 | return true; | |
5086 | } | |
3bffc610 | 5087 | |
2d18eae8 JR |
5088 | static int |
5089 | scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits, | |
5090 | uint8_t offset) | |
5091 | { | |
5092 | uint32_t key_, mask_; | |
5093 | int n; | |
3bffc610 | 5094 | |
2d18eae8 JR |
5095 | if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) { |
5096 | int len = n; | |
e6cc0bab | 5097 | |
2d18eae8 | 5098 | if (set_be32_bf(key, bits, offset, key_)) { |
e6cc0bab | 5099 | if (mask) { |
2d18eae8 JR |
5100 | if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) { |
5101 | len += n; | |
3bffc610 | 5102 | |
2d18eae8 JR |
5103 | if (!set_be32_bf(mask, bits, offset, mask_)) { |
5104 | return 0; | |
5105 | } | |
5106 | } else { | |
5107 | *mask |= htonl(((1U << bits) - 1) << offset); | |
5108 | } | |
e6cc0bab | 5109 | } |
2d18eae8 | 5110 | return len; |
3bffc610 BP |
5111 | } |
5112 | } | |
2d18eae8 JR |
5113 | return 0; |
5114 | } | |
3bffc610 | 5115 | |
2d18eae8 JR |
5116 | static int |
5117 | scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
5118 | { | |
5119 | return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT); | |
5120 | } | |
3bffc610 | 5121 | |
2d18eae8 JR |
5122 | static int |
5123 | scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
5124 | { | |
5125 | return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT); | |
5126 | } | |
e6cc0bab | 5127 | |
2d18eae8 JR |
5128 | static int |
5129 | scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
5130 | { | |
5131 | return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT); | |
5132 | } | |
e6cc0bab | 5133 | |
2d18eae8 JR |
5134 | static int |
5135 | scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask) | |
5136 | { | |
5137 | return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT); | |
5138 | } | |
5139 | ||
65da723b JG |
5140 | static int |
5141 | scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask) | |
5142 | { | |
5143 | const char *s_base = s; | |
3139b8e9 JR |
5144 | ovs_be16 id = 0, id_mask = 0; |
5145 | uint8_t flags = 0, flags_mask = 0; | |
4e9c96f0 | 5146 | int len; |
65da723b JG |
5147 | |
5148 | if (!strncmp(s, "id=", 3)) { | |
5149 | s += 3; | |
4e9c96f0 YS |
5150 | len = scan_be16(s, &id, mask ? &id_mask : NULL); |
5151 | if (len == 0) { | |
2d3deb58 | 5152 | return 0; |
4e9c96f0 YS |
5153 | } |
5154 | s += len; | |
65da723b JG |
5155 | } |
5156 | ||
5157 | if (s[0] == ',') { | |
5158 | s++; | |
5159 | } | |
5160 | if (!strncmp(s, "flags=", 6)) { | |
5161 | s += 6; | |
4e9c96f0 YS |
5162 | len = scan_u8(s, &flags, mask ? &flags_mask : NULL); |
5163 | if (len == 0) { | |
2d3deb58 | 5164 | return 0; |
4e9c96f0 YS |
5165 | } |
5166 | s += len; | |
65da723b JG |
5167 | } |
5168 | ||
5169 | if (!strncmp(s, "))", 2)) { | |
5170 | s += 2; | |
5171 | ||
5172 | *key = (flags << 16) | ntohs(id); | |
5173 | if (mask) { | |
5174 | *mask = (flags_mask << 16) | ntohs(id_mask); | |
5175 | } | |
5176 | ||
5177 | return s - s_base; | |
5178 | } | |
5179 | ||
5180 | return 0; | |
5181 | } | |
5182 | ||
3c6d05a0 WT |
5183 | static int |
5184 | scan_gtpu_metadata(const char *s, | |
5185 | struct gtpu_metadata *key, | |
5186 | struct gtpu_metadata *mask) | |
5187 | { | |
5188 | const char *s_base = s; | |
1e1a1566 IM |
5189 | uint8_t flags = 0, flags_ma = 0; |
5190 | uint8_t msgtype = 0, msgtype_ma = 0; | |
3c6d05a0 WT |
5191 | int len; |
5192 | ||
5193 | if (!strncmp(s, "flags=", 6)) { | |
5194 | s += 6; | |
5195 | len = scan_u8(s, &flags, mask ? &flags_ma : NULL); | |
5196 | if (len == 0) { | |
5197 | return 0; | |
5198 | } | |
5199 | s += len; | |
5200 | } | |
5201 | ||
5202 | if (s[0] == ',') { | |
5203 | s++; | |
5204 | } | |
5205 | ||
5206 | if (!strncmp(s, "msgtype=", 8)) { | |
5207 | s += 8; | |
5208 | len = scan_u8(s, &msgtype, mask ? &msgtype_ma : NULL); | |
5209 | if (len == 0) { | |
5210 | return 0; | |
5211 | } | |
5212 | s += len; | |
5213 | } | |
5214 | ||
5215 | if (!strncmp(s, ")", 1)) { | |
5216 | s += 1; | |
5217 | key->flags = flags; | |
5218 | key->msgtype = msgtype; | |
5219 | if (mask) { | |
5220 | mask->flags = flags_ma; | |
5221 | mask->msgtype = msgtype_ma; | |
5222 | } | |
5223 | } | |
5224 | return s - s_base; | |
5225 | } | |
5226 | ||
98514eea WT |
5227 | static int |
5228 | scan_erspan_metadata(const char *s, | |
5229 | struct erspan_metadata *key, | |
5230 | struct erspan_metadata *mask) | |
5231 | { | |
5232 | const char *s_base = s; | |
5233 | uint32_t idx = 0, idx_mask = 0; | |
5234 | uint8_t ver = 0, dir = 0, hwid = 0; | |
5235 | uint8_t ver_mask = 0, dir_mask = 0, hwid_mask = 0; | |
4e9c96f0 | 5236 | int len; |
98514eea WT |
5237 | |
5238 | if (!strncmp(s, "ver=", 4)) { | |
5239 | s += 4; | |
4e9c96f0 YS |
5240 | len = scan_u8(s, &ver, mask ? &ver_mask : NULL); |
5241 | if (len == 0) { | |
2d3deb58 | 5242 | return 0; |
4e9c96f0 YS |
5243 | } |
5244 | s += len; | |
98514eea WT |
5245 | } |
5246 | ||
5247 | if (s[0] == ',') { | |
5248 | s++; | |
5249 | } | |
5250 | ||
5251 | if (ver == 1) { | |
5252 | if (!strncmp(s, "idx=", 4)) { | |
5253 | s += 4; | |
4e9c96f0 YS |
5254 | len = scan_u32(s, &idx, mask ? &idx_mask : NULL); |
5255 | if (len == 0) { | |
2d3deb58 | 5256 | return 0; |
4e9c96f0 YS |
5257 | } |
5258 | s += len; | |
98514eea WT |
5259 | } |
5260 | ||
5261 | if (!strncmp(s, ")", 1)) { | |
5262 | s += 1; | |
5263 | key->version = ver; | |
5264 | key->u.index = htonl(idx); | |
5265 | if (mask) { | |
5266 | mask->u.index = htonl(idx_mask); | |
5267 | } | |
5268 | } | |
5269 | return s - s_base; | |
5270 | ||
5271 | } else if (ver == 2) { | |
5272 | if (!strncmp(s, "dir=", 4)) { | |
5273 | s += 4; | |
4e9c96f0 YS |
5274 | len = scan_u8(s, &dir, mask ? &dir_mask : NULL); |
5275 | if (len == 0) { | |
2d3deb58 | 5276 | return 0; |
4e9c96f0 YS |
5277 | } |
5278 | s += len; | |
98514eea WT |
5279 | } |
5280 | if (s[0] == ',') { | |
5281 | s++; | |
5282 | } | |
5283 | if (!strncmp(s, "hwid=", 5)) { | |
5284 | s += 5; | |
4e9c96f0 YS |
5285 | len = scan_u8(s, &hwid, mask ? &hwid_mask : NULL); |
5286 | if (len == 0) { | |
2d3deb58 | 5287 | return 0; |
4e9c96f0 YS |
5288 | } |
5289 | s += len; | |
98514eea WT |
5290 | } |
5291 | ||
5292 | if (!strncmp(s, ")", 1)) { | |
5293 | s += 1; | |
5294 | key->version = ver; | |
5295 | key->u.md2.hwid = hwid; | |
5296 | key->u.md2.dir = dir; | |
5297 | if (mask) { | |
5298 | mask->u.md2.hwid = hwid_mask; | |
5299 | mask->u.md2.dir = dir_mask; | |
5300 | } | |
5301 | } | |
5302 | return s - s_base; | |
5303 | } | |
5304 | ||
5305 | return 0; | |
5306 | } | |
5307 | ||
622a0a8e JG |
5308 | static int |
5309 | scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask) | |
5310 | { | |
5311 | const char *s_base = s; | |
c05c01cd JG |
5312 | struct geneve_opt *opt = key->d; |
5313 | struct geneve_opt *opt_mask = mask ? mask->d : NULL; | |
622a0a8e | 5314 | int len_remain = sizeof key->d; |
4e9c96f0 | 5315 | int len; |
622a0a8e JG |
5316 | |
5317 | while (s[0] == '{' && len_remain >= sizeof *opt) { | |
5318 | int data_len = 0; | |
5319 | ||
5320 | s++; | |
5321 | len_remain -= sizeof *opt; | |
5322 | ||
5323 | if (!strncmp(s, "class=", 6)) { | |
5324 | s += 6; | |
4e9c96f0 YS |
5325 | len = scan_be16(s, &opt->opt_class, |
5326 | mask ? &opt_mask->opt_class : NULL); | |
5327 | if (len == 0) { | |
2d3deb58 | 5328 | return 0; |
4e9c96f0 YS |
5329 | } |
5330 | s += len; | |
622a0a8e JG |
5331 | } else if (mask) { |
5332 | memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class); | |
5333 | } | |
5334 | ||
5335 | if (s[0] == ',') { | |
5336 | s++; | |
5337 | } | |
5338 | if (!strncmp(s, "type=", 5)) { | |
5339 | s += 5; | |
4e9c96f0 YS |
5340 | len = scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL); |
5341 | if (len == 0) { | |
2d3deb58 | 5342 | return 0; |
4e9c96f0 YS |
5343 | } |
5344 | s += len; | |
622a0a8e JG |
5345 | } else if (mask) { |
5346 | memset(&opt_mask->type, 0, sizeof opt_mask->type); | |
5347 | } | |
5348 | ||
5349 | if (s[0] == ',') { | |
5350 | s++; | |
5351 | } | |
5352 | if (!strncmp(s, "len=", 4)) { | |
5353 | uint8_t opt_len, opt_len_mask; | |
5354 | s += 4; | |
4e9c96f0 YS |
5355 | len = scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL); |
5356 | if (len == 0) { | |
2d3deb58 | 5357 | return 0; |
4e9c96f0 YS |
5358 | } |
5359 | s += len; | |
622a0a8e JG |
5360 | |
5361 | if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) { | |
5362 | return 0; | |
5363 | } | |
5364 | opt->length = opt_len / 4; | |
5365 | if (mask) { | |
5366 | opt_mask->length = opt_len_mask; | |
5367 | } | |
5368 | data_len = opt_len; | |
5369 | } else if (mask) { | |
5370 | memset(&opt_mask->type, 0, sizeof opt_mask->type); | |
5371 | } | |
5372 | ||
5373 | if (s[0] == ',') { | |
5374 | s++; | |
0f6379e7 IM |
5375 | if (parse_int_string(s, (uint8_t *)(opt + 1), |
5376 | data_len, (char **)&s)) { | |
5377 | return 0; | |
5378 | } | |
622a0a8e | 5379 | } |
622a0a8e JG |
5380 | if (mask) { |
5381 | if (s[0] == '/') { | |
5382 | s++; | |
5383 | if (parse_int_string(s, (uint8_t *)(opt_mask + 1), | |
5384 | data_len, (char **)&s)) { | |
5385 | return 0; | |
5386 | } | |
5387 | } | |
5388 | opt_mask->r1 = 0; | |
5389 | opt_mask->r2 = 0; | |
5390 | opt_mask->r3 = 0; | |
5391 | } | |
5392 | ||
5393 | if (s[0] == '}') { | |
5394 | s++; | |
5395 | opt += 1 + data_len / 4; | |
5396 | if (mask) { | |
5397 | opt_mask += 1 + data_len / 4; | |
5398 | } | |
5399 | len_remain -= data_len; | |
2d3deb58 YS |
5400 | } else { |
5401 | return 0; | |
622a0a8e JG |
5402 | } |
5403 | } | |
5404 | ||
5405 | if (s[0] == ')') { | |
4e9c96f0 | 5406 | len = sizeof key->d - len_remain; |
622a0a8e JG |
5407 | |
5408 | s++; | |
5409 | key->len = len; | |
5410 | if (mask) { | |
5411 | mask->len = len; | |
5412 | } | |
5413 | return s - s_base; | |
5414 | } | |
5415 | ||
5416 | return 0; | |
5417 | } | |
5418 | ||
65da723b JG |
5419 | static void |
5420 | tun_flags_to_attr(struct ofpbuf *a, const void *data_) | |
5421 | { | |
5422 | const uint16_t *flags = data_; | |
5423 | ||
5424 | if (*flags & FLOW_TNL_F_DONT_FRAGMENT) { | |
5425 | nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT); | |
5426 | } | |
5427 | if (*flags & FLOW_TNL_F_CSUM) { | |
5428 | nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM); | |
5429 | } | |
5430 | if (*flags & FLOW_TNL_F_OAM) { | |
5431 | nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM); | |
5432 | } | |
5433 | } | |
5434 | ||
5435 | static void | |
5436 | vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_) | |
5437 | { | |
5438 | const uint32_t *gbp = data_; | |
5439 | ||
5440 | if (*gbp) { | |
5441 | size_t vxlan_opts_ofs; | |
5442 | ||
5443 | vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS); | |
5444 | nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp); | |
5445 | nl_msg_end_nested(a, vxlan_opts_ofs); | |
5446 | } | |
5447 | } | |
5448 | ||
622a0a8e JG |
5449 | static void |
5450 | geneve_to_attr(struct ofpbuf *a, const void *data_) | |
5451 | { | |
5452 | const struct geneve_scan *geneve = data_; | |
5453 | ||
5454 | nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d, | |
5455 | geneve->len); | |
5456 | } | |
5457 | ||
98514eea WT |
5458 | static void |
5459 | erspan_to_attr(struct ofpbuf *a, const void *data_) | |
5460 | { | |
5461 | const struct erspan_metadata *md = data_; | |
5462 | ||
5463 | nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS, md, | |
5464 | sizeof *md); | |
5465 | } | |
5466 | ||
3c6d05a0 WT |
5467 | static void |
5468 | gtpu_to_attr(struct ofpbuf *a, const void *data_) | |
5469 | { | |
5470 | const struct gtpu_metadata *md = data_; | |
5471 | ||
5472 | nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GTPU_OPTS, md, | |
5473 | sizeof *md); | |
5474 | } | |
5475 | ||
65da723b JG |
5476 | #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \ |
5477 | { \ | |
5478 | unsigned long call_fn = (unsigned long)FUNC; \ | |
5479 | if (call_fn) { \ | |
5480 | typedef void (*fn)(struct ofpbuf *, const void *); \ | |
5481 | fn func = FUNC; \ | |
5482 | func(BUF, &(DATA)); \ | |
5483 | } else { \ | |
5484 | nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \ | |
5485 | } \ | |
2d18eae8 JR |
5486 | } |
5487 | ||
5488 | #define SCAN_IF(NAME) \ | |
5489 | if (strncmp(s, NAME, strlen(NAME)) == 0) { \ | |
5490 | const char *start = s; \ | |
5491 | int len; \ | |
5492 | \ | |
5493 | s += strlen(NAME) | |
5494 | ||
5495 | /* Usually no special initialization is needed. */ | |
5496 | #define SCAN_BEGIN(NAME, TYPE) \ | |
5497 | SCAN_IF(NAME); \ | |
5498 | TYPE skey, smask; \ | |
5499 | memset(&skey, 0, sizeof skey); \ | |
5500 | memset(&smask, 0, sizeof smask); \ | |
5501 | do { \ | |
5502 | len = 0; | |
5503 | ||
657ac953 JR |
5504 | /* Init as fully-masked as mask will not be scanned. */ |
5505 | #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \ | |
5506 | SCAN_IF(NAME); \ | |
5507 | TYPE skey, smask; \ | |
5508 | memset(&skey, 0, sizeof skey); \ | |
5509 | memset(&smask, 0xff, sizeof smask); \ | |
5510 | do { \ | |
5511 | len = 0; | |
5512 | ||
2d18eae8 JR |
5513 | /* VLAN needs special initialization. */ |
5514 | #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \ | |
5515 | SCAN_IF(NAME); \ | |
5516 | TYPE skey = KEY_INIT; \ | |
5517 | TYPE smask = MASK_INIT; \ | |
5518 | do { \ | |
5519 | len = 0; | |
5520 | ||
5521 | /* Scan unnamed entry as 'TYPE' */ | |
5522 | #define SCAN_TYPE(TYPE, KEY, MASK) \ | |
5523 | len = scan_##TYPE(s, KEY, MASK); \ | |
5524 | if (len == 0) { \ | |
5525 | return -EINVAL; \ | |
5526 | } \ | |
5527 | s += len | |
5528 | ||
5529 | /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */ | |
5530 | #define SCAN_FIELD(NAME, TYPE, FIELD) \ | |
5531 | if (strncmp(s, NAME, strlen(NAME)) == 0) { \ | |
5532 | s += strlen(NAME); \ | |
5533 | SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \ | |
5534 | continue; \ | |
5535 | } | |
5536 | ||
5537 | #define SCAN_FINISH() \ | |
5538 | } while (*s++ == ',' && len != 0); \ | |
5539 | if (s[-1] != ')') { \ | |
5540 | return -EINVAL; \ | |
5541 | } | |
5542 | ||
5543 | #define SCAN_FINISH_SINGLE() \ | |
5544 | } while (false); \ | |
5545 | if (*s++ != ')') { \ | |
5546 | return -EINVAL; \ | |
5547 | } | |
5548 | ||
65da723b JG |
5549 | /* Beginning of nested attribute. */ |
5550 | #define SCAN_BEGIN_NESTED(NAME, ATTR) \ | |
5551 | SCAN_IF(NAME); \ | |
c3013fd3 | 5552 | size_t key_offset, mask_offset = 0; \ |
65da723b JG |
5553 | key_offset = nl_msg_start_nested(key, ATTR); \ |
5554 | if (mask) { \ | |
5555 | mask_offset = nl_msg_start_nested(mask, ATTR); \ | |
5556 | } \ | |
5557 | do { \ | |
5558 | len = 0; | |
5559 | ||
bb930389 IM |
5560 | #define SCAN_END_NESTED() \ |
5561 | SCAN_FINISH(); \ | |
5562 | if (nl_attr_oversized(key->size - key_offset - NLA_HDRLEN)) { \ | |
5563 | return -E2BIG; \ | |
5564 | } \ | |
5565 | nl_msg_end_nested(key, key_offset); \ | |
5566 | if (mask) { \ | |
5567 | nl_msg_end_nested(mask, mask_offset); \ | |
5568 | } \ | |
5569 | return s - start; \ | |
65da723b JG |
5570 | } |
5571 | ||
5572 | #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \ | |
5573 | if (strncmp(s, NAME, strlen(NAME)) == 0) { \ | |
5574 | TYPE skey, smask; \ | |
5575 | memset(&skey, 0, sizeof skey); \ | |
5576 | memset(&smask, 0xff, sizeof smask); \ | |
5577 | s += strlen(NAME); \ | |
5578 | SCAN_TYPE(SCAN_AS, &skey, &smask); \ | |
5579 | SCAN_PUT(ATTR, FUNC); \ | |
5580 | continue; \ | |
5581 | } | |
5582 | ||
5583 | #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \ | |
5584 | SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL) | |
5585 | ||
5586 | #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \ | |
5587 | SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC) | |
5588 | ||
5589 | #define SCAN_PUT(ATTR, FUNC) \ | |
00ec4017 HE |
5590 | SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \ |
5591 | if (mask) \ | |
5592 | SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \ | |
2d18eae8 JR |
5593 | |
5594 | #define SCAN_END(ATTR) \ | |
5595 | SCAN_FINISH(); \ | |
65da723b | 5596 | SCAN_PUT(ATTR, NULL); \ |
2d18eae8 JR |
5597 | return s - start; \ |
5598 | } | |
5599 | ||
8f79bb4d JR |
5600 | #define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT) \ |
5601 | SCAN_IF(NAME); \ | |
5602 | TYPE skey[CNT], smask[CNT]; \ | |
5603 | memset(&skey, 0, sizeof skey); \ | |
5604 | memset(&smask, 0, sizeof smask); \ | |
5605 | int idx = 0, cnt = CNT; \ | |
5606 | uint64_t fields = 0; \ | |
5607 | do { \ | |
5608 | int field = 0; \ | |
5609 | len = 0; | |
5610 | ||
5611 | /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */ | |
5612 | #define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD) \ | |
5613 | if (strncmp(s, NAME, strlen(NAME)) == 0) { \ | |
5614 | if (fields & (1UL << field)) { \ | |
5615 | fields = 0; \ | |
5616 | if (++idx == cnt) { \ | |
5617 | break; \ | |
5618 | } \ | |
5619 | } \ | |
5620 | s += strlen(NAME); \ | |
5621 | SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \ | |
5622 | fields |= 1UL << field; \ | |
5623 | continue; \ | |
5624 | } \ | |
5625 | field++; | |
5626 | ||
5627 | #define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT) \ | |
5628 | nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \ | |
5629 | ||
5630 | #define SCAN_PUT_ARRAY(ATTR, CNT) \ | |
5631 | SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT); \ | |
5632 | if (mask) { \ | |
5633 | SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \ | |
5634 | } | |
5635 | ||
5636 | #define SCAN_END_ARRAY(ATTR) \ | |
5637 | SCAN_FINISH(); \ | |
5638 | if (idx == cnt) { \ | |
5639 | return -EINVAL; \ | |
5640 | } \ | |
5641 | SCAN_PUT_ARRAY(ATTR, idx + 1); \ | |
5642 | return s - start; \ | |
5643 | } | |
5644 | ||
2d18eae8 JR |
5645 | #define SCAN_END_SINGLE(ATTR) \ |
5646 | SCAN_FINISH_SINGLE(); \ | |
65da723b | 5647 | SCAN_PUT(ATTR, NULL); \ |
2d18eae8 JR |
5648 | return s - start; \ |
5649 | } | |
5650 | ||
5651 | #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \ | |
5652 | SCAN_BEGIN(NAME, TYPE) { \ | |
5653 | SCAN_TYPE(SCAN_AS, &skey, &smask); \ | |
5654 | } SCAN_END_SINGLE(ATTR) | |
5655 | ||
657ac953 JR |
5656 | #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \ |
5657 | SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \ | |
5658 | SCAN_TYPE(SCAN_AS, &skey, NULL); \ | |
2d18eae8 JR |
5659 | } SCAN_END_SINGLE(ATTR) |
5660 | ||
5661 | /* scan_port needs one extra argument. */ | |
5662 | #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \ | |
5663 | SCAN_BEGIN(NAME, TYPE) { \ | |
ac20d3fe YS |
5664 | len = scan_port(s, &skey, &smask, \ |
5665 | context->port_names); \ | |
2d18eae8 JR |
5666 | if (len == 0) { \ |
5667 | return -EINVAL; \ | |
5668 | } \ | |
5669 | s += len; \ | |
5670 | } SCAN_END_SINGLE(ATTR) | |
3bffc610 | 5671 | |
f59cb331 YY |
5672 | static int |
5673 | parse_odp_nsh_key_mask_attr(const char *s, struct ofpbuf *key, | |
5674 | struct ofpbuf *mask) | |
5675 | { | |
5676 | if (strncmp(s, "nsh(", 4) == 0) { | |
5677 | const char *start = s; | |
5678 | int len; | |
17553f27 YY |
5679 | struct ovs_key_nsh skey, smask; |
5680 | uint32_t spi = 0, spi_mask = 0; | |
5681 | uint8_t si = 0, si_mask = 0; | |
f59cb331 YY |
5682 | |
5683 | s += 4; | |
5684 | ||
5685 | memset(&skey, 0, sizeof skey); | |
5686 | memset(&smask, 0, sizeof smask); | |
5687 | do { | |
5688 | len = 0; | |
5689 | ||
5690 | if (strncmp(s, "flags=", 6) == 0) { | |
5691 | s += 6; | |
5692 | len = scan_u8(s, &skey.flags, mask ? &smask.flags : NULL); | |
5693 | if (len == 0) { | |
5694 | return -EINVAL; | |
5695 | } | |
5696 | s += len; | |
5697 | continue; | |
5698 | } | |
5699 | ||
5700 | if (strncmp(s, "mdtype=", 7) == 0) { | |
5701 | s += 7; | |
5702 | len = scan_u8(s, &skey.mdtype, mask ? &smask.mdtype : NULL); | |
5703 | if (len == 0) { | |
5704 | return -EINVAL; | |
5705 | } | |
5706 | s += len; | |
5707 | continue; | |
5708 | } | |
5709 | ||
5710 | if (strncmp(s, "np=", 3) == 0) { | |
5711 | s += 3; | |
5712 | len = scan_u8(s, &skey.np, mask ? &smask.np : NULL); | |
5713 | if (len == 0) { | |
5714 | return -EINVAL; | |
5715 | } | |
5716 | s += len; | |
5717 | continue; | |
5718 | } | |
5719 | ||
5720 | if (strncmp(s, "spi=", 4) == 0) { | |
5721 | s += 4; | |
17553f27 | 5722 | len = scan_u32(s, &spi, mask ? &spi_mask : NULL); |
f59cb331 YY |
5723 | if (len == 0) { |
5724 | return -EINVAL; | |
5725 | } | |
5726 | s += len; | |
5727 | continue; | |
5728 | } | |
5729 | ||
5730 | if (strncmp(s, "si=", 3) == 0) { | |
5731 | s += 3; | |
17553f27 | 5732 | len = scan_u8(s, &si, mask ? &si_mask : NULL); |
f59cb331 YY |
5733 | if (len == 0) { |
5734 | return -EINVAL; | |
5735 | } | |
5736 | s += len; | |
5737 | continue; | |
5738 | } | |
5739 | ||
5740 | if (strncmp(s, "c1=", 3) == 0) { | |
5741 | s += 3; | |
5742 | len = scan_be32(s, &skey.context[0], | |
5743 | mask ? &smask.context[0] : NULL); | |
5744 | if (len == 0) { | |
5745 | return -EINVAL; | |
5746 | } | |
5747 | s += len; | |
5748 | continue; | |
5749 | } | |
5750 | ||
5751 | if (strncmp(s, "c2=", 3) == 0) { | |
5752 | s += 3; | |
5753 | len = scan_be32(s, &skey.context[1], | |
5754 | mask ? &smask.context[1] : NULL); | |
5755 | if (len == 0) { | |
5756 | return -EINVAL; | |
5757 | } | |
5758 | s += len; | |
5759 | continue; | |
5760 | } | |
5761 | ||
5762 | if (strncmp(s, "c3=", 3) == 0) { | |
5763 | s += 3; | |
5764 | len = scan_be32(s, &skey.context[2], | |
5765 | mask ? &smask.context[2] : NULL); | |
5766 | if (len == 0) { | |
5767 | return -EINVAL; | |
5768 | } | |
5769 | s += len; | |
5770 | continue; | |
5771 | } | |
5772 | ||
5773 | if (strncmp(s, "c4=", 3) == 0) { | |
5774 | s += 3; | |
5775 | len = scan_be32(s, &skey.context[3], | |
5776 | mask ? &smask.context[3] : NULL); | |
5777 | if (len == 0) { | |
5778 | return -EINVAL; | |
5779 | } | |
5780 | s += len; | |
5781 | continue; | |
5782 | } | |
5783 | } while (*s++ == ',' && len != 0); | |
5784 | if (s[-1] != ')') { | |
5785 | return -EINVAL; | |
5786 | } | |
5787 | ||
17553f27 YY |
5788 | skey.path_hdr = nsh_spi_si_to_path_hdr(spi, si); |
5789 | smask.path_hdr = nsh_spi_si_to_path_hdr(spi_mask, si_mask); | |
5790 | ||
f59cb331 YY |
5791 | nsh_key_to_attr(key, &skey, NULL, 0, false); |
5792 | if (mask) { | |
5793 | nsh_key_to_attr(mask, &smask, NULL, 0, true); | |
5794 | } | |
5795 | return s - start; | |
5796 | } | |
5797 | return 0; | |
5798 | } | |
5799 | ||
2d18eae8 | 5800 | static int |
ac20d3fe | 5801 | parse_odp_key_mask_attr(struct parse_odp_context *context, const char *s, |
2d18eae8 | 5802 | struct ofpbuf *key, struct ofpbuf *mask) |
5fbe9d15 TA |
5803 | { |
5804 | int retval; | |
5805 | ||
5806 | context->depth++; | |
5807 | ||
5808 | if (context->depth == MAX_ODP_NESTED) { | |
5809 | retval = -EINVAL; | |
5810 | } else { | |
5811 | retval = parse_odp_key_mask_attr__(context, s, key, mask); | |
5812 | } | |
5813 | ||
5814 | context->depth--; | |
5815 | ||
5816 | return retval; | |
5817 | } | |
5818 | ||
5819 | static int | |
5820 | parse_odp_key_mask_attr__(struct parse_odp_context *context, const char *s, | |
5821 | struct ofpbuf *key, struct ofpbuf *mask) | |
2d18eae8 JR |
5822 | { |
5823 | SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY); | |
5824 | SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK); | |
657ac953 JR |
5825 | SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32, |
5826 | OVS_KEY_ATTR_RECIRC_ID); | |
2d18eae8 JR |
5827 | SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH); |
5828 | ||
07659514 JS |
5829 | SCAN_SINGLE("ct_state(", uint32_t, ct_state, OVS_KEY_ATTR_CT_STATE); |
5830 | SCAN_SINGLE("ct_zone(", uint16_t, u16, OVS_KEY_ATTR_CT_ZONE); | |
8e53fe8c | 5831 | SCAN_SINGLE("ct_mark(", uint32_t, u32, OVS_KEY_ATTR_CT_MARK); |
9daf2348 | 5832 | SCAN_SINGLE("ct_label(", ovs_u128, u128, OVS_KEY_ATTR_CT_LABELS); |
07659514 | 5833 | |
daf4d3c1 JR |
5834 | SCAN_BEGIN("ct_tuple4(", struct ovs_key_ct_tuple_ipv4) { |
5835 | SCAN_FIELD("src=", ipv4, ipv4_src); | |
5836 | SCAN_FIELD("dst=", ipv4, ipv4_dst); | |
5837 | SCAN_FIELD("proto=", u8, ipv4_proto); | |
5838 | SCAN_FIELD("tp_src=", be16, src_port); | |
5839 | SCAN_FIELD("tp_dst=", be16, dst_port); | |
5840 | } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4); | |
5841 | ||
5842 | SCAN_BEGIN("ct_tuple6(", struct ovs_key_ct_tuple_ipv6) { | |
5843 | SCAN_FIELD("src=", in6_addr, ipv6_src); | |
5844 | SCAN_FIELD("dst=", in6_addr, ipv6_dst); | |
5845 | SCAN_FIELD("proto=", u8, ipv6_proto); | |
5846 | SCAN_FIELD("tp_src=", be16, src_port); | |
5847 | SCAN_FIELD("tp_dst=", be16, dst_port); | |
5848 | } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6); | |
5849 | ||
65da723b JG |
5850 | SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) { |
5851 | SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID); | |
5852 | SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC); | |
5853 | SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST); | |
ffe4c74f JB |
5854 | SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_SRC); |
5855 | SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_DST); | |
65da723b JG |
5856 | SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS); |
5857 | SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL); | |
5858 | SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC); | |
5859 | SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST); | |
98514eea WT |
5860 | SCAN_FIELD_NESTED_FUNC("erspan(", struct erspan_metadata, erspan_metadata, |
5861 | erspan_to_attr); | |
65da723b | 5862 | SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr); |
622a0a8e JG |
5863 | SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve, |
5864 | geneve_to_attr); | |
3c6d05a0 WT |
5865 | SCAN_FIELD_NESTED_FUNC("gtpu(", struct gtpu_metadata, gtpu_metadata, |
5866 | gtpu_to_attr); | |
65da723b JG |
5867 | SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr); |
5868 | } SCAN_END_NESTED(); | |
2d18eae8 JR |
5869 | |
5870 | SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT); | |
5871 | ||
5872 | SCAN_BEGIN("eth(", struct ovs_key_ethernet) { | |
5873 | SCAN_FIELD("src=", eth, eth_src); | |
5874 | SCAN_FIELD("dst=", eth, eth_dst); | |
5875 | } SCAN_END(OVS_KEY_ATTR_ETHERNET); | |
5876 | ||
5877 | SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__, | |
5878 | { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) { | |
5879 | SCAN_FIELD("vid=", vid, tci); | |
5880 | SCAN_FIELD("pcp=", pcp, tci); | |
5881 | SCAN_FIELD("cfi=", cfi, tci); | |
5882 | } SCAN_END(OVS_KEY_ATTR_VLAN); | |
5883 | ||
5884 | SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE); | |
5885 | ||
ab83c8c5 | 5886 | SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls, FLOW_MAX_MPLS_LABELS) { |
8f79bb4d JR |
5887 | SCAN_FIELD_ARRAY("label=", mpls_label, mpls_lse); |
5888 | SCAN_FIELD_ARRAY("tc=", mpls_tc, mpls_lse); | |
5889 | SCAN_FIELD_ARRAY("ttl=", mpls_ttl, mpls_lse); | |
5890 | SCAN_FIELD_ARRAY("bos=", mpls_bos, mpls_lse); | |
5891 | } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS); | |
2d18eae8 JR |
5892 | |
5893 | SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) { | |
5894 | SCAN_FIELD("src=", ipv4, ipv4_src); | |
5895 | SCAN_FIELD("dst=", ipv4, ipv4_dst); | |
5896 | SCAN_FIELD("proto=", u8, ipv4_proto); | |
5897 | SCAN_FIELD("tos=", u8, ipv4_tos); | |
5898 | SCAN_FIELD("ttl=", u8, ipv4_ttl); | |
5899 | SCAN_FIELD("frag=", frag, ipv4_frag); | |
5900 | } SCAN_END(OVS_KEY_ATTR_IPV4); | |
5901 | ||
5902 | SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) { | |
932c96b7 JR |
5903 | SCAN_FIELD("src=", in6_addr, ipv6_src); |
5904 | SCAN_FIELD("dst=", in6_addr, ipv6_dst); | |
2d18eae8 JR |
5905 | SCAN_FIELD("label=", ipv6_label, ipv6_label); |
5906 | SCAN_FIELD("proto=", u8, ipv6_proto); | |
5907 | SCAN_FIELD("tclass=", u8, ipv6_tclass); | |
5908 | SCAN_FIELD("hlimit=", u8, ipv6_hlimit); | |
5909 | SCAN_FIELD("frag=", frag, ipv6_frag); | |
5910 | } SCAN_END(OVS_KEY_ATTR_IPV6); | |
5911 | ||
5912 | SCAN_BEGIN("tcp(", struct ovs_key_tcp) { | |
5913 | SCAN_FIELD("src=", be16, tcp_src); | |
5914 | SCAN_FIELD("dst=", be16, tcp_dst); | |
5915 | } SCAN_END(OVS_KEY_ATTR_TCP); | |
5916 | ||
5917 | SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS); | |
5918 | ||
5919 | SCAN_BEGIN("udp(", struct ovs_key_udp) { | |
5920 | SCAN_FIELD("src=", be16, udp_src); | |
5921 | SCAN_FIELD("dst=", be16, udp_dst); | |
5922 | } SCAN_END(OVS_KEY_ATTR_UDP); | |
5923 | ||
5924 | SCAN_BEGIN("sctp(", struct ovs_key_sctp) { | |
5925 | SCAN_FIELD("src=", be16, sctp_src); | |
5926 | SCAN_FIELD("dst=", be16, sctp_dst); | |
5927 | } SCAN_END(OVS_KEY_ATTR_SCTP); | |
5928 | ||
5929 | SCAN_BEGIN("icmp(", struct ovs_key_icmp) { | |
5930 | SCAN_FIELD("type=", u8, icmp_type); | |
5931 | SCAN_FIELD("code=", u8, icmp_code); | |
5932 | } SCAN_END(OVS_KEY_ATTR_ICMP); | |
5933 | ||
5934 | SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) { | |
5935 | SCAN_FIELD("type=", u8, icmpv6_type); | |
5936 | SCAN_FIELD("code=", u8, icmpv6_code); | |
5937 | } SCAN_END(OVS_KEY_ATTR_ICMPV6); | |
5938 | ||
5939 | SCAN_BEGIN("arp(", struct ovs_key_arp) { | |
5940 | SCAN_FIELD("sip=", ipv4, arp_sip); | |
5941 | SCAN_FIELD("tip=", ipv4, arp_tip); | |
5942 | SCAN_FIELD("op=", be16, arp_op); | |
5943 | SCAN_FIELD("sha=", eth, arp_sha); | |
5944 | SCAN_FIELD("tha=", eth, arp_tha); | |
5945 | } SCAN_END(OVS_KEY_ATTR_ARP); | |
5946 | ||
5947 | SCAN_BEGIN("nd(", struct ovs_key_nd) { | |
932c96b7 | 5948 | SCAN_FIELD("target=", in6_addr, nd_target); |
2d18eae8 JR |
5949 | SCAN_FIELD("sll=", eth, nd_sll); |
5950 | SCAN_FIELD("tll=", eth, nd_tll); | |
5951 | } SCAN_END(OVS_KEY_ATTR_ND); | |
5952 | ||
9b2b8497 VDA |
5953 | SCAN_BEGIN("nd_ext(", struct ovs_key_nd_extensions) { |
5954 | SCAN_FIELD("nd_reserved=", be32, nd_reserved); | |
5955 | SCAN_FIELD("nd_options_type=", u8, nd_options_type); | |
5956 | } SCAN_END(OVS_KEY_ATTR_ND_EXTENSIONS); | |
5957 | ||
3d4b2e6e JS |
5958 | struct packet_type { |
5959 | ovs_be16 ns; | |
5960 | ovs_be16 id; | |
5961 | }; | |
5962 | SCAN_BEGIN("packet_type(", struct packet_type) { | |
5963 | SCAN_FIELD("ns=", be16, ns); | |
5964 | SCAN_FIELD("id=", be16, id); | |
5965 | } SCAN_END(OVS_KEY_ATTR_PACKET_TYPE); | |
5966 | ||
f59cb331 YY |
5967 | /* nsh is nested, it needs special process */ |
5968 | int ret = parse_odp_nsh_key_mask_attr(s, key, mask); | |
5969 | if (ret < 0) { | |
5fbe9d15 | 5970 | return ret; |
f59cb331 | 5971 | } else { |
5fbe9d15 | 5972 | s += ret; |
f59cb331 | 5973 | } |
3d2fbd70 | 5974 | |
2d18eae8 | 5975 | /* Encap open-coded. */ |
fea393b1 BP |
5976 | if (!strncmp(s, "encap(", 6)) { |
5977 | const char *start = s; | |
e6cc0bab | 5978 | size_t encap, encap_mask = 0; |
fea393b1 BP |
5979 | |
5980 | encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP); | |
e6cc0bab AZ |
5981 | if (mask) { |
5982 | encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP); | |
5983 | } | |
fea393b1 BP |
5984 | |
5985 | s += 6; | |
5986 | for (;;) { | |
5987 | int retval; | |
5988 | ||
70fbe375 | 5989 | s += strspn(s, delimiters); |
fea393b1 BP |
5990 | if (!*s) { |
5991 | return -EINVAL; | |
5992 | } else if (*s == ')') { | |
5993 | break; | |
5994 | } | |
5995 | ||
ac20d3fe | 5996 | retval = parse_odp_key_mask_attr(context, s, key, mask); |
fea393b1 BP |
5997 | if (retval < 0) { |
5998 | return retval; | |
5999 | } | |
401eacfb YS |
6000 | |
6001 | if (nl_attr_oversized(key->size - encap - NLA_HDRLEN)) { | |
6002 | return -E2BIG; | |
6003 | } | |
fea393b1 BP |
6004 | s += retval; |
6005 | } | |
6006 | s++; | |
6007 | ||
6008 | nl_msg_end_nested(key, encap); | |
e6cc0bab AZ |
6009 | if (mask) { |
6010 | nl_msg_end_nested(mask, encap_mask); | |
6011 | } | |
fea393b1 BP |
6012 | |
6013 | return s - start; | |
6014 | } | |
6015 | ||
3bffc610 BP |
6016 | return -EINVAL; |
6017 | } | |
6018 | ||
d40533fc BP |
6019 | /* Parses the string representation of a datapath flow key, in the format |
6020 | * output by odp_flow_key_format(). Returns 0 if successful, otherwise a | |
6021 | * positive errno value. On success, stores NULL into '*errorp' and the flow | |
6022 | * key is appended to 'key' as a series of Netlink attributes. On failure, | |
6023 | * stores a malloc()'d error message in '*errorp' without changing the data in | |
6024 | * 'key'. Either way, 'key''s data might be reallocated. | |
3bffc610 | 6025 | * |
44bac24b BP |
6026 | * If 'port_names' is nonnull, it points to an simap that maps from a port name |
6027 | * to a port number. (Port names may be used instead of port numbers in | |
6028 | * in_port.) | |
b2a60db8 | 6029 | * |
3bffc610 BP |
6030 | * On success, the attributes appended to 'key' are individually syntactically |
6031 | * valid, but they may not be valid as a sequence. 'key' might, for example, | |
34118cae | 6032 | * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */ |
3bffc610 | 6033 | int |
e6cc0bab | 6034 | odp_flow_from_string(const char *s, const struct simap *port_names, |
d40533fc BP |
6035 | struct ofpbuf *key, struct ofpbuf *mask, |
6036 | char **errorp) | |
3bffc610 | 6037 | { |
3885c72c TA |
6038 | if (errorp) { |
6039 | *errorp = NULL; | |
6040 | } | |
d40533fc | 6041 | |
6fd6ed71 | 6042 | const size_t old_size = key->size; |
ac20d3fe YS |
6043 | struct parse_odp_context context = (struct parse_odp_context) { |
6044 | .port_names = port_names, | |
6045 | }; | |
3bffc610 BP |
6046 | for (;;) { |
6047 | int retval; | |
6048 | ||
7202cbe5 | 6049 | s += strspn(s, delimiters); |
3bffc610 BP |
6050 | if (!*s) { |
6051 | return 0; | |
6052 | } | |
6053 | ||
a1da3f29 YS |
6054 | /* Skip UFID. */ |
6055 | ovs_u128 ufid; | |
6056 | retval = odp_ufid_from_string(s, &ufid); | |
6057 | if (retval < 0) { | |
3885c72c TA |
6058 | if (errorp) { |
6059 | *errorp = xasprintf("syntax error at %s", s); | |
6060 | } | |
a1da3f29 YS |
6061 | key->size = old_size; |
6062 | return -retval; | |
6063 | } else if (retval > 0) { | |
6064 | s += retval; | |
6065 | s += s[0] == ' ' ? 1 : 0; | |
6066 | } | |
6067 | ||
ac20d3fe | 6068 | retval = parse_odp_key_mask_attr(&context, s, key, mask); |
3bffc610 | 6069 | if (retval < 0) { |
3885c72c TA |
6070 | if (errorp) { |
6071 | *errorp = xasprintf("syntax error at %s", s); | |
6072 | } | |
6fd6ed71 | 6073 | key->size = old_size; |
3bffc610 BP |
6074 | return -retval; |
6075 | } | |
6076 | s += retval; | |
6077 | } | |
6078 | ||
6079 | return 0; | |
6080 | } | |
6081 | ||
7257b535 | 6082 | static uint8_t |
3cea18ec | 6083 | ovs_to_odp_frag(uint8_t nw_frag, bool is_mask) |
7257b535 | 6084 | { |
3cea18ec JR |
6085 | if (is_mask) { |
6086 | /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type, | |
6087 | * not a set of flags or bitfields. Hence, if the struct flow nw_frag | |
6088 | * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we | |
6089 | * must use a zero mask for the netlink frag field, and all ones mask | |
6090 | * otherwise. */ | |
6091 | return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0; | |
6092 | } | |
2c0f0be1 JR |
6093 | return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE |
6094 | : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER | |
6095 | : OVS_FRAG_TYPE_FIRST; | |
7257b535 BP |
6096 | } |
6097 | ||
3cea18ec JR |
6098 | static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *); |
6099 | static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *); | |
6100 | static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *, | |
6101 | bool is_mask); | |
6102 | static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *, | |
6103 | bool is_mask); | |
6104 | static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *, | |
6105 | bool is_mask); | |
6106 | static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *, | |
6107 | bool is_mask); | |
6108 | static void get_arp_key(const struct flow *, struct ovs_key_arp *); | |
6109 | static void put_arp_key(const struct ovs_key_arp *, struct flow *); | |
e60e935b SRCSA |
6110 | static void get_nd_key(const struct flow *, struct ovs_key_nd *); |
6111 | static void put_nd_key(const struct ovs_key_nd *, struct flow *); | |
3d2fbd70 JS |
6112 | static void get_nsh_key(const struct flow *flow, struct ovs_key_nsh *nsh, |
6113 | bool is_mask); | |
6114 | static void put_nsh_key(const struct ovs_key_nsh *nsh, struct flow *flow, | |
6115 | bool is_mask); | |
3cea18ec JR |
6116 | |
6117 | /* These share the same layout. */ | |
6118 | union ovs_key_tp { | |
6119 | struct ovs_key_tcp tcp; | |
6120 | struct ovs_key_udp udp; | |
6121 | struct ovs_key_sctp sctp; | |
6122 | }; | |
6123 | ||
6124 | static void get_tp_key(const struct flow *, union ovs_key_tp *); | |
6125 | static void put_tp_key(const union ovs_key_tp *, struct flow *); | |
431495b1 | 6126 | |
661cbcd5 | 6127 | static void |
5262eea1 JG |
6128 | odp_flow_key_from_flow__(const struct odp_flow_key_parms *parms, |
6129 | bool export_mask, struct ofpbuf *buf) | |
14608a15 | 6130 | { |
74286173 BP |
6131 | /* New "struct flow" fields that are visible to the datapath (including all |
6132 | * data fields) should be translated into equivalent datapath flow fields | |
6133 | * here (you will have to add a OVS_KEY_ATTR_* for them). */ | |
3c6d05a0 | 6134 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 42); |
74286173 | 6135 | |
df2c07f4 | 6136 | struct ovs_key_ethernet *eth_key; |
f0fb825a EG |
6137 | size_t encap[FLOW_MAX_VLAN_HEADERS] = {0}; |
6138 | size_t max_vlans; | |
5262eea1 | 6139 | const struct flow *flow = parms->flow; |
beb75a40 JS |
6140 | const struct flow *mask = parms->mask; |
6141 | const struct flow *data = export_mask ? mask : flow; | |
661cbcd5 | 6142 | |
54bb0348 | 6143 | nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority); |
abff858b | 6144 | |
3f82ac1f TZ |
6145 | if (flow_tnl_dst_is_set(&flow->tunnel) || |
6146 | flow_tnl_src_is_set(&flow->tunnel) || export_mask) { | |
ec1f6f32 | 6147 | tun_key_to_attr(buf, &data->tunnel, &parms->flow->tunnel, |
c6d87201 | 6148 | parms->key_buf, NULL); |
36956a7d BP |
6149 | } |
6150 | ||
1362e248 | 6151 | nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark); |
72e8bf28 | 6152 | |
07659514 JS |
6153 | if (parms->support.ct_state) { |
6154 | nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE, | |
6155 | ovs_to_odp_ct_state(data->ct_state)); | |
6156 | } | |
6157 | if (parms->support.ct_zone) { | |
6158 | nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, data->ct_zone); | |
6159 | } | |
8e53fe8c JS |
6160 | if (parms->support.ct_mark) { |
6161 | nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, data->ct_mark); | |
6162 | } | |
9daf2348 JS |
6163 | if (parms->support.ct_label) { |
6164 | nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &data->ct_label, | |
6165 | sizeof(data->ct_label)); | |
6166 | } | |
7b5bbe5d JS |
6167 | if (flow->ct_nw_proto) { |
6168 | if (parms->support.ct_orig_tuple | |
6169 | && flow->dl_type == htons(ETH_TYPE_IP)) { | |
67eb8110 IM |
6170 | struct ovs_key_ct_tuple_ipv4 *ct; |
6171 | ||
6172 | /* 'struct ovs_key_ct_tuple_ipv4' has padding, clear it. */ | |
6173 | ct = nl_msg_put_unspec_zero(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4, | |
6174 | sizeof *ct); | |
6175 | ct->ipv4_src = data->ct_nw_src; | |
6176 | ct->ipv4_dst = data->ct_nw_dst; | |
6177 | ct->src_port = data->ct_tp_src; | |
6178 | ct->dst_port = data->ct_tp_dst; | |
6179 | ct->ipv4_proto = data->ct_nw_proto; | |
7b5bbe5d JS |
6180 | } else if (parms->support.ct_orig_tuple6 |
6181 | && flow->dl_type == htons(ETH_TYPE_IPV6)) { | |
67eb8110 IM |
6182 | struct ovs_key_ct_tuple_ipv6 *ct; |
6183 | ||
6184 | /* 'struct ovs_key_ct_tuple_ipv6' has padding, clear it. */ | |
6185 | ct = nl_msg_put_unspec_zero(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6, | |
6186 | sizeof *ct); | |
6187 | ct->ipv6_src = data->ct_ipv6_src; | |
6188 | ct->ipv6_dst = data->ct_ipv6_dst; | |
6189 | ct->src_port = data->ct_tp_src; | |
6190 | ct->dst_port = data->ct_tp_dst; | |
6191 | ct->ipv6_proto = data->ct_nw_proto; | |
daf4d3c1 JR |
6192 | } |
6193 | } | |
2494ccd7 | 6194 | if (parms->support.recirc) { |
572f732a | 6195 | nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id); |
572f732a AZ |
6196 | nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash); |
6197 | } | |
6198 | ||
098d2a97 | 6199 | /* Add an ingress port attribute if this is a mask or 'in_port.odp_port' |
661cbcd5 | 6200 | * is not the magical value "ODPP_NONE". */ |
098d2a97 JG |
6201 | if (export_mask || flow->in_port.odp_port != ODPP_NONE) { |
6202 | nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, data->in_port.odp_port); | |
18886b60 | 6203 | } |
36956a7d | 6204 | |
3d4b2e6e | 6205 | nl_msg_put_be32(buf, OVS_KEY_ATTR_PACKET_TYPE, data->packet_type); |
36956a7d | 6206 | |
f0fb825a EG |
6207 | if (OVS_UNLIKELY(parms->probe)) { |
6208 | max_vlans = FLOW_MAX_VLAN_HEADERS; | |
6209 | } else { | |
6210 | max_vlans = MIN(parms->support.max_vlan_headers, flow_vlan_limit); | |
6211 | } | |
f0fb825a | 6212 | |
beb75a40 JS |
6213 | /* Conditionally add L2 attributes for Ethernet packets */ |
6214 | if (flow->packet_type == htonl(PT_ETH)) { | |
6215 | eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET, | |
6216 | sizeof *eth_key); | |
6217 | get_ethernet_key(data, eth_key); | |
6218 | ||
6219 | for (int encaps = 0; encaps < max_vlans; encaps++) { | |
6220 | ovs_be16 tpid = flow->vlans[encaps].tpid; | |
6221 | ||
6222 | if (flow->vlans[encaps].tci == htons(0)) { | |
6223 | if (eth_type_vlan(flow->dl_type)) { | |
6224 | /* If VLAN was truncated the tpid is in dl_type */ | |
6225 | tpid = flow->dl_type; | |
6226 | } else { | |
6227 | break; | |
6228 | } | |
f0fb825a | 6229 | } |
f0fb825a | 6230 | |
beb75a40 JS |
6231 | if (export_mask) { |
6232 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX); | |
6233 | } else { | |
6234 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, tpid); | |
6235 | } | |
6236 | nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlans[encaps].tci); | |
6237 | encap[encaps] = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP); | |
6238 | if (flow->vlans[encaps].tci == htons(0)) { | |
6239 | goto unencap; | |
6240 | } | |
8ddc056d | 6241 | } |
36956a7d BP |
6242 | } |
6243 | ||
6244 | if (ntohs(flow->dl_type) < ETH_TYPE_MIN) { | |
661cbcd5 JP |
6245 | /* For backwards compatibility with kernels that don't support |
6246 | * wildcarding, the following convention is used to encode the | |
6247 | * OVS_KEY_ATTR_ETHERTYPE for key and mask: | |
6248 | * | |
6249 | * key mask matches | |
6250 | * -------- -------- ------- | |
6251 | * >0x5ff 0xffff Specified Ethernet II Ethertype. | |
6252 | * >0x5ff 0 Any Ethernet II or non-Ethernet II frame. | |
6253 | * <none> 0xffff Any non-Ethernet II frame (except valid | |
6254 | * 802.3 SNAP packet with valid eth_type). | |
6255 | */ | |
fbfe01de | 6256 | if (export_mask) { |
b8266395 | 6257 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX); |
661cbcd5 | 6258 | } |
fea393b1 | 6259 | goto unencap; |
36956a7d BP |
6260 | } |
6261 | ||
661cbcd5 | 6262 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type); |
36956a7d | 6263 | |
f0fb825a EG |
6264 | if (eth_type_vlan(flow->dl_type)) { |
6265 | goto unencap; | |
6266 | } | |
6267 | ||
36956a7d | 6268 | if (flow->dl_type == htons(ETH_TYPE_IP)) { |
df2c07f4 | 6269 | struct ovs_key_ipv4 *ipv4_key; |
36956a7d | 6270 | |
df2c07f4 | 6271 | ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4, |
36956a7d | 6272 | sizeof *ipv4_key); |
3cea18ec | 6273 | get_ipv4_key(data, ipv4_key, export_mask); |
d31f1109 | 6274 | } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) { |
df2c07f4 | 6275 | struct ovs_key_ipv6 *ipv6_key; |
d31f1109 | 6276 | |
df2c07f4 | 6277 | ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6, |
d31f1109 | 6278 | sizeof *ipv6_key); |
3cea18ec | 6279 | get_ipv6_key(data, ipv6_key, export_mask); |
8087f5ff MM |
6280 | } else if (flow->dl_type == htons(ETH_TYPE_ARP) || |
6281 | flow->dl_type == htons(ETH_TYPE_RARP)) { | |
df2c07f4 | 6282 | struct ovs_key_arp *arp_key; |
d31f1109 | 6283 | |
3cea18ec JR |
6284 | arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP, |
6285 | sizeof *arp_key); | |
6286 | get_arp_key(data, arp_key); | |
b0a17866 | 6287 | } else if (eth_type_mpls(flow->dl_type)) { |
b02475c5 | 6288 | struct ovs_key_mpls *mpls_key; |
8bfd0fda | 6289 | int i, n; |
b02475c5 | 6290 | |
8bfd0fda | 6291 | n = flow_count_mpls_labels(flow, NULL); |
5262eea1 | 6292 | if (export_mask) { |
2494ccd7 | 6293 | n = MIN(n, parms->support.max_mpls_depth); |
5262eea1 | 6294 | } |
b02475c5 | 6295 | mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS, |
8bfd0fda BP |
6296 | n * sizeof *mpls_key); |
6297 | for (i = 0; i < n; i++) { | |
6298 | mpls_key[i].mpls_lse = data->mpls_lse[i]; | |
6299 | } | |
3d2fbd70 | 6300 | } else if (flow->dl_type == htons(ETH_TYPE_NSH)) { |
f59cb331 | 6301 | nsh_key_to_attr(buf, &data->nsh, NULL, 0, export_mask); |
b02475c5 SH |
6302 | } |
6303 | ||
48cecbdc | 6304 | if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) { |
6767a2cc | 6305 | if (flow->nw_proto == IPPROTO_TCP) { |
3cea18ec | 6306 | union ovs_key_tp *tcp_key; |
36956a7d | 6307 | |
df2c07f4 | 6308 | tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP, |
36956a7d | 6309 | sizeof *tcp_key); |
3cea18ec | 6310 | get_tp_key(data, tcp_key); |
7cb32412 | 6311 | if (data->tcp_flags || (mask && mask->tcp_flags)) { |
dc235f7f JR |
6312 | nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags); |
6313 | } | |
6767a2cc | 6314 | } else if (flow->nw_proto == IPPROTO_UDP) { |
3cea18ec | 6315 | union ovs_key_tp *udp_key; |
36956a7d | 6316 | |
df2c07f4 | 6317 | udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP, |
36956a7d | 6318 | sizeof *udp_key); |
3cea18ec | 6319 | get_tp_key(data, udp_key); |
c6bcb685 | 6320 | } else if (flow->nw_proto == IPPROTO_SCTP) { |
3cea18ec | 6321 | union ovs_key_tp *sctp_key; |
c6bcb685 JS |
6322 | |
6323 | sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP, | |
6324 | sizeof *sctp_key); | |
3cea18ec | 6325 | get_tp_key(data, sctp_key); |
d31f1109 JP |
6326 | } else if (flow->dl_type == htons(ETH_TYPE_IP) |
6327 | && flow->nw_proto == IPPROTO_ICMP) { | |
df2c07f4 | 6328 | struct ovs_key_icmp *icmp_key; |
36956a7d | 6329 | |
df2c07f4 | 6330 | icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP, |
36956a7d | 6331 | sizeof *icmp_key); |
661cbcd5 JP |
6332 | icmp_key->icmp_type = ntohs(data->tp_src); |
6333 | icmp_key->icmp_code = ntohs(data->tp_dst); | |
d31f1109 JP |
6334 | } else if (flow->dl_type == htons(ETH_TYPE_IPV6) |
6335 | && flow->nw_proto == IPPROTO_ICMPV6) { | |
df2c07f4 | 6336 | struct ovs_key_icmpv6 *icmpv6_key; |
d31f1109 | 6337 | |
df2c07f4 | 6338 | icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6, |
d31f1109 | 6339 | sizeof *icmpv6_key); |
661cbcd5 JP |
6340 | icmpv6_key->icmpv6_type = ntohs(data->tp_src); |
6341 | icmpv6_key->icmpv6_code = ntohs(data->tp_dst); | |
685a51a5 | 6342 | |
c17fcc0a | 6343 | if (is_nd(flow, NULL) |
a63e3dc9 DDP |
6344 | /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP |
6345 | * type and code are 8 bits wide. Therefore, an exact match | |
6346 | * looks like htons(0xff), not htons(0xffff). See | |
6347 | * xlate_wc_finish() for details. */ | |
6348 | && (!export_mask || (data->tp_src == htons(0xff) | |
6349 | && data->tp_dst == htons(0xff)))) { | |
df2c07f4 | 6350 | struct ovs_key_nd *nd_key; |
df2c07f4 | 6351 | nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND, |
685a51a5 | 6352 | sizeof *nd_key); |
932c96b7 | 6353 | nd_key->nd_target = data->nd_target; |
74ff3298 JR |
6354 | nd_key->nd_sll = data->arp_sha; |
6355 | nd_key->nd_tll = data->arp_tha; | |
9b2b8497 | 6356 | |
d0d57149 | 6357 | /* Add ND Extensions Attr only if supported and reserved field |
9b2b8497 | 6358 | * or options type is set. */ |
d0d57149 FL |
6359 | if (parms->support.nd_ext) { |
6360 | struct ovs_key_nd_extensions *nd_ext_key; | |
6361 | ||
6362 | if (data->igmp_group_ip4 != 0 || data->tcp_flags != 0) { | |
8594d9ae PH |
6363 | /* 'struct ovs_key_nd_extensions' has padding, |
6364 | * clear it. */ | |
6365 | nd_ext_key = nl_msg_put_unspec_zero(buf, | |
9b2b8497 VDA |
6366 | OVS_KEY_ATTR_ND_EXTENSIONS, |
6367 | sizeof *nd_ext_key); | |
d0d57149 FL |
6368 | nd_ext_key->nd_reserved = data->igmp_group_ip4; |
6369 | nd_ext_key->nd_options_type = ntohs(data->tcp_flags); | |
6370 | } | |
9b2b8497 | 6371 | } |
685a51a5 | 6372 | } |
36956a7d | 6373 | } |
36956a7d | 6374 | } |
fea393b1 BP |
6375 | |
6376 | unencap: | |
f0fb825a EG |
6377 | for (int encaps = max_vlans - 1; encaps >= 0; encaps--) { |
6378 | if (encap[encaps]) { | |
6379 | nl_msg_end_nested(buf, encap[encaps]); | |
6380 | } | |
fea393b1 | 6381 | } |
36956a7d | 6382 | } |
661cbcd5 JP |
6383 | |
6384 | /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'. | |
661cbcd5 JP |
6385 | * |
6386 | * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be | |
5262eea1 | 6387 | * capable of being expanded to allow for that much space. */ |
661cbcd5 | 6388 | void |
5262eea1 JG |
6389 | odp_flow_key_from_flow(const struct odp_flow_key_parms *parms, |
6390 | struct ofpbuf *buf) | |
661cbcd5 | 6391 | { |
5262eea1 | 6392 | odp_flow_key_from_flow__(parms, false, buf); |
661cbcd5 JP |
6393 | } |
6394 | ||
6395 | /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to | |
5262eea1 | 6396 | * 'buf'. |
661cbcd5 JP |
6397 | * |
6398 | * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be | |
5262eea1 | 6399 | * capable of being expanded to allow for that much space. */ |
661cbcd5 | 6400 | void |
5262eea1 JG |
6401 | odp_flow_key_from_mask(const struct odp_flow_key_parms *parms, |
6402 | struct ofpbuf *buf) | |
661cbcd5 | 6403 | { |
5262eea1 | 6404 | odp_flow_key_from_flow__(parms, true, buf); |
661cbcd5 | 6405 | } |
36956a7d | 6406 | |
758c456d JR |
6407 | /* Generate ODP flow key from the given packet metadata */ |
6408 | void | |
beb75a40 | 6409 | odp_key_from_dp_packet(struct ofpbuf *buf, const struct dp_packet *packet) |
758c456d | 6410 | { |
beb75a40 JS |
6411 | const struct pkt_metadata *md = &packet->md; |
6412 | ||
758c456d JR |
6413 | nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority); |
6414 | ||
29b1dd93 HZ |
6415 | if (md->dp_hash) { |
6416 | nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, md->dp_hash); | |
6417 | } | |
6418 | ||
ffe4c74f | 6419 | if (flow_tnl_dst_is_set(&md->tunnel)) { |
c6d87201 | 6420 | tun_key_to_attr(buf, &md->tunnel, &md->tunnel, NULL, NULL); |
758c456d JR |
6421 | } |
6422 | ||
6423 | nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark); | |
6424 | ||
07659514 JS |
6425 | if (md->ct_state) { |
6426 | nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE, | |
6427 | ovs_to_odp_ct_state(md->ct_state)); | |
6428 | if (md->ct_zone) { | |
6429 | nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, md->ct_zone); | |
6430 | } | |
8e53fe8c JS |
6431 | if (md->ct_mark) { |
6432 | nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, md->ct_mark); | |
6433 | } | |
2ff8484b | 6434 | if (!ovs_u128_is_zero(md->ct_label)) { |
9daf2348 JS |
6435 | nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &md->ct_label, |
6436 | sizeof(md->ct_label)); | |
6437 | } | |
daf4d3c1 JR |
6438 | if (md->ct_orig_tuple_ipv6) { |
6439 | if (md->ct_orig_tuple.ipv6.ipv6_proto) { | |
6440 | nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6, | |
6441 | &md->ct_orig_tuple.ipv6, | |
6442 | sizeof md->ct_orig_tuple.ipv6); | |
6443 | } | |
6444 | } else { | |
6445 | if (md->ct_orig_tuple.ipv4.ipv4_proto) { | |
6446 | nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4, | |
6447 | &md->ct_orig_tuple.ipv4, | |
6448 | sizeof md->ct_orig_tuple.ipv4); | |
6449 | } | |
6450 | } | |
07659514 JS |
6451 | } |
6452 | ||
758c456d JR |
6453 | /* Add an ingress port attribute if 'odp_in_port' is not the magical |
6454 | * value "ODPP_NONE". */ | |
b5e7e61a AZ |
6455 | if (md->in_port.odp_port != ODPP_NONE) { |
6456 | nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port); | |
758c456d | 6457 | } |
beb75a40 JS |
6458 | |
6459 | /* Add OVS_KEY_ATTR_ETHERNET for non-Ethernet packets */ | |
6460 | if (pt_ns(packet->packet_type) == OFPHTN_ETHERTYPE) { | |
6461 | nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, | |
6462 | pt_ns_type_be(packet->packet_type)); | |
6463 | } | |
758c456d JR |
6464 | } |
6465 | ||
6466 | /* Generate packet metadata from the given ODP flow key. */ | |
6467 | void | |
beb75a40 JS |
6468 | odp_key_to_dp_packet(const struct nlattr *key, size_t key_len, |
6469 | struct dp_packet *packet) | |
758c456d | 6470 | { |
beb75a40 | 6471 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
758c456d | 6472 | const struct nlattr *nla; |
beb75a40 JS |
6473 | struct pkt_metadata *md = &packet->md; |
6474 | ovs_be32 packet_type = htonl(PT_UNKNOWN); | |
6475 | ovs_be16 ethertype = 0; | |
758c456d | 6476 | size_t left; |
758c456d | 6477 | |
35303d71 | 6478 | pkt_metadata_init(md, ODPP_NONE); |
758c456d JR |
6479 | |
6480 | NL_ATTR_FOR_EACH (nla, left, key, key_len) { | |
08ef583d | 6481 | enum ovs_key_attr type = nl_attr_type(nla); |
758c456d | 6482 | size_t len = nl_attr_get_size(nla); |
6b8da9e9 JG |
6483 | int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens, |
6484 | OVS_KEY_ATTR_MAX, type); | |
758c456d JR |
6485 | |
6486 | if (len != expected_len && expected_len >= 0) { | |
6487 | continue; | |
6488 | } | |
6489 | ||
572f732a AZ |
6490 | switch (type) { |
6491 | case OVS_KEY_ATTR_RECIRC_ID: | |
6492 | md->recirc_id = nl_attr_get_u32(nla); | |
572f732a AZ |
6493 | break; |
6494 | case OVS_KEY_ATTR_DP_HASH: | |
6495 | md->dp_hash = nl_attr_get_u32(nla); | |
572f732a AZ |
6496 | break; |
6497 | case OVS_KEY_ATTR_PRIORITY: | |
758c456d | 6498 | md->skb_priority = nl_attr_get_u32(nla); |
572f732a AZ |
6499 | break; |
6500 | case OVS_KEY_ATTR_SKB_MARK: | |
758c456d | 6501 | md->pkt_mark = nl_attr_get_u32(nla); |
572f732a | 6502 | break; |
07659514 JS |
6503 | case OVS_KEY_ATTR_CT_STATE: |
6504 | md->ct_state = odp_to_ovs_ct_state(nl_attr_get_u32(nla)); | |
07659514 JS |
6505 | break; |
6506 | case OVS_KEY_ATTR_CT_ZONE: | |
6507 | md->ct_zone = nl_attr_get_u16(nla); | |
07659514 | 6508 | break; |
8e53fe8c JS |
6509 | case OVS_KEY_ATTR_CT_MARK: |
6510 | md->ct_mark = nl_attr_get_u32(nla); | |
8e53fe8c | 6511 | break; |
9daf2348 | 6512 | case OVS_KEY_ATTR_CT_LABELS: { |
ab79d262 | 6513 | md->ct_label = nl_attr_get_u128(nla); |
9daf2348 JS |
6514 | break; |
6515 | } | |
daf4d3c1 JR |
6516 | case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: { |
6517 | const struct ovs_key_ct_tuple_ipv4 *ct = nl_attr_get(nla); | |
6518 | md->ct_orig_tuple.ipv4 = *ct; | |
6519 | md->ct_orig_tuple_ipv6 = false; | |
daf4d3c1 JR |
6520 | break; |
6521 | } | |
6522 | case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: { | |
6523 | const struct ovs_key_ct_tuple_ipv6 *ct = nl_attr_get(nla); | |
6524 | ||
6525 | md->ct_orig_tuple.ipv6 = *ct; | |
6526 | md->ct_orig_tuple_ipv6 = true; | |
daf4d3c1 JR |
6527 | break; |
6528 | } | |
572f732a | 6529 | case OVS_KEY_ATTR_TUNNEL: { |
758c456d JR |
6530 | enum odp_key_fitness res; |
6531 | ||
d40533fc | 6532 | res = odp_tun_key_from_attr(nla, &md->tunnel, NULL); |
758c456d JR |
6533 | if (res == ODP_FIT_ERROR) { |
6534 | memset(&md->tunnel, 0, sizeof md->tunnel); | |
758c456d | 6535 | } |
572f732a AZ |
6536 | break; |
6537 | } | |
6538 | case OVS_KEY_ATTR_IN_PORT: | |
b5e7e61a | 6539 | md->in_port.odp_port = nl_attr_get_odp_port(nla); |
572f732a | 6540 | break; |
beb75a40 JS |
6541 | case OVS_KEY_ATTR_ETHERNET: |
6542 | /* Presence of OVS_KEY_ATTR_ETHERNET indicates Ethernet packet. */ | |
6543 | packet_type = htonl(PT_ETH); | |
beb75a40 JS |
6544 | break; |
6545 | case OVS_KEY_ATTR_ETHERTYPE: | |
6546 | ethertype = nl_attr_get_be16(nla); | |
beb75a40 | 6547 | break; |
08ef583d YHW |
6548 | case OVS_KEY_ATTR_UNSPEC: |
6549 | case OVS_KEY_ATTR_ENCAP: | |
6550 | case OVS_KEY_ATTR_VLAN: | |
6551 | case OVS_KEY_ATTR_IPV4: | |
6552 | case OVS_KEY_ATTR_IPV6: | |
6553 | case OVS_KEY_ATTR_TCP: | |
6554 | case OVS_KEY_ATTR_UDP: | |
6555 | case OVS_KEY_ATTR_ICMP: | |
6556 | case OVS_KEY_ATTR_ICMPV6: | |
6557 | case OVS_KEY_ATTR_ARP: | |
6558 | case OVS_KEY_ATTR_ND: | |
9b2b8497 | 6559 | case OVS_KEY_ATTR_ND_EXTENSIONS: |
08ef583d YHW |
6560 | case OVS_KEY_ATTR_SCTP: |
6561 | case OVS_KEY_ATTR_TCP_FLAGS: | |
6562 | case OVS_KEY_ATTR_MPLS: | |
6563 | case OVS_KEY_ATTR_PACKET_TYPE: | |
3d2fbd70 | 6564 | case OVS_KEY_ATTR_NSH: |
08ef583d | 6565 | case __OVS_KEY_ATTR_MAX: |
572f732a AZ |
6566 | default: |
6567 | break; | |
758c456d | 6568 | } |
758c456d | 6569 | } |
beb75a40 JS |
6570 | |
6571 | if (packet_type == htonl(PT_ETH)) { | |
6572 | packet->packet_type = htonl(PT_ETH); | |
6573 | } else if (packet_type == htonl(PT_UNKNOWN) && ethertype != 0) { | |
6574 | packet->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE, | |
6575 | ntohs(ethertype)); | |
6576 | } else { | |
6577 | VLOG_ERR_RL(&rl, "Packet without ETHERTYPE. Unknown packet_type."); | |
6578 | } | |
758c456d JR |
6579 | } |
6580 | ||
7a5e0ee7 IM |
6581 | /* Places the hash of the 'key_len' bytes starting at 'key' into '*hash'. |
6582 | * Generated value has format of random UUID. */ | |
6583 | void | |
6584 | odp_flow_key_hash(const void *key, size_t key_len, ovs_u128 *hash) | |
b0f7b9b5 | 6585 | { |
7a5e0ee7 IM |
6586 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; |
6587 | static uint32_t secret; | |
6588 | ||
6589 | if (ovsthread_once_start(&once)) { | |
6590 | secret = random_uint32(); | |
6591 | ovsthread_once_done(&once); | |
6592 | } | |
6593 | hash_bytes128(key, key_len, secret, hash); | |
6594 | uuid_set_bits_v4((struct uuid *)hash); | |
b0f7b9b5 BP |
6595 | } |
6596 | ||
34118cae BP |
6597 | static void |
6598 | log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title, | |
b0f7b9b5 | 6599 | uint64_t attrs, int out_of_range_attr, |
34118cae BP |
6600 | const struct nlattr *key, size_t key_len) |
6601 | { | |
6602 | struct ds s; | |
6603 | int i; | |
6604 | ||
b0f7b9b5 | 6605 | if (VLOG_DROP_DBG(rl)) { |
34118cae BP |
6606 | return; |
6607 | } | |
6608 | ||
6609 | ds_init(&s); | |
b0f7b9b5 BP |
6610 | for (i = 0; i < 64; i++) { |
6611 | if (attrs & (UINT64_C(1) << i)) { | |
e6603631 BP |
6612 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
6613 | ||
6614 | ds_put_format(&s, " %s", | |
6615 | ovs_key_attr_to_string(i, namebuf, sizeof namebuf)); | |
34118cae BP |
6616 | } |
6617 | } | |
b0f7b9b5 BP |
6618 | if (out_of_range_attr) { |
6619 | ds_put_format(&s, " %d (and possibly others)", out_of_range_attr); | |
6620 | } | |
34118cae BP |
6621 | |
6622 | ds_put_cstr(&s, ": "); | |
6623 | odp_flow_key_format(key, key_len, &s); | |
6624 | ||
b0f7b9b5 | 6625 | VLOG_DBG("%s:%s", title, ds_cstr(&s)); |
34118cae BP |
6626 | ds_destroy(&s); |
6627 | } | |
6628 | ||
3cea18ec JR |
6629 | static uint8_t |
6630 | odp_to_ovs_frag(uint8_t odp_frag, bool is_mask) | |
7257b535 | 6631 | { |
34118cae BP |
6632 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
6633 | ||
3cea18ec JR |
6634 | if (is_mask) { |
6635 | return odp_frag ? FLOW_NW_FRAG_MASK : 0; | |
6636 | } | |
6637 | ||
9e44d715 | 6638 | if (odp_frag > OVS_FRAG_TYPE_LATER) { |
b0f7b9b5 | 6639 | VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag); |
3cea18ec | 6640 | return 0xff; /* Error. */ |
7257b535 BP |
6641 | } |
6642 | ||
3cea18ec JR |
6643 | return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0 |
6644 | : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY | |
6645 | : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER; | |
7257b535 BP |
6646 | } |
6647 | ||
d40533fc BP |
6648 | /* Parses the attributes in the 'key_len' bytes of 'key' into 'attrs', which |
6649 | * must have OVS_KEY_ATTR_MAX + 1 elements. Stores each attribute in 'key' | |
6650 | * into the corresponding element of 'attrs'. | |
6651 | * | |
6652 | * Stores a bitmask of the attributes' indexes found in 'key' into | |
6653 | * '*present_attrsp'. | |
6654 | * | |
6655 | * If an attribute beyond OVS_KEY_ATTR_MAX is found, stores its attribute type | |
6656 | * (or one of them, if more than one) into '*out_of_range_attrp', otherwise 0. | |
6657 | * | |
6658 | * If 'errorp' is nonnull and the function returns false, stores a malloc()'d | |
6659 | * error message in '*errorp'. */ | |
b0f7b9b5 | 6660 | static bool |
fea393b1 | 6661 | parse_flow_nlattrs(const struct nlattr *key, size_t key_len, |
b0f7b9b5 | 6662 | const struct nlattr *attrs[], uint64_t *present_attrsp, |
d40533fc | 6663 | int *out_of_range_attrp, char **errorp) |
36956a7d | 6664 | { |
b0f7b9b5 | 6665 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10); |
36956a7d | 6666 | const struct nlattr *nla; |
34118cae | 6667 | uint64_t present_attrs; |
36956a7d BP |
6668 | size_t left; |
6669 | ||
2aef1214 | 6670 | BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs); |
34118cae | 6671 | present_attrs = 0; |
b0f7b9b5 | 6672 | *out_of_range_attrp = 0; |
36956a7d | 6673 | NL_ATTR_FOR_EACH (nla, left, key, key_len) { |
34118cae BP |
6674 | uint16_t type = nl_attr_type(nla); |
6675 | size_t len = nl_attr_get_size(nla); | |
6b8da9e9 JG |
6676 | int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens, |
6677 | OVS_KEY_ATTR_MAX, type); | |
34118cae | 6678 | |
b0f7b9b5 | 6679 | if (len != expected_len && expected_len >= 0) { |
e6603631 BP |
6680 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
6681 | ||
d40533fc BP |
6682 | odp_parse_error(&rl, errorp, "attribute %s has length %"PRIuSIZE" " |
6683 | "but should have length %d", | |
6684 | ovs_key_attr_to_string(type, namebuf, | |
6685 | sizeof namebuf), | |
6686 | len, expected_len); | |
b0f7b9b5 | 6687 | return false; |
34118cae BP |
6688 | } |
6689 | ||
2aef1214 | 6690 | if (type > OVS_KEY_ATTR_MAX) { |
b0f7b9b5 BP |
6691 | *out_of_range_attrp = type; |
6692 | } else { | |
6693 | if (present_attrs & (UINT64_C(1) << type)) { | |
e6603631 BP |
6694 | char namebuf[OVS_KEY_ATTR_BUFSIZE]; |
6695 | ||
d40533fc BP |
6696 | odp_parse_error(&rl, errorp, |
6697 | "duplicate %s attribute in flow key", | |
6698 | ovs_key_attr_to_string(type, namebuf, | |
6699 | sizeof namebuf)); | |
b0f7b9b5 BP |
6700 | return false; |
6701 | } | |
6702 | ||
6703 | present_attrs |= UINT64_C(1) << type; | |
6704 | attrs[type] = nla; | |
6705 | } | |
34118cae BP |
6706 | } |
6707 | if (left) { | |
d40533fc | 6708 | odp_parse_error(&rl, errorp, "trailing garbage in flow key"); |
b0f7b9b5 | 6709 | return false; |
34118cae BP |
6710 | } |
6711 | ||
fea393b1 | 6712 | *present_attrsp = present_attrs; |
b0f7b9b5 | 6713 | return true; |
fea393b1 BP |
6714 | } |
6715 | ||
b0f7b9b5 BP |
6716 | static enum odp_key_fitness |
6717 | check_expectations(uint64_t present_attrs, int out_of_range_attr, | |
6718 | uint64_t expected_attrs, | |
fea393b1 BP |
6719 | const struct nlattr *key, size_t key_len) |
6720 | { | |
6721 | uint64_t missing_attrs; | |
6722 | uint64_t extra_attrs; | |
6723 | ||
6724 | missing_attrs = expected_attrs & ~present_attrs; | |
6725 | if (missing_attrs) { | |
b0f7b9b5 BP |
6726 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10); |
6727 | log_odp_key_attributes(&rl, "expected but not present", | |
6728 | missing_attrs, 0, key, key_len); | |
6729 | return ODP_FIT_TOO_LITTLE; | |
fea393b1 BP |
6730 | } |
6731 | ||
6732 | extra_attrs = present_attrs & ~expected_attrs; | |
b0f7b9b5 BP |
6733 | if (extra_attrs || out_of_range_attr) { |
6734 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10); | |
6735 | log_odp_key_attributes(&rl, "present but not expected", | |
6736 | extra_attrs, out_of_range_attr, key, key_len); | |
6737 | return ODP_FIT_TOO_MUCH; | |
fea393b1 BP |
6738 | } |
6739 | ||
b0f7b9b5 | 6740 | return ODP_FIT_PERFECT; |
fea393b1 BP |
6741 | } |
6742 | ||
d40533fc BP |
6743 | /* Initializes 'flow->dl_type' based on the attributes in 'attrs', in which the |
6744 | * attributes in the bit-mask 'present_attrs' are present. Returns true if | |
6745 | * successful, false on failure. | |
6746 | * | |
6747 | * Sets 1-bits in '*expected_attrs' for the attributes in 'attrs' that were | |
6748 | * consulted. 'flow' is assumed to be a flow key unless 'src_flow' is nonnull, | |
6749 | * in which case 'flow' is a flow mask and 'src_flow' is its corresponding | |
6750 | * previously parsed flow key. | |
6751 | * | |
6752 | * If 'errorp' is nonnull and the function returns false, stores a malloc()'d | |
6753 | * error message in '*errorp'. */ | |
b0f7b9b5 BP |
6754 | static bool |
6755 | parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1], | |
6756 | uint64_t present_attrs, uint64_t *expected_attrs, | |
d40533fc BP |
6757 | struct flow *flow, const struct flow *src_flow, |
6758 | char **errorp) | |
fea393b1 BP |
6759 | { |
6760 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4a221615 | 6761 | bool is_mask = flow != src_flow; |
36956a7d | 6762 | |
fea393b1 | 6763 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) { |
34118cae | 6764 | flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]); |
4a221615 | 6765 | if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) { |
d40533fc BP |
6766 | odp_parse_error(&rl, errorp, |
6767 | "invalid Ethertype %"PRIu16" in flow key", | |
6768 | ntohs(flow->dl_type)); | |
b0f7b9b5 | 6769 | return false; |
34118cae | 6770 | } |
4a221615 GY |
6771 | if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN && |
6772 | flow->dl_type != htons(0xffff)) { | |
d40533fc BP |
6773 | odp_parse_error(&rl, errorp, "can't bitwise match non-Ethernet II " |
6774 | "\"Ethertype\" %#"PRIx16" (with mask %#"PRIx16")", | |
6775 | ntohs(src_flow->dl_type), ntohs(flow->dl_type)); | |
4a221615 GY |
6776 | return false; |
6777 | } | |
b0f7b9b5 | 6778 | *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE; |
34118cae | 6779 | } else { |
4a221615 | 6780 | if (!is_mask) { |
beb75a40 JS |
6781 | /* Default ethertype for well-known L3 packets. */ |
6782 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) { | |
6783 | flow->dl_type = htons(ETH_TYPE_IP); | |
6784 | } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) { | |
6785 | flow->dl_type = htons(ETH_TYPE_IPV6); | |
6786 | } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) { | |
6787 | flow->dl_type = htons(ETH_TYPE_MPLS); | |
6788 | } else { | |
6789 | flow->dl_type = htons(FLOW_DL_TYPE_NONE); | |
6790 | } | |
6791 | } else if (src_flow->packet_type != htonl(PT_ETH)) { | |
6792 | /* dl_type is mandatory for non-Ethernet packets */ | |
6793 | flow->dl_type = htons(0xffff); | |
4a221615 GY |
6794 | } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) { |
6795 | /* See comments in odp_flow_key_from_flow__(). */ | |
d40533fc BP |
6796 | odp_parse_error(&rl, errorp, |
6797 | "mask expected for non-Ethernet II frame"); | |
4a221615 GY |
6798 | return false; |
6799 | } | |
34118cae | 6800 | } |
b0f7b9b5 BP |
6801 | return true; |
6802 | } | |
6803 | ||
d40533fc BP |
6804 | /* Initializes MPLS, L3, and L4 fields in 'flow' based on the attributes in |
6805 | * 'attrs', in which the attributes in the bit-mask 'present_attrs' are | |
6806 | * present. The caller also indicates an out-of-range attribute | |
6807 | * 'out_of_range_attr' if one was present when parsing (if so, the fitness | |
6808 | * cannot be perfect). | |
6809 | * | |
6810 | * Sets 1-bits in '*expected_attrs' for the attributes in 'attrs' that were | |
6811 | * consulted. 'flow' is assumed to be a flow key unless 'src_flow' is nonnull, | |
6812 | * in which case 'flow' is a flow mask and 'src_flow' is its corresponding | |
6813 | * previously parsed flow key. | |
6814 | * | |
6815 | * Returns fitness based on any discrepancies between present and expected | |
6816 | * attributes, except that a 'need_check' of false overrides this. | |
6817 | * | |
6818 | * If 'errorp' is nonnull and the function returns false, stores a malloc()'d | |
6819 | * error message in '*errorp'. 'key' and 'key_len' are just used for error | |
6820 | * reporting in this case. */ | |
b0f7b9b5 | 6821 | static enum odp_key_fitness |
b02475c5 SH |
6822 | parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1], |
6823 | uint64_t present_attrs, int out_of_range_attr, | |
c5ad9811 | 6824 | uint64_t *expected_attrs, struct flow *flow, |
4a221615 | 6825 | const struct nlattr *key, size_t key_len, |
d40533fc | 6826 | const struct flow *src_flow, bool need_check, char **errorp) |
b0f7b9b5 BP |
6827 | { |
6828 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4a221615 GY |
6829 | bool is_mask = src_flow != flow; |
6830 | const void *check_start = NULL; | |
6831 | size_t check_len = 0; | |
6832 | enum ovs_key_attr expected_bit = 0xff; | |
6833 | ||
6834 | if (eth_type_mpls(src_flow->dl_type)) { | |
7e6621e9 | 6835 | if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) { |
c5ad9811 | 6836 | *expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS); |
7e6621e9 JS |
6837 | } |
6838 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) { | |
6839 | size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]); | |
6840 | const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]); | |
6841 | int n = size / sizeof(ovs_be32); | |
6842 | int i; | |
4a221615 | 6843 | |
7e6621e9 | 6844 | if (!size || size % sizeof(ovs_be32)) { |
d40533fc BP |
6845 | odp_parse_error(&rl, errorp, |
6846 | "MPLS LSEs have invalid length %"PRIuSIZE, | |
6847 | size); | |
7e6621e9 | 6848 | return ODP_FIT_ERROR; |
91a77332 | 6849 | } |
8bfd0fda | 6850 | if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) { |
d40533fc BP |
6851 | odp_parse_error(&rl, errorp, |
6852 | "unexpected MPLS Ethertype mask %x"PRIx16, | |
6853 | ntohs(flow->dl_type)); | |
4a221615 GY |
6854 | return ODP_FIT_ERROR; |
6855 | } | |
8bfd0fda | 6856 | |
7e6621e9 JS |
6857 | for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) { |
6858 | flow->mpls_lse[i] = mpls_lse[i]; | |
6859 | } | |
6860 | if (n > FLOW_MAX_MPLS_LABELS) { | |
6861 | return ODP_FIT_TOO_MUCH; | |
6862 | } | |
8bfd0fda | 6863 | |
7e6621e9 JS |
6864 | if (!is_mask) { |
6865 | /* BOS may be set only in the innermost label. */ | |
6866 | for (i = 0; i < n - 1; i++) { | |
6867 | if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) { | |
d40533fc BP |
6868 | odp_parse_error(&rl, errorp, |
6869 | "MPLS BOS set in non-innermost label"); | |
7e6621e9 JS |
6870 | return ODP_FIT_ERROR; |
6871 | } | |
8bfd0fda | 6872 | } |
8bfd0fda | 6873 | |
7e6621e9 JS |
6874 | /* BOS must be set in the innermost label. */ |
6875 | if (n < FLOW_MAX_MPLS_LABELS | |
6876 | && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) { | |
6877 | return ODP_FIT_TOO_LITTLE; | |
6878 | } | |
8bfd0fda BP |
6879 | } |
6880 | } | |
6881 | ||
4a221615 GY |
6882 | goto done; |
6883 | } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) { | |
6884 | if (!is_mask) { | |
c5ad9811 | 6885 | *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4; |
b02475c5 | 6886 | } |
fea393b1 | 6887 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) { |
34118cae | 6888 | const struct ovs_key_ipv4 *ipv4_key; |
36956a7d | 6889 | |
34118cae | 6890 | ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]); |
3cea18ec JR |
6891 | put_ipv4_key(ipv4_key, flow, is_mask); |
6892 | if (flow->nw_frag > FLOW_NW_FRAG_MASK) { | |
d40533fc BP |
6893 | odp_parse_error(&rl, errorp, "OVS_KEY_ATTR_IPV4 has invalid " |
6894 | "nw_frag %#"PRIx8, flow->nw_frag); | |
3cea18ec JR |
6895 | return ODP_FIT_ERROR; |
6896 | } | |
d40533fc | 6897 | |
4a221615 | 6898 | if (is_mask) { |
4a221615 GY |
6899 | check_start = ipv4_key; |
6900 | check_len = sizeof *ipv4_key; | |
6901 | expected_bit = OVS_KEY_ATTR_IPV4; | |
36956a7d | 6902 | } |
34118cae | 6903 | } |
4a221615 GY |
6904 | } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) { |
6905 | if (!is_mask) { | |
c5ad9811 | 6906 | *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6; |
4a221615 | 6907 | } |
fea393b1 | 6908 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) { |
34118cae | 6909 | const struct ovs_key_ipv6 *ipv6_key; |
36956a7d | 6910 | |
34118cae | 6911 | ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]); |
3cea18ec JR |
6912 | put_ipv6_key(ipv6_key, flow, is_mask); |
6913 | if (flow->nw_frag > FLOW_NW_FRAG_MASK) { | |
d40533fc BP |
6914 | odp_parse_error(&rl, errorp, "OVS_KEY_ATTR_IPV6 has invalid " |
6915 | "nw_frag %#"PRIx8, flow->nw_frag); | |
3cea18ec JR |
6916 | return ODP_FIT_ERROR; |
6917 | } | |
4a221615 | 6918 | if (is_mask) { |
4a221615 GY |
6919 | check_start = ipv6_key; |
6920 | check_len = sizeof *ipv6_key; | |
6921 | expected_bit = OVS_KEY_ATTR_IPV6; | |
d31f1109 | 6922 | } |
34118cae | 6923 | } |
4a221615 GY |
6924 | } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) || |
6925 | src_flow->dl_type == htons(ETH_TYPE_RARP)) { | |
6926 | if (!is_mask) { | |
c5ad9811 | 6927 | *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP; |
4a221615 | 6928 | } |
fea393b1 | 6929 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) { |
34118cae | 6930 | const struct ovs_key_arp *arp_key; |
d31f1109 | 6931 | |
34118cae | 6932 | arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]); |
4a221615 | 6933 | if (!is_mask && (arp_key->arp_op & htons(0xff00))) { |
d40533fc BP |
6934 | odp_parse_error(&rl, errorp, |
6935 | "unsupported ARP opcode %"PRIu16" in flow " | |
6936 | "key", ntohs(arp_key->arp_op)); | |
b0f7b9b5 | 6937 | return ODP_FIT_ERROR; |
36956a7d | 6938 | } |
3cea18ec | 6939 | put_arp_key(arp_key, flow); |
4a221615 GY |
6940 | if (is_mask) { |
6941 | check_start = arp_key; | |
6942 | check_len = sizeof *arp_key; | |
6943 | expected_bit = OVS_KEY_ATTR_ARP; | |
6944 | } | |
6945 | } | |
3d2fbd70 JS |
6946 | } else if (src_flow->dl_type == htons(ETH_TYPE_NSH)) { |
6947 | if (!is_mask) { | |
c5ad9811 | 6948 | *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_NSH; |
3d2fbd70 JS |
6949 | } |
6950 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_NSH)) { | |
569c3df2 IM |
6951 | if (odp_nsh_key_from_attr__(attrs[OVS_KEY_ATTR_NSH], |
6952 | is_mask, &flow->nsh, | |
6953 | NULL, errorp) == ODP_FIT_ERROR) { | |
d40533fc BP |
6954 | return ODP_FIT_ERROR; |
6955 | } | |
3d2fbd70 | 6956 | if (is_mask) { |
f59cb331 YY |
6957 | check_start = nl_attr_get(attrs[OVS_KEY_ATTR_NSH]); |
6958 | check_len = nl_attr_get_size(attrs[OVS_KEY_ATTR_NSH]); | |
3d2fbd70 JS |
6959 | expected_bit = OVS_KEY_ATTR_NSH; |
6960 | } | |
6961 | } | |
4a221615 GY |
6962 | } else { |
6963 | goto done; | |
6964 | } | |
df4eeb20 | 6965 | if (check_len > 0) { /* Happens only when 'is_mask'. */ |
4a221615 GY |
6966 | if (!is_all_zeros(check_start, check_len) && |
6967 | flow->dl_type != htons(0xffff)) { | |
d40533fc BP |
6968 | odp_parse_error(&rl, errorp, "unexpected L3 matching with " |
6969 | "masked Ethertype %#"PRIx16"/%#"PRIx16, | |
6970 | ntohs(src_flow->dl_type), | |
6971 | ntohs(flow->dl_type)); | |
4a221615 GY |
6972 | return ODP_FIT_ERROR; |
6973 | } else { | |
c5ad9811 | 6974 | *expected_attrs |= UINT64_C(1) << expected_bit; |
36956a7d | 6975 | } |
36956a7d | 6976 | } |
36956a7d | 6977 | |
4a221615 GY |
6978 | expected_bit = OVS_KEY_ATTR_UNSPEC; |
6979 | if (src_flow->nw_proto == IPPROTO_TCP | |
6980 | && (src_flow->dl_type == htons(ETH_TYPE_IP) || | |
6981 | src_flow->dl_type == htons(ETH_TYPE_IPV6)) | |
6982 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
6983 | if (!is_mask) { | |
c5ad9811 | 6984 | *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP; |
4a221615 | 6985 | } |
fea393b1 | 6986 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) { |
3cea18ec | 6987 | const union ovs_key_tp *tcp_key; |
36956a7d | 6988 | |
34118cae | 6989 | tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]); |
3cea18ec | 6990 | put_tp_key(tcp_key, flow); |
4a221615 GY |
6991 | expected_bit = OVS_KEY_ATTR_TCP; |
6992 | } | |
dc235f7f | 6993 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) { |
c5ad9811 | 6994 | *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS; |
dc235f7f JR |
6995 | flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]); |
6996 | } | |
4a221615 GY |
6997 | } else if (src_flow->nw_proto == IPPROTO_UDP |
6998 | && (src_flow->dl_type == htons(ETH_TYPE_IP) || | |
6999 | src_flow->dl_type == htons(ETH_TYPE_IPV6)) | |
7000 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
7001 | if (!is_mask) { | |
c5ad9811 | 7002 | *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP; |
7257b535 | 7003 | } |
fea393b1 | 7004 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) { |
3cea18ec | 7005 | const union ovs_key_tp *udp_key; |
34118cae BP |
7006 | |
7007 | udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]); | |
3cea18ec | 7008 | put_tp_key(udp_key, flow); |
4a221615 GY |
7009 | expected_bit = OVS_KEY_ATTR_UDP; |
7010 | } | |
12848ebf GS |
7011 | } else if (src_flow->nw_proto == IPPROTO_SCTP |
7012 | && (src_flow->dl_type == htons(ETH_TYPE_IP) || | |
7013 | src_flow->dl_type == htons(ETH_TYPE_IPV6)) | |
7014 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
c6bcb685 | 7015 | if (!is_mask) { |
c5ad9811 | 7016 | *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP; |
c6bcb685 JS |
7017 | } |
7018 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) { | |
3cea18ec | 7019 | const union ovs_key_tp *sctp_key; |
c6bcb685 JS |
7020 | |
7021 | sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]); | |
3cea18ec | 7022 | put_tp_key(sctp_key, flow); |
c6bcb685 JS |
7023 | expected_bit = OVS_KEY_ATTR_SCTP; |
7024 | } | |
4a221615 GY |
7025 | } else if (src_flow->nw_proto == IPPROTO_ICMP |
7026 | && src_flow->dl_type == htons(ETH_TYPE_IP) | |
7027 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
7028 | if (!is_mask) { | |
c5ad9811 | 7029 | *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP; |
d31f1109 | 7030 | } |
fea393b1 | 7031 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) { |
34118cae BP |
7032 | const struct ovs_key_icmp *icmp_key; |
7033 | ||
7034 | icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]); | |
7035 | flow->tp_src = htons(icmp_key->icmp_type); | |
7036 | flow->tp_dst = htons(icmp_key->icmp_code); | |
4a221615 GY |
7037 | expected_bit = OVS_KEY_ATTR_ICMP; |
7038 | } | |
7039 | } else if (src_flow->nw_proto == IPPROTO_ICMPV6 | |
7040 | && src_flow->dl_type == htons(ETH_TYPE_IPV6) | |
7041 | && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) { | |
7042 | if (!is_mask) { | |
c5ad9811 | 7043 | *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6; |
685a51a5 | 7044 | } |
fea393b1 | 7045 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) { |
34118cae BP |
7046 | const struct ovs_key_icmpv6 *icmpv6_key; |
7047 | ||
7048 | icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]); | |
7049 | flow->tp_src = htons(icmpv6_key->icmpv6_type); | |
7050 | flow->tp_dst = htons(icmpv6_key->icmpv6_code); | |
4a221615 | 7051 | expected_bit = OVS_KEY_ATTR_ICMPV6; |
c17fcc0a | 7052 | if (is_nd(src_flow, NULL)) { |
4a221615 | 7053 | if (!is_mask) { |
c5ad9811 | 7054 | *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND; |
4a221615 | 7055 | } |
fea393b1 | 7056 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) { |
34118cae BP |
7057 | const struct ovs_key_nd *nd_key; |
7058 | ||
7059 | nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]); | |
932c96b7 | 7060 | flow->nd_target = nd_key->nd_target; |
74ff3298 JR |
7061 | flow->arp_sha = nd_key->nd_sll; |
7062 | flow->arp_tha = nd_key->nd_tll; | |
4a221615 | 7063 | if (is_mask) { |
a63e3dc9 DDP |
7064 | /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, |
7065 | * ICMP type and code are 8 bits wide. Therefore, an | |
7066 | * exact match looks like htons(0xff), not | |
7067 | * htons(0xffff). See xlate_wc_finish() for details. | |
7068 | * */ | |
53cb9c3e | 7069 | if (!is_all_zeros(nd_key, sizeof *nd_key) && |
a63e3dc9 DDP |
7070 | (flow->tp_src != htons(0xff) || |
7071 | flow->tp_dst != htons(0xff))) { | |
d40533fc BP |
7072 | odp_parse_error(&rl, errorp, |
7073 | "ICMP (src,dst) masks should be " | |
7074 | "(0xff,0xff) but are actually " | |
7075 | "(%#"PRIx16",%#"PRIx16")", | |
7076 | ntohs(flow->tp_src), | |
7077 | ntohs(flow->tp_dst)); | |
4a221615 GY |
7078 | return ODP_FIT_ERROR; |
7079 | } else { | |
c5ad9811 | 7080 | *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND; |
4a221615 GY |
7081 | } |
7082 | } | |
34118cae | 7083 | } |
9b2b8497 VDA |
7084 | if (present_attrs & |
7085 | (UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS)) { | |
7086 | const struct ovs_key_nd_extensions *nd_ext_key; | |
7087 | if (!is_mask) { | |
7088 | *expected_attrs |= | |
7089 | UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS; | |
7090 | } | |
7091 | ||
7092 | nd_ext_key = | |
7093 | nl_attr_get(attrs[OVS_KEY_ATTR_ND_EXTENSIONS]); | |
7094 | flow->igmp_group_ip4 = nd_ext_key->nd_reserved; | |
7095 | flow->tcp_flags = htons(nd_ext_key->nd_options_type); | |
7096 | ||
7097 | if (is_mask) { | |
7098 | /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, | |
7099 | * ICMP type and code are 8 bits wide. Therefore, an | |
7100 | * exact match looks like htons(0xff), not | |
7101 | * htons(0xffff). See xlate_wc_finish() for details. | |
7102 | * */ | |
7103 | if (!is_all_zeros(nd_ext_key, sizeof *nd_ext_key) && | |
7104 | (flow->tp_src != htons(0xff) || | |
7105 | flow->tp_dst != htons(0xff))) { | |
7106 | return ODP_FIT_ERROR; | |
7107 | } else { | |
7108 | *expected_attrs |= | |
7109 | UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS; | |
7110 | } | |
7111 | } | |
7112 | } | |
34118cae | 7113 | } |
7257b535 | 7114 | } |
c645550b BP |
7115 | } else if (src_flow->nw_proto == IPPROTO_IGMP |
7116 | && src_flow->dl_type == htons(ETH_TYPE_IP)) { | |
7117 | /* OVS userspace parses the IGMP type, code, and group, but its | |
7118 | * datapaths do not, so there is always missing information. */ | |
7119 | return ODP_FIT_TOO_LITTLE; | |
34118cae | 7120 | } |
4a221615 GY |
7121 | if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) { |
7122 | if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) { | |
d40533fc BP |
7123 | odp_parse_error(&rl, errorp, "flow matches on L4 ports but does " |
7124 | "not define an L4 protocol"); | |
4a221615 GY |
7125 | return ODP_FIT_ERROR; |
7126 | } else { | |
c5ad9811 | 7127 | *expected_attrs |= UINT64_C(1) << expected_bit; |
4a221615 GY |
7128 | } |
7129 | } | |
7257b535 | 7130 | |
4a221615 | 7131 | done: |
c5ad9811 YW |
7132 | return need_check ? check_expectations(present_attrs, out_of_range_attr, |
7133 | *expected_attrs, key, key_len) : ODP_FIT_PERFECT; | |
b0f7b9b5 BP |
7134 | } |
7135 | ||
7136 | /* Parse 802.1Q header then encapsulated L3 attributes. */ | |
7137 | static enum odp_key_fitness | |
7138 | parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1], | |
7139 | uint64_t present_attrs, int out_of_range_attr, | |
7140 | uint64_t expected_attrs, struct flow *flow, | |
4a221615 | 7141 | const struct nlattr *key, size_t key_len, |
d40533fc | 7142 | const struct flow *src_flow, char **errorp) |
b0f7b9b5 BP |
7143 | { |
7144 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4a221615 | 7145 | bool is_mask = src_flow != flow; |
b0f7b9b5 | 7146 | |
f0fb825a | 7147 | const struct nlattr *encap; |
b0f7b9b5 | 7148 | enum odp_key_fitness encap_fitness; |
f0fb825a EG |
7149 | enum odp_key_fitness fitness = ODP_FIT_ERROR; |
7150 | int encaps = 0; | |
b0f7b9b5 | 7151 | |
f0fb825a EG |
7152 | while (encaps < flow_vlan_limit && |
7153 | (is_mask | |
7154 | ? (src_flow->vlans[encaps].tci & htons(VLAN_CFI)) != 0 | |
7155 | : eth_type_vlan(flow->dl_type))) { | |
7156 | ||
7157 | encap = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP) | |
7158 | ? attrs[OVS_KEY_ATTR_ENCAP] : NULL); | |
7159 | ||
7160 | /* Calculate fitness of outer attributes. */ | |
7161 | if (!is_mask) { | |
7162 | expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) | | |
7163 | (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)); | |
7164 | } else { | |
7165 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) { | |
7166 | expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN); | |
7167 | } | |
7168 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) { | |
7169 | expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP); | |
7170 | } | |
4a221615 | 7171 | } |
f0fb825a EG |
7172 | fitness = check_expectations(present_attrs, out_of_range_attr, |
7173 | expected_attrs, key, key_len); | |
7174 | ||
7175 | /* Set vlan_tci. | |
7176 | * Remove the TPID from dl_type since it's not the real Ethertype. */ | |
7177 | flow->vlans[encaps].tpid = flow->dl_type; | |
7178 | flow->dl_type = htons(0); | |
7179 | flow->vlans[encaps].tci = | |
7180 | (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN) | |
7181 | ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]) | |
7182 | : htons(0)); | |
7183 | if (!is_mask) { | |
f889568f | 7184 | if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) || |
7185 | !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) { | |
f0fb825a EG |
7186 | return ODP_FIT_TOO_LITTLE; |
7187 | } else if (flow->vlans[encaps].tci == htons(0)) { | |
7188 | /* Corner case for a truncated 802.1Q header. */ | |
7189 | if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) { | |
7190 | return ODP_FIT_TOO_MUCH; | |
7191 | } | |
7192 | return fitness; | |
7193 | } else if (!(flow->vlans[encaps].tci & htons(VLAN_CFI))) { | |
d40533fc BP |
7194 | odp_parse_error( |
7195 | &rl, errorp, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero " | |
7196 | "but CFI bit is not set", ntohs(flow->vlans[encaps].tci)); | |
f0fb825a EG |
7197 | return ODP_FIT_ERROR; |
7198 | } | |
7199 | } else { | |
7200 | if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) { | |
7201 | return fitness; | |
7202 | } | |
4a221615 | 7203 | } |
b0f7b9b5 | 7204 | |
f0fb825a EG |
7205 | /* Now parse the encapsulated attributes. */ |
7206 | if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap), | |
d40533fc BP |
7207 | attrs, &present_attrs, &out_of_range_attr, |
7208 | errorp)) { | |
3e634810 BP |
7209 | return ODP_FIT_ERROR; |
7210 | } | |
f0fb825a EG |
7211 | expected_attrs = 0; |
7212 | ||
7213 | if (!parse_ethertype(attrs, present_attrs, &expected_attrs, | |
d40533fc | 7214 | flow, src_flow, errorp)) { |
f0fb825a | 7215 | return ODP_FIT_ERROR; |
4a221615 | 7216 | } |
c5ad9811 YW |
7217 | encap_fitness = parse_l2_5_onward(attrs, present_attrs, |
7218 | out_of_range_attr, | |
7219 | &expected_attrs, | |
7220 | flow, key, key_len, | |
d40533fc | 7221 | src_flow, false, errorp); |
c5ad9811 YW |
7222 | if (encap_fitness != ODP_FIT_PERFECT) { |
7223 | return encap_fitness; | |
7224 | } | |
f0fb825a | 7225 | encaps++; |
b0f7b9b5 | 7226 | } |
b0f7b9b5 | 7227 | |
c5ad9811 YW |
7228 | return check_expectations(present_attrs, out_of_range_attr, |
7229 | expected_attrs, key, key_len); | |
b0f7b9b5 BP |
7230 | } |
7231 | ||
4a221615 GY |
7232 | static enum odp_key_fitness |
7233 | odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len, | |
d40533fc BP |
7234 | struct flow *flow, const struct flow *src_flow, |
7235 | char **errorp) | |
b0f7b9b5 | 7236 | { |
74286173 BP |
7237 | /* New "struct flow" fields that are visible to the datapath (including all |
7238 | * data fields) should be translated from equivalent datapath flow fields | |
7239 | * here (you will have to add a OVS_KEY_ATTR_* for them). */ | |
3c6d05a0 | 7240 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 42); |
74286173 | 7241 | |
d40533fc BP |
7242 | enum odp_key_fitness fitness = ODP_FIT_ERROR; |
7243 | if (errorp) { | |
7244 | *errorp = NULL; | |
7245 | } | |
7246 | ||
b0f7b9b5 BP |
7247 | const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1]; |
7248 | uint64_t expected_attrs; | |
7249 | uint64_t present_attrs; | |
7250 | int out_of_range_attr; | |
4a221615 | 7251 | bool is_mask = src_flow != flow; |
b0f7b9b5 BP |
7252 | |
7253 | memset(flow, 0, sizeof *flow); | |
7254 | ||
7255 | /* Parse attributes. */ | |
7256 | if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs, | |
d40533fc BP |
7257 | &out_of_range_attr, errorp)) { |
7258 | goto exit; | |
b0f7b9b5 BP |
7259 | } |
7260 | expected_attrs = 0; | |
7261 | ||
7262 | /* Metadata. */ | |
572f732a AZ |
7263 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) { |
7264 | flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]); | |
7265 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID; | |
7266 | } else if (is_mask) { | |
8c1b077f | 7267 | /* Always exact match recirc_id if it is not specified. */ |
572f732a AZ |
7268 | flow->recirc_id = UINT32_MAX; |
7269 | } | |
7270 | ||
7271 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) { | |
7272 | flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]); | |
7273 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH; | |
7274 | } | |
b0f7b9b5 | 7275 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) { |
deedf7e7 | 7276 | flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]); |
b0f7b9b5 BP |
7277 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY; |
7278 | } | |
7279 | ||
72e8bf28 | 7280 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) { |
1362e248 | 7281 | flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]); |
72e8bf28 AA |
7282 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK; |
7283 | } | |
7284 | ||
07659514 JS |
7285 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE)) { |
7286 | uint32_t odp_state = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_STATE]); | |
7287 | ||
7288 | flow->ct_state = odp_to_ovs_ct_state(odp_state); | |
7289 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE; | |
7290 | } | |
7291 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE)) { | |
7292 | flow->ct_zone = nl_attr_get_u16(attrs[OVS_KEY_ATTR_CT_ZONE]); | |
7293 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE; | |
7294 | } | |
8e53fe8c JS |
7295 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK)) { |
7296 | flow->ct_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_MARK]); | |
7297 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK; | |
7298 | } | |
9daf2348 | 7299 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS)) { |
ab79d262 | 7300 | flow->ct_label = nl_attr_get_u128(attrs[OVS_KEY_ATTR_CT_LABELS]); |
9daf2348 JS |
7301 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS; |
7302 | } | |
daf4d3c1 JR |
7303 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) { |
7304 | const struct ovs_key_ct_tuple_ipv4 *ct = nl_attr_get(attrs[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]); | |
7305 | flow->ct_nw_src = ct->ipv4_src; | |
7306 | flow->ct_nw_dst = ct->ipv4_dst; | |
7307 | flow->ct_nw_proto = ct->ipv4_proto; | |
7308 | flow->ct_tp_src = ct->src_port; | |
7309 | flow->ct_tp_dst = ct->dst_port; | |
7310 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4; | |
7311 | } | |
7312 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) { | |
7313 | const struct ovs_key_ct_tuple_ipv6 *ct = nl_attr_get(attrs[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]); | |
7314 | ||
7315 | flow->ct_ipv6_src = ct->ipv6_src; | |
7316 | flow->ct_ipv6_dst = ct->ipv6_dst; | |
7317 | flow->ct_nw_proto = ct->ipv6_proto; | |
7318 | flow->ct_tp_src = ct->src_port; | |
7319 | flow->ct_tp_dst = ct->dst_port; | |
7320 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6; | |
7321 | } | |
07659514 | 7322 | |
9b405f1a PS |
7323 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) { |
7324 | enum odp_key_fitness res; | |
05fb0928 | 7325 | |
8d8ab6c2 | 7326 | res = odp_tun_key_from_attr__(attrs[OVS_KEY_ATTR_TUNNEL], is_mask, |
d40533fc | 7327 | &flow->tunnel, errorp); |
9b405f1a | 7328 | if (res == ODP_FIT_ERROR) { |
d40533fc | 7329 | goto exit; |
9b405f1a PS |
7330 | } else if (res == ODP_FIT_PERFECT) { |
7331 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL; | |
05fb0928 JR |
7332 | } |
7333 | } | |
7334 | ||
b0f7b9b5 | 7335 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) { |
4e022ec0 AW |
7336 | flow->in_port.odp_port |
7337 | = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]); | |
b0f7b9b5 | 7338 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT; |
4a221615 | 7339 | } else if (!is_mask) { |
4e022ec0 | 7340 | flow->in_port.odp_port = ODPP_NONE; |
b0f7b9b5 BP |
7341 | } |
7342 | ||
beb75a40 JS |
7343 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE)) { |
7344 | flow->packet_type | |
7345 | = nl_attr_get_be32(attrs[OVS_KEY_ATTR_PACKET_TYPE]); | |
7346 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE; | |
29b5c0c3 BP |
7347 | if (pt_ns(src_flow->packet_type) == OFPHTN_ETHERTYPE) { |
7348 | flow->dl_type = pt_ns_type_be(flow->packet_type); | |
7349 | } | |
beb75a40 JS |
7350 | } else if (!is_mask) { |
7351 | flow->packet_type = htonl(PT_ETH); | |
7352 | } | |
7353 | ||
7354 | /* Check for Ethernet header. */ | |
b0f7b9b5 BP |
7355 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) { |
7356 | const struct ovs_key_ethernet *eth_key; | |
7357 | ||
7358 | eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]); | |
3cea18ec | 7359 | put_ethernet_key(eth_key, flow); |
beb75a40 JS |
7360 | if (!is_mask) { |
7361 | flow->packet_type = htonl(PT_ETH); | |
4a221615 | 7362 | } |
4a221615 | 7363 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET; |
b0f7b9b5 | 7364 | } |
beb75a40 JS |
7365 | else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) { |
7366 | ovs_be16 ethertype = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]); | |
7367 | if (!is_mask) { | |
7368 | flow->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE, | |
7369 | ntohs(ethertype)); | |
7370 | } | |
7371 | expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE; | |
7372 | } | |
b0f7b9b5 BP |
7373 | |
7374 | /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */ | |
4a221615 | 7375 | if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, |
d40533fc BP |
7376 | src_flow, errorp)) { |
7377 | goto exit; | |
b0f7b9b5 BP |
7378 | } |
7379 | ||
21e70add | 7380 | if (is_mask |
f0fb825a EG |
7381 | ? (src_flow->vlans[0].tci & htons(VLAN_CFI)) != 0 |
7382 | : eth_type_vlan(src_flow->dl_type)) { | |
d40533fc BP |
7383 | fitness = parse_8021q_onward(attrs, present_attrs, out_of_range_attr, |
7384 | expected_attrs, flow, key, key_len, | |
7385 | src_flow, errorp); | |
7386 | } else { | |
7387 | if (is_mask) { | |
7388 | /* A missing VLAN mask means exact match on vlan_tci 0 (== no | |
7389 | * VLAN). */ | |
7390 | flow->vlans[0].tpid = htons(0xffff); | |
7391 | flow->vlans[0].tci = htons(0xffff); | |
7392 | if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) { | |
7393 | flow->vlans[0].tci = nl_attr_get_be16( | |
7394 | attrs[OVS_KEY_ATTR_VLAN]); | |
7395 | expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN); | |
7396 | } | |
7397 | } | |
7398 | fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr, | |
7399 | &expected_attrs, flow, key, key_len, | |
7400 | src_flow, true, errorp); | |
4a221615 | 7401 | } |
d40533fc BP |
7402 | |
7403 | exit:; | |
7404 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
7405 | if (fitness == ODP_FIT_ERROR && (errorp || !VLOG_DROP_WARN(&rl))) { | |
7406 | struct ds s = DS_EMPTY_INITIALIZER; | |
7407 | if (is_mask) { | |
7408 | ds_put_cstr(&s, "the flow mask in error is: "); | |
7409 | odp_flow_key_format(key, key_len, &s); | |
7410 | ds_put_cstr(&s, ", for the following flow key: "); | |
7411 | flow_format(&s, src_flow, NULL); | |
7412 | } else { | |
7413 | ds_put_cstr(&s, "the flow key in error is: "); | |
7414 | odp_flow_key_format(key, key_len, &s); | |
4a221615 | 7415 | } |
d40533fc BP |
7416 | if (errorp) { |
7417 | char *old_error = *errorp; | |
7418 | *errorp = xasprintf("%s; %s", old_error, ds_cstr(&s)); | |
7419 | free(old_error); | |
7420 | } else { | |
7421 | VLOG_WARN("%s", ds_cstr(&s)); | |
7422 | } | |
7423 | ds_destroy(&s); | |
b0f7b9b5 | 7424 | } |
d40533fc | 7425 | return fitness; |
4a221615 GY |
7426 | } |
7427 | ||
7428 | /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow | |
7429 | * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well | |
7430 | * 'key' fits our expectations for what a flow key should contain. | |
7431 | * | |
7432 | * The 'in_port' will be the datapath's understanding of the port. The | |
7433 | * caller will need to translate with odp_port_to_ofp_port() if the | |
7434 | * OpenFlow port is needed. | |
7435 | * | |
7436 | * This function doesn't take the packet itself as an argument because none of | |
7437 | * the currently understood OVS_KEY_ATTR_* attributes require it. Currently, | |
7438 | * it is always possible to infer which additional attribute(s) should appear | |
7439 | * by looking at the attributes for lower-level protocols, e.g. if the network | |
7440 | * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we | |
7441 | * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it | |
d40533fc BP |
7442 | * must be absent. |
7443 | * | |
7444 | * If 'errorp' is nonnull, this function uses it for detailed error reports: if | |
7445 | * the return value is ODP_FIT_ERROR, it stores a malloc()'d error string in | |
7446 | * '*errorp', otherwise NULL. */ | |
4a221615 GY |
7447 | enum odp_key_fitness |
7448 | odp_flow_key_to_flow(const struct nlattr *key, size_t key_len, | |
d40533fc | 7449 | struct flow *flow, char **errorp) |
4a221615 | 7450 | { |
d40533fc | 7451 | return odp_flow_key_to_flow__(key, key_len, flow, flow, errorp); |
4a221615 GY |
7452 | } |
7453 | ||
8d8ab6c2 JG |
7454 | /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key' |
7455 | * to a mask structure in 'mask'. 'flow' must be a previously translated flow | |
7456 | * corresponding to 'mask' and similarly flow_key/flow_key_len must be the | |
7457 | * attributes from that flow. Returns an ODP_FIT_* value that indicates how | |
d40533fc BP |
7458 | * well 'key' fits our expectations for what a flow key should contain. |
7459 | * | |
7460 | * If 'errorp' is nonnull, this function uses it for detailed error reports: if | |
7461 | * the return value is ODP_FIT_ERROR, it stores a malloc()'d error string in | |
7462 | * '*errorp', otherwise NULL. */ | |
8d8ab6c2 JG |
7463 | enum odp_key_fitness |
7464 | odp_flow_key_to_mask(const struct nlattr *mask_key, size_t mask_key_len, | |
d40533fc BP |
7465 | struct flow_wildcards *mask, const struct flow *src_flow, |
7466 | char **errorp) | |
ca8d3442 DDP |
7467 | { |
7468 | if (mask_key_len) { | |
7469 | return odp_flow_key_to_flow__(mask_key, mask_key_len, | |
d40533fc | 7470 | &mask->masks, src_flow, errorp); |
ca8d3442 | 7471 | } else { |
d40533fc BP |
7472 | if (errorp) { |
7473 | *errorp = NULL; | |
7474 | } | |
7475 | ||
ca8d3442 DDP |
7476 | /* A missing mask means that the flow should be exact matched. |
7477 | * Generate an appropriate exact wildcard for the flow. */ | |
7478 | flow_wildcards_init_for_packet(mask, src_flow); | |
7479 | ||
7480 | return ODP_FIT_PERFECT; | |
7481 | } | |
7482 | } | |
39db78a0 | 7483 | |
8b668ee3 PB |
7484 | /* Converts the netlink formated key/mask to match. |
7485 | * Fails if odp_flow_key_from_key/mask and odp_flow_key_key/mask | |
7486 | * disagree on the acceptable form of flow */ | |
7487 | int | |
7488 | parse_key_and_mask_to_match(const struct nlattr *key, size_t key_len, | |
7489 | const struct nlattr *mask, size_t mask_len, | |
7490 | struct match *match) | |
7491 | { | |
7492 | enum odp_key_fitness fitness; | |
7493 | ||
d40533fc | 7494 | fitness = odp_flow_key_to_flow(key, key_len, &match->flow, NULL); |
8b668ee3 PB |
7495 | if (fitness) { |
7496 | /* This should not happen: it indicates that | |
7497 | * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on | |
7498 | * the acceptable form of a flow. Log the problem as an error, | |
7499 | * with enough details to enable debugging. */ | |
7500 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
7501 | ||
7502 | if (!VLOG_DROP_ERR(&rl)) { | |
7503 | struct ds s; | |
7504 | ||
7505 | ds_init(&s); | |
7506 | odp_flow_format(key, key_len, NULL, 0, NULL, &s, true); | |
7507 | VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s)); | |
7508 | ds_destroy(&s); | |
7509 | } | |
7510 | ||
7511 | return EINVAL; | |
7512 | } | |
7513 | ||
d40533fc BP |
7514 | fitness = odp_flow_key_to_mask(mask, mask_len, &match->wc, &match->flow, |
7515 | NULL); | |
8b668ee3 PB |
7516 | if (fitness) { |
7517 | /* This should not happen: it indicates that | |
7518 | * odp_flow_key_from_mask() and odp_flow_key_to_mask() | |
7519 | * disagree on the acceptable form of a mask. Log the problem | |
7520 | * as an error, with enough details to enable debugging. */ | |
7521 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
7522 | ||
7523 | if (!VLOG_DROP_ERR(&rl)) { | |
7524 | struct ds s; | |
7525 | ||
7526 | ds_init(&s); | |
7527 | odp_flow_format(key, key_len, mask, mask_len, NULL, &s, | |
7528 | true); | |
7529 | VLOG_ERR("internal error parsing flow mask %s (%s)", | |
7530 | ds_cstr(&s), odp_key_fitness_to_string(fitness)); | |
7531 | ds_destroy(&s); | |
7532 | } | |
7533 | ||
7534 | return EINVAL; | |
7535 | } | |
7536 | ||
7537 | return 0; | |
7538 | } | |
7539 | ||
6814e51f BP |
7540 | /* Returns 'fitness' as a string, for use in debug messages. */ |
7541 | const char * | |
7542 | odp_key_fitness_to_string(enum odp_key_fitness fitness) | |
7543 | { | |
7544 | switch (fitness) { | |
7545 | case ODP_FIT_PERFECT: | |
7546 | return "OK"; | |
7547 | case ODP_FIT_TOO_MUCH: | |
7548 | return "too_much"; | |
7549 | case ODP_FIT_TOO_LITTLE: | |
7550 | return "too_little"; | |
7551 | case ODP_FIT_ERROR: | |
7552 | return "error"; | |
7553 | default: | |
7554 | return "<unknown>"; | |
7555 | } | |
7556 | } | |
7557 | ||
39db78a0 | 7558 | /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies |
e995e3df BP |
7559 | * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute |
7560 | * whose contents are the 'userdata_size' bytes at 'userdata' and returns the | |
7561 | * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is | |
7562 | * null, then the return value is not meaningful.) */ | |
39db78a0 | 7563 | size_t |
e995e3df BP |
7564 | odp_put_userspace_action(uint32_t pid, |
7565 | const void *userdata, size_t userdata_size, | |
8b7ea2d4 | 7566 | odp_port_t tunnel_out_port, |
7321bda3 | 7567 | bool include_actions, |
39db78a0 BP |
7568 | struct ofpbuf *odp_actions) |
7569 | { | |
e995e3df | 7570 | size_t userdata_ofs; |
39db78a0 BP |
7571 | size_t offset; |
7572 | ||
7573 | offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE); | |
7574 | nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid); | |
e995e3df | 7575 | if (userdata) { |
6fd6ed71 | 7576 | userdata_ofs = odp_actions->size + NLA_HDRLEN; |
96ed775f BP |
7577 | |
7578 | /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel | |
7579 | * module before Linux 3.10 required the userdata to be exactly 8 bytes | |
7580 | * long: | |
7581 | * | |
7582 | * - The kernel rejected shorter userdata with -ERANGE. | |
7583 | * | |
7584 | * - The kernel silently dropped userdata beyond the first 8 bytes. | |
7585 | * | |
7586 | * Thus, for maximum compatibility, always put at least 8 bytes. (We | |
7587 | * separately disable features that required more than 8 bytes.) */ | |
7588 | memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA, | |
7589 | MAX(8, userdata_size)), | |
7590 | userdata, userdata_size); | |
e995e3df BP |
7591 | } else { |
7592 | userdata_ofs = 0; | |
39db78a0 | 7593 | } |
8b7ea2d4 WZ |
7594 | if (tunnel_out_port != ODPP_NONE) { |
7595 | nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT, | |
7596 | tunnel_out_port); | |
7597 | } | |
7321bda3 NM |
7598 | if (include_actions) { |
7599 | nl_msg_put_flag(odp_actions, OVS_USERSPACE_ATTR_ACTIONS); | |
7600 | } | |
39db78a0 BP |
7601 | nl_msg_end_nested(odp_actions, offset); |
7602 | ||
e995e3df | 7603 | return userdata_ofs; |
39db78a0 | 7604 | } |
b9ad7294 | 7605 | |
88fc5281 JS |
7606 | void |
7607 | odp_put_pop_eth_action(struct ofpbuf *odp_actions) | |
7608 | { | |
7609 | nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_ETH); | |
7610 | } | |
7611 | ||
7612 | void | |
7613 | odp_put_push_eth_action(struct ofpbuf *odp_actions, | |
7614 | const struct eth_addr *eth_src, | |
7615 | const struct eth_addr *eth_dst) | |
7616 | { | |
7617 | struct ovs_action_push_eth eth; | |
7618 | ||
7619 | memset(ð, 0, sizeof eth); | |
7620 | if (eth_src) { | |
7621 | eth.addresses.eth_src = *eth_src; | |
7622 | } | |
7623 | if (eth_dst) { | |
7624 | eth.addresses.eth_dst = *eth_dst; | |
7625 | } | |
7626 | ||
7627 | nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_ETH, | |
7628 | ð, sizeof eth); | |
7629 | } | |
7630 | ||
b9ad7294 EJ |
7631 | void |
7632 | odp_put_tunnel_action(const struct flow_tnl *tunnel, | |
c6d87201 | 7633 | struct ofpbuf *odp_actions, const char *tnl_type) |
b9ad7294 EJ |
7634 | { |
7635 | size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET); | |
c6d87201 | 7636 | tun_key_to_attr(odp_actions, tunnel, tunnel, NULL, tnl_type); |
b9ad7294 EJ |
7637 | nl_msg_end_nested(odp_actions, offset); |
7638 | } | |
a36de779 PS |
7639 | |
7640 | void | |
7641 | odp_put_tnl_push_action(struct ofpbuf *odp_actions, | |
7642 | struct ovs_action_push_tnl *data) | |
7643 | { | |
7644 | int size = offsetof(struct ovs_action_push_tnl, header); | |
7645 | ||
7646 | size += data->header_len; | |
7647 | nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size); | |
7648 | } | |
7649 | ||
5bbda0aa EJ |
7650 | \f |
7651 | /* The commit_odp_actions() function and its helpers. */ | |
7652 | ||
7653 | static void | |
7654 | commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type, | |
7655 | const void *key, size_t key_size) | |
7656 | { | |
7657 | size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET); | |
7658 | nl_msg_put_unspec(odp_actions, key_type, key, key_size); | |
7659 | nl_msg_end_nested(odp_actions, offset); | |
7660 | } | |
7661 | ||
6d670e7f JR |
7662 | /* Masked set actions have a mask following the data within the netlink |
7663 | * attribute. The unmasked bits in the data will be cleared as the data | |
7664 | * is copied to the action. */ | |
7665 | void | |
7666 | commit_masked_set_action(struct ofpbuf *odp_actions, | |
7667 | enum ovs_key_attr key_type, | |
7668 | const void *key_, const void *mask_, size_t key_size) | |
7669 | { | |
7670 | size_t offset = nl_msg_start_nested(odp_actions, | |
7671 | OVS_ACTION_ATTR_SET_MASKED); | |
7672 | char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2); | |
7673 | const char *key = key_, *mask = mask_; | |
7674 | ||
7675 | memcpy(data + key_size, mask, key_size); | |
7676 | /* Clear unmasked bits while copying. */ | |
7677 | while (key_size--) { | |
7678 | *data++ = *key++ & *mask++; | |
7679 | } | |
7680 | nl_msg_end_nested(odp_actions, offset); | |
7681 | } | |
7682 | ||
b9ad7294 EJ |
7683 | /* If any of the flow key data that ODP actions can modify are different in |
7684 | * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to | |
7685 | * 'odp_actions' that change the flow tunneling information in key from | |
7686 | * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the | |
7687 | * same way. In other words, operates the same as commit_odp_actions(), but | |
7688 | * only on tunneling information. */ | |
7689 | void | |
7690 | commit_odp_tunnel_action(const struct flow *flow, struct flow *base, | |
c6d87201 | 7691 | struct ofpbuf *odp_actions, const char *tnl_type) |
5bbda0aa | 7692 | { |
ffe4c74f JB |
7693 | /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel |
7694 | * must have non-zero ipv6_dst. */ | |
7695 | if (flow_tnl_dst_is_set(&flow->tunnel)) { | |
fc80de30 JR |
7696 | if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) { |
7697 | return; | |
7698 | } | |
7699 | memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel); | |
c6d87201 | 7700 | odp_put_tunnel_action(&base->tunnel, odp_actions, tnl_type); |
05fb0928 | 7701 | } |
5bbda0aa EJ |
7702 | } |
7703 | ||
dbf4a928 EB |
7704 | struct offsetof_sizeof { |
7705 | int offset; | |
7706 | int size; | |
7707 | }; | |
7708 | ||
e8bf7774 IM |
7709 | |
7710 | /* Performs bitwise OR over the fields in 'dst_' and 'src_' specified in | |
7711 | * 'offsetof_sizeof_arr' array. Result is stored in 'dst_'. */ | |
7712 | static void | |
7713 | or_masks(void *dst_, const void *src_, | |
7714 | struct offsetof_sizeof *offsetof_sizeof_arr) | |
7715 | { | |
7716 | int field, size, offset; | |
7717 | const uint8_t *src = src_; | |
7718 | uint8_t *dst = dst_; | |
7719 | ||
7720 | for (field = 0; ; field++) { | |
7721 | size = offsetof_sizeof_arr[field].size; | |
7722 | offset = offsetof_sizeof_arr[field].offset; | |
7723 | ||
7724 | if (!size) { | |
7725 | return; | |
7726 | } | |
7727 | or_bytes(dst + offset, src + offset, size); | |
7728 | } | |
7729 | } | |
7730 | ||
dbf4a928 EB |
7731 | /* Compares each of the fields in 'key0' and 'key1'. The fields are specified |
7732 | * in 'offsetof_sizeof_arr', which is an array terminated by a 0-size field. | |
7733 | * Returns true if all of the fields are equal, false if at least one differs. | |
7734 | * As a side effect, for each field that is the same in 'key0' and 'key1', | |
7735 | * zeros the corresponding bytes in 'mask'. */ | |
7736 | static bool | |
7737 | keycmp_mask(const void *key0, const void *key1, | |
7738 | struct offsetof_sizeof *offsetof_sizeof_arr, void *mask) | |
7739 | { | |
7740 | bool differ = false; | |
7741 | ||
7742 | for (int field = 0 ; ; field++) { | |
7743 | int size = offsetof_sizeof_arr[field].size; | |
7744 | int offset = offsetof_sizeof_arr[field].offset; | |
7745 | if (size == 0) { | |
7746 | break; | |
7747 | } | |
7748 | ||
7749 | char *pkey0 = ((char *)key0) + offset; | |
7750 | char *pkey1 = ((char *)key1) + offset; | |
7751 | char *pmask = ((char *)mask) + offset; | |
7752 | if (memcmp(pkey0, pkey1, size) == 0) { | |
7753 | memset(pmask, 0, size); | |
7754 | } else { | |
7755 | differ = true; | |
7756 | } | |
7757 | } | |
7758 | ||
7759 | return differ; | |
7760 | } | |
7761 | ||
d23df9a8 JR |
7762 | static bool |
7763 | commit(enum ovs_key_attr attr, bool use_masked_set, | |
7764 | const void *key, void *base, void *mask, size_t size, | |
dbf4a928 | 7765 | struct offsetof_sizeof *offsetof_sizeof_arr, |
d23df9a8 | 7766 | struct ofpbuf *odp_actions) |
5bbda0aa | 7767 | { |
dbf4a928 | 7768 | if (keycmp_mask(key, base, offsetof_sizeof_arr, mask)) { |
d23df9a8 | 7769 | bool fully_masked = odp_mask_is_exact(attr, mask, size); |
5bbda0aa | 7770 | |
d23df9a8 JR |
7771 | if (use_masked_set && !fully_masked) { |
7772 | commit_masked_set_action(odp_actions, attr, key, mask, size); | |
7773 | } else { | |
7774 | if (!fully_masked) { | |
7775 | memset(mask, 0xff, size); | |
7776 | } | |
7777 | commit_set_action(odp_actions, attr, key, size); | |
7778 | } | |
7779 | memcpy(base, key, size); | |
7780 | return true; | |
7781 | } else { | |
7782 | /* Mask bits are set when we have either read or set the corresponding | |
7783 | * values. Masked bits will be exact-matched, no need to set them | |
7784 | * if the value did not actually change. */ | |
7785 | return false; | |
5bbda0aa | 7786 | } |
d23df9a8 | 7787 | } |
5bbda0aa | 7788 | |
d23df9a8 JR |
7789 | static void |
7790 | get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth) | |
7791 | { | |
74ff3298 JR |
7792 | eth->eth_src = flow->dl_src; |
7793 | eth->eth_dst = flow->dl_dst; | |
d23df9a8 | 7794 | } |
1dd35f8a | 7795 | |
d23df9a8 JR |
7796 | static void |
7797 | put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow) | |
7798 | { | |
74ff3298 JR |
7799 | flow->dl_src = eth->eth_src; |
7800 | flow->dl_dst = eth->eth_dst; | |
d23df9a8 | 7801 | } |
5bbda0aa | 7802 | |
d23df9a8 | 7803 | static void |
f839892a JS |
7804 | commit_set_ether_action(const struct flow *flow, struct flow *base_flow, |
7805 | struct ofpbuf *odp_actions, | |
7806 | struct flow_wildcards *wc, | |
7807 | bool use_masked) | |
d23df9a8 | 7808 | { |
e8bf7774 | 7809 | struct ovs_key_ethernet key, base, mask, orig_mask; |
dbf4a928 EB |
7810 | struct offsetof_sizeof ovs_key_ethernet_offsetof_sizeof_arr[] = |
7811 | OVS_KEY_ETHERNET_OFFSETOF_SIZEOF_ARR; | |
e8bf7774 | 7812 | |
f839892a JS |
7813 | if (flow->packet_type != htonl(PT_ETH)) { |
7814 | return; | |
7815 | } | |
7816 | ||
d23df9a8 JR |
7817 | get_ethernet_key(flow, &key); |
7818 | get_ethernet_key(base_flow, &base); | |
7819 | get_ethernet_key(&wc->masks, &mask); | |
e8bf7774 | 7820 | memcpy(&orig_mask, &mask, sizeof mask); |
d23df9a8 JR |
7821 | |
7822 | if (commit(OVS_KEY_ATTR_ETHERNET, use_masked, | |
dbf4a928 EB |
7823 | &key, &base, &mask, sizeof key, |
7824 | ovs_key_ethernet_offsetof_sizeof_arr, odp_actions)) { | |
d23df9a8 | 7825 | put_ethernet_key(&base, base_flow); |
e8bf7774 | 7826 | or_masks(&mask, &orig_mask, ovs_key_ethernet_offsetof_sizeof_arr); |
d23df9a8 JR |
7827 | put_ethernet_key(&mask, &wc->masks); |
7828 | } | |
5bbda0aa EJ |
7829 | } |
7830 | ||
7831 | static void | |
f0fb825a EG |
7832 | commit_vlan_action(const struct flow* flow, struct flow *base, |
7833 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) | |
5bbda0aa | 7834 | { |
f0fb825a EG |
7835 | int base_n = flow_count_vlan_headers(base); |
7836 | int flow_n = flow_count_vlan_headers(flow); | |
7837 | flow_skip_common_vlan_headers(base, &base_n, flow, &flow_n); | |
1dd35f8a | 7838 | |
f0fb825a EG |
7839 | /* Pop all mismatching vlan of base, push those of flow */ |
7840 | for (; base_n >= 0; base_n--) { | |
5bbda0aa | 7841 | nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN); |
f0fb825a | 7842 | wc->masks.vlans[base_n].qtag = OVS_BE32_MAX; |
5bbda0aa EJ |
7843 | } |
7844 | ||
f0fb825a | 7845 | for (; flow_n >= 0; flow_n--) { |
5bbda0aa EJ |
7846 | struct ovs_action_push_vlan vlan; |
7847 | ||
f0fb825a EG |
7848 | vlan.vlan_tpid = flow->vlans[flow_n].tpid; |
7849 | vlan.vlan_tci = flow->vlans[flow_n].tci; | |
5bbda0aa EJ |
7850 | nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN, |
7851 | &vlan, sizeof vlan); | |
7852 | } | |
f0fb825a | 7853 | memcpy(base->vlans, flow->vlans, sizeof(base->vlans)); |
5bbda0aa EJ |
7854 | } |
7855 | ||
22d38fca | 7856 | /* Wildcarding already done at action translation time. */ |
b02475c5 SH |
7857 | static void |
7858 | commit_mpls_action(const struct flow *flow, struct flow *base, | |
22d38fca | 7859 | struct ofpbuf *odp_actions) |
b02475c5 | 7860 | { |
22d38fca JR |
7861 | int base_n = flow_count_mpls_labels(base, NULL); |
7862 | int flow_n = flow_count_mpls_labels(flow, NULL); | |
8bfd0fda | 7863 | int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n, |
22d38fca | 7864 | NULL); |
8bfd0fda BP |
7865 | |
7866 | while (base_n > common_n) { | |
7867 | if (base_n - 1 == common_n && flow_n > common_n) { | |
7868 | /* If there is only one more LSE in base than there are common | |
7869 | * between base and flow; and flow has at least one more LSE than | |
7870 | * is common then the topmost LSE of base may be updated using | |
7871 | * set */ | |
7872 | struct ovs_key_mpls mpls_key; | |
7873 | ||
7874 | mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n]; | |
7875 | commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS, | |
7876 | &mpls_key, sizeof mpls_key); | |
7877 | flow_set_mpls_lse(base, 0, mpls_key.mpls_lse); | |
7878 | common_n++; | |
7879 | } else { | |
7880 | /* Otherwise, if there more LSEs in base than are common between | |
7881 | * base and flow then pop the topmost one. */ | |
7882 | ovs_be16 dl_type; | |
8bfd0fda BP |
7883 | /* If all the LSEs are to be popped and this is not the outermost |
7884 | * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the | |
7885 | * POP_MPLS action instead of flow->dl_type. | |
7886 | * | |
7887 | * This is because the POP_MPLS action requires its ethertype | |
7888 | * argument to be an MPLS ethernet type but in this case | |
7889 | * flow->dl_type will be a non-MPLS ethernet type. | |
7890 | * | |
7891 | * When the final POP_MPLS action occurs it use flow->dl_type and | |
7892 | * the and the resulting packet will have the desired dl_type. */ | |
7893 | if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) { | |
7894 | dl_type = htons(ETH_TYPE_MPLS); | |
7895 | } else { | |
7896 | dl_type = flow->dl_type; | |
7897 | } | |
7898 | nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type); | |
500db308 | 7899 | ovs_assert(flow_pop_mpls(base, base_n, flow->dl_type, NULL)); |
8bfd0fda BP |
7900 | base_n--; |
7901 | } | |
b02475c5 SH |
7902 | } |
7903 | ||
8bfd0fda BP |
7904 | /* If, after the above popping and setting, there are more LSEs in flow |
7905 | * than base then some LSEs need to be pushed. */ | |
7906 | while (base_n < flow_n) { | |
b02475c5 SH |
7907 | struct ovs_action_push_mpls *mpls; |
7908 | ||
8bfd0fda BP |
7909 | mpls = nl_msg_put_unspec_zero(odp_actions, |
7910 | OVS_ACTION_ATTR_PUSH_MPLS, | |
9ddf12cc | 7911 | sizeof *mpls); |
b02475c5 | 7912 | mpls->mpls_ethertype = flow->dl_type; |
8bfd0fda | 7913 | mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1]; |
742c0ac3 JR |
7914 | /* Update base flow's MPLS stack, but do not clear L3. We need the L3 |
7915 | * headers if the flow is restored later due to returning from a patch | |
7916 | * port or group bucket. */ | |
7917 | flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL, false); | |
8bfd0fda BP |
7918 | flow_set_mpls_lse(base, 0, mpls->mpls_lse); |
7919 | base_n++; | |
b0a17866 | 7920 | } |
b02475c5 SH |
7921 | } |
7922 | ||
5bbda0aa | 7923 | static void |
3cea18ec | 7924 | get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask) |
5bbda0aa | 7925 | { |
d23df9a8 JR |
7926 | ipv4->ipv4_src = flow->nw_src; |
7927 | ipv4->ipv4_dst = flow->nw_dst; | |
7928 | ipv4->ipv4_proto = flow->nw_proto; | |
7929 | ipv4->ipv4_tos = flow->nw_tos; | |
7930 | ipv4->ipv4_ttl = flow->nw_ttl; | |
3cea18ec | 7931 | ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask); |
d23df9a8 | 7932 | } |
5bbda0aa | 7933 | |
d23df9a8 | 7934 | static void |
3cea18ec | 7935 | put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask) |
d23df9a8 JR |
7936 | { |
7937 | flow->nw_src = ipv4->ipv4_src; | |
7938 | flow->nw_dst = ipv4->ipv4_dst; | |
7939 | flow->nw_proto = ipv4->ipv4_proto; | |
7940 | flow->nw_tos = ipv4->ipv4_tos; | |
7941 | flow->nw_ttl = ipv4->ipv4_ttl; | |
3cea18ec | 7942 | flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask); |
d23df9a8 JR |
7943 | } |
7944 | ||
7945 | static void | |
7946 | commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow, | |
7947 | struct ofpbuf *odp_actions, struct flow_wildcards *wc, | |
7948 | bool use_masked) | |
7949 | { | |
e8bf7774 | 7950 | struct ovs_key_ipv4 key, mask, orig_mask, base; |
dbf4a928 EB |
7951 | struct offsetof_sizeof ovs_key_ipv4_offsetof_sizeof_arr[] = |
7952 | OVS_KEY_IPV4_OFFSETOF_SIZEOF_ARR; | |
5bbda0aa | 7953 | |
d23df9a8 JR |
7954 | /* Check that nw_proto and nw_frag remain unchanged. */ |
7955 | ovs_assert(flow->nw_proto == base_flow->nw_proto && | |
7956 | flow->nw_frag == base_flow->nw_frag); | |
1dd35f8a | 7957 | |
3cea18ec JR |
7958 | get_ipv4_key(flow, &key, false); |
7959 | get_ipv4_key(base_flow, &base, false); | |
7960 | get_ipv4_key(&wc->masks, &mask, true); | |
e8bf7774 | 7961 | memcpy(&orig_mask, &mask, sizeof mask); |
d23df9a8 JR |
7962 | mask.ipv4_proto = 0; /* Not writeable. */ |
7963 | mask.ipv4_frag = 0; /* Not writable. */ | |
5bbda0aa | 7964 | |
b51d0390 JL |
7965 | if (flow_tnl_dst_is_set(&base_flow->tunnel) && |
7966 | ((base_flow->nw_tos ^ flow->nw_tos) & IP_ECN_MASK) == 0) { | |
7967 | mask.ipv4_tos &= ~IP_ECN_MASK; | |
7968 | } | |
7969 | ||
d23df9a8 | 7970 | if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key, |
dbf4a928 | 7971 | ovs_key_ipv4_offsetof_sizeof_arr, odp_actions)) { |
3cea18ec | 7972 | put_ipv4_key(&base, base_flow, false); |
e8bf7774 IM |
7973 | or_masks(&mask, &orig_mask, ovs_key_ipv4_offsetof_sizeof_arr); |
7974 | put_ipv4_key(&mask, &wc->masks, true); | |
d23df9a8 | 7975 | } |
5bbda0aa EJ |
7976 | } |
7977 | ||
c4f2731d | 7978 | static void |
3cea18ec | 7979 | get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask) |
c4f2731d | 7980 | { |
932c96b7 JR |
7981 | ipv6->ipv6_src = flow->ipv6_src; |
7982 | ipv6->ipv6_dst = flow->ipv6_dst; | |
d23df9a8 JR |
7983 | ipv6->ipv6_label = flow->ipv6_label; |
7984 | ipv6->ipv6_proto = flow->nw_proto; | |
7985 | ipv6->ipv6_tclass = flow->nw_tos; | |
7986 | ipv6->ipv6_hlimit = flow->nw_ttl; | |
3cea18ec | 7987 | ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask); |
d23df9a8 | 7988 | } |
c4f2731d | 7989 | |
d23df9a8 | 7990 | static void |
3cea18ec | 7991 | put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask) |
d23df9a8 | 7992 | { |
932c96b7 JR |
7993 | flow->ipv6_src = ipv6->ipv6_src; |
7994 | flow->ipv6_dst = ipv6->ipv6_dst; | |
d23df9a8 JR |
7995 | flow->ipv6_label = ipv6->ipv6_label; |
7996 | flow->nw_proto = ipv6->ipv6_proto; | |
7997 | flow->nw_tos = ipv6->ipv6_tclass; | |
7998 | flow->nw_ttl = ipv6->ipv6_hlimit; | |
3cea18ec | 7999 | flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask); |
d23df9a8 | 8000 | } |
c4f2731d | 8001 | |
d23df9a8 JR |
8002 | static void |
8003 | commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow, | |
8004 | struct ofpbuf *odp_actions, struct flow_wildcards *wc, | |
8005 | bool use_masked) | |
8006 | { | |
e8bf7774 | 8007 | struct ovs_key_ipv6 key, mask, orig_mask, base; |
dbf4a928 EB |
8008 | struct offsetof_sizeof ovs_key_ipv6_offsetof_sizeof_arr[] = |
8009 | OVS_KEY_IPV6_OFFSETOF_SIZEOF_ARR; | |
1dd35f8a | 8010 | |
d23df9a8 JR |
8011 | /* Check that nw_proto and nw_frag remain unchanged. */ |
8012 | ovs_assert(flow->nw_proto == base_flow->nw_proto && | |
8013 | flow->nw_frag == base_flow->nw_frag); | |
c4f2731d | 8014 | |
3cea18ec JR |
8015 | get_ipv6_key(flow, &key, false); |
8016 | get_ipv6_key(base_flow, &base, false); | |
8017 | get_ipv6_key(&wc->masks, &mask, true); | |
e8bf7774 | 8018 | memcpy(&orig_mask, &mask, sizeof mask); |
d23df9a8 JR |
8019 | mask.ipv6_proto = 0; /* Not writeable. */ |
8020 | mask.ipv6_frag = 0; /* Not writable. */ | |
f8977154 | 8021 | mask.ipv6_label &= htonl(IPV6_LABEL_MASK); /* Not writable. */ |
c4f2731d | 8022 | |
b51d0390 JL |
8023 | if (flow_tnl_dst_is_set(&base_flow->tunnel) && |
8024 | ((base_flow->nw_tos ^ flow->nw_tos) & IP_ECN_MASK) == 0) { | |
8025 | mask.ipv6_tclass &= ~IP_ECN_MASK; | |
8026 | } | |
8027 | ||
d23df9a8 | 8028 | if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key, |
dbf4a928 | 8029 | ovs_key_ipv6_offsetof_sizeof_arr, odp_actions)) { |
3cea18ec | 8030 | put_ipv6_key(&base, base_flow, false); |
e8bf7774 IM |
8031 | or_masks(&mask, &orig_mask, ovs_key_ipv6_offsetof_sizeof_arr); |
8032 | put_ipv6_key(&mask, &wc->masks, true); | |
d23df9a8 | 8033 | } |
c4f2731d PS |
8034 | } |
8035 | ||
d23df9a8 JR |
8036 | static void |
8037 | get_arp_key(const struct flow *flow, struct ovs_key_arp *arp) | |
f6c8a6b1 | 8038 | { |
d23df9a8 JR |
8039 | /* ARP key has padding, clear it. */ |
8040 | memset(arp, 0, sizeof *arp); | |
f6c8a6b1 | 8041 | |
d23df9a8 JR |
8042 | arp->arp_sip = flow->nw_src; |
8043 | arp->arp_tip = flow->nw_dst; | |
a3db6e47 TZ |
8044 | arp->arp_op = flow->nw_proto == UINT8_MAX ? |
8045 | OVS_BE16_MAX : htons(flow->nw_proto); | |
74ff3298 JR |
8046 | arp->arp_sha = flow->arp_sha; |
8047 | arp->arp_tha = flow->arp_tha; | |
d23df9a8 | 8048 | } |
f6c8a6b1 | 8049 | |
d23df9a8 JR |
8050 | static void |
8051 | put_arp_key(const struct ovs_key_arp *arp, struct flow *flow) | |
8052 | { | |
8053 | flow->nw_src = arp->arp_sip; | |
8054 | flow->nw_dst = arp->arp_tip; | |
8055 | flow->nw_proto = ntohs(arp->arp_op); | |
74ff3298 JR |
8056 | flow->arp_sha = arp->arp_sha; |
8057 | flow->arp_tha = arp->arp_tha; | |
d23df9a8 | 8058 | } |
f6c8a6b1 | 8059 | |
d23df9a8 JR |
8060 | static enum slow_path_reason |
8061 | commit_set_arp_action(const struct flow *flow, struct flow *base_flow, | |
8062 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) | |
8063 | { | |
e8bf7774 | 8064 | struct ovs_key_arp key, mask, orig_mask, base; |
dbf4a928 EB |
8065 | struct offsetof_sizeof ovs_key_arp_offsetof_sizeof_arr[] = |
8066 | OVS_KEY_ARP_OFFSETOF_SIZEOF_ARR; | |
f6c8a6b1 | 8067 | |
d23df9a8 JR |
8068 | get_arp_key(flow, &key); |
8069 | get_arp_key(base_flow, &base); | |
8070 | get_arp_key(&wc->masks, &mask); | |
e8bf7774 | 8071 | memcpy(&orig_mask, &mask, sizeof mask); |
f6c8a6b1 | 8072 | |
d23df9a8 | 8073 | if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key, |
dbf4a928 | 8074 | ovs_key_arp_offsetof_sizeof_arr, odp_actions)) { |
d23df9a8 | 8075 | put_arp_key(&base, base_flow); |
e8bf7774 | 8076 | or_masks(&mask, &orig_mask, ovs_key_arp_offsetof_sizeof_arr); |
d23df9a8 JR |
8077 | put_arp_key(&mask, &wc->masks); |
8078 | return SLOW_ACTION; | |
8079 | } | |
8080 | return 0; | |
f6c8a6b1 BP |
8081 | } |
8082 | ||
f6ecf944 JP |
8083 | static void |
8084 | get_icmp_key(const struct flow *flow, struct ovs_key_icmp *icmp) | |
8085 | { | |
8086 | /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */ | |
8087 | icmp->icmp_type = ntohs(flow->tp_src); | |
8088 | icmp->icmp_code = ntohs(flow->tp_dst); | |
8089 | } | |
8090 | ||
8091 | static void | |
8092 | put_icmp_key(const struct ovs_key_icmp *icmp, struct flow *flow) | |
8093 | { | |
8094 | /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */ | |
8095 | flow->tp_src = htons(icmp->icmp_type); | |
8096 | flow->tp_dst = htons(icmp->icmp_code); | |
8097 | } | |
8098 | ||
8099 | static enum slow_path_reason | |
8100 | commit_set_icmp_action(const struct flow *flow, struct flow *base_flow, | |
8101 | struct ofpbuf *odp_actions, struct flow_wildcards *wc) | |
8102 | { | |
e8bf7774 | 8103 | struct ovs_key_icmp key, mask, orig_mask, base; |
dbf4a928 EB |
8104 | struct offsetof_sizeof ovs_key_icmp_offsetof_sizeof_arr[] = |
8105 | OVS_KEY_ICMP_OFFSETOF_SIZEOF_ARR; | |
f6ecf944 JP |
8106 | enum ovs_key_attr attr; |
8107 | ||
a75636c8 | 8108 | if (is_icmpv4(flow, NULL)) { |
efa6665e | 8109 | attr = OVS_KEY_ATTR_ICMP; |
a75636c8 | 8110 | } else if (is_icmpv6(flow, NULL)) { |
efa6665e DDP |
8111 | attr = OVS_KEY_ATTR_ICMPV6; |
8112 | } else { | |
8113 | return 0; | |
8114 | } | |
8115 | ||
f6ecf944 JP |
8116 | get_icmp_key(flow, &key); |
8117 | get_icmp_key(base_flow, &base); | |
8118 | get_icmp_key(&wc->masks, &mask); | |
e8bf7774 | 8119 | memcpy(&orig_mask, &mask, sizeof mask); |
f6ecf944 | 8120 | |
dbf4a928 EB |
8121 | if (commit(attr, false, &key, &base, &mask, sizeof key, |
8122 | ovs_key_icmp_offsetof_sizeof_arr, odp_actions)) { | |
f6ecf944 | 8123 | put_icmp_key(&base, base_flow); |
e8bf7774 | 8124 | or_masks(&mask, &orig_mask, ovs_key_icmp_offsetof_sizeof_arr); |
f6ecf944 JP |
8125 | put_icmp_key(&mask, &wc->masks); |
8126 | return SLOW_ACTION; | |
8127 | } | |
8128 | return 0; | |
8129 | } | |
8130 | ||
e60e935b SRCSA |
8131 | static void |
8132 | get_nd_key(const struct flow *flow, struct ovs_key_nd *nd) | |
8133 | { | |
932c96b7 | 8134 | nd->nd_target = flow->nd_target; |
e60e935b | 8135 | /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */ |
74ff3298 JR |
8136 | nd->nd_sll = flow->arp_sha; |
8137 | nd->nd_tll = flow->arp_tha; | |
e60e935b SRCSA |
8138 | } |
8139 | ||
8140 | static void | |
8141 | put_nd_key(const struct ovs_key_nd *nd, struct flow *flow) | |
8142 | { | |
932c96b7 | 8143 | flow->nd_target = nd->nd_target; |
e60e935b | 8144 | /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */ |
74ff3298 JR |
8145 | flow->arp_sha = nd->nd_sll; |
8146 | flow->arp_tha = nd->nd_tll; | |
e60e935b SRCSA |
8147 | } |
8148 | ||
9b2b8497 VDA |
8149 | static void |
8150 | get_nd_extensions_key(const struct flow *flow, | |
8151 | struct ovs_key_nd_extensions *nd_ext) | |
8152 | { | |
8153 | /* ND Extensions key has padding, clear it. */ | |
8154 | memset(nd_ext, 0, sizeof *nd_ext); | |
8155 | nd_ext->nd_reserved = flow->igmp_group_ip4; | |
8156 | nd_ext->nd_options_type = ntohs(flow->tcp_flags); | |
8157 | } | |
8158 | ||
8159 | static void | |
8160 | put_nd_extensions_key(const struct ovs_key_nd_extensions *nd_ext, | |
8161 | struct flow *flow) | |
8162 | { | |
8163 | flow->igmp_group_ip4 = nd_ext->nd_reserved; | |
8164 | flow->tcp_flags = htons(nd_ext->nd_options_type); | |
8165 | } | |
8166 | ||
e60e935b SRCSA |
8167 | static enum slow_path_reason |
8168 | commit_set_nd_action(const struct flow *flow, struct flow *base_flow, | |
8169 | struct ofpbuf *odp_actions, | |
8170 | struct flow_wildcards *wc, bool use_masked) | |
8171 | { | |
e8bf7774 | 8172 | struct ovs_key_nd key, mask, orig_mask, base; |
dbf4a928 EB |
8173 | struct offsetof_sizeof ovs_key_nd_offsetof_sizeof_arr[] = |
8174 | OVS_KEY_ND_OFFSETOF_SIZEOF_ARR; | |
e60e935b SRCSA |
8175 | |
8176 | get_nd_key(flow, &key); | |
8177 | get_nd_key(base_flow, &base); | |
8178 | get_nd_key(&wc->masks, &mask); | |
e8bf7774 | 8179 | memcpy(&orig_mask, &mask, sizeof mask); |
e60e935b SRCSA |
8180 | |
8181 | if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key, | |
dbf4a928 | 8182 | ovs_key_nd_offsetof_sizeof_arr, odp_actions)) { |
e60e935b | 8183 | put_nd_key(&base, base_flow); |
e8bf7774 | 8184 | or_masks(&mask, &orig_mask, ovs_key_nd_offsetof_sizeof_arr); |
e60e935b SRCSA |
8185 | put_nd_key(&mask, &wc->masks); |
8186 | return SLOW_ACTION; | |
8187 | } | |
8188 | ||
8189 | return 0; | |
8190 | } | |
8191 | ||
9b2b8497 VDA |
8192 | static enum slow_path_reason |
8193 | commit_set_nd_extensions_action(const struct flow *flow, | |
8194 | struct flow *base_flow, | |
8195 | struct ofpbuf *odp_actions, | |
8196 | struct flow_wildcards *wc, bool use_masked) | |
8197 | { | |
e8bf7774 | 8198 | struct ovs_key_nd_extensions key, mask, orig_mask, base; |
dbf4a928 EB |
8199 | struct offsetof_sizeof ovs_key_nd_extensions_offsetof_sizeof_arr[] = |
8200 | OVS_KEY_ND_EXTENSIONS_OFFSETOF_SIZEOF_ARR; | |
9b2b8497 VDA |
8201 | |
8202 | get_nd_extensions_key(flow, &key); | |
8203 | get_nd_extensions_key(base_flow, &base); | |
8204 | get_nd_extensions_key(&wc->masks, &mask); | |
e8bf7774 | 8205 | memcpy(&orig_mask, &mask, sizeof mask); |
9b2b8497 | 8206 | |
dbf4a928 EB |
8207 | if (commit(OVS_KEY_ATTR_ND_EXTENSIONS, use_masked, &key, &base, &mask, |
8208 | sizeof key, ovs_key_nd_extensions_offsetof_sizeof_arr, | |
8209 | odp_actions)) { | |
9b2b8497 | 8210 | put_nd_extensions_key(&base, base_flow); |
e8bf7774 | 8211 | or_masks(&mask, &orig_mask, ovs_key_nd_extensions_offsetof_sizeof_arr); |
9b2b8497 VDA |
8212 | put_nd_extensions_key(&mask, &wc->masks); |
8213 | return SLOW_ACTION; | |
8214 | } | |
8215 | return 0; | |
8216 | } | |
8217 | ||
f6c8a6b1 | 8218 | static enum slow_path_reason |
c4f2731d | 8219 | commit_set_nw_action(const struct flow *flow, struct flow *base, |
d23df9a8 JR |
8220 | struct ofpbuf *odp_actions, struct flow_wildcards *wc, |
8221 | bool use_masked) | |
c4f2731d | 8222 | { |
9b2b8497 VDA |
8223 | uint32_t reason; |
8224 | ||
f6c8a6b1 | 8225 | /* Check if 'flow' really has an L3 header. */ |
c4f2731d | 8226 | if (!flow->nw_proto) { |
f6c8a6b1 | 8227 | return 0; |
c4f2731d PS |
8228 | } |
8229 | ||
f6c8a6b1 BP |
8230 | switch (ntohs(base->dl_type)) { |
8231 | case ETH_TYPE_IP: | |
d23df9a8 | 8232 | commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked); |
f6c8a6b1 BP |
8233 | break; |
8234 | ||
8235 | case ETH_TYPE_IPV6: | |
d23df9a8 | 8236 | commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked); |
9b2b8497 VDA |
8237 | if (base->nw_proto == IPPROTO_ICMPV6) { |
8238 | /* Commit extended attrs first to make sure | |
8239 | correct options are added.*/ | |
8240 | reason = commit_set_nd_extensions_action(flow, base, | |
8241 | odp_actions, wc, use_masked); | |
8242 | reason |= commit_set_nd_action(flow, base, odp_actions, | |
8243 | wc, use_masked); | |
8244 | return reason; | |
8245 | } | |
8246 | break; | |
f6c8a6b1 BP |
8247 | |
8248 | case ETH_TYPE_ARP: | |
8249 | return commit_set_arp_action(flow, base, odp_actions, wc); | |
c4f2731d | 8250 | } |
f6c8a6b1 BP |
8251 | |
8252 | return 0; | |
c4f2731d PS |
8253 | } |
8254 | ||
17553f27 | 8255 | static inline void |
3d2fbd70 JS |
8256 | get_nsh_key(const struct flow *flow, struct ovs_key_nsh *nsh, bool is_mask) |
8257 | { | |
17553f27 YY |
8258 | *nsh = flow->nsh; |
8259 | if (!is_mask) { | |
8260 | if (nsh->mdtype != NSH_M_TYPE1) { | |
8261 | memset(nsh->context, 0, sizeof(nsh->context)); | |
3d2fbd70 JS |
8262 | } |
8263 | } | |
8264 | } | |
8265 | ||
17553f27 | 8266 | static inline void |
3d2fbd70 JS |
8267 | put_nsh_key(const struct ovs_key_nsh *nsh, struct flow *flow, |
8268 | bool is_mask OVS_UNUSED) | |
8269 | { | |
17553f27 YY |
8270 | flow->nsh = *nsh; |
8271 | if (flow->nsh.mdtype != NSH_M_TYPE1) { | |
8272 | memset(flow->nsh.context, 0, sizeof(flow->nsh.context)); | |
3d2fbd70 JS |
8273 | } |
8274 | } | |
8275 | ||
f59cb331 | 8276 | static bool |
17553f27 | 8277 | commit_nsh(const struct ovs_key_nsh * flow_nsh, bool use_masked_set, |
f59cb331 YY |
8278 | const struct ovs_key_nsh *key, struct ovs_key_nsh *base, |
8279 | struct ovs_key_nsh *mask, size_t size, | |
8280 | struct ofpbuf *odp_actions) | |
8281 | { | |
8282 | enum ovs_key_attr attr = OVS_KEY_ATTR_NSH; | |
8283 | ||
8284 | if (memcmp(key, base, size) == 0) { | |
8285 | /* Mask bits are set when we have either read or set the corresponding | |
8286 | * values. Masked bits will be exact-matched, no need to set them | |
8287 | * if the value did not actually change. */ | |
8288 | return false; | |
8289 | } | |
8290 | ||
8291 | bool fully_masked = odp_mask_is_exact(attr, mask, size); | |
8292 | ||
8293 | if (use_masked_set && !fully_masked) { | |
8294 | size_t nsh_key_ofs; | |
8295 | struct ovs_nsh_key_base nsh_base; | |
8296 | struct ovs_nsh_key_base nsh_base_mask; | |
8297 | struct ovs_nsh_key_md1 md1; | |
8298 | struct ovs_nsh_key_md1 md1_mask; | |
8299 | size_t offset = nl_msg_start_nested(odp_actions, | |
8300 | OVS_ACTION_ATTR_SET_MASKED); | |
8301 | ||
8302 | nsh_base.flags = key->flags; | |
17553f27 | 8303 | nsh_base.ttl = key->ttl; |
f59cb331 YY |
8304 | nsh_base.mdtype = key->mdtype; |
8305 | nsh_base.np = key->np; | |
8306 | nsh_base.path_hdr = key->path_hdr; | |
8307 | ||
8308 | nsh_base_mask.flags = mask->flags; | |
17553f27 | 8309 | nsh_base_mask.ttl = mask->ttl; |
f59cb331 YY |
8310 | nsh_base_mask.mdtype = mask->mdtype; |
8311 | nsh_base_mask.np = mask->np; | |
8312 | nsh_base_mask.path_hdr = mask->path_hdr; | |
8313 | ||
8314 | /* OVS_KEY_ATTR_NSH keys */ | |
8315 | nsh_key_ofs = nl_msg_start_nested(odp_actions, OVS_KEY_ATTR_NSH); | |
8316 | ||
81fdabb9 | 8317 | /* put value and mask for OVS_NSH_KEY_ATTR_BASE */ |
f59cb331 YY |
8318 | char *data = nl_msg_put_unspec_uninit(odp_actions, |
8319 | OVS_NSH_KEY_ATTR_BASE, | |
81fdabb9 | 8320 | 2 * sizeof(nsh_base)); |
f59cb331 YY |
8321 | const char *lkey = (char *)&nsh_base, *lmask = (char *)&nsh_base_mask; |
8322 | size_t lkey_size = sizeof(nsh_base); | |
8323 | ||
8324 | while (lkey_size--) { | |
8325 | *data++ = *lkey++ & *lmask++; | |
8326 | } | |
81fdabb9 YY |
8327 | lmask = (char *)&nsh_base_mask; |
8328 | memcpy(data, lmask, sizeof(nsh_base_mask)); | |
f59cb331 YY |
8329 | |
8330 | switch (key->mdtype) { | |
8331 | case NSH_M_TYPE1: | |
8332 | memcpy(md1.context, key->context, sizeof key->context); | |
8333 | memcpy(md1_mask.context, mask->context, sizeof mask->context); | |
8334 | ||
81fdabb9 | 8335 | /* put value and mask for OVS_NSH_KEY_ATTR_MD1 */ |
f59cb331 YY |
8336 | data = nl_msg_put_unspec_uninit(odp_actions, |
8337 | OVS_NSH_KEY_ATTR_MD1, | |
81fdabb9 | 8338 | 2 * sizeof(md1)); |
f59cb331 YY |
8339 | lkey = (char *)&md1; |
8340 | lmask = (char *)&md1_mask; | |
8341 | lkey_size = sizeof(md1); | |
8342 | ||
8343 | while (lkey_size--) { | |
8344 | *data++ = *lkey++ & *lmask++; | |
8345 | } | |
f59cb331 YY |
8346 | lmask = (char *)&md1_mask; |
8347 | memcpy(data, lmask, sizeof(md1_mask)); | |
8348 | break; | |
8349 | case NSH_M_TYPE2: | |
8350 | default: | |
8351 | /* No match support for other MD formats yet. */ | |
8352 | break; | |
8353 | } | |
81fdabb9 | 8354 | |
f59cb331 YY |
8355 | nl_msg_end_nested(odp_actions, nsh_key_ofs); |
8356 | ||
8357 | nl_msg_end_nested(odp_actions, offset); | |
8358 | } else { | |
8359 | if (!fully_masked) { | |
8360 | memset(mask, 0xff, size); | |
8361 | } | |
8362 | size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET); | |
8363 | nsh_key_to_attr(odp_actions, flow_nsh, NULL, 0, false); | |
8364 | nl_msg_end_nested(odp_actions, offset); | |
8365 | } | |
8366 | memcpy(base, key, size); | |
8367 | return true; | |
8368 | } | |
8369 | ||
3d2fbd70 JS |
8370 | static void |
8371 | commit_set_nsh_action(const struct flow *flow, struct flow *base_flow, | |
8372 | struct ofpbuf *odp_actions, | |
8373 | struct flow_wildcards *wc, | |
8374 | bool use_masked) | |
8375 | { | |
8376 | struct ovs_key_nsh key, mask, base; | |
8377 | ||
8378 | if (flow->dl_type != htons(ETH_TYPE_NSH) || | |
8379 | !memcmp(&base_flow->nsh, &flow->nsh, sizeof base_flow->nsh)) { | |
8380 | return; | |
8381 | } | |
8382 | ||
8383 | /* Check that mdtype and np remain unchanged. */ | |
8384 | ovs_assert(flow->nsh.mdtype == base_flow->nsh.mdtype && | |
8385 | flow->nsh.np == base_flow->nsh.np); | |
8386 | ||
8387 | get_nsh_key(flow, &key, false); | |
8388 | get_nsh_key(base_flow, &base, false); | |
8389 | get_nsh_key(&wc->masks, &mask, true); | |
8390 | mask.mdtype = 0; /* Not writable. */ | |
8391 | mask.np = 0; /* Not writable. */ | |
8392 | ||
f59cb331 YY |
8393 | if (commit_nsh(&base_flow->nsh, use_masked, &key, &base, &mask, |
8394 | sizeof key, odp_actions)) { | |
3d2fbd70 JS |
8395 | put_nsh_key(&base, base_flow, false); |
8396 | if (mask.mdtype != 0) { /* Mask was changed by commit(). */ | |
8397 | put_nsh_key(&mask, &wc->masks, true); | |
8398 | } | |
8399 | } | |
8400 | } | |
8401 | ||
d23df9a8 JR |
8402 | /* TCP, UDP, and SCTP keys have the same layout. */ |
8403 | BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) && | |
8404 | sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp)); | |
8405 | ||
5bbda0aa | 8406 | static void |
3cea18ec | 8407 | get_tp_key(const struct flow *flow, union ovs_key_tp *tp) |
5bbda0aa | 8408 | { |
3cea18ec JR |
8409 | tp->tcp.tcp_src = flow->tp_src; |
8410 | tp->tcp.tcp_dst = flow->tp_dst; | |
d23df9a8 JR |
8411 | } |
8412 | ||
8413 | static void | |
3cea18ec | 8414 | put_tp_key(const union ovs_key_tp *tp, struct flow *flow) |
d23df9a8 | 8415 | { |
3cea18ec JR |
8416 | flow->tp_src = tp->tcp.tcp_src; |
8417 | flow->tp_dst = tp->tcp.tcp_dst; | |
d23df9a8 JR |
8418 | } |
8419 | ||
8420 | static void | |
8421 | commit_set_port_action(const struct flow *flow, struct flow *base_flow, | |
8422 | struct ofpbuf *odp_actions, struct flow_wildcards *wc, | |
8423 | bool use_masked) | |
8424 | { | |
8425 | enum ovs_key_attr key_type; | |
e8bf7774 | 8426 | union ovs_key_tp key, mask, orig_mask, base; |
dbf4a928 EB |
8427 | struct offsetof_sizeof ovs_key_tp_offsetof_sizeof_arr[] = |
8428 | OVS_KEY_TCP_OFFSETOF_SIZEOF_ARR; | |
d23df9a8 | 8429 | |
791a09be SH |
8430 | /* Check if 'flow' really has an L3 header. */ |
8431 | if (!flow->nw_proto) { | |
8432 | return; | |
8433 | } | |
8434 | ||
d23df9a8 | 8435 | if (!is_ip_any(base_flow)) { |
5bbda0aa EJ |
8436 | return; |
8437 | } | |
8438 | ||
5bbda0aa | 8439 | if (flow->nw_proto == IPPROTO_TCP) { |
d23df9a8 | 8440 | key_type = OVS_KEY_ATTR_TCP; |
5bbda0aa | 8441 | } else if (flow->nw_proto == IPPROTO_UDP) { |
d23df9a8 | 8442 | key_type = OVS_KEY_ATTR_UDP; |
c6bcb685 | 8443 | } else if (flow->nw_proto == IPPROTO_SCTP) { |
d23df9a8 JR |
8444 | key_type = OVS_KEY_ATTR_SCTP; |
8445 | } else { | |
8446 | return; | |
8447 | } | |
c6bcb685 | 8448 | |
d23df9a8 JR |
8449 | get_tp_key(flow, &key); |
8450 | get_tp_key(base_flow, &base); | |
8451 | get_tp_key(&wc->masks, &mask); | |
e8bf7774 | 8452 | memcpy(&orig_mask, &mask, sizeof mask); |
c6bcb685 | 8453 | |
d23df9a8 | 8454 | if (commit(key_type, use_masked, &key, &base, &mask, sizeof key, |
dbf4a928 | 8455 | ovs_key_tp_offsetof_sizeof_arr, odp_actions)) { |
d23df9a8 | 8456 | put_tp_key(&base, base_flow); |
e8bf7774 | 8457 | or_masks(&mask, &orig_mask, ovs_key_tp_offsetof_sizeof_arr); |
d23df9a8 | 8458 | put_tp_key(&mask, &wc->masks); |
5bbda0aa EJ |
8459 | } |
8460 | } | |
8461 | ||
8462 | static void | |
d23df9a8 | 8463 | commit_set_priority_action(const struct flow *flow, struct flow *base_flow, |
1dd35f8a | 8464 | struct ofpbuf *odp_actions, |
d23df9a8 JR |
8465 | struct flow_wildcards *wc, |
8466 | bool use_masked) | |
5bbda0aa | 8467 | { |
d23df9a8 | 8468 | uint32_t key, mask, base; |
dbf4a928 EB |
8469 | struct offsetof_sizeof ovs_key_prio_offsetof_sizeof_arr[] = { |
8470 | {0, sizeof(uint32_t)}, | |
8471 | {0, 0} | |
8472 | }; | |
1dd35f8a | 8473 | |
d23df9a8 JR |
8474 | key = flow->skb_priority; |
8475 | base = base_flow->skb_priority; | |
8476 | mask = wc->masks.skb_priority; | |
5bbda0aa | 8477 | |
d23df9a8 | 8478 | if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask, |
dbf4a928 | 8479 | sizeof key, ovs_key_prio_offsetof_sizeof_arr, odp_actions)) { |
d23df9a8 | 8480 | base_flow->skb_priority = base; |
e8bf7774 | 8481 | wc->masks.skb_priority |= mask; |
d23df9a8 | 8482 | } |
5bbda0aa EJ |
8483 | } |
8484 | ||
72e8bf28 | 8485 | static void |
d23df9a8 | 8486 | commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow, |
1dd35f8a | 8487 | struct ofpbuf *odp_actions, |
d23df9a8 JR |
8488 | struct flow_wildcards *wc, |
8489 | bool use_masked) | |
72e8bf28 | 8490 | { |
d23df9a8 | 8491 | uint32_t key, mask, base; |
dbf4a928 EB |
8492 | struct offsetof_sizeof ovs_key_pkt_mark_offsetof_sizeof_arr[] = { |
8493 | {0, sizeof(uint32_t)}, | |
8494 | {0, 0} | |
8495 | }; | |
1dd35f8a | 8496 | |
d23df9a8 JR |
8497 | key = flow->pkt_mark; |
8498 | base = base_flow->pkt_mark; | |
8499 | mask = wc->masks.pkt_mark; | |
72e8bf28 | 8500 | |
d23df9a8 | 8501 | if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask, |
dbf4a928 EB |
8502 | sizeof key, ovs_key_pkt_mark_offsetof_sizeof_arr, |
8503 | odp_actions)) { | |
d23df9a8 | 8504 | base_flow->pkt_mark = base; |
e8bf7774 | 8505 | wc->masks.pkt_mark |= mask; |
d23df9a8 | 8506 | } |
72e8bf28 | 8507 | } |
7fd91025 | 8508 | |
1fc11c59 | 8509 | static void |
f59cb331 | 8510 | odp_put_pop_nsh_action(struct ofpbuf *odp_actions) |
1fc11c59 | 8511 | { |
f59cb331 | 8512 | nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_NSH); |
1fc11c59 JS |
8513 | } |
8514 | ||
8515 | static void | |
f59cb331 | 8516 | odp_put_push_nsh_action(struct ofpbuf *odp_actions, |
1fc11c59 JS |
8517 | const struct flow *flow, |
8518 | struct ofpbuf *encap_data) | |
8519 | { | |
f59cb331 YY |
8520 | uint8_t * metadata = NULL; |
8521 | uint8_t md_size = 0; | |
1fc11c59 | 8522 | |
f59cb331 | 8523 | switch (flow->nsh.mdtype) { |
1fc11c59 JS |
8524 | case NSH_M_TYPE2: |
8525 | if (encap_data) { | |
f59cb331 YY |
8526 | ovs_assert(encap_data->size < NSH_CTX_HDRS_MAX_LEN); |
8527 | metadata = encap_data->data; | |
8528 | md_size = encap_data->size; | |
1fc11c59 | 8529 | } else { |
f59cb331 | 8530 | md_size = 0; |
1fc11c59 JS |
8531 | } |
8532 | break; | |
8533 | default: | |
f59cb331 | 8534 | md_size = 0; |
1fc11c59 JS |
8535 | break; |
8536 | } | |
f59cb331 YY |
8537 | size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_PUSH_NSH); |
8538 | nsh_key_to_attr(odp_actions, &flow->nsh, metadata, md_size, false); | |
8539 | nl_msg_end_nested(odp_actions, offset); | |
1fc11c59 JS |
8540 | } |
8541 | ||
f839892a | 8542 | static void |
88ec1e0a | 8543 | commit_encap_decap_action(const struct flow *flow, |
f839892a JS |
8544 | struct flow *base_flow, |
8545 | struct ofpbuf *odp_actions, | |
8546 | struct flow_wildcards *wc, | |
88ec1e0a | 8547 | bool pending_encap, bool pending_decap, |
1fc11c59 | 8548 | struct ofpbuf *encap_data) |
f839892a | 8549 | { |
f839892a JS |
8550 | if (pending_encap) { |
8551 | switch (ntohl(flow->packet_type)) { | |
8552 | case PT_ETH: { | |
8553 | /* push_eth */ | |
8554 | odp_put_push_eth_action(odp_actions, &flow->dl_src, | |
8555 | &flow->dl_dst); | |
8556 | base_flow->packet_type = flow->packet_type; | |
8557 | base_flow->dl_src = flow->dl_src; | |
8558 | base_flow->dl_dst = flow->dl_dst; | |
8559 | break; | |
8560 | } | |
1fc11c59 | 8561 | case PT_NSH: |
f59cb331 YY |
8562 | /* push_nsh */ |
8563 | odp_put_push_nsh_action(odp_actions, flow, encap_data); | |
1fc11c59 JS |
8564 | base_flow->packet_type = flow->packet_type; |
8565 | /* Update all packet headers in base_flow. */ | |
8566 | memcpy(&base_flow->dl_dst, &flow->dl_dst, | |
8567 | sizeof(*flow) - offsetof(struct flow, dl_dst)); | |
8568 | break; | |
f839892a | 8569 | default: |
1fc11c59 JS |
8570 | /* Only the above protocols are supported for encap. |
8571 | * The check is done at action translation. */ | |
f839892a JS |
8572 | OVS_NOT_REACHED(); |
8573 | } | |
88ec1e0a | 8574 | } else if (pending_decap || flow->packet_type != base_flow->packet_type) { |
1fc11c59 | 8575 | /* This is an explicit or implicit decap case. */ |
f839892a JS |
8576 | if (pt_ns(flow->packet_type) == OFPHTN_ETHERTYPE && |
8577 | base_flow->packet_type == htonl(PT_ETH)) { | |
1fc11c59 | 8578 | /* Generate pop_eth and continue without recirculation. */ |
f839892a JS |
8579 | odp_put_pop_eth_action(odp_actions); |
8580 | base_flow->packet_type = flow->packet_type; | |
8581 | base_flow->dl_src = eth_addr_zero; | |
8582 | base_flow->dl_dst = eth_addr_zero; | |
8583 | } else { | |
1fc11c59 JS |
8584 | /* All other decap cases require recirculation. |
8585 | * No need to update the base flow here. */ | |
8586 | switch (ntohl(base_flow->packet_type)) { | |
8587 | case PT_NSH: | |
f59cb331 YY |
8588 | /* pop_nsh. */ |
8589 | odp_put_pop_nsh_action(odp_actions); | |
1fc11c59 JS |
8590 | break; |
8591 | default: | |
8592 | /* Checks are done during translation. */ | |
8593 | OVS_NOT_REACHED(); | |
8594 | } | |
f839892a JS |
8595 | } |
8596 | } | |
8597 | ||
8598 | wc->masks.packet_type = OVS_BE32_MAX; | |
8599 | } | |
8600 | ||
5bbda0aa EJ |
8601 | /* If any of the flow key data that ODP actions can modify are different in |
8602 | * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow | |
b9ad7294 EJ |
8603 | * key from 'base' into 'flow', and then changes 'base' the same way. Does not |
8604 | * commit set_tunnel actions. Users should call commit_odp_tunnel_action() | |
1dd35f8a | 8605 | * in addition to this function if needed. Sets fields in 'wc' that are |
7fd91025 BP |
8606 | * used as part of the action. |
8607 | * | |
005bb872 BP |
8608 | * In the common case, this function returns 0. If the flow key modification |
8609 | * requires the flow's packets to be forced into the userspace slow path, this | |
8610 | * function returns SLOW_ACTION. This only happens when there is no ODP action | |
8611 | * to modify some field that was actually modified. For example, there is no | |
8612 | * ODP action to modify any ARP field, so such a modification triggers | |
8613 | * SLOW_ACTION. (When this happens, packets that need such modification get | |
8614 | * flushed to userspace and handled there, which works OK but much more slowly | |
8615 | * than if the datapath handled it directly.) */ | |
7fd91025 | 8616 | enum slow_path_reason |
5bbda0aa | 8617 | commit_odp_actions(const struct flow *flow, struct flow *base, |
d23df9a8 | 8618 | struct ofpbuf *odp_actions, struct flow_wildcards *wc, |
88ec1e0a | 8619 | bool use_masked, bool pending_encap, bool pending_decap, |
1fc11c59 | 8620 | struct ofpbuf *encap_data) |
5bbda0aa | 8621 | { |
005bb872 BP |
8622 | /* If you add a field that OpenFlow actions can change, and that is visible |
8623 | * to the datapath (including all data fields), then you should also add | |
8624 | * code here to commit changes to the field. */ | |
3c6d05a0 | 8625 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 42); |
005bb872 | 8626 | |
f6ecf944 | 8627 | enum slow_path_reason slow1, slow2; |
742c0ac3 | 8628 | bool mpls_done = false; |
f6c8a6b1 | 8629 | |
88ec1e0a JS |
8630 | commit_encap_decap_action(flow, base, odp_actions, wc, |
8631 | pending_encap, pending_decap, encap_data); | |
f839892a | 8632 | commit_set_ether_action(flow, base, odp_actions, wc, use_masked); |
742c0ac3 JR |
8633 | /* Make packet a non-MPLS packet before committing L3/4 actions, |
8634 | * which would otherwise do nothing. */ | |
8635 | if (eth_type_mpls(base->dl_type) && !eth_type_mpls(flow->dl_type)) { | |
8636 | commit_mpls_action(flow, base, odp_actions); | |
8637 | mpls_done = true; | |
8638 | } | |
3d2fbd70 | 8639 | commit_set_nsh_action(flow, base, odp_actions, wc, use_masked); |
f6ecf944 | 8640 | slow1 = commit_set_nw_action(flow, base, odp_actions, wc, use_masked); |
d23df9a8 | 8641 | commit_set_port_action(flow, base, odp_actions, wc, use_masked); |
f6ecf944 | 8642 | slow2 = commit_set_icmp_action(flow, base, odp_actions, wc); |
742c0ac3 JR |
8643 | if (!mpls_done) { |
8644 | commit_mpls_action(flow, base, odp_actions); | |
8645 | } | |
f0fb825a | 8646 | commit_vlan_action(flow, base, odp_actions, wc); |
d23df9a8 JR |
8647 | commit_set_priority_action(flow, base, odp_actions, wc, use_masked); |
8648 | commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked); | |
7fd91025 | 8649 | |
f6ecf944 | 8650 | return slow1 ? slow1 : slow2; |
5bbda0aa | 8651 | } |