]>
Commit | Line | Data |
---|---|---|
09246b99 | 1 | /* |
9f6e20b7 | 2 | * Copyright (c) 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc. |
09246b99 BP |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
18 | ||
19 | #include "nx-match.h" | |
20 | ||
685a51a5 JP |
21 | #include <netinet/icmp6.h> |
22 | ||
09246b99 | 23 | #include "classifier.h" |
b1c5bf1f | 24 | #include "colors.h" |
ee89ea7b | 25 | #include "openvswitch/hmap.h" |
b598f214 BW |
26 | #include "openflow/nicira-ext.h" |
27 | #include "openvswitch/dynamic-string.h" | |
064d7f84 | 28 | #include "openvswitch/meta-flow.h" |
b598f214 BW |
29 | #include "openvswitch/ofp-actions.h" |
30 | #include "openvswitch/ofp-errors.h" | |
f4248336 | 31 | #include "openvswitch/ofp-util.h" |
64c96779 | 32 | #include "openvswitch/ofpbuf.h" |
b598f214 | 33 | #include "openvswitch/vlog.h" |
09246b99 | 34 | #include "packets.h" |
ee89ea7b | 35 | #include "openvswitch/shash.h" |
9558d2a5 | 36 | #include "tun-metadata.h" |
09246b99 | 37 | #include "unaligned.h" |
ddc4f8e2 | 38 | #include "util.h" |
09246b99 BP |
39 | |
40 | VLOG_DEFINE_THIS_MODULE(nx_match); | |
41 | ||
508a9338 BP |
42 | /* OXM headers. |
43 | * | |
44 | * | |
45 | * Standard OXM/NXM | |
46 | * ================ | |
47 | * | |
48 | * The header is 32 bits long. It looks like this: | |
49 | * | |
50 | * |31 16 15 9| 8 7 0 | |
51 | * +----------------------------------+---------------+--+------------------+ | |
52 | * | oxm_class | oxm_field |hm| oxm_length | | |
53 | * +----------------------------------+---------------+--+------------------+ | |
54 | * | |
55 | * where hm stands for oxm_hasmask. It is followed by oxm_length bytes of | |
56 | * payload. When oxm_hasmask is 0, the payload is the value of the field | |
57 | * identified by the header; when oxm_hasmask is 1, the payload is a value for | |
58 | * the field followed by a mask of equal length. | |
59 | * | |
60 | * Internally, we represent a standard OXM header as a 64-bit integer with the | |
61 | * above information in the most-significant bits. | |
62 | * | |
63 | * | |
64 | * Experimenter OXM | |
65 | * ================ | |
66 | * | |
67 | * The header is 64 bits long. It looks like the diagram above except that a | |
68 | * 32-bit experimenter ID, which we call oxm_vendor and which identifies a | |
69 | * vendor, is inserted just before the payload. Experimenter OXMs are | |
70 | * identified by an all-1-bits oxm_class (OFPXMC12_EXPERIMENTER). The | |
71 | * oxm_length value *includes* the experimenter ID, so that the real payload is | |
72 | * only oxm_length - 4 bytes long. | |
73 | * | |
74 | * Internally, we represent an experimenter OXM header as a 64-bit integer with | |
75 | * the standard header in the upper 32 bits and the experimenter ID in the | |
76 | * lower 32 bits. (It would be more convenient to swap the positions of the | |
77 | * two 32-bit words, but this would be more error-prone because experimenter | |
78 | * OXMs are very rarely used, so accidentally passing one through a 32-bit type | |
79 | * somewhere in the OVS code would be hard to find.) | |
80 | */ | |
81 | ||
178742f9 BP |
82 | /* |
83 | * OXM Class IDs. | |
84 | * The high order bit differentiate reserved classes from member classes. | |
85 | * Classes 0x0000 to 0x7FFF are member classes, allocated by ONF. | |
86 | * Classes 0x8000 to 0xFFFE are reserved classes, reserved for standardisation. | |
87 | */ | |
88 | enum ofp12_oxm_class { | |
89 | OFPXMC12_NXM_0 = 0x0000, /* Backward compatibility with NXM */ | |
90 | OFPXMC12_NXM_1 = 0x0001, /* Backward compatibility with NXM */ | |
91 | OFPXMC12_OPENFLOW_BASIC = 0x8000, /* Basic class for OpenFlow */ | |
92 | OFPXMC15_PACKET_REGS = 0x8001, /* Packet registers (pipeline fields). */ | |
93 | OFPXMC12_EXPERIMENTER = 0xffff, /* Experimenter class */ | |
94 | }; | |
95 | ||
508a9338 BP |
96 | /* Functions for extracting raw field values from OXM/NXM headers. */ |
97 | static uint32_t nxm_vendor(uint64_t header) { return header; } | |
98 | static int nxm_class(uint64_t header) { return header >> 48; } | |
99 | static int nxm_field(uint64_t header) { return (header >> 41) & 0x7f; } | |
100 | static bool nxm_hasmask(uint64_t header) { return (header >> 40) & 1; } | |
101 | static int nxm_length(uint64_t header) { return (header >> 32) & 0xff; } | |
899bb63d | 102 | static uint64_t nxm_no_len(uint64_t header) { return header & 0xffffff80ffffffffULL; } |
508a9338 BP |
103 | |
104 | static bool | |
105 | is_experimenter_oxm(uint64_t header) | |
106 | { | |
107 | return nxm_class(header) == OFPXMC12_EXPERIMENTER; | |
108 | } | |
109 | ||
110 | /* The OXM header "length" field is somewhat tricky: | |
111 | * | |
112 | * - For a standard OXM header, the length is the number of bytes of the | |
113 | * payload, and the payload consists of just the value (and mask, if | |
114 | * present). | |
115 | * | |
116 | * - For an experimenter OXM header, the length is the number of bytes in | |
117 | * the payload plus 4 (the length of the experimenter ID). That is, the | |
118 | * experimenter ID is included in oxm_length. | |
119 | * | |
120 | * This function returns the length of the experimenter ID field in 'header'. | |
121 | * That is, for an experimenter OXM (when an experimenter ID is present), it | |
122 | * returns 4, and for a standard OXM (when no experimenter ID is present), it | |
123 | * returns 0. */ | |
124 | static int | |
125 | nxm_experimenter_len(uint64_t header) | |
126 | { | |
127 | return is_experimenter_oxm(header) ? 4 : 0; | |
128 | } | |
129 | ||
130 | /* Returns the number of bytes that follow the header for an NXM/OXM entry | |
131 | * with the given 'header'. */ | |
132 | static int | |
133 | nxm_payload_len(uint64_t header) | |
134 | { | |
135 | return nxm_length(header) - nxm_experimenter_len(header); | |
136 | } | |
137 | ||
138 | /* Returns the number of bytes in the header for an NXM/OXM entry with the | |
139 | * given 'header'. */ | |
140 | static int | |
141 | nxm_header_len(uint64_t header) | |
142 | { | |
143 | return 4 + nxm_experimenter_len(header); | |
144 | } | |
178742f9 | 145 | |
508a9338 BP |
146 | #define NXM_HEADER(VENDOR, CLASS, FIELD, HASMASK, LENGTH) \ |
147 | (((uint64_t) (CLASS) << 48) | \ | |
148 | ((uint64_t) (FIELD) << 41) | \ | |
149 | ((uint64_t) (HASMASK) << 40) | \ | |
150 | ((uint64_t) (LENGTH) << 32) | \ | |
151 | (VENDOR)) | |
178742f9 | 152 | |
508a9338 BP |
153 | #define NXM_HEADER_FMT "%#"PRIx32":%d:%d:%d:%d" |
154 | #define NXM_HEADER_ARGS(HEADER) \ | |
155 | nxm_vendor(HEADER), nxm_class(HEADER), nxm_field(HEADER), \ | |
178742f9 BP |
156 | nxm_hasmask(HEADER), nxm_length(HEADER) |
157 | ||
158 | /* Functions for turning the "hasmask" bit on or off. (This also requires | |
159 | * adjusting the length.) */ | |
508a9338 BP |
160 | static uint64_t |
161 | nxm_make_exact_header(uint64_t header) | |
178742f9 | 162 | { |
508a9338 BP |
163 | int new_len = nxm_payload_len(header) / 2 + nxm_experimenter_len(header); |
164 | return NXM_HEADER(nxm_vendor(header), nxm_class(header), | |
165 | nxm_field(header), 0, new_len); | |
178742f9 | 166 | } |
508a9338 BP |
167 | static uint64_t |
168 | nxm_make_wild_header(uint64_t header) | |
178742f9 | 169 | { |
508a9338 BP |
170 | int new_len = nxm_payload_len(header) * 2 + nxm_experimenter_len(header); |
171 | return NXM_HEADER(nxm_vendor(header), nxm_class(header), | |
172 | nxm_field(header), 1, new_len); | |
178742f9 BP |
173 | } |
174 | ||
175 | /* Flow cookie. | |
176 | * | |
177 | * This may be used to gain the OpenFlow 1.1-like ability to restrict | |
178 | * certain NXM-based Flow Mod and Flow Stats Request messages to flows | |
179 | * with specific cookies. See the "nx_flow_mod" and "nx_flow_stats_request" | |
180 | * structure definitions for more details. This match is otherwise not | |
181 | * allowed. */ | |
508a9338 | 182 | #define NXM_NX_COOKIE NXM_HEADER (0, 0x0001, 30, 0, 8) |
178742f9 BP |
183 | #define NXM_NX_COOKIE_W nxm_make_wild_header(NXM_NX_COOKIE) |
184 | ||
185 | struct nxm_field { | |
508a9338 | 186 | uint64_t header; |
178742f9 BP |
187 | enum ofp_version version; |
188 | const char *name; /* e.g. "NXM_OF_IN_PORT". */ | |
189 | ||
190 | enum mf_field_id id; | |
191 | }; | |
192 | ||
508a9338 | 193 | static const struct nxm_field *nxm_field_by_header(uint64_t header); |
178742f9 | 194 | static const struct nxm_field *nxm_field_by_name(const char *name, size_t len); |
e6556fe3 BP |
195 | static const struct nxm_field *nxm_field_by_mf_id(enum mf_field_id, |
196 | enum ofp_version); | |
178742f9 | 197 | |
508a9338 | 198 | static void nx_put_header__(struct ofpbuf *, uint64_t header, bool masked); |
dc3eb953 JG |
199 | static void nx_put_header_len(struct ofpbuf *, enum mf_field_id field, |
200 | enum ofp_version version, bool masked, | |
201 | size_t n_bytes); | |
f8047558 | 202 | |
09246b99 BP |
203 | /* Rate limit for nx_match parse errors. These always indicate a bug in the |
204 | * peer and so there's not much point in showing a lot of them. */ | |
205 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
206 | ||
178742f9 BP |
207 | static const struct nxm_field * |
208 | mf_parse_subfield_name(const char *name, int name_len, bool *wild); | |
209 | ||
178742f9 BP |
210 | /* Returns the preferred OXM header to use for field 'id' in OpenFlow version |
211 | * 'version'. Specify 0 for 'version' if an NXM legacy header should be | |
212 | * preferred over any standardized OXM header. Returns 0 if field 'id' cannot | |
213 | * be expressed in NXM or OXM. */ | |
508a9338 | 214 | static uint64_t |
178742f9 BP |
215 | mf_oxm_header(enum mf_field_id id, enum ofp_version version) |
216 | { | |
e6556fe3 | 217 | const struct nxm_field *f = nxm_field_by_mf_id(id, version); |
178742f9 BP |
218 | return f ? f->header : 0; |
219 | } | |
220 | ||
508a9338 BP |
221 | /* Returns the 32-bit OXM or NXM header to use for field 'id', preferring an |
222 | * NXM legacy header over any standardized OXM header. Returns 0 if field 'id' | |
223 | * cannot be expressed with a 32-bit NXM or OXM header. | |
224 | * | |
225 | * Whenever possible, use nx_pull_header() instead of this function, because | |
226 | * this function cannot support 64-bit experimenter OXM headers. */ | |
227 | uint32_t | |
228 | mf_nxm_header(enum mf_field_id id) | |
229 | { | |
230 | uint64_t oxm = mf_oxm_header(id, 0); | |
231 | return is_experimenter_oxm(oxm) ? 0 : oxm >> 32; | |
232 | } | |
233 | ||
234 | static const struct mf_field * | |
235 | mf_from_oxm_header(uint64_t header) | |
236 | { | |
237 | const struct nxm_field *f = nxm_field_by_header(header); | |
238 | return f ? mf_from_id(f->id) : NULL; | |
239 | } | |
240 | ||
178742f9 BP |
241 | /* Returns the "struct mf_field" that corresponds to NXM or OXM header |
242 | * 'header', or NULL if 'header' doesn't correspond to any known field. */ | |
243 | const struct mf_field * | |
244 | mf_from_nxm_header(uint32_t header) | |
245 | { | |
508a9338 | 246 | return mf_from_oxm_header((uint64_t) header << 32); |
178742f9 BP |
247 | } |
248 | ||
09246b99 BP |
249 | /* Returns the width of the data for a field with the given 'header', in |
250 | * bytes. */ | |
178742f9 | 251 | static int |
508a9338 | 252 | nxm_field_bytes(uint64_t header) |
09246b99 | 253 | { |
508a9338 | 254 | unsigned int length = nxm_payload_len(header); |
178742f9 | 255 | return nxm_hasmask(header) ? length / 2 : length; |
09246b99 | 256 | } |
e6556fe3 | 257 | \f |
6a885fd0 | 258 | /* nx_pull_match() and helpers. */ |
09246b99 | 259 | |
178742f9 BP |
260 | /* Given NXM/OXM value 'value' and mask 'mask' associated with 'header', checks |
261 | * for any 1-bit in the value where there is a 0-bit in the mask. Returns 0 if | |
262 | * none, otherwise an error code. */ | |
263 | static bool | |
508a9338 | 264 | is_mask_consistent(uint64_t header, const uint8_t *value, const uint8_t *mask) |
09246b99 | 265 | { |
178742f9 BP |
266 | unsigned int width = nxm_field_bytes(header); |
267 | unsigned int i; | |
09246b99 | 268 | |
178742f9 BP |
269 | for (i = 0; i < width; i++) { |
270 | if (value[i] & ~mask[i]) { | |
271 | if (!VLOG_DROP_WARN(&rl)) { | |
272 | VLOG_WARN_RL(&rl, "Rejecting NXM/OXM entry "NXM_HEADER_FMT " " | |
273 | "with 1-bits in value for bits wildcarded by the " | |
274 | "mask.", NXM_HEADER_ARGS(header)); | |
275 | } | |
276 | return false; | |
09246b99 | 277 | } |
09246b99 | 278 | } |
178742f9 BP |
279 | return true; |
280 | } | |
09246b99 | 281 | |
178742f9 | 282 | static bool |
508a9338 | 283 | is_cookie_pseudoheader(uint64_t header) |
178742f9 BP |
284 | { |
285 | return header == NXM_NX_COOKIE || header == NXM_NX_COOKIE_W; | |
286 | } | |
287 | ||
288 | static enum ofperr | |
508a9338 | 289 | nx_pull_header__(struct ofpbuf *b, bool allow_cookie, uint64_t *header, |
178742f9 BP |
290 | const struct mf_field **field) |
291 | { | |
6fd6ed71 | 292 | if (b->size < 4) { |
508a9338 | 293 | goto bad_len; |
09246b99 | 294 | } |
508a9338 | 295 | |
6fd6ed71 | 296 | *header = ((uint64_t) ntohl(get_unaligned_be32(b->data))) << 32; |
508a9338 | 297 | if (is_experimenter_oxm(*header)) { |
6fd6ed71 | 298 | if (b->size < 8) { |
508a9338 BP |
299 | goto bad_len; |
300 | } | |
6fd6ed71 | 301 | *header = ntohll(get_unaligned_be64(b->data)); |
508a9338 | 302 | } |
1cb20095 | 303 | if (nxm_length(*header) < nxm_experimenter_len(*header)) { |
508a9338 BP |
304 | VLOG_WARN_RL(&rl, "OXM header "NXM_HEADER_FMT" has invalid length %d " |
305 | "(minimum is %d)", | |
306 | NXM_HEADER_ARGS(*header), nxm_length(*header), | |
1cb20095 | 307 | nxm_header_len(*header)); |
178742f9 BP |
308 | goto error; |
309 | } | |
508a9338 BP |
310 | ofpbuf_pull(b, nxm_header_len(*header)); |
311 | ||
178742f9 | 312 | if (field) { |
508a9338 | 313 | *field = mf_from_oxm_header(*header); |
178742f9 BP |
314 | if (!*field && !(allow_cookie && is_cookie_pseudoheader(*header))) { |
315 | VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" is unknown", | |
316 | NXM_HEADER_ARGS(*header)); | |
317 | return OFPERR_OFPBMC_BAD_FIELD; | |
318 | } | |
09246b99 BP |
319 | } |
320 | ||
178742f9 BP |
321 | return 0; |
322 | ||
508a9338 BP |
323 | bad_len: |
324 | VLOG_DBG_RL(&rl, "encountered partial (%"PRIu32"-byte) OXM entry", | |
6fd6ed71 | 325 | b->size); |
178742f9 BP |
326 | error: |
327 | *header = 0; | |
38221f4e BP |
328 | if (field) { |
329 | *field = NULL; | |
330 | } | |
178742f9 | 331 | return OFPERR_OFPBMC_BAD_LEN; |
09246b99 BP |
332 | } |
333 | ||
11b8d049 JG |
334 | static void |
335 | copy_entry_value(const struct mf_field *field, union mf_value *value, | |
336 | const uint8_t *payload, int width) | |
337 | { | |
338 | int copy_len; | |
339 | void *copy_dst; | |
340 | ||
341 | copy_dst = value; | |
342 | copy_len = MIN(width, field ? field->n_bytes : sizeof *value); | |
343 | ||
344 | if (field && field->variable_len) { | |
345 | memset(value, 0, field->n_bytes); | |
346 | copy_dst = &value->u8 + field->n_bytes - copy_len; | |
347 | } | |
348 | ||
349 | memcpy(copy_dst, payload, copy_len); | |
350 | } | |
351 | ||
3947cc76 | 352 | static enum ofperr |
508a9338 | 353 | nx_pull_entry__(struct ofpbuf *b, bool allow_cookie, uint64_t *header, |
11b8d049 | 354 | const struct mf_field **field_, |
178742f9 | 355 | union mf_value *value, union mf_value *mask) |
e1cfc4e4 | 356 | { |
11b8d049 | 357 | const struct mf_field *field; |
178742f9 BP |
358 | enum ofperr header_error; |
359 | unsigned int payload_len; | |
360 | const uint8_t *payload; | |
361 | int width; | |
e1cfc4e4 | 362 | |
11b8d049 | 363 | header_error = nx_pull_header__(b, allow_cookie, header, &field); |
178742f9 BP |
364 | if (header_error && header_error != OFPERR_OFPBMC_BAD_FIELD) { |
365 | return header_error; | |
366 | } | |
367 | ||
508a9338 | 368 | payload_len = nxm_payload_len(*header); |
178742f9 BP |
369 | payload = ofpbuf_try_pull(b, payload_len); |
370 | if (!payload) { | |
371 | VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" calls for %u-byte " | |
372 | "payload but only %"PRIu32" bytes follow OXM header", | |
6fd6ed71 | 373 | NXM_HEADER_ARGS(*header), payload_len, b->size); |
178742f9 BP |
374 | return OFPERR_OFPBMC_BAD_LEN; |
375 | } | |
376 | ||
377 | width = nxm_field_bytes(*header); | |
378 | if (nxm_hasmask(*header) | |
379 | && !is_mask_consistent(*header, payload, payload + width)) { | |
380 | return OFPERR_OFPBMC_BAD_WILDCARDS; | |
381 | } | |
382 | ||
11b8d049 JG |
383 | copy_entry_value(field, value, payload, width); |
384 | ||
178742f9 BP |
385 | if (mask) { |
386 | if (nxm_hasmask(*header)) { | |
11b8d049 | 387 | copy_entry_value(field, mask, payload + width, width); |
178742f9 | 388 | } else { |
11b8d049 | 389 | memset(mask, 0xff, sizeof *mask); |
178742f9 BP |
390 | } |
391 | } else if (nxm_hasmask(*header)) { | |
392 | VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" includes mask but " | |
393 | "masked OXMs are not allowed here", | |
394 | NXM_HEADER_ARGS(*header)); | |
395 | return OFPERR_OFPBMC_BAD_MASK; | |
396 | } | |
397 | ||
11b8d049 JG |
398 | if (field_) { |
399 | *field_ = field; | |
400 | return header_error; | |
401 | } | |
402 | ||
403 | return 0; | |
178742f9 BP |
404 | } |
405 | ||
406 | /* Attempts to pull an NXM or OXM header, value, and mask (if present) from the | |
407 | * beginning of 'b'. If successful, stores a pointer to the "struct mf_field" | |
408 | * corresponding to the pulled header in '*field', the value into '*value', | |
409 | * and the mask into '*mask', and returns 0. On error, returns an OpenFlow | |
410 | * error; in this case, some bytes might have been pulled off 'b' anyhow, and | |
411 | * the output parameters might have been modified. | |
412 | * | |
413 | * If a NULL 'mask' is supplied, masked OXM or NXM entries are treated as | |
414 | * errors (with OFPERR_OFPBMC_BAD_MASK). | |
415 | */ | |
416 | enum ofperr | |
417 | nx_pull_entry(struct ofpbuf *b, const struct mf_field **field, | |
418 | union mf_value *value, union mf_value *mask) | |
419 | { | |
508a9338 | 420 | uint64_t header; |
178742f9 BP |
421 | |
422 | return nx_pull_entry__(b, false, &header, field, value, mask); | |
423 | } | |
424 | ||
425 | /* Attempts to pull an NXM or OXM header from the beginning of 'b'. If | |
426 | * successful, stores a pointer to the "struct mf_field" corresponding to the | |
427 | * pulled header in '*field', stores the header's hasmask bit in '*masked' | |
428 | * (true if hasmask=1, false if hasmask=0), and returns 0. On error, returns | |
429 | * an OpenFlow error; in this case, some bytes might have been pulled off 'b' | |
430 | * anyhow, and the output parameters might have been modified. | |
431 | * | |
432 | * If NULL 'masked' is supplied, masked OXM or NXM headers are treated as | |
433 | * errors (with OFPERR_OFPBMC_BAD_MASK). | |
434 | */ | |
435 | enum ofperr | |
436 | nx_pull_header(struct ofpbuf *b, const struct mf_field **field, bool *masked) | |
437 | { | |
438 | enum ofperr error; | |
508a9338 | 439 | uint64_t header; |
178742f9 BP |
440 | |
441 | error = nx_pull_header__(b, false, &header, field); | |
442 | if (masked) { | |
443 | *masked = !error && nxm_hasmask(header); | |
444 | } else if (!error && nxm_hasmask(header)) { | |
445 | error = OFPERR_OFPBMC_BAD_MASK; | |
446 | } | |
447 | return error; | |
448 | } | |
449 | ||
450 | static enum ofperr | |
451 | nx_pull_match_entry(struct ofpbuf *b, bool allow_cookie, | |
452 | const struct mf_field **field, | |
453 | union mf_value *value, union mf_value *mask) | |
454 | { | |
455 | enum ofperr error; | |
508a9338 | 456 | uint64_t header; |
178742f9 BP |
457 | |
458 | error = nx_pull_entry__(b, allow_cookie, &header, field, value, mask); | |
459 | if (error) { | |
460 | return error; | |
461 | } | |
462 | if (field && *field) { | |
463 | if (!mf_is_mask_valid(*field, mask)) { | |
464 | VLOG_DBG_RL(&rl, "bad mask for field %s", (*field)->name); | |
465 | return OFPERR_OFPBMC_BAD_MASK; | |
466 | } | |
467 | if (!mf_is_value_valid(*field, value)) { | |
468 | VLOG_DBG_RL(&rl, "bad value for field %s", (*field)->name); | |
469 | return OFPERR_OFPBMC_BAD_VALUE; | |
e1cfc4e4 BP |
470 | } |
471 | } | |
3947cc76 | 472 | return 0; |
e1cfc4e4 BP |
473 | } |
474 | ||
90bf1e07 | 475 | static enum ofperr |
7623f4dd | 476 | nx_pull_raw(const uint8_t *p, unsigned int match_len, bool strict, |
8d8ab6c2 JG |
477 | struct match *match, ovs_be64 *cookie, ovs_be64 *cookie_mask, |
478 | const struct tun_table *tun_table) | |
09246b99 | 479 | { |
cb22974d | 480 | ovs_assert((cookie != NULL) == (cookie_mask != NULL)); |
e729e793 | 481 | |
81a76618 | 482 | match_init_catchall(match); |
8d8ab6c2 | 483 | match->flow.tunnel.metadata.tab = tun_table; |
a3a0c29e BP |
484 | if (cookie) { |
485 | *cookie = *cookie_mask = htonll(0); | |
486 | } | |
a3a0c29e | 487 | |
0a2869d5 | 488 | struct ofpbuf b = ofpbuf_const_initializer(p, match_len); |
6fd6ed71 PS |
489 | while (b.size) { |
490 | const uint8_t *pos = b.data; | |
178742f9 BP |
491 | const struct mf_field *field; |
492 | union mf_value value; | |
493 | union mf_value mask; | |
90bf1e07 | 494 | enum ofperr error; |
09246b99 | 495 | |
178742f9 BP |
496 | error = nx_pull_match_entry(&b, cookie != NULL, &field, &value, &mask); |
497 | if (error) { | |
498 | if (error == OFPERR_OFPBMC_BAD_FIELD && !strict) { | |
499 | continue; | |
500 | } | |
501 | } else if (!field) { | |
502 | if (!cookie) { | |
2e0525bc | 503 | error = OFPERR_OFPBMC_BAD_FIELD; |
178742f9 BP |
504 | } else if (*cookie_mask) { |
505 | error = OFPERR_OFPBMC_DUP_FIELD; | |
102ce766 | 506 | } else { |
178742f9 BP |
507 | *cookie = value.be64; |
508 | *cookie_mask = mask.be64; | |
102ce766 | 509 | } |
aff49b8c | 510 | } else if (!mf_are_prereqs_ok(field, &match->flow, NULL)) { |
2e0525bc | 511 | error = OFPERR_OFPBMC_BAD_PREREQ; |
178742f9 | 512 | } else if (!mf_is_all_wild(field, &match->wc)) { |
2e0525bc | 513 | error = OFPERR_OFPBMC_DUP_FIELD; |
72333065 | 514 | } else { |
4f7b100c JG |
515 | char *err_str; |
516 | ||
517 | mf_set(field, &value, &mask, match, &err_str); | |
518 | if (err_str) { | |
519 | VLOG_DBG_RL(&rl, "error parsing OXM at offset %"PRIdPTR" " | |
520 | "within match (%s)", pos - p, err_str); | |
521 | free(err_str); | |
522 | return OFPERR_OFPBMC_BAD_VALUE; | |
523 | } | |
e729e793 JP |
524 | } |
525 | ||
09246b99 | 526 | if (error) { |
178742f9 BP |
527 | VLOG_DBG_RL(&rl, "error parsing OXM at offset %"PRIdPTR" " |
528 | "within match (%s)", pos - | |
529 | p, ofperr_to_string(error)); | |
09246b99 BP |
530 | return error; |
531 | } | |
09246b99 BP |
532 | } |
533 | ||
8d8ab6c2 | 534 | match->flow.tunnel.metadata.tab = NULL; |
178742f9 | 535 | return 0; |
09246b99 | 536 | } |
102ce766 | 537 | |
7623f4dd SH |
538 | static enum ofperr |
539 | nx_pull_match__(struct ofpbuf *b, unsigned int match_len, bool strict, | |
81a76618 | 540 | struct match *match, |
8d8ab6c2 JG |
541 | ovs_be64 *cookie, ovs_be64 *cookie_mask, |
542 | const struct tun_table *tun_table) | |
7623f4dd SH |
543 | { |
544 | uint8_t *p = NULL; | |
545 | ||
546 | if (match_len) { | |
547 | p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8)); | |
548 | if (!p) { | |
549 | VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a " | |
550 | "multiple of 8, is longer than space in message (max " | |
6fd6ed71 | 551 | "length %"PRIu32")", match_len, b->size); |
7623f4dd SH |
552 | return OFPERR_OFPBMC_BAD_LEN; |
553 | } | |
554 | } | |
555 | ||
8d8ab6c2 JG |
556 | return nx_pull_raw(p, match_len, strict, match, cookie, cookie_mask, |
557 | tun_table); | |
7623f4dd SH |
558 | } |
559 | ||
102ce766 | 560 | /* Parses the nx_match formatted match description in 'b' with length |
81a76618 BP |
561 | * 'match_len'. Stores the results in 'match'. If 'cookie' and 'cookie_mask' |
562 | * are valid pointers, then stores the cookie and mask in them if 'b' contains | |
563 | * a "NXM_NX_COOKIE*" match. Otherwise, stores 0 in both. | |
102ce766 | 564 | * |
81a76618 | 565 | * Fails with an error upon encountering an unknown NXM header. |
102ce766 EJ |
566 | * |
567 | * Returns 0 if successful, otherwise an OpenFlow error code. */ | |
90bf1e07 | 568 | enum ofperr |
81a76618 | 569 | nx_pull_match(struct ofpbuf *b, unsigned int match_len, struct match *match, |
8d8ab6c2 JG |
570 | ovs_be64 *cookie, ovs_be64 *cookie_mask, |
571 | const struct tun_table *tun_table) | |
102ce766 | 572 | { |
8d8ab6c2 JG |
573 | return nx_pull_match__(b, match_len, true, match, cookie, cookie_mask, |
574 | tun_table); | |
102ce766 EJ |
575 | } |
576 | ||
81a76618 BP |
577 | /* Behaves the same as nx_pull_match(), but skips over unknown NXM headers, |
578 | * instead of failing with an error. */ | |
90bf1e07 | 579 | enum ofperr |
102ce766 | 580 | nx_pull_match_loose(struct ofpbuf *b, unsigned int match_len, |
81a76618 | 581 | struct match *match, |
8d8ab6c2 JG |
582 | ovs_be64 *cookie, ovs_be64 *cookie_mask, |
583 | const struct tun_table *tun_table) | |
102ce766 | 584 | { |
8d8ab6c2 JG |
585 | return nx_pull_match__(b, match_len, false, match, cookie, cookie_mask, |
586 | tun_table); | |
102ce766 | 587 | } |
7623f4dd SH |
588 | |
589 | static enum ofperr | |
8d8ab6c2 JG |
590 | oxm_pull_match__(struct ofpbuf *b, bool strict, |
591 | const struct tun_table *tun_table, struct match *match) | |
7623f4dd | 592 | { |
6fd6ed71 | 593 | struct ofp11_match_header *omh = b->data; |
7623f4dd SH |
594 | uint8_t *p; |
595 | uint16_t match_len; | |
596 | ||
6fd6ed71 | 597 | if (b->size < sizeof *omh) { |
7623f4dd SH |
598 | return OFPERR_OFPBMC_BAD_LEN; |
599 | } | |
600 | ||
601 | match_len = ntohs(omh->length); | |
602 | if (match_len < sizeof *omh) { | |
603 | return OFPERR_OFPBMC_BAD_LEN; | |
604 | } | |
605 | ||
606 | if (omh->type != htons(OFPMT_OXM)) { | |
607 | return OFPERR_OFPBMC_BAD_TYPE; | |
608 | } | |
609 | ||
610 | p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8)); | |
611 | if (!p) { | |
612 | VLOG_DBG_RL(&rl, "oxm length %u, rounded up to a " | |
613 | "multiple of 8, is longer than space in message (max " | |
6fd6ed71 | 614 | "length %"PRIu32")", match_len, b->size); |
7623f4dd SH |
615 | return OFPERR_OFPBMC_BAD_LEN; |
616 | } | |
617 | ||
618 | return nx_pull_raw(p + sizeof *omh, match_len - sizeof *omh, | |
8d8ab6c2 | 619 | strict, match, NULL, NULL, tun_table); |
7623f4dd SH |
620 | } |
621 | ||
aa0667bc YT |
622 | /* Parses the oxm formatted match description preceded by a struct |
623 | * ofp11_match_header in 'b'. Stores the result in 'match'. | |
7623f4dd SH |
624 | * |
625 | * Fails with an error when encountering unknown OXM headers. | |
626 | * | |
627 | * Returns 0 if successful, otherwise an OpenFlow error code. */ | |
628 | enum ofperr | |
8d8ab6c2 JG |
629 | oxm_pull_match(struct ofpbuf *b, const struct tun_table *tun_table, |
630 | struct match *match) | |
7623f4dd | 631 | { |
8d8ab6c2 | 632 | return oxm_pull_match__(b, true, tun_table, match); |
7623f4dd SH |
633 | } |
634 | ||
635 | /* Behaves the same as oxm_pull_match() with one exception. Skips over unknown | |
aa0667bc | 636 | * OXM headers instead of failing with an error when they are encountered. */ |
7623f4dd | 637 | enum ofperr |
8d8ab6c2 JG |
638 | oxm_pull_match_loose(struct ofpbuf *b, const struct tun_table *tun_table, |
639 | struct match *match) | |
7623f4dd | 640 | { |
8d8ab6c2 | 641 | return oxm_pull_match__(b, false, tun_table, match); |
7623f4dd | 642 | } |
bc65c25a | 643 | |
9f6e20b7 BP |
644 | /* Parses the OXM match description in the 'oxm_len' bytes in 'oxm'. Stores |
645 | * the result in 'match'. | |
646 | * | |
647 | * Fails with an error when encountering unknown OXM headers. | |
648 | * | |
649 | * Returns 0 if successful, otherwise an OpenFlow error code. */ | |
650 | enum ofperr | |
8d8ab6c2 JG |
651 | oxm_decode_match(const void *oxm, size_t oxm_len, |
652 | const struct tun_table *tun_table, struct match *match) | |
9f6e20b7 | 653 | { |
8d8ab6c2 | 654 | return nx_pull_raw(oxm, oxm_len, true, match, NULL, NULL, tun_table); |
9f6e20b7 BP |
655 | } |
656 | ||
bc65c25a SH |
657 | /* Verify an array of OXM TLVs treating value of each TLV as a mask, |
658 | * disallowing masks in each TLV and ignoring pre-requisites. */ | |
659 | enum ofperr | |
660 | oxm_pull_field_array(const void *fields_data, size_t fields_len, | |
661 | struct field_array *fa) | |
662 | { | |
0a2869d5 | 663 | struct ofpbuf b = ofpbuf_const_initializer(fields_data, fields_len); |
bc65c25a SH |
664 | while (b.size) { |
665 | const uint8_t *pos = b.data; | |
666 | const struct mf_field *field; | |
667 | union mf_value value; | |
668 | enum ofperr error; | |
669 | uint64_t header; | |
670 | ||
671 | error = nx_pull_entry__(&b, false, &header, &field, &value, NULL); | |
672 | if (error) { | |
673 | VLOG_DBG_RL(&rl, "error pulling field array field"); | |
674 | return error; | |
675 | } else if (!field) { | |
676 | VLOG_DBG_RL(&rl, "unknown field array field"); | |
677 | error = OFPERR_OFPBMC_BAD_FIELD; | |
678 | } else if (bitmap_is_set(fa->used.bm, field->id)) { | |
679 | VLOG_DBG_RL(&rl, "duplicate field array field '%s'", field->name); | |
680 | error = OFPERR_OFPBMC_DUP_FIELD; | |
681 | } else if (!mf_is_mask_valid(field, &value)) { | |
682 | VLOG_DBG_RL(&rl, "bad mask in field array field '%s'", field->name); | |
683 | return OFPERR_OFPBMC_BAD_MASK; | |
684 | } else { | |
685 | field_array_set(field->id, &value, fa); | |
686 | } | |
687 | ||
688 | if (error) { | |
689 | const uint8_t *start = fields_data; | |
690 | ||
691 | VLOG_DBG_RL(&rl, "error parsing OXM at offset %"PRIdPTR" " | |
692 | "within field array (%s)", pos - start, | |
693 | ofperr_to_string(error)); | |
694 | return error; | |
695 | } | |
696 | } | |
697 | ||
698 | return 0; | |
699 | } | |
09246b99 BP |
700 | \f |
701 | /* nx_put_match() and helpers. | |
702 | * | |
703 | * 'put' functions whose names end in 'w' add a wildcarded field. | |
704 | * 'put' functions whose names end in 'm' add a field that might be wildcarded. | |
705 | * Other 'put' functions add exact-match fields. | |
706 | */ | |
1cb20095 JG |
707 | void |
708 | nxm_put__(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version, | |
709 | const void *value, const void *mask, size_t n_bytes) | |
09246b99 | 710 | { |
1cb20095 | 711 | nx_put_header_len(b, field, version, !!mask, n_bytes); |
f8047558 | 712 | ofpbuf_put(b, value, n_bytes); |
1cb20095 JG |
713 | if (mask) { |
714 | ofpbuf_put(b, mask, n_bytes); | |
715 | } | |
716 | ||
09246b99 BP |
717 | } |
718 | ||
1cb20095 | 719 | static void |
f8047558 BP |
720 | nxm_put(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version, |
721 | const void *value, const void *mask, size_t n_bytes) | |
66642cb4 | 722 | { |
f8047558 BP |
723 | if (!is_all_zeros(mask, n_bytes)) { |
724 | bool masked = !is_all_ones(mask, n_bytes); | |
1cb20095 | 725 | nxm_put__(b, field, version, value, masked ? mask : NULL, n_bytes); |
66642cb4 BP |
726 | } |
727 | } | |
728 | ||
09246b99 | 729 | static void |
f8047558 BP |
730 | nxm_put_8m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version, |
731 | uint8_t value, uint8_t mask) | |
09246b99 | 732 | { |
f8047558 | 733 | nxm_put(b, field, version, &value, &mask, sizeof value); |
09246b99 BP |
734 | } |
735 | ||
736 | static void | |
f8047558 BP |
737 | nxm_put_8(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version, |
738 | uint8_t value) | |
09246b99 | 739 | { |
1cb20095 | 740 | nxm_put__(b, field, version, &value, NULL, sizeof value); |
09246b99 BP |
741 | } |
742 | ||
743 | static void | |
f8047558 BP |
744 | nxm_put_16m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version, |
745 | ovs_be16 value, ovs_be16 mask) | |
09246b99 | 746 | { |
f8047558 | 747 | nxm_put(b, field, version, &value, &mask, sizeof value); |
09246b99 BP |
748 | } |
749 | ||
750 | static void | |
f8047558 BP |
751 | nxm_put_16(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version, |
752 | ovs_be16 value) | |
09246b99 | 753 | { |
1cb20095 | 754 | nxm_put__(b, field, version, &value, NULL, sizeof value); |
09246b99 BP |
755 | } |
756 | ||
8368c090 | 757 | static void |
f8047558 BP |
758 | nxm_put_32m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version, |
759 | ovs_be32 value, ovs_be32 mask) | |
8368c090 | 760 | { |
f8047558 | 761 | nxm_put(b, field, version, &value, &mask, sizeof value); |
8368c090 BP |
762 | } |
763 | ||
764 | static void | |
f8047558 BP |
765 | nxm_put_32(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version, |
766 | ovs_be32 value) | |
8368c090 | 767 | { |
1cb20095 | 768 | nxm_put__(b, field, version, &value, NULL, sizeof value); |
8368c090 BP |
769 | } |
770 | ||
09246b99 | 771 | static void |
f8047558 BP |
772 | nxm_put_64m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version, |
773 | ovs_be64 value, ovs_be64 mask) | |
09246b99 | 774 | { |
f8047558 | 775 | nxm_put(b, field, version, &value, &mask, sizeof value); |
09246b99 BP |
776 | } |
777 | ||
8ff10dd5 JP |
778 | static void |
779 | nxm_put_128m(struct ofpbuf *b, | |
780 | enum mf_field_id field, enum ofp_version version, | |
781 | const ovs_be128 value, const ovs_be128 mask) | |
782 | { | |
783 | nxm_put(b, field, version, &value, &mask, sizeof(value)); | |
784 | } | |
785 | ||
1e37a2d7 | 786 | static void |
f8047558 BP |
787 | nxm_put_eth_masked(struct ofpbuf *b, |
788 | enum mf_field_id field, enum ofp_version version, | |
74ff3298 | 789 | const struct eth_addr value, const struct eth_addr mask) |
1e37a2d7 | 790 | { |
74ff3298 | 791 | nxm_put(b, field, version, value.ea, mask.ea, ETH_ADDR_LEN); |
1e37a2d7 BP |
792 | } |
793 | ||
d31f1109 | 794 | static void |
f8047558 BP |
795 | nxm_put_ipv6(struct ofpbuf *b, |
796 | enum mf_field_id field, enum ofp_version version, | |
d31f1109 JP |
797 | const struct in6_addr *value, const struct in6_addr *mask) |
798 | { | |
f8047558 BP |
799 | nxm_put(b, field, version, value->s6_addr, mask->s6_addr, |
800 | sizeof value->s6_addr); | |
d31f1109 JP |
801 | } |
802 | ||
7257b535 | 803 | static void |
f8047558 BP |
804 | nxm_put_frag(struct ofpbuf *b, const struct match *match, |
805 | enum ofp_version version) | |
7257b535 | 806 | { |
f8047558 BP |
807 | uint8_t nw_frag = match->flow.nw_frag & FLOW_NW_FRAG_MASK; |
808 | uint8_t nw_frag_mask = match->wc.masks.nw_frag & FLOW_NW_FRAG_MASK; | |
7257b535 | 809 | |
f8047558 BP |
810 | nxm_put_8m(b, MFF_IP_FRAG, version, nw_frag, |
811 | nw_frag_mask == FLOW_NW_FRAG_MASK ? UINT8_MAX : nw_frag_mask); | |
7257b535 BP |
812 | } |
813 | ||
5e10215d BP |
814 | /* Appends to 'b' a set of OXM or NXM matches for the IPv4 or IPv6 fields in |
815 | * 'match'. */ | |
8e7082b0 | 816 | static void |
9d84066c | 817 | nxm_put_ip(struct ofpbuf *b, const struct match *match, enum ofp_version oxm) |
8e7082b0 | 818 | { |
81a76618 | 819 | const struct flow *flow = &match->flow; |
8e7082b0 | 820 | |
5e10215d | 821 | if (flow->dl_type == htons(ETH_TYPE_IP)) { |
f8047558 | 822 | nxm_put_32m(b, MFF_IPV4_SRC, oxm, |
5e10215d | 823 | flow->nw_src, match->wc.masks.nw_src); |
f8047558 | 824 | nxm_put_32m(b, MFF_IPV4_DST, oxm, |
5e10215d BP |
825 | flow->nw_dst, match->wc.masks.nw_dst); |
826 | } else { | |
f8047558 | 827 | nxm_put_ipv6(b, MFF_IPV6_SRC, oxm, |
5e10215d | 828 | &flow->ipv6_src, &match->wc.masks.ipv6_src); |
f8047558 | 829 | nxm_put_ipv6(b, MFF_IPV6_DST, oxm, |
5e10215d BP |
830 | &flow->ipv6_dst, &match->wc.masks.ipv6_dst); |
831 | } | |
832 | ||
21119b3e | 833 | nxm_put_frag(b, match, oxm); |
8e7082b0 | 834 | |
81a76618 | 835 | if (match->wc.masks.nw_tos & IP_DSCP_MASK) { |
1638b906 | 836 | if (oxm) { |
f8047558 | 837 | nxm_put_8(b, MFF_IP_DSCP_SHIFTED, oxm, |
9d84066c | 838 | flow->nw_tos >> 2); |
1638b906 | 839 | } else { |
f8047558 | 840 | nxm_put_8(b, MFF_IP_DSCP, oxm, |
9d84066c | 841 | flow->nw_tos & IP_DSCP_MASK); |
1638b906 | 842 | } |
8e7082b0 BP |
843 | } |
844 | ||
81a76618 | 845 | if (match->wc.masks.nw_tos & IP_ECN_MASK) { |
f8047558 | 846 | nxm_put_8(b, MFF_IP_ECN, oxm, |
b5ae8913 | 847 | flow->nw_tos & IP_ECN_MASK); |
8e7082b0 BP |
848 | } |
849 | ||
5a21d9c2 | 850 | if (match->wc.masks.nw_ttl) { |
f8047558 | 851 | nxm_put_8(b, MFF_IP_TTL, oxm, flow->nw_ttl); |
8e7082b0 BP |
852 | } |
853 | ||
f8047558 | 854 | nxm_put_32m(b, MFF_IPV6_LABEL, oxm, |
5e10215d BP |
855 | flow->ipv6_label, match->wc.masks.ipv6_label); |
856 | ||
81a76618 | 857 | if (match->wc.masks.nw_proto) { |
f8047558 | 858 | nxm_put_8(b, MFF_IP_PROTO, oxm, flow->nw_proto); |
8e7082b0 BP |
859 | |
860 | if (flow->nw_proto == IPPROTO_TCP) { | |
f8047558 | 861 | nxm_put_16m(b, MFF_TCP_SRC, oxm, |
81a76618 | 862 | flow->tp_src, match->wc.masks.tp_src); |
f8047558 | 863 | nxm_put_16m(b, MFF_TCP_DST, oxm, |
81a76618 | 864 | flow->tp_dst, match->wc.masks.tp_dst); |
f8047558 | 865 | nxm_put_16m(b, MFF_TCP_FLAGS, oxm, |
dc235f7f | 866 | flow->tcp_flags, match->wc.masks.tcp_flags); |
8e7082b0 | 867 | } else if (flow->nw_proto == IPPROTO_UDP) { |
f8047558 | 868 | nxm_put_16m(b, MFF_UDP_SRC, oxm, |
81a76618 | 869 | flow->tp_src, match->wc.masks.tp_src); |
f8047558 | 870 | nxm_put_16m(b, MFF_UDP_DST, oxm, |
81a76618 | 871 | flow->tp_dst, match->wc.masks.tp_dst); |
0d56eaf2 | 872 | } else if (flow->nw_proto == IPPROTO_SCTP) { |
f8047558 | 873 | nxm_put_16m(b, MFF_SCTP_SRC, oxm, flow->tp_src, |
0d56eaf2 | 874 | match->wc.masks.tp_src); |
f8047558 | 875 | nxm_put_16m(b, MFF_SCTP_DST, oxm, flow->tp_dst, |
0d56eaf2 | 876 | match->wc.masks.tp_dst); |
a75636c8 | 877 | } else if (is_icmpv4(flow, NULL)) { |
5e10215d | 878 | if (match->wc.masks.tp_src) { |
f8047558 | 879 | nxm_put_8(b, MFF_ICMPV4_TYPE, oxm, |
5e10215d BP |
880 | ntohs(flow->tp_src)); |
881 | } | |
882 | if (match->wc.masks.tp_dst) { | |
f8047558 | 883 | nxm_put_8(b, MFF_ICMPV4_CODE, oxm, |
5e10215d BP |
884 | ntohs(flow->tp_dst)); |
885 | } | |
a75636c8 | 886 | } else if (is_icmpv6(flow, NULL)) { |
81a76618 | 887 | if (match->wc.masks.tp_src) { |
f8047558 | 888 | nxm_put_8(b, MFF_ICMPV6_TYPE, oxm, |
5e10215d | 889 | ntohs(flow->tp_src)); |
8e7082b0 | 890 | } |
81a76618 | 891 | if (match->wc.masks.tp_dst) { |
f8047558 | 892 | nxm_put_8(b, MFF_ICMPV6_CODE, oxm, |
5e10215d BP |
893 | ntohs(flow->tp_dst)); |
894 | } | |
c17fcc0a | 895 | if (is_nd(flow, NULL)) { |
f8047558 | 896 | nxm_put_ipv6(b, MFF_ND_TARGET, oxm, |
5e10215d BP |
897 | &flow->nd_target, &match->wc.masks.nd_target); |
898 | if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) { | |
f8047558 | 899 | nxm_put_eth_masked(b, MFF_ND_SLL, oxm, |
5e10215d BP |
900 | flow->arp_sha, match->wc.masks.arp_sha); |
901 | } | |
902 | if (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) { | |
f8047558 | 903 | nxm_put_eth_masked(b, MFF_ND_TLL, oxm, |
5e10215d BP |
904 | flow->arp_tha, match->wc.masks.arp_tha); |
905 | } | |
8e7082b0 BP |
906 | } |
907 | } | |
908 | } | |
909 | } | |
910 | ||
81a76618 | 911 | /* Appends to 'b' the nx_match format that expresses 'match'. For Flow Mod and |
7623f4dd SH |
912 | * Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied. |
913 | * Otherwise, 'cookie_mask' should be zero. | |
4d0ed519 | 914 | * |
9d84066c BP |
915 | * Specify 'oxm' as 0 to express the match in NXM format; otherwise, specify |
916 | * 'oxm' as the OpenFlow version number for the OXM format to use. | |
917 | * | |
4d0ed519 BP |
918 | * This function can cause 'b''s data to be reallocated. |
919 | * | |
920 | * Returns the number of bytes appended to 'b', excluding padding. | |
921 | * | |
81a76618 | 922 | * If 'match' is a catch-all rule that matches every packet, then this function |
4d0ed519 | 923 | * appends nothing to 'b' and returns 0. */ |
7623f4dd | 924 | static int |
9d84066c | 925 | nx_put_raw(struct ofpbuf *b, enum ofp_version oxm, const struct match *match, |
7623f4dd | 926 | ovs_be64 cookie, ovs_be64 cookie_mask) |
09246b99 | 927 | { |
81a76618 | 928 | const struct flow *flow = &match->flow; |
6fd6ed71 | 929 | const size_t start_len = b->size; |
09246b99 | 930 | int match_len; |
b6c9e612 | 931 | int i; |
09246b99 | 932 | |
847b8b02 | 933 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 36); |
a877206f | 934 | |
09246b99 | 935 | /* Metadata. */ |
a79f29f2 | 936 | if (match->wc.masks.dp_hash) { |
f8047558 | 937 | nxm_put_32m(b, MFF_DP_HASH, oxm, |
447b6582 | 938 | htonl(flow->dp_hash), htonl(match->wc.masks.dp_hash)); |
a79f29f2 AZ |
939 | } |
940 | ||
941 | if (match->wc.masks.recirc_id) { | |
f8047558 | 942 | nxm_put_32(b, MFF_RECIRC_ID, oxm, htonl(flow->recirc_id)); |
a79f29f2 AZ |
943 | } |
944 | ||
18080541 BP |
945 | if (match->wc.masks.conj_id) { |
946 | nxm_put_32(b, MFF_CONJ_ID, oxm, htonl(flow->conj_id)); | |
947 | } | |
948 | ||
4e022ec0 AW |
949 | if (match->wc.masks.in_port.ofp_port) { |
950 | ofp_port_t in_port = flow->in_port.ofp_port; | |
b5ae8913 | 951 | if (oxm) { |
f8047558 | 952 | nxm_put_32(b, MFF_IN_PORT_OXM, oxm, |
9d84066c | 953 | ofputil_port_to_ofp11(in_port)); |
b5ae8913 | 954 | } else { |
f8047558 | 955 | nxm_put_16(b, MFF_IN_PORT, oxm, |
9d84066c | 956 | htons(ofp_to_u16(in_port))); |
b5ae8913 | 957 | } |
09246b99 | 958 | } |
c61f3870 BP |
959 | if (match->wc.masks.actset_output) { |
960 | nxm_put_32(b, MFF_ACTSET_OUTPUT, oxm, | |
961 | ofputil_port_to_ofp11(flow->actset_output)); | |
962 | } | |
09246b99 BP |
963 | |
964 | /* Ethernet. */ | |
f8047558 | 965 | nxm_put_eth_masked(b, MFF_ETH_SRC, oxm, |
81a76618 | 966 | flow->dl_src, match->wc.masks.dl_src); |
f8047558 | 967 | nxm_put_eth_masked(b, MFF_ETH_DST, oxm, |
81a76618 | 968 | flow->dl_dst, match->wc.masks.dl_dst); |
f8047558 | 969 | nxm_put_16m(b, MFF_ETH_TYPE, oxm, |
e2170cff | 970 | ofputil_dl_type_to_openflow(flow->dl_type), |
81a76618 | 971 | match->wc.masks.dl_type); |
09246b99 | 972 | |
95f61ba8 SH |
973 | /* 802.1Q. */ |
974 | if (oxm) { | |
26720e24 BP |
975 | ovs_be16 VID_CFI_MASK = htons(VLAN_VID_MASK | VLAN_CFI); |
976 | ovs_be16 vid = flow->vlan_tci & VID_CFI_MASK; | |
81a76618 | 977 | ovs_be16 mask = match->wc.masks.vlan_tci & VID_CFI_MASK; |
95f61ba8 SH |
978 | |
979 | if (mask == htons(VLAN_VID_MASK | VLAN_CFI)) { | |
f8047558 | 980 | nxm_put_16(b, MFF_VLAN_VID, oxm, vid); |
95f61ba8 | 981 | } else if (mask) { |
f8047558 | 982 | nxm_put_16m(b, MFF_VLAN_VID, oxm, vid, mask); |
95f61ba8 SH |
983 | } |
984 | ||
81a76618 | 985 | if (vid && vlan_tci_to_pcp(match->wc.masks.vlan_tci)) { |
f8047558 | 986 | nxm_put_8(b, MFF_VLAN_PCP, oxm, |
9d84066c | 987 | vlan_tci_to_pcp(flow->vlan_tci)); |
95f61ba8 SH |
988 | } |
989 | ||
990 | } else { | |
f8047558 | 991 | nxm_put_16m(b, MFF_VLAN_TCI, oxm, flow->vlan_tci, |
81a76618 | 992 | match->wc.masks.vlan_tci); |
95f61ba8 | 993 | } |
09246b99 | 994 | |
b02475c5 SH |
995 | /* MPLS. */ |
996 | if (eth_type_mpls(flow->dl_type)) { | |
8bfd0fda | 997 | if (match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) { |
f8047558 | 998 | nxm_put_8(b, MFF_MPLS_TC, oxm, |
9d84066c | 999 | mpls_lse_to_tc(flow->mpls_lse[0])); |
b02475c5 SH |
1000 | } |
1001 | ||
8bfd0fda | 1002 | if (match->wc.masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) { |
f8047558 | 1003 | nxm_put_8(b, MFF_MPLS_BOS, oxm, |
9d84066c | 1004 | mpls_lse_to_bos(flow->mpls_lse[0])); |
b02475c5 SH |
1005 | } |
1006 | ||
8bfd0fda | 1007 | if (match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) { |
f8047558 | 1008 | nxm_put_32(b, MFF_MPLS_LABEL, oxm, |
8bfd0fda | 1009 | htonl(mpls_lse_to_label(flow->mpls_lse[0]))); |
b02475c5 SH |
1010 | } |
1011 | } | |
1012 | ||
66642cb4 | 1013 | /* L3. */ |
5e10215d BP |
1014 | if (is_ip_any(flow)) { |
1015 | nxm_put_ip(b, match, oxm); | |
8087f5ff MM |
1016 | } else if (flow->dl_type == htons(ETH_TYPE_ARP) || |
1017 | flow->dl_type == htons(ETH_TYPE_RARP)) { | |
09246b99 | 1018 | /* ARP. */ |
81a76618 | 1019 | if (match->wc.masks.nw_proto) { |
f8047558 | 1020 | nxm_put_16(b, MFF_ARP_OP, oxm, |
b5ae8913 | 1021 | htons(flow->nw_proto)); |
09246b99 | 1022 | } |
f8047558 | 1023 | nxm_put_32m(b, MFF_ARP_SPA, oxm, |
81a76618 | 1024 | flow->nw_src, match->wc.masks.nw_src); |
f8047558 | 1025 | nxm_put_32m(b, MFF_ARP_TPA, oxm, |
81a76618 | 1026 | flow->nw_dst, match->wc.masks.nw_dst); |
f8047558 | 1027 | nxm_put_eth_masked(b, MFF_ARP_SHA, oxm, |
81a76618 | 1028 | flow->arp_sha, match->wc.masks.arp_sha); |
f8047558 | 1029 | nxm_put_eth_masked(b, MFF_ARP_THA, oxm, |
81a76618 | 1030 | flow->arp_tha, match->wc.masks.arp_tha); |
09246b99 BP |
1031 | } |
1032 | ||
1033 | /* Tunnel ID. */ | |
f8047558 | 1034 | nxm_put_64m(b, MFF_TUN_ID, oxm, |
0ad90c84 JR |
1035 | flow->tunnel.tun_id, match->wc.masks.tunnel.tun_id); |
1036 | ||
1037 | /* Other tunnel metadata. */ | |
b666962b JG |
1038 | nxm_put_16m(b, MFF_TUN_FLAGS, oxm, |
1039 | htons(flow->tunnel.flags), htons(match->wc.masks.tunnel.flags)); | |
f8047558 | 1040 | nxm_put_32m(b, MFF_TUN_SRC, oxm, |
0ad90c84 | 1041 | flow->tunnel.ip_src, match->wc.masks.tunnel.ip_src); |
f8047558 | 1042 | nxm_put_32m(b, MFF_TUN_DST, oxm, |
0ad90c84 | 1043 | flow->tunnel.ip_dst, match->wc.masks.tunnel.ip_dst); |
7dad8e9a TLSC |
1044 | nxm_put_ipv6(b, MFF_TUN_IPV6_SRC, oxm, |
1045 | &flow->tunnel.ipv6_src, &match->wc.masks.tunnel.ipv6_src); | |
1046 | nxm_put_ipv6(b, MFF_TUN_IPV6_DST, oxm, | |
1047 | &flow->tunnel.ipv6_dst, &match->wc.masks.tunnel.ipv6_dst); | |
ac6073e3 MC |
1048 | nxm_put_16m(b, MFF_TUN_GBP_ID, oxm, |
1049 | flow->tunnel.gbp_id, match->wc.masks.tunnel.gbp_id); | |
1050 | nxm_put_8m(b, MFF_TUN_GBP_FLAGS, oxm, | |
1051 | flow->tunnel.gbp_flags, match->wc.masks.tunnel.gbp_flags); | |
9558d2a5 | 1052 | tun_metadata_to_nx_match(b, oxm, match); |
09246b99 | 1053 | |
b6c9e612 | 1054 | /* Registers. */ |
a678b23e BP |
1055 | if (oxm < OFP15_VERSION) { |
1056 | for (i = 0; i < FLOW_N_REGS; i++) { | |
f8047558 | 1057 | nxm_put_32m(b, MFF_REG0 + i, oxm, |
a678b23e BP |
1058 | htonl(flow->regs[i]), htonl(match->wc.masks.regs[i])); |
1059 | } | |
1060 | } else { | |
1061 | for (i = 0; i < FLOW_N_XREGS; i++) { | |
f8047558 | 1062 | nxm_put_64m(b, MFF_XREG0 + i, oxm, |
a678b23e BP |
1063 | htonll(flow_get_xreg(flow, i)), |
1064 | htonll(flow_get_xreg(&match->wc.masks, i))); | |
1065 | } | |
b6c9e612 BP |
1066 | } |
1067 | ||
8e53fe8c | 1068 | /* Packet mark. */ |
f8047558 | 1069 | nxm_put_32m(b, MFF_PKT_MARK, oxm, htonl(flow->pkt_mark), |
ac923e91 JG |
1070 | htonl(match->wc.masks.pkt_mark)); |
1071 | ||
07659514 JS |
1072 | /* Connection tracking. */ |
1073 | nxm_put_32m(b, MFF_CT_STATE, oxm, htonl(flow->ct_state), | |
1074 | htonl(match->wc.masks.ct_state)); | |
1075 | nxm_put_16m(b, MFF_CT_ZONE, oxm, htons(flow->ct_zone), | |
1076 | htons(match->wc.masks.ct_zone)); | |
8e53fe8c JS |
1077 | nxm_put_32m(b, MFF_CT_MARK, oxm, htonl(flow->ct_mark), |
1078 | htonl(match->wc.masks.ct_mark)); | |
8ff10dd5 JP |
1079 | nxm_put_128m(b, MFF_CT_LABEL, oxm, hton128(flow->ct_label), |
1080 | hton128(match->wc.masks.ct_label)); | |
07659514 | 1081 | |
969fc56c | 1082 | /* OpenFlow 1.1+ Metadata. */ |
f8047558 | 1083 | nxm_put_64m(b, MFF_METADATA, oxm, |
9d84066c | 1084 | flow->metadata, match->wc.masks.metadata); |
969fc56c | 1085 | |
e729e793 | 1086 | /* Cookie. */ |
f8047558 BP |
1087 | if (cookie_mask) { |
1088 | bool masked = cookie_mask != OVS_BE64_MAX; | |
1089 | ||
1090 | cookie &= cookie_mask; | |
1091 | nx_put_header__(b, NXM_NX_COOKIE, masked); | |
1092 | ofpbuf_put(b, &cookie, sizeof cookie); | |
1093 | if (masked) { | |
1094 | ofpbuf_put(b, &cookie_mask, sizeof cookie_mask); | |
1095 | } | |
1096 | } | |
e729e793 | 1097 | |
6fd6ed71 | 1098 | match_len = b->size - start_len; |
7623f4dd SH |
1099 | return match_len; |
1100 | } | |
1101 | ||
81a76618 | 1102 | /* Appends to 'b' the nx_match format that expresses 'match', plus enough zero |
7623f4dd SH |
1103 | * bytes to pad the nx_match out to a multiple of 8. For Flow Mod and Flow |
1104 | * Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied. | |
1105 | * Otherwise, 'cookie_mask' should be zero. | |
1106 | * | |
1107 | * This function can cause 'b''s data to be reallocated. | |
1108 | * | |
1109 | * Returns the number of bytes appended to 'b', excluding padding. The return | |
1110 | * value can be zero if it appended nothing at all to 'b' (which happens if | |
1111 | * 'cr' is a catch-all rule that matches every packet). */ | |
1112 | int | |
81a76618 | 1113 | nx_put_match(struct ofpbuf *b, const struct match *match, |
7623f4dd SH |
1114 | ovs_be64 cookie, ovs_be64 cookie_mask) |
1115 | { | |
9d84066c | 1116 | int match_len = nx_put_raw(b, 0, match, cookie, cookie_mask); |
7623f4dd | 1117 | |
f6e984d7 | 1118 | ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8)); |
7623f4dd SH |
1119 | return match_len; |
1120 | } | |
1121 | ||
9d84066c | 1122 | /* Appends to 'b' an struct ofp11_match_header followed by the OXM format that |
9f6e20b7 | 1123 | * expresses 'match', plus enough zero bytes to pad the data appended out to a |
81a76618 | 1124 | * multiple of 8. |
7623f4dd | 1125 | * |
9d84066c BP |
1126 | * OXM differs slightly among versions of OpenFlow. Specify the OpenFlow |
1127 | * version in use as 'version'. | |
1128 | * | |
7623f4dd SH |
1129 | * This function can cause 'b''s data to be reallocated. |
1130 | * | |
1131 | * Returns the number of bytes appended to 'b', excluding the padding. Never | |
1132 | * returns zero. */ | |
1133 | int | |
9d84066c BP |
1134 | oxm_put_match(struct ofpbuf *b, const struct match *match, |
1135 | enum ofp_version version) | |
7623f4dd SH |
1136 | { |
1137 | int match_len; | |
1138 | struct ofp11_match_header *omh; | |
6fd6ed71 | 1139 | size_t start_len = b->size; |
7623f4dd SH |
1140 | ovs_be64 cookie = htonll(0), cookie_mask = htonll(0); |
1141 | ||
1142 | ofpbuf_put_uninit(b, sizeof *omh); | |
9d84066c BP |
1143 | match_len = (nx_put_raw(b, version, match, cookie, cookie_mask) |
1144 | + sizeof *omh); | |
f6e984d7 | 1145 | ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8)); |
7623f4dd | 1146 | |
db5a1019 | 1147 | omh = ofpbuf_at(b, start_len, sizeof *omh); |
7623f4dd SH |
1148 | omh->type = htons(OFPMT_OXM); |
1149 | omh->length = htons(match_len); | |
1150 | ||
09246b99 BP |
1151 | return match_len; |
1152 | } | |
178742f9 | 1153 | |
9f6e20b7 BP |
1154 | /* Appends to 'b' the OXM formats that expresses 'match', without header or |
1155 | * padding. | |
1156 | * | |
1157 | * OXM differs slightly among versions of OpenFlow. Specify the OpenFlow | |
1158 | * version in use as 'version'. | |
1159 | * | |
1160 | * This function can cause 'b''s data to be reallocated. */ | |
1161 | void | |
1162 | oxm_put_raw(struct ofpbuf *b, const struct match *match, | |
1163 | enum ofp_version version) | |
1164 | { | |
1165 | nx_put_raw(b, version, match, 0, 0); | |
1166 | } | |
1167 | ||
53eb84a5 SH |
1168 | /* Appends to 'b' the nx_match format that expresses the tlv corresponding |
1169 | * to 'id'. If mask is not all-ones then it is also formated as the value | |
1170 | * of the tlv. */ | |
1171 | static void | |
1172 | nx_format_mask_tlv(struct ds *ds, enum mf_field_id id, | |
1173 | const union mf_value *mask) | |
1174 | { | |
1175 | const struct mf_field *mf = mf_from_id(id); | |
1176 | ||
1177 | ds_put_format(ds, "%s", mf->name); | |
1178 | ||
1179 | if (!is_all_ones(mask, mf->n_bytes)) { | |
1180 | ds_put_char(ds, '='); | |
1181 | mf_format(mf, mask, NULL, ds); | |
1182 | } | |
1183 | ||
1184 | ds_put_char(ds, ','); | |
1185 | } | |
1186 | ||
1187 | /* Appends a string representation of 'fa_' to 'ds'. | |
1188 | * The TLVS value of 'fa_' is treated as a mask and | |
1189 | * only the name of fields is formated if it is all ones. */ | |
1190 | void | |
1191 | oxm_format_field_array(struct ds *ds, const struct field_array *fa) | |
1192 | { | |
1193 | size_t start_len = ds->length; | |
e8dba719 | 1194 | size_t i, offset = 0; |
53eb84a5 | 1195 | |
e8dba719 JR |
1196 | BITMAP_FOR_EACH_1 (i, MFF_N_IDS, fa->used.bm) { |
1197 | const struct mf_field *mf = mf_from_id(i); | |
1198 | union mf_value value; | |
1199 | ||
1200 | memcpy(&value, fa->values + offset, mf->n_bytes); | |
1201 | nx_format_mask_tlv(ds, i, &value); | |
1202 | offset += mf->n_bytes; | |
53eb84a5 SH |
1203 | } |
1204 | ||
1205 | if (ds->length > start_len) { | |
1206 | ds_chomp(ds, ','); | |
1207 | } | |
1208 | } | |
1209 | ||
1210 | /* Appends to 'b' a series of OXM TLVs corresponding to the series | |
1211 | * of enum mf_field_id and value tuples in 'fa_'. | |
1212 | * | |
1213 | * OXM differs slightly among versions of OpenFlow. Specify the OpenFlow | |
1214 | * version in use as 'version'. | |
1215 | * | |
1216 | * This function can cause 'b''s data to be reallocated. | |
1217 | * | |
1218 | * Returns the number of bytes appended to 'b'. May return zero. */ | |
1219 | int | |
1220 | oxm_put_field_array(struct ofpbuf *b, const struct field_array *fa, | |
1221 | enum ofp_version version) | |
1222 | { | |
1223 | size_t start_len = b->size; | |
53eb84a5 SH |
1224 | |
1225 | /* Field arrays are only used with the group selection method | |
d5651b84 | 1226 | * property and group properties are only available in OpenFlow 1.5+. |
53eb84a5 SH |
1227 | * So the following assertion should never fail. |
1228 | * | |
1229 | * If support for older OpenFlow versions is desired then some care | |
1230 | * will need to be taken of different TLVs that handle the same | |
1231 | * flow fields. In particular: | |
1232 | * - VLAN_TCI, VLAN_VID and MFF_VLAN_PCP | |
1233 | * - IP_DSCP_MASK and DSCP_SHIFTED | |
1234 | * - REGS and XREGS | |
1235 | */ | |
1236 | ovs_assert(version >= OFP15_VERSION); | |
1237 | ||
e8dba719 JR |
1238 | size_t i, offset = 0; |
1239 | ||
1240 | BITMAP_FOR_EACH_1 (i, MFF_N_IDS, fa->used.bm) { | |
1241 | const struct mf_field *mf = mf_from_id(i); | |
1242 | union mf_value value; | |
1243 | ||
1244 | memcpy(&value, fa->values + offset, mf->n_bytes); | |
1245 | ||
1246 | int len = mf_field_len(mf, &value, NULL, NULL); | |
1247 | nxm_put__(b, i, version, &value + mf->n_bytes - len, NULL, len); | |
1248 | offset += mf->n_bytes; | |
53eb84a5 SH |
1249 | } |
1250 | ||
1251 | return b->size - start_len; | |
1252 | } | |
1253 | ||
f8047558 | 1254 | static void |
508a9338 | 1255 | nx_put_header__(struct ofpbuf *b, uint64_t header, bool masked) |
f8047558 | 1256 | { |
508a9338 BP |
1257 | uint64_t masked_header = masked ? nxm_make_wild_header(header) : header; |
1258 | ovs_be64 network_header = htonll(masked_header); | |
f8047558 | 1259 | |
508a9338 | 1260 | ofpbuf_put(b, &network_header, nxm_header_len(header)); |
f8047558 BP |
1261 | } |
1262 | ||
178742f9 BP |
1263 | void |
1264 | nx_put_header(struct ofpbuf *b, enum mf_field_id field, | |
1265 | enum ofp_version version, bool masked) | |
1266 | { | |
f8047558 | 1267 | nx_put_header__(b, mf_oxm_header(field, version), masked); |
178742f9 BP |
1268 | } |
1269 | ||
dc3eb953 JG |
1270 | static void |
1271 | nx_put_header_len(struct ofpbuf *b, enum mf_field_id field, | |
1272 | enum ofp_version version, bool masked, size_t n_bytes) | |
1273 | { | |
1274 | uint64_t header = mf_oxm_header(field, version); | |
1275 | ||
1276 | header = NXM_HEADER(nxm_vendor(header), nxm_class(header), | |
1277 | nxm_field(header), false, | |
1278 | nxm_experimenter_len(header) + n_bytes); | |
1279 | ||
1280 | nx_put_header__(b, header, masked); | |
1281 | } | |
1282 | ||
178742f9 BP |
1283 | void |
1284 | nx_put_entry(struct ofpbuf *b, | |
1285 | enum mf_field_id field, enum ofp_version version, | |
1286 | const union mf_value *value, const union mf_value *mask) | |
1287 | { | |
4ede8c79 | 1288 | const struct mf_field *mf = mf_from_id(field); |
1cb20095 | 1289 | bool masked; |
4ede8c79 | 1290 | int len, offset; |
178742f9 | 1291 | |
1cb20095 | 1292 | len = mf_field_len(mf, value, mask, &masked); |
4ede8c79 JG |
1293 | offset = mf->n_bytes - len; |
1294 | ||
1295 | nx_put_header_len(b, field, version, masked, len); | |
1296 | ofpbuf_put(b, &value->u8 + offset, len); | |
178742f9 | 1297 | if (masked) { |
4ede8c79 | 1298 | ofpbuf_put(b, &mask->u8 + offset, len); |
178742f9 BP |
1299 | } |
1300 | } | |
09246b99 BP |
1301 | \f |
1302 | /* nx_match_to_string() and helpers. */ | |
1303 | ||
508a9338 | 1304 | static void format_nxm_field_name(struct ds *, uint64_t header); |
f393f81e | 1305 | |
09246b99 BP |
1306 | char * |
1307 | nx_match_to_string(const uint8_t *p, unsigned int match_len) | |
1308 | { | |
09246b99 BP |
1309 | if (!match_len) { |
1310 | return xstrdup("<any>"); | |
1311 | } | |
1312 | ||
0a2869d5 BP |
1313 | struct ofpbuf b = ofpbuf_const_initializer(p, match_len); |
1314 | struct ds s = DS_EMPTY_INITIALIZER; | |
6fd6ed71 | 1315 | while (b.size) { |
178742f9 BP |
1316 | union mf_value value; |
1317 | union mf_value mask; | |
1318 | enum ofperr error; | |
508a9338 | 1319 | uint64_t header; |
178742f9 BP |
1320 | int value_len; |
1321 | ||
1322 | error = nx_pull_entry__(&b, true, &header, NULL, &value, &mask); | |
1323 | if (error) { | |
1324 | break; | |
1325 | } | |
1326 | value_len = MIN(sizeof value, nxm_field_bytes(header)); | |
09246b99 BP |
1327 | |
1328 | if (s.length) { | |
1329 | ds_put_cstr(&s, ", "); | |
1330 | } | |
1331 | ||
f393f81e | 1332 | format_nxm_field_name(&s, header); |
09246b99 BP |
1333 | ds_put_char(&s, '('); |
1334 | ||
178742f9 BP |
1335 | for (int i = 0; i < value_len; i++) { |
1336 | ds_put_format(&s, "%02x", ((const uint8_t *) &value)[i]); | |
09246b99 | 1337 | } |
178742f9 | 1338 | if (nxm_hasmask(header)) { |
09246b99 | 1339 | ds_put_char(&s, '/'); |
178742f9 BP |
1340 | for (int i = 0; i < value_len; i++) { |
1341 | ds_put_format(&s, "%02x", ((const uint8_t *) &mask)[i]); | |
09246b99 BP |
1342 | } |
1343 | } | |
1344 | ds_put_char(&s, ')'); | |
09246b99 BP |
1345 | } |
1346 | ||
6fd6ed71 | 1347 | if (b.size) { |
09246b99 BP |
1348 | if (s.length) { |
1349 | ds_put_cstr(&s, ", "); | |
1350 | } | |
1351 | ||
6fd6ed71 | 1352 | ds_put_format(&s, "<%u invalid bytes>", b.size); |
09246b99 BP |
1353 | } |
1354 | ||
1355 | return ds_steal_cstr(&s); | |
1356 | } | |
1357 | ||
7623f4dd | 1358 | char * |
db5a1019 | 1359 | oxm_match_to_string(const struct ofpbuf *p, unsigned int match_len) |
7623f4dd | 1360 | { |
6fd6ed71 | 1361 | const struct ofp11_match_header *omh = p->data; |
7623f4dd SH |
1362 | uint16_t match_len_; |
1363 | struct ds s; | |
1364 | ||
1365 | ds_init(&s); | |
1366 | ||
1367 | if (match_len < sizeof *omh) { | |
1368 | ds_put_format(&s, "<match too short: %u>", match_len); | |
1369 | goto err; | |
1370 | } | |
1371 | ||
1372 | if (omh->type != htons(OFPMT_OXM)) { | |
1373 | ds_put_format(&s, "<bad match type field: %u>", ntohs(omh->type)); | |
1374 | goto err; | |
1375 | } | |
1376 | ||
1377 | match_len_ = ntohs(omh->length); | |
1378 | if (match_len_ < sizeof *omh) { | |
1379 | ds_put_format(&s, "<match length field too short: %u>", match_len_); | |
1380 | goto err; | |
1381 | } | |
1382 | ||
1383 | if (match_len_ != match_len) { | |
1384 | ds_put_format(&s, "<match length field incorrect: %u != %u>", | |
1385 | match_len_, match_len); | |
1386 | goto err; | |
1387 | } | |
1388 | ||
db5a1019 AW |
1389 | return nx_match_to_string(ofpbuf_at(p, sizeof *omh, 0), |
1390 | match_len - sizeof *omh); | |
7623f4dd SH |
1391 | |
1392 | err: | |
1393 | return ds_steal_cstr(&s); | |
1394 | } | |
1395 | ||
178742f9 BP |
1396 | void |
1397 | nx_format_field_name(enum mf_field_id id, enum ofp_version version, | |
1398 | struct ds *s) | |
1399 | { | |
1400 | format_nxm_field_name(s, mf_oxm_header(id, version)); | |
1401 | } | |
1402 | ||
f393f81e | 1403 | static void |
508a9338 | 1404 | format_nxm_field_name(struct ds *s, uint64_t header) |
f393f81e | 1405 | { |
178742f9 BP |
1406 | const struct nxm_field *f = nxm_field_by_header(header); |
1407 | if (f) { | |
1408 | ds_put_cstr(s, f->name); | |
1409 | if (nxm_hasmask(header)) { | |
28da1f8f BP |
1410 | ds_put_cstr(s, "_W"); |
1411 | } | |
e729e793 JP |
1412 | } else if (header == NXM_NX_COOKIE) { |
1413 | ds_put_cstr(s, "NXM_NX_COOKIE"); | |
1414 | } else if (header == NXM_NX_COOKIE_W) { | |
1415 | ds_put_cstr(s, "NXM_NX_COOKIE_W"); | |
f393f81e | 1416 | } else { |
c8058af7 | 1417 | ds_put_format(s, "%d:%d", nxm_class(header), nxm_field(header)); |
f393f81e BP |
1418 | } |
1419 | } | |
1420 | ||
178742f9 BP |
1421 | static bool |
1422 | streq_len(const char *a, size_t a_len, const char *b) | |
1423 | { | |
1424 | return strlen(b) == a_len && !memcmp(a, b, a_len); | |
1425 | } | |
1426 | ||
508a9338 | 1427 | static uint64_t |
558d80cb | 1428 | parse_nxm_field_name(const char *name, int name_len) |
09246b99 | 1429 | { |
178742f9 | 1430 | const struct nxm_field *f; |
28da1f8f | 1431 | bool wild; |
28da1f8f | 1432 | |
178742f9 BP |
1433 | f = mf_parse_subfield_name(name, name_len, &wild); |
1434 | if (f) { | |
b5ae8913 | 1435 | if (!wild) { |
178742f9 BP |
1436 | return f->header; |
1437 | } else if (mf_from_id(f->id)->maskable != MFM_NONE) { | |
1438 | return nxm_make_wild_header(f->header); | |
09246b99 BP |
1439 | } |
1440 | } | |
1441 | ||
178742f9 BP |
1442 | if (streq_len(name, name_len, "NXM_NX_COOKIE")) { |
1443 | return NXM_NX_COOKIE; | |
1444 | } else if (streq_len(name, name_len, "NXM_NX_COOKIE_W")) { | |
1445 | return NXM_NX_COOKIE_W; | |
e729e793 JP |
1446 | } |
1447 | ||
508a9338 | 1448 | /* Check whether it's a field header value as hex. |
558d80cb BP |
1449 | * (This isn't ordinarily useful except for testing error behavior.) */ |
1450 | if (name_len == 8) { | |
508a9338 | 1451 | uint64_t header; |
0429d959 BP |
1452 | bool ok; |
1453 | ||
508a9338 | 1454 | header = hexits_value(name, name_len, &ok) << 32; |
0429d959 | 1455 | if (ok) { |
558d80cb BP |
1456 | return header; |
1457 | } | |
508a9338 BP |
1458 | } else if (name_len == 16) { |
1459 | uint64_t header; | |
1460 | bool ok; | |
1461 | ||
1462 | header = hexits_value(name, name_len, &ok); | |
1463 | if (ok && is_experimenter_oxm(header)) { | |
1464 | return header; | |
1465 | } | |
558d80cb BP |
1466 | } |
1467 | ||
1468 | return 0; | |
09246b99 | 1469 | } |
09246b99 BP |
1470 | \f |
1471 | /* nx_match_from_string(). */ | |
1472 | ||
7623f4dd SH |
1473 | static int |
1474 | nx_match_from_string_raw(const char *s, struct ofpbuf *b) | |
09246b99 BP |
1475 | { |
1476 | const char *full_s = s; | |
6fd6ed71 | 1477 | const size_t start_len = b->size; |
09246b99 BP |
1478 | |
1479 | if (!strcmp(s, "<any>")) { | |
6fd6ed71 | 1480 | /* Ensure that 'b->data' isn't actually null. */ |
09246b99 BP |
1481 | ofpbuf_prealloc_tailroom(b, 1); |
1482 | return 0; | |
1483 | } | |
1484 | ||
1485 | for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) { | |
558d80cb | 1486 | const char *name; |
508a9338 | 1487 | uint64_t header; |
00fe22f8 | 1488 | ovs_be64 nw_header; |
09246b99 | 1489 | int name_len; |
78090f63 | 1490 | size_t n; |
09246b99 | 1491 | |
558d80cb | 1492 | name = s; |
09246b99 BP |
1493 | name_len = strcspn(s, "("); |
1494 | if (s[name_len] != '(') { | |
1495 | ovs_fatal(0, "%s: missing ( at end of nx_match", full_s); | |
1496 | } | |
1497 | ||
558d80cb BP |
1498 | header = parse_nxm_field_name(name, name_len); |
1499 | if (!header) { | |
09246b99 BP |
1500 | ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s); |
1501 | } | |
1502 | ||
1503 | s += name_len + 1; | |
1504 | ||
e659c96b | 1505 | b->header = ofpbuf_put_uninit(b, nxm_header_len(header)); |
78090f63 BP |
1506 | s = ofpbuf_put_hex(b, s, &n); |
1507 | if (n != nxm_field_bytes(header)) { | |
00fe22f8 JG |
1508 | const struct mf_field *field = mf_from_oxm_header(header); |
1509 | ||
1510 | if (field && field->variable_len) { | |
1511 | if (n <= field->n_bytes) { | |
1512 | int len = (nxm_hasmask(header) ? n * 2 : n) + | |
1513 | nxm_experimenter_len(header); | |
1514 | ||
1515 | header = NXM_HEADER(nxm_vendor(header), nxm_class(header), | |
1516 | nxm_field(header), | |
1517 | nxm_hasmask(header) ? 1 : 0, len); | |
1518 | } else { | |
1519 | ovs_fatal(0, "expected to read at most %d bytes but got " | |
1520 | "%"PRIuSIZE, field->n_bytes, n); | |
1521 | } | |
1522 | } else { | |
1523 | ovs_fatal(0, "expected to read %d bytes but got %"PRIuSIZE, | |
1524 | nxm_field_bytes(header), n); | |
1525 | } | |
78090f63 | 1526 | } |
00fe22f8 | 1527 | nw_header = htonll(header); |
e659c96b | 1528 | memcpy(b->header, &nw_header, nxm_header_len(header)); |
00fe22f8 | 1529 | |
178742f9 | 1530 | if (nxm_hasmask(header)) { |
09246b99 BP |
1531 | s += strspn(s, " "); |
1532 | if (*s != '/') { | |
558d80cb BP |
1533 | ovs_fatal(0, "%s: missing / in masked field %.*s", |
1534 | full_s, name_len, name); | |
09246b99 | 1535 | } |
78090f63 BP |
1536 | s = ofpbuf_put_hex(b, s + 1, &n); |
1537 | if (n != nxm_field_bytes(header)) { | |
1538 | ovs_fatal(0, "%.2s: hex digits expected", s); | |
1539 | } | |
09246b99 BP |
1540 | } |
1541 | ||
1542 | s += strspn(s, " "); | |
1543 | if (*s != ')') { | |
558d80cb BP |
1544 | ovs_fatal(0, "%s: missing ) following field %.*s", |
1545 | full_s, name_len, name); | |
09246b99 BP |
1546 | } |
1547 | s++; | |
1548 | } | |
1549 | ||
6fd6ed71 | 1550 | return b->size - start_len; |
7623f4dd SH |
1551 | } |
1552 | ||
1553 | int | |
1554 | nx_match_from_string(const char *s, struct ofpbuf *b) | |
1555 | { | |
1556 | int match_len = nx_match_from_string_raw(s, b); | |
f6e984d7 | 1557 | ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8)); |
7623f4dd SH |
1558 | return match_len; |
1559 | } | |
1560 | ||
1561 | int | |
1562 | oxm_match_from_string(const char *s, struct ofpbuf *b) | |
1563 | { | |
1564 | int match_len; | |
1565 | struct ofp11_match_header *omh; | |
6fd6ed71 | 1566 | size_t start_len = b->size; |
7623f4dd SH |
1567 | |
1568 | ofpbuf_put_uninit(b, sizeof *omh); | |
1569 | match_len = nx_match_from_string_raw(s, b) + sizeof *omh; | |
f6e984d7 | 1570 | ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8)); |
7623f4dd | 1571 | |
db5a1019 | 1572 | omh = ofpbuf_at(b, start_len, sizeof *omh); |
7623f4dd SH |
1573 | omh->type = htons(OFPMT_OXM); |
1574 | omh->length = htons(match_len); | |
1575 | ||
09246b99 BP |
1576 | return match_len; |
1577 | } | |
b6c9e612 | 1578 | \f |
bdda5aca BP |
1579 | /* Parses 's' as a "move" action, in the form described in ovs-ofctl(8), into |
1580 | * '*move'. | |
1581 | * | |
1582 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
1583 | * error. The caller is responsible for freeing the returned string. */ | |
cab50449 | 1584 | char * OVS_WARN_UNUSED_RESULT |
f25d0cf3 | 1585 | nxm_parse_reg_move(struct ofpact_reg_move *move, const char *s) |
f393f81e BP |
1586 | { |
1587 | const char *full_s = s; | |
bdda5aca | 1588 | char *error; |
f393f81e | 1589 | |
bdda5aca BP |
1590 | error = mf_parse_subfield__(&move->src, &s); |
1591 | if (error) { | |
1592 | return error; | |
1593 | } | |
f393f81e | 1594 | if (strncmp(s, "->", 2)) { |
bdda5aca | 1595 | return xasprintf("%s: missing `->' following source", full_s); |
f393f81e BP |
1596 | } |
1597 | s += 2; | |
bdda5aca BP |
1598 | error = mf_parse_subfield(&move->dst, s); |
1599 | if (error) { | |
1600 | return error; | |
f393f81e BP |
1601 | } |
1602 | ||
f25d0cf3 | 1603 | if (move->src.n_bits != move->dst.n_bits) { |
bdda5aca BP |
1604 | return xasprintf("%s: source field is %d bits wide but destination is " |
1605 | "%d bits wide", full_s, | |
1606 | move->src.n_bits, move->dst.n_bits); | |
f393f81e | 1607 | } |
bdda5aca | 1608 | return NULL; |
f393f81e | 1609 | } |
f393f81e | 1610 | \f |
7eb4b1f1 | 1611 | /* nxm_format_reg_move(). */ |
f393f81e | 1612 | |
f393f81e | 1613 | void |
f25d0cf3 | 1614 | nxm_format_reg_move(const struct ofpact_reg_move *move, struct ds *s) |
f393f81e | 1615 | { |
b1c5bf1f | 1616 | ds_put_format(s, "%smove:%s", colors.special, colors.end); |
f25d0cf3 | 1617 | mf_format_subfield(&move->src, s); |
b1c5bf1f | 1618 | ds_put_format(s, "%s->%s", colors.special, colors.end); |
f25d0cf3 | 1619 | mf_format_subfield(&move->dst, s); |
f393f81e BP |
1620 | } |
1621 | ||
f25d0cf3 BP |
1622 | \f |
1623 | enum ofperr | |
f25d0cf3 | 1624 | nxm_reg_move_check(const struct ofpact_reg_move *move, const struct flow *flow) |
43edca57 | 1625 | { |
90bf1e07 | 1626 | enum ofperr error; |
43edca57 | 1627 | |
f25d0cf3 | 1628 | error = mf_check_src(&move->src, flow); |
43edca57 EJ |
1629 | if (error) { |
1630 | return error; | |
b6c9e612 BP |
1631 | } |
1632 | ||
b8778a0d | 1633 | return mf_check_dst(&move->dst, flow); |
f25d0cf3 | 1634 | } |
f25d0cf3 | 1635 | \f |
7eb4b1f1 | 1636 | /* nxm_execute_reg_move(). */ |
b6c9e612 | 1637 | |
816fd533 | 1638 | void |
f25d0cf3 | 1639 | nxm_reg_load(const struct mf_subfield *dst, uint64_t src_data, |
f74e7df7 | 1640 | struct flow *flow, struct flow_wildcards *wc) |
816fd533 | 1641 | { |
9bab681f | 1642 | union mf_subvalue src_subvalue; |
f74e7df7 | 1643 | union mf_subvalue mask_value; |
9bab681f | 1644 | ovs_be64 src_data_be = htonll(src_data); |
f25d0cf3 | 1645 | |
f74e7df7 JP |
1646 | memset(&mask_value, 0xff, sizeof mask_value); |
1647 | mf_write_subfield_flow(dst, &mask_value, &wc->masks); | |
1648 | ||
9bab681f IY |
1649 | bitwise_copy(&src_data_be, sizeof src_data_be, 0, |
1650 | &src_subvalue, sizeof src_subvalue, 0, | |
1651 | sizeof src_data_be * 8); | |
1652 | mf_write_subfield_flow(dst, &src_subvalue, flow); | |
b6c9e612 | 1653 | } |
bd85dac1 AZ |
1654 | \f |
1655 | /* nxm_parse_stack_action, works for both push() and pop(). */ | |
bdda5aca BP |
1656 | |
1657 | /* Parses 's' as a "push" or "pop" action, in the form described in | |
1658 | * ovs-ofctl(8), into '*stack_action'. | |
1659 | * | |
1660 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
1661 | * error. The caller is responsible for freeing the returned string. */ | |
cab50449 | 1662 | char * OVS_WARN_UNUSED_RESULT |
bd85dac1 AZ |
1663 | nxm_parse_stack_action(struct ofpact_stack *stack_action, const char *s) |
1664 | { | |
bdda5aca BP |
1665 | char *error; |
1666 | ||
1667 | error = mf_parse_subfield__(&stack_action->subfield, &s); | |
1668 | if (error) { | |
1669 | return error; | |
1670 | } | |
1671 | ||
bd85dac1 | 1672 | if (*s != '\0') { |
bdda5aca | 1673 | return xasprintf("%s: trailing garbage following push or pop", s); |
bd85dac1 | 1674 | } |
bdda5aca BP |
1675 | |
1676 | return NULL; | |
bd85dac1 AZ |
1677 | } |
1678 | ||
1679 | void | |
1680 | nxm_format_stack_push(const struct ofpact_stack *push, struct ds *s) | |
1681 | { | |
b1c5bf1f | 1682 | ds_put_format(s, "%spush:%s", colors.param, colors.end); |
bd85dac1 AZ |
1683 | mf_format_subfield(&push->subfield, s); |
1684 | } | |
1685 | ||
1686 | void | |
1687 | nxm_format_stack_pop(const struct ofpact_stack *pop, struct ds *s) | |
1688 | { | |
b1c5bf1f | 1689 | ds_put_format(s, "%spop:%s", colors.param, colors.end); |
bd85dac1 AZ |
1690 | mf_format_subfield(&pop->subfield, s); |
1691 | } | |
1692 | ||
bd85dac1 AZ |
1693 | enum ofperr |
1694 | nxm_stack_push_check(const struct ofpact_stack *push, | |
1695 | const struct flow *flow) | |
1696 | { | |
1697 | return mf_check_src(&push->subfield, flow); | |
1698 | } | |
1699 | ||
1700 | enum ofperr | |
1701 | nxm_stack_pop_check(const struct ofpact_stack *pop, | |
1702 | const struct flow *flow) | |
1703 | { | |
1704 | return mf_check_dst(&pop->subfield, flow); | |
1705 | } | |
1706 | ||
bd85dac1 AZ |
1707 | /* nxm_execute_stack_push(), nxm_execute_stack_pop(). */ |
1708 | static void | |
1709 | nx_stack_push(struct ofpbuf *stack, union mf_subvalue *v) | |
1710 | { | |
1711 | ofpbuf_put(stack, v, sizeof *v); | |
1712 | } | |
1713 | ||
1714 | static union mf_subvalue * | |
1715 | nx_stack_pop(struct ofpbuf *stack) | |
1716 | { | |
1717 | union mf_subvalue *v = NULL; | |
1718 | ||
6fd6ed71 | 1719 | if (stack->size) { |
1f317cb5 | 1720 | |
6fd6ed71 | 1721 | stack->size -= sizeof *v; |
bd85dac1 AZ |
1722 | v = (union mf_subvalue *) ofpbuf_tail(stack); |
1723 | } | |
1724 | ||
1725 | return v; | |
1726 | } | |
1727 | ||
1728 | void | |
1729 | nxm_execute_stack_push(const struct ofpact_stack *push, | |
bcd2633a JP |
1730 | const struct flow *flow, struct flow_wildcards *wc, |
1731 | struct ofpbuf *stack) | |
bd85dac1 | 1732 | { |
bcd2633a | 1733 | union mf_subvalue mask_value; |
bd85dac1 AZ |
1734 | union mf_subvalue dst_value; |
1735 | ||
bcd2633a JP |
1736 | memset(&mask_value, 0xff, sizeof mask_value); |
1737 | mf_write_subfield_flow(&push->subfield, &mask_value, &wc->masks); | |
1738 | ||
bd85dac1 AZ |
1739 | mf_read_subfield(&push->subfield, flow, &dst_value); |
1740 | nx_stack_push(stack, &dst_value); | |
1741 | } | |
1742 | ||
1743 | void | |
1744 | nxm_execute_stack_pop(const struct ofpact_stack *pop, | |
f74e7df7 JP |
1745 | struct flow *flow, struct flow_wildcards *wc, |
1746 | struct ofpbuf *stack) | |
bd85dac1 AZ |
1747 | { |
1748 | union mf_subvalue *src_value; | |
1749 | ||
1750 | src_value = nx_stack_pop(stack); | |
1751 | ||
1752 | /* Only pop if stack is not empty. Otherwise, give warning. */ | |
1753 | if (src_value) { | |
f74e7df7 JP |
1754 | union mf_subvalue mask_value; |
1755 | ||
1756 | memset(&mask_value, 0xff, sizeof mask_value); | |
1757 | mf_write_subfield_flow(&pop->subfield, &mask_value, &wc->masks); | |
bd85dac1 AZ |
1758 | mf_write_subfield_flow(&pop->subfield, src_value, flow); |
1759 | } else { | |
1760 | if (!VLOG_DROP_WARN(&rl)) { | |
1761 | char *flow_str = flow_to_string(flow); | |
1774d762 | 1762 | VLOG_WARN_RL(&rl, "Failed to pop from an empty stack. On flow\n" |
bd85dac1 AZ |
1763 | " %s", flow_str); |
1764 | free(flow_str); | |
1765 | } | |
1766 | } | |
1767 | } | |
178742f9 BP |
1768 | \f |
1769 | /* Formats 'sf' into 's' in a format normally acceptable to | |
1770 | * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if | |
1771 | * sf->field has no NXM name.) */ | |
1772 | void | |
1773 | mf_format_subfield(const struct mf_subfield *sf, struct ds *s) | |
1774 | { | |
1775 | if (!sf->field) { | |
1776 | ds_put_cstr(s, "<unknown>"); | |
1777 | } else { | |
e6556fe3 | 1778 | const struct nxm_field *f = nxm_field_by_mf_id(sf->field->id, 0); |
178742f9 BP |
1779 | ds_put_cstr(s, f ? f->name : sf->field->name); |
1780 | } | |
1781 | ||
1782 | if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) { | |
1783 | ds_put_cstr(s, "[]"); | |
1784 | } else if (sf->n_bits == 1) { | |
1785 | ds_put_format(s, "[%d]", sf->ofs); | |
1786 | } else { | |
1787 | ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1); | |
1788 | } | |
1789 | } | |
1790 | ||
1791 | static const struct nxm_field * | |
1792 | mf_parse_subfield_name(const char *name, int name_len, bool *wild) | |
1793 | { | |
1794 | *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2); | |
1795 | if (*wild) { | |
1796 | name_len -= 2; | |
1797 | } | |
1798 | ||
1799 | return nxm_field_by_name(name, name_len); | |
1800 | } | |
1801 | ||
1802 | /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful, | |
1803 | * returns NULL and advances '*sp' to the first byte following the parsed | |
1804 | * string. On failure, returns a malloc()'d error message, does not modify | |
1805 | * '*sp', and does not properly initialize 'sf'. | |
1806 | * | |
1807 | * The syntax parsed from '*sp' takes the form "header[start..end]" where | |
1808 | * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive) | |
1809 | * bit indexes. "..end" may be omitted to indicate a single bit. "start..end" | |
1810 | * may both be omitted (the [] are still required) to indicate an entire | |
1811 | * field. */ | |
cab50449 | 1812 | char * OVS_WARN_UNUSED_RESULT |
178742f9 BP |
1813 | mf_parse_subfield__(struct mf_subfield *sf, const char **sp) |
1814 | { | |
1815 | const struct mf_field *field; | |
1816 | const struct nxm_field *f; | |
1817 | const char *name; | |
1818 | int start, end; | |
1819 | const char *s; | |
1820 | int name_len; | |
1821 | bool wild; | |
1822 | ||
1823 | s = *sp; | |
1824 | name = s; | |
1825 | name_len = strcspn(s, "["); | |
1826 | if (s[name_len] != '[') { | |
1827 | return xasprintf("%s: missing [ looking for field name", *sp); | |
1828 | } | |
1829 | ||
1830 | f = mf_parse_subfield_name(name, name_len, &wild); | |
1831 | if (!f) { | |
1832 | return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s); | |
1833 | } | |
1834 | field = mf_from_id(f->id); | |
1835 | ||
1836 | s += name_len; | |
1837 | if (ovs_scan(s, "[%d..%d]", &start, &end)) { | |
1838 | /* Nothing to do. */ | |
1839 | } else if (ovs_scan(s, "[%d]", &start)) { | |
1840 | end = start; | |
1841 | } else if (!strncmp(s, "[]", 2)) { | |
1842 | start = 0; | |
1843 | end = field->n_bits - 1; | |
1844 | } else { | |
1845 | return xasprintf("%s: syntax error expecting [] or [<bit>] or " | |
1846 | "[<start>..<end>]", *sp); | |
1847 | } | |
1848 | s = strchr(s, ']') + 1; | |
1849 | ||
1850 | if (start > end) { | |
1851 | return xasprintf("%s: starting bit %d is after ending bit %d", | |
1852 | *sp, start, end); | |
1853 | } else if (start >= field->n_bits) { | |
1854 | return xasprintf("%s: starting bit %d is not valid because field is " | |
1855 | "only %d bits wide", *sp, start, field->n_bits); | |
1856 | } else if (end >= field->n_bits){ | |
1857 | return xasprintf("%s: ending bit %d is not valid because field is " | |
1858 | "only %d bits wide", *sp, end, field->n_bits); | |
1859 | } | |
1860 | ||
1861 | sf->field = field; | |
1862 | sf->ofs = start; | |
1863 | sf->n_bits = end - start + 1; | |
1864 | ||
1865 | *sp = s; | |
1866 | return NULL; | |
1867 | } | |
1868 | ||
1869 | /* Parses a subfield from the entirety of 's' into 'sf'. Returns NULL if | |
1870 | * successful, otherwise a malloc()'d string describing the error. The caller | |
1871 | * is responsible for freeing the returned string. | |
1872 | * | |
1873 | * The syntax parsed from 's' takes the form "header[start..end]" where | |
1874 | * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive) | |
1875 | * bit indexes. "..end" may be omitted to indicate a single bit. "start..end" | |
1876 | * may both be omitted (the [] are still required) to indicate an entire | |
1877 | * field. */ | |
cab50449 | 1878 | char * OVS_WARN_UNUSED_RESULT |
178742f9 BP |
1879 | mf_parse_subfield(struct mf_subfield *sf, const char *s) |
1880 | { | |
1881 | char *error = mf_parse_subfield__(sf, &s); | |
1882 | if (!error && s[0]) { | |
1883 | error = xstrdup("unexpected input following field syntax"); | |
1884 | } | |
1885 | return error; | |
1886 | } | |
1887 | \f | |
1888 | /* Returns an bitmap in which each bit corresponds to the like-numbered field | |
1889 | * in the OFPXMC12_OPENFLOW_BASIC OXM class, in which the bit values are taken | |
1890 | * from the 'fields' bitmap. Only fields defined in OpenFlow 'version' are | |
1891 | * considered. | |
1892 | * | |
1893 | * This is useful for encoding OpenFlow 1.2 table stats messages. */ | |
1894 | ovs_be64 | |
1895 | oxm_bitmap_from_mf_bitmap(const struct mf_bitmap *fields, | |
1896 | enum ofp_version version) | |
1897 | { | |
1898 | uint64_t oxm_bitmap = 0; | |
1899 | int i; | |
1900 | ||
1901 | BITMAP_FOR_EACH_1 (i, MFF_N_IDS, fields->bm) { | |
508a9338 | 1902 | uint64_t oxm = mf_oxm_header(i, version); |
c8058af7 | 1903 | uint32_t class = nxm_class(oxm); |
178742f9 BP |
1904 | int field = nxm_field(oxm); |
1905 | ||
c8058af7 | 1906 | if (class == OFPXMC12_OPENFLOW_BASIC && field < 64) { |
178742f9 BP |
1907 | oxm_bitmap |= UINT64_C(1) << field; |
1908 | } | |
1909 | } | |
1910 | return htonll(oxm_bitmap); | |
1911 | } | |
1912 | ||
1913 | /* Opposite conversion from oxm_bitmap_from_mf_bitmap(). | |
1914 | * | |
1915 | * This is useful for decoding OpenFlow 1.2 table stats messages. */ | |
1916 | struct mf_bitmap | |
1917 | oxm_bitmap_to_mf_bitmap(ovs_be64 oxm_bitmap, enum ofp_version version) | |
1918 | { | |
1919 | struct mf_bitmap fields = MF_BITMAP_INITIALIZER; | |
1920 | ||
1921 | for (enum mf_field_id id = 0; id < MFF_N_IDS; id++) { | |
e6556fe3 BP |
1922 | uint64_t oxm = mf_oxm_header(id, version); |
1923 | if (oxm && version >= nxm_field_by_header(oxm)->version) { | |
c8058af7 | 1924 | uint32_t class = nxm_class(oxm); |
178742f9 BP |
1925 | int field = nxm_field(oxm); |
1926 | ||
c8058af7 | 1927 | if (class == OFPXMC12_OPENFLOW_BASIC |
178742f9 BP |
1928 | && field < 64 |
1929 | && oxm_bitmap & htonll(UINT64_C(1) << field)) { | |
1930 | bitmap_set1(fields.bm, id); | |
1931 | } | |
1932 | } | |
1933 | } | |
1934 | return fields; | |
1935 | } | |
1936 | ||
1937 | /* Returns a bitmap of fields that can be encoded in OXM and that can be | |
1938 | * modified with a "set_field" action. */ | |
1939 | struct mf_bitmap | |
1940 | oxm_writable_fields(void) | |
1941 | { | |
1942 | struct mf_bitmap b = MF_BITMAP_INITIALIZER; | |
1943 | int i; | |
1944 | ||
1945 | for (i = 0; i < MFF_N_IDS; i++) { | |
1946 | if (mf_oxm_header(i, 0) && mf_from_id(i)->writable) { | |
1947 | bitmap_set1(b.bm, i); | |
1948 | } | |
1949 | } | |
1950 | return b; | |
1951 | } | |
1952 | ||
1953 | /* Returns a bitmap of fields that can be encoded in OXM and that can be | |
1954 | * matched in a flow table. */ | |
1955 | struct mf_bitmap | |
1956 | oxm_matchable_fields(void) | |
1957 | { | |
1958 | struct mf_bitmap b = MF_BITMAP_INITIALIZER; | |
1959 | int i; | |
1960 | ||
1961 | for (i = 0; i < MFF_N_IDS; i++) { | |
1962 | if (mf_oxm_header(i, 0)) { | |
1963 | bitmap_set1(b.bm, i); | |
1964 | } | |
1965 | } | |
1966 | return b; | |
1967 | } | |
1968 | ||
1969 | /* Returns a bitmap of fields that can be encoded in OXM and that can be | |
1970 | * matched in a flow table with an arbitrary bitmask. */ | |
1971 | struct mf_bitmap | |
1972 | oxm_maskable_fields(void) | |
1973 | { | |
1974 | struct mf_bitmap b = MF_BITMAP_INITIALIZER; | |
1975 | int i; | |
1976 | ||
1977 | for (i = 0; i < MFF_N_IDS; i++) { | |
1978 | if (mf_oxm_header(i, 0) && mf_from_id(i)->maskable == MFM_FULLY) { | |
1979 | bitmap_set1(b.bm, i); | |
1980 | } | |
1981 | } | |
1982 | return b; | |
1983 | } | |
1984 | \f | |
1985 | struct nxm_field_index { | |
e6556fe3 BP |
1986 | struct hmap_node header_node; /* In nxm_header_map. */ |
1987 | struct hmap_node name_node; /* In nxm_name_map. */ | |
ca6ba700 | 1988 | struct ovs_list mf_node; /* In mf_mf_map[nf.id]. */ |
e6556fe3 | 1989 | const struct nxm_field nf; |
178742f9 BP |
1990 | }; |
1991 | ||
1992 | #include "nx-match.inc" | |
1993 | ||
1994 | static struct hmap nxm_header_map; | |
1995 | static struct hmap nxm_name_map; | |
ca6ba700 | 1996 | static struct ovs_list nxm_mf_map[MFF_N_IDS]; |
178742f9 BP |
1997 | |
1998 | static void | |
1999 | nxm_init(void) | |
2000 | { | |
2001 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; | |
2002 | if (ovsthread_once_start(&once)) { | |
2003 | hmap_init(&nxm_header_map); | |
2004 | hmap_init(&nxm_name_map); | |
e6556fe3 | 2005 | for (int i = 0; i < MFF_N_IDS; i++) { |
417e7e66 | 2006 | ovs_list_init(&nxm_mf_map[i]); |
e6556fe3 | 2007 | } |
178742f9 BP |
2008 | for (struct nxm_field_index *nfi = all_nxm_fields; |
2009 | nfi < &all_nxm_fields[ARRAY_SIZE(all_nxm_fields)]; nfi++) { | |
2010 | hmap_insert(&nxm_header_map, &nfi->header_node, | |
899bb63d | 2011 | hash_uint64(nxm_no_len(nfi->nf.header))); |
178742f9 BP |
2012 | hmap_insert(&nxm_name_map, &nfi->name_node, |
2013 | hash_string(nfi->nf.name, 0)); | |
417e7e66 | 2014 | ovs_list_push_back(&nxm_mf_map[nfi->nf.id], &nfi->mf_node); |
178742f9 BP |
2015 | } |
2016 | ovsthread_once_done(&once); | |
2017 | } | |
2018 | } | |
2019 | ||
2020 | static const struct nxm_field * | |
508a9338 | 2021 | nxm_field_by_header(uint64_t header) |
178742f9 BP |
2022 | { |
2023 | const struct nxm_field_index *nfi; | |
899bb63d | 2024 | uint64_t header_no_len; |
178742f9 BP |
2025 | |
2026 | nxm_init(); | |
2027 | if (nxm_hasmask(header)) { | |
2028 | header = nxm_make_exact_header(header); | |
2029 | } | |
2030 | ||
899bb63d JG |
2031 | header_no_len = nxm_no_len(header); |
2032 | ||
2033 | HMAP_FOR_EACH_IN_BUCKET (nfi, header_node, hash_uint64(header_no_len), | |
178742f9 | 2034 | &nxm_header_map) { |
899bb63d JG |
2035 | if (header_no_len == nxm_no_len(nfi->nf.header)) { |
2036 | if (nxm_length(header) == nxm_length(nfi->nf.header) || | |
2037 | mf_from_id(nfi->nf.id)->variable_len) { | |
2038 | return &nfi->nf; | |
2039 | } else { | |
2040 | return NULL; | |
2041 | } | |
178742f9 BP |
2042 | } |
2043 | } | |
2044 | return NULL; | |
2045 | } | |
2046 | ||
2047 | static const struct nxm_field * | |
2048 | nxm_field_by_name(const char *name, size_t len) | |
2049 | { | |
2050 | const struct nxm_field_index *nfi; | |
2051 | ||
2052 | nxm_init(); | |
2053 | HMAP_FOR_EACH_WITH_HASH (nfi, name_node, hash_bytes(name, len, 0), | |
2054 | &nxm_name_map) { | |
2055 | if (strlen(nfi->nf.name) == len && !memcmp(nfi->nf.name, name, len)) { | |
2056 | return &nfi->nf; | |
2057 | } | |
2058 | } | |
2059 | return NULL; | |
2060 | } | |
2061 | ||
2062 | static const struct nxm_field * | |
e6556fe3 | 2063 | nxm_field_by_mf_id(enum mf_field_id id, enum ofp_version version) |
178742f9 | 2064 | { |
e6556fe3 BP |
2065 | const struct nxm_field_index *nfi; |
2066 | const struct nxm_field *f; | |
178742f9 | 2067 | |
178742f9 | 2068 | nxm_init(); |
178742f9 | 2069 | |
e6556fe3 BP |
2070 | f = NULL; |
2071 | LIST_FOR_EACH (nfi, mf_node, &nxm_mf_map[id]) { | |
2072 | if (!f || version >= nfi->nf.version) { | |
2073 | f = &nfi->nf; | |
2074 | } | |
2075 | } | |
2076 | return f; | |
2077 | } |