]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ofp-actions.c
Add support for connection tracking.
[mirror_ovs.git] / lib / ofp-actions.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
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 #include "ofp-actions.h"
19 #include "bundle.h"
20 #include "byte-order.h"
21 #include "compiler.h"
22 #include "dummy.h"
23 #include "dynamic-string.h"
24 #include "hmap.h"
25 #include "learn.h"
26 #include "meta-flow.h"
27 #include "multipath.h"
28 #include "nx-match.h"
29 #include "ofp-parse.h"
30 #include "ofp-util.h"
31 #include "ofpbuf.h"
32 #include "unaligned.h"
33 #include "util.h"
34 #include "openvswitch/vlog.h"
35
36 VLOG_DEFINE_THIS_MODULE(ofp_actions);
37
38 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
39
40 struct ofp_action_header;
41
42 /* Raw identifiers for OpenFlow actions.
43 *
44 * Decoding and encoding OpenFlow actions across multiple versions is difficult
45 * to do in a clean, consistent way. This enumeration lays out all of the
46 * forms of actions that Open vSwitch supports.
47 *
48 * The comments here must follow a stylized form because the
49 * "extract-ofp-actions" program parses them at build time to generate data
50 * tables.
51 *
52 * - The first part of each comment specifies the vendor, OpenFlow versions,
53 * and type for each protocol that supports the action:
54 *
55 * # The vendor is OF for standard OpenFlow actions, NX for Nicira
56 * extension actions. (Support for other vendors can be added, but
57 * it can't be done just based on a vendor ID definition alone
58 * because OpenFlow doesn't define a standard way to specify a
59 * subtype for vendor actions, so other vendors might do it different
60 * from Nicira.)
61 *
62 * # The version can specify a specific OpenFlow version, a version
63 * range delimited by "-", or an open-ended range with "+".
64 *
65 * # The type, in parentheses, is the action type number (for standard
66 * OpenFlow actions) or subtype (for vendor extension actions).
67 *
68 * # Optionally one may add "is deprecated" followed by a
69 * human-readable reason in parentheses (which will be used in log
70 * messages), if a particular action should no longer be used.
71 *
72 * Multiple such specifications may be separated by commas.
73 *
74 * - The second part describes the action's wire format. It may be:
75 *
76 * # "struct <name>": The struct fully specifies the wire format. The
77 * action is exactly the size of the struct. (Thus, the struct must
78 * be an exact multiple of 8 bytes in size.)
79 *
80 * # "struct <name>, ...": The struct specifies the beginning of the
81 * wire format. An instance of the action is either the struct's
82 * exact size, or a multiple of 8 bytes longer.
83 *
84 * # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
85 * vendor extension) header, followed by 0 or more pad bytes to align
86 * to a multiple of <N> bits, followed by an argument of the given
87 * type, followed by 0 or more pad bytes to bring the total action up
88 * to a multiple of 8 bytes.
89 *
90 * # "void": The action is just a (standard or vendor extension)
91 * header.
92 *
93 * - Optional additional text enclosed in square brackets is commentary for
94 * the human reader.
95 */
96 enum ofp_raw_action_type {
97 /* ## ----------------- ## */
98 /* ## Standard actions. ## */
99 /* ## ----------------- ## */
100
101 /* OF1.0(0): struct ofp10_action_output. */
102 OFPAT_RAW10_OUTPUT,
103 /* OF1.1+(0): struct ofp11_action_output. */
104 OFPAT_RAW11_OUTPUT,
105
106 /* OF1.0(1): uint16_t. */
107 OFPAT_RAW10_SET_VLAN_VID,
108 /* OF1.0(2): uint8_t. */
109 OFPAT_RAW10_SET_VLAN_PCP,
110
111 /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
112 *
113 * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
114 * modification actions: the 1.0 versions push a VLAN header if none is
115 * present, but the 1.1 versions do not. That is the only reason that we
116 * distinguish their raw action types.] */
117 OFPAT_RAW11_SET_VLAN_VID,
118 /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
119 OFPAT_RAW11_SET_VLAN_PCP,
120
121 /* OF1.1+(17): ovs_be16.
122 *
123 * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
124 * TCI.] */
125 OFPAT_RAW11_PUSH_VLAN,
126
127 /* OF1.0(3): void. */
128 OFPAT_RAW10_STRIP_VLAN,
129 /* OF1.1+(18): void. */
130 OFPAT_RAW11_POP_VLAN,
131
132 /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
133 * ofp_action_dl_addr. */
134 OFPAT_RAW_SET_DL_SRC,
135
136 /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
137 * ofp_action_dl_addr. */
138 OFPAT_RAW_SET_DL_DST,
139
140 /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
141 * ovs_be32. */
142 OFPAT_RAW_SET_NW_SRC,
143
144 /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
145 * ovs_be32. */
146 OFPAT_RAW_SET_NW_DST,
147
148 /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
149 OFPAT_RAW_SET_NW_TOS,
150
151 /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
152 OFPAT_RAW11_SET_NW_ECN,
153
154 /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
155 * ovs_be16. */
156 OFPAT_RAW_SET_TP_SRC,
157
158 /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
159 * ovs_be16. */
160 OFPAT_RAW_SET_TP_DST,
161
162 /* OF1.0(11): struct ofp10_action_enqueue. */
163 OFPAT_RAW10_ENQUEUE,
164
165 /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
166 * ovs_be32. */
167 OFPAT_RAW_SET_MPLS_LABEL,
168
169 /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
170 * uint8_t. */
171 OFPAT_RAW_SET_MPLS_TC,
172
173 /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
174 * uint8_t. */
175 OFPAT_RAW_SET_MPLS_TTL,
176
177 /* NX1.0(26), OF1.1+(16): void. */
178 OFPAT_RAW_DEC_MPLS_TTL,
179
180 /* NX1.0(23), OF1.1+(19): ovs_be16.
181 *
182 * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
183 OFPAT_RAW_PUSH_MPLS,
184
185 /* NX1.0(24), OF1.1+(20): ovs_be16.
186 *
187 * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
188 * ETH_TYPE_MPLS otherwise, not the label.] */
189 OFPAT_RAW_POP_MPLS,
190
191 /* NX1.0(4), OF1.1+(21): uint32_t. */
192 OFPAT_RAW_SET_QUEUE,
193
194 /* OF1.1+(22): uint32_t. */
195 OFPAT_RAW11_GROUP,
196
197 /* OF1.1+(23): uint8_t. */
198 OFPAT_RAW11_SET_NW_TTL,
199
200 /* NX1.0(18), OF1.1+(24): void. */
201 OFPAT_RAW_DEC_NW_TTL,
202 /* NX1.0+(21): struct nx_action_cnt_ids, ... */
203 NXAST_RAW_DEC_TTL_CNT_IDS,
204
205 /* OF1.2-1.4(25): struct ofp12_action_set_field, ... */
206 OFPAT_RAW12_SET_FIELD,
207 /* OF1.5+(25): struct ofp12_action_set_field, ... */
208 OFPAT_RAW15_SET_FIELD,
209 /* NX1.0-1.4(7): struct nx_action_reg_load.
210 *
211 * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
212 * we drop reg_load.] */
213 NXAST_RAW_REG_LOAD,
214 /* NX1.0-1.4(33): struct nx_action_reg_load2, ...
215 *
216 * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
217 * we drop reg_load2.] */
218 NXAST_RAW_REG_LOAD2,
219
220 /* OF1.5+(28): struct ofp15_action_copy_field, ... */
221 OFPAT_RAW15_COPY_FIELD,
222 /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... */
223 ONFACT_RAW13_COPY_FIELD,
224 /* NX1.0-1.4(6): struct nx_action_reg_move, ... */
225 NXAST_RAW_REG_MOVE,
226
227 /* ## ------------------------- ## */
228 /* ## Nicira extension actions. ## */
229 /* ## ------------------------- ## */
230
231 /* Actions similar to standard actions are listed with the standard actions. */
232
233 /* NX1.0+(1): uint16_t. */
234 NXAST_RAW_RESUBMIT,
235 /* NX1.0+(14): struct nx_action_resubmit. */
236 NXAST_RAW_RESUBMIT_TABLE,
237
238 /* NX1.0+(2): uint32_t. */
239 NXAST_RAW_SET_TUNNEL,
240 /* NX1.0+(9): uint64_t. */
241 NXAST_RAW_SET_TUNNEL64,
242
243 /* NX1.0+(5): void. */
244 NXAST_RAW_POP_QUEUE,
245
246 /* NX1.0+(8): struct nx_action_note, ... */
247 NXAST_RAW_NOTE,
248
249 /* NX1.0+(10): struct nx_action_multipath. */
250 NXAST_RAW_MULTIPATH,
251
252 /* NX1.0+(12): struct nx_action_bundle, ... */
253 NXAST_RAW_BUNDLE,
254 /* NX1.0+(13): struct nx_action_bundle, ... */
255 NXAST_RAW_BUNDLE_LOAD,
256
257 /* NX1.0+(15): struct nx_action_output_reg. */
258 NXAST_RAW_OUTPUT_REG,
259 /* NX1.0+(32): struct nx_action_output_reg2. */
260 NXAST_RAW_OUTPUT_REG2,
261
262 /* NX1.0+(16): struct nx_action_learn, ... */
263 NXAST_RAW_LEARN,
264
265 /* NX1.0+(17): void. */
266 NXAST_RAW_EXIT,
267
268 /* NX1.0+(19): struct nx_action_fin_timeout. */
269 NXAST_RAW_FIN_TIMEOUT,
270
271 /* NX1.0+(20): struct nx_action_controller. */
272 NXAST_RAW_CONTROLLER,
273
274 /* NX1.0+(22): struct nx_action_write_metadata. */
275 NXAST_RAW_WRITE_METADATA,
276
277 /* NX1.0+(27): struct nx_action_stack. */
278 NXAST_RAW_STACK_PUSH,
279
280 /* NX1.0+(28): struct nx_action_stack. */
281 NXAST_RAW_STACK_POP,
282
283 /* NX1.0+(29): struct nx_action_sample. */
284 NXAST_RAW_SAMPLE,
285
286 /* NX1.0+(34): struct nx_action_conjunction. */
287 NXAST_RAW_CONJUNCTION,
288
289 /* NX1.0+(35): struct nx_action_conntrack, ... */
290 NXAST_RAW_CT,
291
292 /* ## ------------------ ## */
293 /* ## Debugging actions. ## */
294 /* ## ------------------ ## */
295
296 /* These are intentionally undocumented, subject to change, and ovs-vswitchd */
297 /* accepts them only if started with --enable-dummy. */
298
299 /* NX1.0+(255): void. */
300 NXAST_RAW_DEBUG_RECIRC,
301 };
302
303 /* OpenFlow actions are always a multiple of 8 bytes in length. */
304 #define OFP_ACTION_ALIGN 8
305
306 /* Define a few functions for working with instructions. */
307 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
308 static inline const struct STRUCT * OVS_UNUSED \
309 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
310 { \
311 ovs_assert(inst->type == htons(ENUM)); \
312 return ALIGNED_CAST(struct STRUCT *, inst); \
313 } \
314 \
315 static inline void OVS_UNUSED \
316 instruction_init_##ENUM(struct STRUCT *s) \
317 { \
318 memset(s, 0, sizeof *s); \
319 s->type = htons(ENUM); \
320 s->len = htons(sizeof *s); \
321 } \
322 \
323 static inline struct STRUCT * OVS_UNUSED \
324 instruction_put_##ENUM(struct ofpbuf *buf) \
325 { \
326 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
327 instruction_init_##ENUM(s); \
328 return s; \
329 }
330 OVS_INSTRUCTIONS
331 #undef DEFINE_INST
332
333 static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
334 size_t ofs);
335 static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
336
337 static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
338 uint32_t allowed_ovsinsts,
339 enum ofpact_type outer_action);
340
341 static void ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version,
342 enum mf_field_id, uint64_t value);
343
344 static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
345 enum ofp_raw_action_type *, uint64_t *arg);
346 static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
347 enum ofp_raw_action_type, uint64_t arg);
348
349 static char *OVS_WARN_UNUSED_RESULT ofpacts_parse(
350 char *str, struct ofpbuf *ofpacts, enum ofputil_protocol *usable_protocols,
351 bool allow_instructions, enum ofpact_type outer_action);
352 static enum ofperr ofpacts_pull_openflow_actions__(
353 struct ofpbuf *openflow, unsigned int actions_len,
354 enum ofp_version version, uint32_t allowed_ovsinsts,
355 struct ofpbuf *ofpacts, enum ofpact_type outer_action);
356
357 /* Pull off existing actions or instructions. Used by nesting actions to keep
358 * ofpacts_parse() oblivious of actions nesting.
359 *
360 * Push the actions back on after nested parsing, e.g.:
361 *
362 * size_t ofs = ofpacts_pull(ofpacts);
363 * ...nested parsing...
364 * ofpbuf_push_uninit(ofpacts, ofs);
365 */
366 static size_t
367 ofpacts_pull(struct ofpbuf *ofpacts)
368 {
369 size_t ofs;
370
371 ofpact_pad(ofpacts);
372 ofs = ofpacts->size;
373 ofpbuf_pull(ofpacts, ofs);
374
375 return ofs;
376 }
377
378 #include "ofp-actions.inc1"
379 \f
380 /* Output actions. */
381
382 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
383 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
384 * number of bytes to send. A 'max_len' of zero means no bytes of the
385 * packet should be sent. */
386 struct ofp10_action_output {
387 ovs_be16 type; /* OFPAT10_OUTPUT. */
388 ovs_be16 len; /* Length is 8. */
389 ovs_be16 port; /* Output port. */
390 ovs_be16 max_len; /* Max length to send to controller. */
391 };
392 OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
393
394 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
395 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
396 * number of bytes to send. A 'max_len' of zero means no bytes of the
397 * packet should be sent.*/
398 struct ofp11_action_output {
399 ovs_be16 type; /* OFPAT11_OUTPUT. */
400 ovs_be16 len; /* Length is 16. */
401 ovs_be32 port; /* Output port. */
402 ovs_be16 max_len; /* Max length to send to controller. */
403 uint8_t pad[6]; /* Pad to 64 bits. */
404 };
405 OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
406
407 static enum ofperr
408 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
409 enum ofp_version ofp_version OVS_UNUSED,
410 struct ofpbuf *out)
411 {
412 struct ofpact_output *output;
413
414 output = ofpact_put_OUTPUT(out);
415 output->port = u16_to_ofp(ntohs(oao->port));
416 output->max_len = ntohs(oao->max_len);
417
418 return ofpact_check_output_port(output->port, OFPP_MAX);
419 }
420
421 static enum ofperr
422 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
423 enum ofp_version ofp_version OVS_UNUSED,
424 struct ofpbuf *out)
425 {
426 struct ofpact_output *output;
427 enum ofperr error;
428
429 output = ofpact_put_OUTPUT(out);
430 output->max_len = ntohs(oao->max_len);
431
432 error = ofputil_port_from_ofp11(oao->port, &output->port);
433 if (error) {
434 return error;
435 }
436
437 return ofpact_check_output_port(output->port, OFPP_MAX);
438 }
439
440 static void
441 encode_OUTPUT(const struct ofpact_output *output,
442 enum ofp_version ofp_version, struct ofpbuf *out)
443 {
444 if (ofp_version == OFP10_VERSION) {
445 struct ofp10_action_output *oao;
446
447 oao = put_OFPAT10_OUTPUT(out);
448 oao->port = htons(ofp_to_u16(output->port));
449 oao->max_len = htons(output->max_len);
450 } else {
451 struct ofp11_action_output *oao;
452
453 oao = put_OFPAT11_OUTPUT(out);
454 oao->port = ofputil_port_to_ofp11(output->port);
455 oao->max_len = htons(output->max_len);
456 }
457 }
458
459 static char * OVS_WARN_UNUSED_RESULT
460 parse_OUTPUT(const char *arg, struct ofpbuf *ofpacts,
461 enum ofputil_protocol *usable_protocols OVS_UNUSED)
462 {
463 if (strchr(arg, '[')) {
464 struct ofpact_output_reg *output_reg;
465
466 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
467 output_reg->max_len = UINT16_MAX;
468 return mf_parse_subfield(&output_reg->src, arg);
469 } else {
470 struct ofpact_output *output;
471
472 output = ofpact_put_OUTPUT(ofpacts);
473 if (!ofputil_port_from_string(arg, &output->port)) {
474 return xasprintf("%s: output to unknown port", arg);
475 }
476 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
477 return NULL;
478 }
479 }
480
481 static void
482 format_OUTPUT(const struct ofpact_output *a, struct ds *s)
483 {
484 if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
485 ds_put_format(s, "output:%"PRIu16, a->port);
486 } else {
487 ofputil_format_port(a->port, s);
488 if (a->port == OFPP_CONTROLLER) {
489 ds_put_format(s, ":%"PRIu16, a->max_len);
490 }
491 }
492 }
493 \f
494 /* Group actions. */
495
496 static enum ofperr
497 decode_OFPAT_RAW11_GROUP(uint32_t group_id,
498 enum ofp_version ofp_version OVS_UNUSED,
499 struct ofpbuf *out)
500 {
501 ofpact_put_GROUP(out)->group_id = group_id;
502 return 0;
503 }
504
505 static void
506 encode_GROUP(const struct ofpact_group *group,
507 enum ofp_version ofp_version, struct ofpbuf *out)
508 {
509 if (ofp_version == OFP10_VERSION) {
510 /* XXX */
511 } else {
512 put_OFPAT11_GROUP(out, group->group_id);
513 }
514 }
515
516 static char * OVS_WARN_UNUSED_RESULT
517 parse_GROUP(char *arg, struct ofpbuf *ofpacts,
518 enum ofputil_protocol *usable_protocols OVS_UNUSED)
519 {
520 return str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
521 }
522
523 static void
524 format_GROUP(const struct ofpact_group *a, struct ds *s)
525 {
526 ds_put_format(s, "group:%"PRIu32, a->group_id);
527 }
528 \f
529 /* Action structure for NXAST_CONTROLLER.
530 *
531 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
532 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
533 * specifying:
534 *
535 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
536 *
537 * - 'controller_id': The ID of the controller connection to which the
538 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
539 * sent only to controllers that have the specified controller connection
540 * ID. See "struct nx_controller_id" for more information. */
541 struct nx_action_controller {
542 ovs_be16 type; /* OFPAT_VENDOR. */
543 ovs_be16 len; /* Length is 16. */
544 ovs_be32 vendor; /* NX_VENDOR_ID. */
545 ovs_be16 subtype; /* NXAST_CONTROLLER. */
546 ovs_be16 max_len; /* Maximum length to send to controller. */
547 ovs_be16 controller_id; /* Controller ID to send packet-in. */
548 uint8_t reason; /* enum ofp_packet_in_reason (OFPR_*). */
549 uint8_t zero; /* Must be zero. */
550 };
551 OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
552
553 static enum ofperr
554 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
555 enum ofp_version ofp_version OVS_UNUSED,
556 struct ofpbuf *out)
557 {
558 struct ofpact_controller *oc;
559
560 oc = ofpact_put_CONTROLLER(out);
561 oc->max_len = ntohs(nac->max_len);
562 oc->controller_id = ntohs(nac->controller_id);
563 oc->reason = nac->reason;
564 return 0;
565 }
566
567 static void
568 encode_CONTROLLER(const struct ofpact_controller *controller,
569 enum ofp_version ofp_version OVS_UNUSED,
570 struct ofpbuf *out)
571 {
572 struct nx_action_controller *nac;
573
574 nac = put_NXAST_CONTROLLER(out);
575 nac->max_len = htons(controller->max_len);
576 nac->controller_id = htons(controller->controller_id);
577 nac->reason = controller->reason;
578 }
579
580 static char * OVS_WARN_UNUSED_RESULT
581 parse_CONTROLLER(char *arg, struct ofpbuf *ofpacts,
582 enum ofputil_protocol *usable_protocols OVS_UNUSED)
583 {
584 enum ofp_packet_in_reason reason = OFPR_ACTION;
585 uint16_t controller_id = 0;
586 uint16_t max_len = UINT16_MAX;
587
588 if (!arg[0]) {
589 /* Use defaults. */
590 } else if (strspn(arg, "0123456789") == strlen(arg)) {
591 char *error = str_to_u16(arg, "max_len", &max_len);
592 if (error) {
593 return error;
594 }
595 } else {
596 char *name, *value;
597
598 while (ofputil_parse_key_value(&arg, &name, &value)) {
599 if (!strcmp(name, "reason")) {
600 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
601 return xasprintf("unknown reason \"%s\"", value);
602 }
603 } else if (!strcmp(name, "max_len")) {
604 char *error = str_to_u16(value, "max_len", &max_len);
605 if (error) {
606 return error;
607 }
608 } else if (!strcmp(name, "id")) {
609 char *error = str_to_u16(value, "id", &controller_id);
610 if (error) {
611 return error;
612 }
613 } else {
614 return xasprintf("unknown key \"%s\" parsing controller "
615 "action", name);
616 }
617 }
618 }
619
620 if (reason == OFPR_ACTION && controller_id == 0) {
621 struct ofpact_output *output;
622
623 output = ofpact_put_OUTPUT(ofpacts);
624 output->port = OFPP_CONTROLLER;
625 output->max_len = max_len;
626 } else {
627 struct ofpact_controller *controller;
628
629 controller = ofpact_put_CONTROLLER(ofpacts);
630 controller->max_len = max_len;
631 controller->reason = reason;
632 controller->controller_id = controller_id;
633 }
634
635 return NULL;
636 }
637
638 static void
639 format_CONTROLLER(const struct ofpact_controller *a, struct ds *s)
640 {
641 if (a->reason == OFPR_ACTION && a->controller_id == 0) {
642 ds_put_format(s, "CONTROLLER:%"PRIu16, a->max_len);
643 } else {
644 enum ofp_packet_in_reason reason = a->reason;
645
646 ds_put_cstr(s, "controller(");
647 if (reason != OFPR_ACTION) {
648 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
649
650 ds_put_format(s, "reason=%s,",
651 ofputil_packet_in_reason_to_string(
652 reason, reasonbuf, sizeof reasonbuf));
653 }
654 if (a->max_len != UINT16_MAX) {
655 ds_put_format(s, "max_len=%"PRIu16",", a->max_len);
656 }
657 if (a->controller_id != 0) {
658 ds_put_format(s, "id=%"PRIu16",", a->controller_id);
659 }
660 ds_chomp(s, ',');
661 ds_put_char(s, ')');
662 }
663 }
664 \f
665 /* Enqueue action. */
666 struct ofp10_action_enqueue {
667 ovs_be16 type; /* OFPAT10_ENQUEUE. */
668 ovs_be16 len; /* Len is 16. */
669 ovs_be16 port; /* Port that queue belongs. Should
670 refer to a valid physical port
671 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
672 uint8_t pad[6]; /* Pad for 64-bit alignment. */
673 ovs_be32 queue_id; /* Where to enqueue the packets. */
674 };
675 OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
676
677 static enum ofperr
678 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
679 enum ofp_version ofp_version OVS_UNUSED,
680 struct ofpbuf *out)
681 {
682 struct ofpact_enqueue *enqueue;
683
684 enqueue = ofpact_put_ENQUEUE(out);
685 enqueue->port = u16_to_ofp(ntohs(oae->port));
686 enqueue->queue = ntohl(oae->queue_id);
687 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
688 && enqueue->port != OFPP_IN_PORT
689 && enqueue->port != OFPP_LOCAL) {
690 return OFPERR_OFPBAC_BAD_OUT_PORT;
691 }
692 return 0;
693 }
694
695 static void
696 encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
697 enum ofp_version ofp_version, struct ofpbuf *out)
698 {
699 if (ofp_version == OFP10_VERSION) {
700 struct ofp10_action_enqueue *oae;
701
702 oae = put_OFPAT10_ENQUEUE(out);
703 oae->port = htons(ofp_to_u16(enqueue->port));
704 oae->queue_id = htonl(enqueue->queue);
705 } else {
706 /* XXX */
707 }
708 }
709
710 static char * OVS_WARN_UNUSED_RESULT
711 parse_ENQUEUE(char *arg, struct ofpbuf *ofpacts,
712 enum ofputil_protocol *usable_protocols OVS_UNUSED)
713 {
714 char *sp = NULL;
715 char *port = strtok_r(arg, ":q,", &sp);
716 char *queue = strtok_r(NULL, "", &sp);
717 struct ofpact_enqueue *enqueue;
718
719 if (port == NULL || queue == NULL) {
720 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
721 "\"enqueue(PORT,QUEUE)\"");
722 }
723
724 enqueue = ofpact_put_ENQUEUE(ofpacts);
725 if (!ofputil_port_from_string(port, &enqueue->port)) {
726 return xasprintf("%s: enqueue to unknown port", port);
727 }
728 return str_to_u32(queue, &enqueue->queue);
729 }
730
731 static void
732 format_ENQUEUE(const struct ofpact_enqueue *a, struct ds *s)
733 {
734 ds_put_format(s, "enqueue:");
735 ofputil_format_port(a->port, s);
736 ds_put_format(s, ":%"PRIu32, a->queue);
737 }
738 \f
739 /* Action structure for NXAST_OUTPUT_REG.
740 *
741 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
742 *
743 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
744 * the NXAST_REG_LOAD action.
745 *
746 * The acceptable nxm_header values for 'src' are the same as the acceptable
747 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
748 *
749 * The 'max_len' field indicates the number of bytes to send when the chosen
750 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
751 * field of OFPAT_OUTPUT.
752 *
753 * The 'zero' field is required to be zeroed for forward compatibility. */
754 struct nx_action_output_reg {
755 ovs_be16 type; /* OFPAT_VENDOR. */
756 ovs_be16 len; /* 24. */
757 ovs_be32 vendor; /* NX_VENDOR_ID. */
758 ovs_be16 subtype; /* NXAST_OUTPUT_REG. */
759
760 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
761 ovs_be32 src; /* Source. */
762
763 ovs_be16 max_len; /* Max length to send to controller. */
764
765 uint8_t zero[6]; /* Reserved, must be zero. */
766 };
767 OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
768
769 /* Action structure for NXAST_OUTPUT_REG2.
770 *
771 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
772 * experimenter OXM as 'src'.
773 */
774 struct nx_action_output_reg2 {
775 ovs_be16 type; /* OFPAT_VENDOR. */
776 ovs_be16 len; /* 24. */
777 ovs_be32 vendor; /* NX_VENDOR_ID. */
778 ovs_be16 subtype; /* NXAST_OUTPUT_REG2. */
779
780 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
781 ovs_be16 max_len; /* Max length to send to controller. */
782
783 /* Followed by:
784 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
785 * - Enough 0-bytes to pad the action out to 24 bytes. */
786 uint8_t pad[10];
787 };
788 OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
789
790 static enum ofperr
791 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
792 enum ofp_version ofp_version OVS_UNUSED,
793 struct ofpbuf *out)
794 {
795 struct ofpact_output_reg *output_reg;
796
797 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
798 return OFPERR_OFPBAC_BAD_ARGUMENT;
799 }
800
801 output_reg = ofpact_put_OUTPUT_REG(out);
802 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
803 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
804 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
805 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
806 output_reg->max_len = ntohs(naor->max_len);
807
808 return mf_check_src(&output_reg->src, NULL);
809 }
810
811 static enum ofperr
812 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
813 enum ofp_version ofp_version OVS_UNUSED,
814 struct ofpbuf *out)
815 {
816 struct ofpact_output_reg *output_reg;
817 enum ofperr error;
818 struct ofpbuf b;
819
820 output_reg = ofpact_put_OUTPUT_REG(out);
821 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
822 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
823 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
824 output_reg->max_len = ntohs(naor->max_len);
825
826 ofpbuf_use_const(&b, naor, ntohs(naor->len));
827 ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
828 error = nx_pull_header(&b, &output_reg->src.field, NULL);
829 if (error) {
830 return error;
831 }
832 if (!is_all_zeros(b.data, b.size)) {
833 return OFPERR_NXBRC_MUST_BE_ZERO;
834 }
835
836 return mf_check_src(&output_reg->src, NULL);
837 }
838
839 static void
840 encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
841 enum ofp_version ofp_version OVS_UNUSED,
842 struct ofpbuf *out)
843 {
844 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
845 * cannot be encoded in the older form, encode it as
846 * NXAST_RAW_OUTPUT_REG2. */
847 if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
848 || !mf_nxm_header(output_reg->src.field->id)) {
849 struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
850 size_t size = out->size;
851
852 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
853 output_reg->src.n_bits);
854 naor->max_len = htons(output_reg->max_len);
855
856 out->size = size - sizeof naor->pad;
857 nx_put_header(out, output_reg->src.field->id, 0, false);
858 out->size = size;
859 } else {
860 struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
861
862 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
863 output_reg->src.n_bits);
864 naor->src = htonl(mf_nxm_header(output_reg->src.field->id));
865 naor->max_len = htons(output_reg->max_len);
866 }
867 }
868
869 static char * OVS_WARN_UNUSED_RESULT
870 parse_OUTPUT_REG(const char *arg, struct ofpbuf *ofpacts,
871 enum ofputil_protocol *usable_protocols OVS_UNUSED)
872 {
873 return parse_OUTPUT(arg, ofpacts, usable_protocols);
874 }
875
876 static void
877 format_OUTPUT_REG(const struct ofpact_output_reg *a, struct ds *s)
878 {
879 ds_put_cstr(s, "output:");
880 mf_format_subfield(&a->src, s);
881 }
882 \f
883 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
884 *
885 * The bundle actions choose a slave from a supplied list of options.
886 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
887 * selection to a register.
888 *
889 * The list of possible slaves follows the nx_action_bundle structure. The size
890 * of each slave is governed by its type as indicated by the 'slave_type'
891 * parameter. The list of slaves should be padded at its end with zeros to make
892 * the total length of the action a multiple of 8.
893 *
894 * Switches infer from the 'slave_type' parameter the size of each slave. All
895 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
896 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
897 * 2 byte width. Switches should reject actions which indicate unknown or
898 * unsupported slave types.
899 *
900 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
901 * slave. If the switch does not support the specified 'algorithm' parameter,
902 * it should reject the action.
903 *
904 * Several algorithms take into account liveness when selecting slaves. The
905 * liveness of a slave is implementation defined (with one exception), but will
906 * generally take into account things like its carrier status and the results
907 * of any link monitoring protocols which happen to be running on it. In order
908 * to give controllers a place-holder value, the OFPP_NONE port is always
909 * considered live.
910 *
911 * Some slave selection strategies require the use of a hash function, in which
912 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
913 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
914 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
915 * 'basis' parameter is used as a universal hash parameter. Different values
916 * of 'basis' yield different hash results.
917 *
918 * The 'zero' parameter at the end of the action structure is reserved for
919 * future use. Switches are required to reject actions which have nonzero
920 * bytes in the 'zero' field.
921 *
922 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
923 * should reject actions which have nonzero bytes in either of these fields.
924 *
925 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
926 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
927 * similar to those for the NXAST_REG_LOAD action. */
928 struct nx_action_bundle {
929 ovs_be16 type; /* OFPAT_VENDOR. */
930 ovs_be16 len; /* Length including slaves. */
931 ovs_be32 vendor; /* NX_VENDOR_ID. */
932 ovs_be16 subtype; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
933
934 /* Slave choice algorithm to apply to hash value. */
935 ovs_be16 algorithm; /* One of NX_BD_ALG_*. */
936
937 /* What fields to hash and how. */
938 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
939 ovs_be16 basis; /* Universal hash parameter. */
940
941 ovs_be32 slave_type; /* NXM_OF_IN_PORT. */
942 ovs_be16 n_slaves; /* Number of slaves. */
943
944 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
945 ovs_be32 dst; /* Destination. */
946
947 uint8_t zero[4]; /* Reserved. Must be zero. */
948 };
949 OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
950
951 static enum ofperr
952 decode_bundle(bool load, const struct nx_action_bundle *nab,
953 struct ofpbuf *ofpacts)
954 {
955 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
956 struct ofpact_bundle *bundle;
957 uint32_t slave_type;
958 size_t slaves_size, i;
959 enum ofperr error;
960
961 bundle = ofpact_put_BUNDLE(ofpacts);
962
963 bundle->n_slaves = ntohs(nab->n_slaves);
964 bundle->basis = ntohs(nab->basis);
965 bundle->fields = ntohs(nab->fields);
966 bundle->algorithm = ntohs(nab->algorithm);
967 slave_type = ntohl(nab->slave_type);
968 slaves_size = ntohs(nab->len) - sizeof *nab;
969
970 error = OFPERR_OFPBAC_BAD_ARGUMENT;
971 if (!flow_hash_fields_valid(bundle->fields)) {
972 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
973 } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
974 VLOG_WARN_RL(&rl, "too many slaves");
975 } else if (bundle->algorithm != NX_BD_ALG_HRW
976 && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
977 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
978 } else if (slave_type != mf_nxm_header(MFF_IN_PORT)) {
979 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
980 } else {
981 error = 0;
982 }
983
984 if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
985 VLOG_WARN_RL(&rl, "reserved field is nonzero");
986 error = OFPERR_OFPBAC_BAD_ARGUMENT;
987 }
988
989 if (load) {
990 bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
991 bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
992 bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
993
994 if (bundle->dst.n_bits < 16) {
995 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
996 "destination.");
997 error = OFPERR_OFPBAC_BAD_ARGUMENT;
998 }
999 } else {
1000 if (nab->ofs_nbits || nab->dst) {
1001 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
1002 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1003 }
1004 }
1005
1006 if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
1007 VLOG_WARN_RL(&rl, "Nicira action %s only has %"PRIuSIZE" bytes "
1008 "allocated for slaves. %"PRIuSIZE" bytes are required "
1009 "for %"PRIu16" slaves.",
1010 load ? "bundle_load" : "bundle", slaves_size,
1011 bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
1012 error = OFPERR_OFPBAC_BAD_LEN;
1013 }
1014
1015 for (i = 0; i < bundle->n_slaves; i++) {
1016 uint16_t ofp_port = ntohs(((ovs_be16 *)(nab + 1))[i]);
1017 ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
1018 }
1019
1020 bundle = ofpacts->header;
1021 ofpact_update_len(ofpacts, &bundle->ofpact);
1022
1023 if (!error) {
1024 error = bundle_check(bundle, OFPP_MAX, NULL);
1025 }
1026 return error;
1027 }
1028
1029 static enum ofperr
1030 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab,
1031 enum ofp_version ofp_version OVS_UNUSED,
1032 struct ofpbuf *out)
1033 {
1034 return decode_bundle(false, nab, out);
1035 }
1036
1037 static enum ofperr
1038 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
1039 enum ofp_version ofp_version OVS_UNUSED,
1040 struct ofpbuf *out)
1041 {
1042 return decode_bundle(true, nab, out);
1043 }
1044
1045 static void
1046 encode_BUNDLE(const struct ofpact_bundle *bundle,
1047 enum ofp_version ofp_version OVS_UNUSED,
1048 struct ofpbuf *out)
1049 {
1050 int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
1051 struct nx_action_bundle *nab;
1052 ovs_be16 *slaves;
1053 size_t i;
1054
1055 nab = (bundle->dst.field
1056 ? put_NXAST_BUNDLE_LOAD(out)
1057 : put_NXAST_BUNDLE(out));
1058 nab->len = htons(ntohs(nab->len) + slaves_len);
1059 nab->algorithm = htons(bundle->algorithm);
1060 nab->fields = htons(bundle->fields);
1061 nab->basis = htons(bundle->basis);
1062 nab->slave_type = htonl(mf_nxm_header(MFF_IN_PORT));
1063 nab->n_slaves = htons(bundle->n_slaves);
1064 if (bundle->dst.field) {
1065 nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1066 bundle->dst.n_bits);
1067 nab->dst = htonl(mf_nxm_header(bundle->dst.field->id));
1068 }
1069
1070 slaves = ofpbuf_put_zeros(out, slaves_len);
1071 for (i = 0; i < bundle->n_slaves; i++) {
1072 slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
1073 }
1074 }
1075
1076 static char * OVS_WARN_UNUSED_RESULT
1077 parse_BUNDLE(const char *arg, struct ofpbuf *ofpacts,
1078 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1079 {
1080 return bundle_parse(arg, ofpacts);
1081 }
1082
1083 static char * OVS_WARN_UNUSED_RESULT
1084 parse_bundle_load(const char *arg, struct ofpbuf *ofpacts)
1085 {
1086 return bundle_parse_load(arg, ofpacts);
1087 }
1088
1089 static void
1090 format_BUNDLE(const struct ofpact_bundle *a, struct ds *s)
1091 {
1092 bundle_format(a, s);
1093 }
1094 \f
1095 /* Set VLAN actions. */
1096
1097 static enum ofperr
1098 decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1099 {
1100 if (vid & ~0xfff) {
1101 return OFPERR_OFPBAC_BAD_ARGUMENT;
1102 } else {
1103 struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1104 vlan_vid->vlan_vid = vid;
1105 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1106 return 0;
1107 }
1108 }
1109
1110 static enum ofperr
1111 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid,
1112 enum ofp_version ofp_version OVS_UNUSED,
1113 struct ofpbuf *out)
1114 {
1115 return decode_set_vlan_vid(vid, true, out);
1116 }
1117
1118 static enum ofperr
1119 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid,
1120 enum ofp_version ofp_version OVS_UNUSED,
1121 struct ofpbuf *out)
1122 {
1123 return decode_set_vlan_vid(vid, false, out);
1124 }
1125
1126 static void
1127 encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1128 enum ofp_version ofp_version, struct ofpbuf *out)
1129 {
1130 uint16_t vid = vlan_vid->vlan_vid;
1131
1132 /* Push a VLAN tag, if none is present and this form of the action calls
1133 * for such a feature. */
1134 if (ofp_version > OFP10_VERSION
1135 && vlan_vid->push_vlan_if_needed
1136 && !vlan_vid->flow_has_vlan) {
1137 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1138 }
1139
1140 if (ofp_version == OFP10_VERSION) {
1141 put_OFPAT10_SET_VLAN_VID(out, vid);
1142 } else if (ofp_version == OFP11_VERSION) {
1143 put_OFPAT11_SET_VLAN_VID(out, vid);
1144 } else {
1145 ofpact_put_set_field(out, ofp_version,
1146 MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1147 }
1148 }
1149
1150 static char * OVS_WARN_UNUSED_RESULT
1151 parse_set_vlan_vid(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1152 {
1153 struct ofpact_vlan_vid *vlan_vid;
1154 uint16_t vid;
1155 char *error;
1156
1157 error = str_to_u16(arg, "VLAN VID", &vid);
1158 if (error) {
1159 return error;
1160 }
1161
1162 if (vid & ~VLAN_VID_MASK) {
1163 return xasprintf("%s: not a valid VLAN VID", arg);
1164 }
1165 vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
1166 vlan_vid->vlan_vid = vid;
1167 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1168 return NULL;
1169 }
1170
1171 static char * OVS_WARN_UNUSED_RESULT
1172 parse_SET_VLAN_VID(char *arg, struct ofpbuf *ofpacts,
1173 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1174 {
1175 return parse_set_vlan_vid(arg, ofpacts, false);
1176 }
1177
1178 static void
1179 format_SET_VLAN_VID(const struct ofpact_vlan_vid *a, struct ds *s)
1180 {
1181 ds_put_format(s, "%s:%"PRIu16,
1182 a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1183 a->vlan_vid);
1184 }
1185 \f
1186 /* Set PCP actions. */
1187
1188 static enum ofperr
1189 decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1190 {
1191 if (pcp & ~7) {
1192 return OFPERR_OFPBAC_BAD_ARGUMENT;
1193 } else {
1194 struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1195 vlan_pcp->vlan_pcp = pcp;
1196 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1197 return 0;
1198 }
1199 }
1200
1201 static enum ofperr
1202 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp,
1203 enum ofp_version ofp_version OVS_UNUSED,
1204 struct ofpbuf *out)
1205 {
1206 return decode_set_vlan_pcp(pcp, true, out);
1207 }
1208
1209 static enum ofperr
1210 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp,
1211 enum ofp_version ofp_version OVS_UNUSED,
1212 struct ofpbuf *out)
1213 {
1214 return decode_set_vlan_pcp(pcp, false, out);
1215 }
1216
1217 static void
1218 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1219 enum ofp_version ofp_version, struct ofpbuf *out)
1220 {
1221 uint8_t pcp = vlan_pcp->vlan_pcp;
1222
1223 /* Push a VLAN tag, if none is present and this form of the action calls
1224 * for such a feature. */
1225 if (ofp_version > OFP10_VERSION
1226 && vlan_pcp->push_vlan_if_needed
1227 && !vlan_pcp->flow_has_vlan) {
1228 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1229 }
1230
1231 if (ofp_version == OFP10_VERSION) {
1232 put_OFPAT10_SET_VLAN_PCP(out, pcp);
1233 } else if (ofp_version == OFP11_VERSION) {
1234 put_OFPAT11_SET_VLAN_PCP(out, pcp);
1235 } else {
1236 ofpact_put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1237 }
1238 }
1239
1240 static char * OVS_WARN_UNUSED_RESULT
1241 parse_set_vlan_pcp(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1242 {
1243 struct ofpact_vlan_pcp *vlan_pcp;
1244 uint8_t pcp;
1245 char *error;
1246
1247 error = str_to_u8(arg, "VLAN PCP", &pcp);
1248 if (error) {
1249 return error;
1250 }
1251
1252 if (pcp & ~7) {
1253 return xasprintf("%s: not a valid VLAN PCP", arg);
1254 }
1255 vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
1256 vlan_pcp->vlan_pcp = pcp;
1257 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1258 return NULL;
1259 }
1260
1261 static char * OVS_WARN_UNUSED_RESULT
1262 parse_SET_VLAN_PCP(char *arg, struct ofpbuf *ofpacts,
1263 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1264 {
1265 return parse_set_vlan_pcp(arg, ofpacts, false);
1266 }
1267
1268 static void
1269 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a, struct ds *s)
1270 {
1271 ds_put_format(s, "%s:%"PRIu8,
1272 a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1273 a->vlan_pcp);
1274 }
1275 \f
1276 /* Strip VLAN actions. */
1277
1278 static enum ofperr
1279 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1280 {
1281 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1282 return 0;
1283 }
1284
1285 static enum ofperr
1286 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1287 {
1288 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1289 return 0;
1290 }
1291
1292 static void
1293 encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1294 enum ofp_version ofp_version, struct ofpbuf *out)
1295 {
1296 if (ofp_version == OFP10_VERSION) {
1297 put_OFPAT10_STRIP_VLAN(out);
1298 } else {
1299 put_OFPAT11_POP_VLAN(out);
1300 }
1301 }
1302
1303 static char * OVS_WARN_UNUSED_RESULT
1304 parse_STRIP_VLAN(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
1305 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1306 {
1307 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1308 return NULL;
1309 }
1310
1311 static char * OVS_WARN_UNUSED_RESULT
1312 parse_pop_vlan(struct ofpbuf *ofpacts)
1313 {
1314 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1315 return NULL;
1316 }
1317
1318 static void
1319 format_STRIP_VLAN(const struct ofpact_null *a, struct ds *s)
1320 {
1321 ds_put_cstr(s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1322 ? "pop_vlan"
1323 : "strip_vlan"));
1324 }
1325 \f
1326 /* Push VLAN action. */
1327
1328 static enum ofperr
1329 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type,
1330 enum ofp_version ofp_version OVS_UNUSED,
1331 struct ofpbuf *out)
1332 {
1333 if (eth_type != htons(ETH_TYPE_VLAN_8021Q)) {
1334 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1335 return OFPERR_OFPBAC_BAD_ARGUMENT;
1336 }
1337 ofpact_put_PUSH_VLAN(out);
1338 return 0;
1339 }
1340
1341 static void
1342 encode_PUSH_VLAN(const struct ofpact_null *null OVS_UNUSED,
1343 enum ofp_version ofp_version, struct ofpbuf *out)
1344 {
1345 if (ofp_version == OFP10_VERSION) {
1346 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1347 * follow this action. */
1348 } else {
1349 /* XXX ETH_TYPE_VLAN_8021AD case */
1350 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1351 }
1352 }
1353
1354 static char * OVS_WARN_UNUSED_RESULT
1355 parse_PUSH_VLAN(char *arg, struct ofpbuf *ofpacts,
1356 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1357 {
1358 uint16_t ethertype;
1359 char *error;
1360
1361 *usable_protocols &= OFPUTIL_P_OF11_UP;
1362 error = str_to_u16(arg, "ethertype", &ethertype);
1363 if (error) {
1364 return error;
1365 }
1366
1367 if (ethertype != ETH_TYPE_VLAN_8021Q) {
1368 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1369 return xasprintf("%s: not a valid VLAN ethertype", arg);
1370 }
1371
1372 ofpact_put_PUSH_VLAN(ofpacts);
1373 return NULL;
1374 }
1375
1376 static void
1377 format_PUSH_VLAN(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
1378 {
1379 /* XXX 802.1AD case*/
1380 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
1381 }
1382 \f
1383 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1384 struct ofp_action_dl_addr {
1385 ovs_be16 type; /* Type. */
1386 ovs_be16 len; /* Length is 16. */
1387 struct eth_addr dl_addr; /* Ethernet address. */
1388 uint8_t pad[6];
1389 };
1390 OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1391
1392 static enum ofperr
1393 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1394 enum ofp_version ofp_version OVS_UNUSED,
1395 struct ofpbuf *out)
1396 {
1397 ofpact_put_SET_ETH_SRC(out)->mac = a->dl_addr;
1398 return 0;
1399 }
1400
1401 static enum ofperr
1402 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1403 enum ofp_version ofp_version OVS_UNUSED,
1404 struct ofpbuf *out)
1405 {
1406 ofpact_put_SET_ETH_DST(out)->mac = a->dl_addr;
1407 return 0;
1408 }
1409
1410 static void
1411 encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1412 enum ofp_raw_action_type raw, enum mf_field_id field,
1413 struct ofpbuf *out)
1414 {
1415 if (ofp_version < OFP12_VERSION) {
1416 struct ofp_action_dl_addr *oada;
1417
1418 oada = ofpact_put_raw(out, ofp_version, raw, 0);
1419 oada->dl_addr = mac->mac;
1420 } else {
1421 ofpact_put_set_field(out, ofp_version, field,
1422 eth_addr_to_uint64(mac->mac));
1423 }
1424 }
1425
1426 static void
1427 encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1428 struct ofpbuf *out)
1429 {
1430 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1431 out);
1432
1433 }
1434
1435 static void
1436 encode_SET_ETH_DST(const struct ofpact_mac *mac,
1437 enum ofp_version ofp_version,
1438 struct ofpbuf *out)
1439 {
1440 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1441 out);
1442
1443 }
1444
1445 static char * OVS_WARN_UNUSED_RESULT
1446 parse_SET_ETH_SRC(char *arg, struct ofpbuf *ofpacts,
1447 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1448 {
1449 return str_to_mac(arg, &ofpact_put_SET_ETH_SRC(ofpacts)->mac);
1450 }
1451
1452 static char * OVS_WARN_UNUSED_RESULT
1453 parse_SET_ETH_DST(char *arg, struct ofpbuf *ofpacts,
1454 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1455 {
1456 return str_to_mac(arg, &ofpact_put_SET_ETH_DST(ofpacts)->mac);
1457 }
1458
1459 static void
1460 format_SET_ETH_SRC(const struct ofpact_mac *a, struct ds *s)
1461 {
1462 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1463 }
1464
1465 static void
1466 format_SET_ETH_DST(const struct ofpact_mac *a, struct ds *s)
1467 {
1468 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1469 }
1470 \f
1471 /* Set IPv4 address actions. */
1472
1473 static enum ofperr
1474 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4,
1475 enum ofp_version ofp_version OVS_UNUSED,
1476 struct ofpbuf *out)
1477 {
1478 ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
1479 return 0;
1480 }
1481
1482 static enum ofperr
1483 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4,
1484 enum ofp_version ofp_version OVS_UNUSED,
1485 struct ofpbuf *out)
1486 {
1487 ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
1488 return 0;
1489 }
1490
1491 static void
1492 encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
1493 enum ofp_version ofp_version,
1494 enum ofp_raw_action_type raw, enum mf_field_id field,
1495 struct ofpbuf *out)
1496 {
1497 ovs_be32 addr = ipv4->ipv4;
1498 if (ofp_version < OFP12_VERSION) {
1499 ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
1500 } else {
1501 ofpact_put_set_field(out, ofp_version, field, ntohl(addr));
1502 }
1503 }
1504
1505 static void
1506 encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
1507 enum ofp_version ofp_version, struct ofpbuf *out)
1508 {
1509 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
1510 out);
1511 }
1512
1513 static void
1514 encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
1515 enum ofp_version ofp_version, struct ofpbuf *out)
1516 {
1517 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
1518 out);
1519 }
1520
1521 static char * OVS_WARN_UNUSED_RESULT
1522 parse_SET_IPV4_SRC(char *arg, struct ofpbuf *ofpacts,
1523 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1524 {
1525 return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
1526 }
1527
1528 static char * OVS_WARN_UNUSED_RESULT
1529 parse_SET_IPV4_DST(char *arg, struct ofpbuf *ofpacts,
1530 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1531 {
1532 return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
1533 }
1534
1535 static void
1536 format_SET_IPV4_SRC(const struct ofpact_ipv4 *a, struct ds *s)
1537 {
1538 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(a->ipv4));
1539 }
1540
1541 static void
1542 format_SET_IPV4_DST(const struct ofpact_ipv4 *a, struct ds *s)
1543 {
1544 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(a->ipv4));
1545 }
1546 \f
1547 /* Set IPv4/v6 TOS actions. */
1548
1549 static enum ofperr
1550 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp,
1551 enum ofp_version ofp_version OVS_UNUSED,
1552 struct ofpbuf *out)
1553 {
1554 if (dscp & ~IP_DSCP_MASK) {
1555 return OFPERR_OFPBAC_BAD_ARGUMENT;
1556 } else {
1557 ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
1558 return 0;
1559 }
1560 }
1561
1562 static void
1563 encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
1564 enum ofp_version ofp_version, struct ofpbuf *out)
1565 {
1566 if (ofp_version < OFP12_VERSION) {
1567 put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
1568 } else {
1569 ofpact_put_set_field(out, ofp_version,
1570 MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
1571 }
1572 }
1573
1574 static char * OVS_WARN_UNUSED_RESULT
1575 parse_SET_IP_DSCP(char *arg, struct ofpbuf *ofpacts,
1576 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1577 {
1578 uint8_t tos;
1579 char *error;
1580
1581 error = str_to_u8(arg, "TOS", &tos);
1582 if (error) {
1583 return error;
1584 }
1585
1586 if (tos & ~IP_DSCP_MASK) {
1587 return xasprintf("%s: not a valid TOS", arg);
1588 }
1589 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
1590 return NULL;
1591 }
1592
1593 static void
1594 format_SET_IP_DSCP(const struct ofpact_dscp *a, struct ds *s)
1595 {
1596 ds_put_format(s, "mod_nw_tos:%d", a->dscp);
1597 }
1598 \f
1599 /* Set IPv4/v6 ECN actions. */
1600
1601 static enum ofperr
1602 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn,
1603 enum ofp_version ofp_version OVS_UNUSED,
1604 struct ofpbuf *out)
1605 {
1606 if (ecn & ~IP_ECN_MASK) {
1607 return OFPERR_OFPBAC_BAD_ARGUMENT;
1608 } else {
1609 ofpact_put_SET_IP_ECN(out)->ecn = ecn;
1610 return 0;
1611 }
1612 }
1613
1614 static void
1615 encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
1616 enum ofp_version ofp_version, struct ofpbuf *out)
1617 {
1618 uint8_t ecn = ip_ecn->ecn;
1619 if (ofp_version == OFP10_VERSION) {
1620 /* XXX */
1621 } else if (ofp_version == OFP11_VERSION) {
1622 put_OFPAT11_SET_NW_ECN(out, ecn);
1623 } else {
1624 ofpact_put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
1625 }
1626 }
1627
1628 static char * OVS_WARN_UNUSED_RESULT
1629 parse_SET_IP_ECN(char *arg, struct ofpbuf *ofpacts,
1630 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1631 {
1632 uint8_t ecn;
1633 char *error;
1634
1635 error = str_to_u8(arg, "ECN", &ecn);
1636 if (error) {
1637 return error;
1638 }
1639
1640 if (ecn & ~IP_ECN_MASK) {
1641 return xasprintf("%s: not a valid ECN", arg);
1642 }
1643 ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
1644 return NULL;
1645 }
1646
1647 static void
1648 format_SET_IP_ECN(const struct ofpact_ecn *a, struct ds *s)
1649 {
1650 ds_put_format(s, "mod_nw_ecn:%d", a->ecn);
1651 }
1652 \f
1653 /* Set IPv4/v6 TTL actions. */
1654
1655 static enum ofperr
1656 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl,
1657 enum ofp_version ofp_version OVS_UNUSED,
1658 struct ofpbuf *out)
1659 {
1660 ofpact_put_SET_IP_TTL(out)->ttl = ttl;
1661 return 0;
1662 }
1663
1664 static void
1665 encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
1666 enum ofp_version ofp_version, struct ofpbuf *out)
1667 {
1668 if (ofp_version >= OFP11_VERSION) {
1669 put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
1670 } else {
1671 /* XXX */
1672 }
1673 }
1674
1675 static char * OVS_WARN_UNUSED_RESULT
1676 parse_SET_IP_TTL(char *arg, struct ofpbuf *ofpacts,
1677 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1678 {
1679 uint8_t ttl;
1680 char *error;
1681
1682 error = str_to_u8(arg, "TTL", &ttl);
1683 if (error) {
1684 return error;
1685 }
1686
1687 ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
1688 return NULL;
1689 }
1690
1691 static void
1692 format_SET_IP_TTL(const struct ofpact_ip_ttl *a, struct ds *s)
1693 {
1694 ds_put_format(s, "mod_nw_ttl:%d", a->ttl);
1695 }
1696 \f
1697 /* Set TCP/UDP/SCTP port actions. */
1698
1699 static enum ofperr
1700 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port,
1701 enum ofp_version ofp_version OVS_UNUSED,
1702 struct ofpbuf *out)
1703 {
1704 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
1705 return 0;
1706 }
1707
1708 static enum ofperr
1709 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port,
1710 enum ofp_version ofp_version OVS_UNUSED,
1711 struct ofpbuf *out)
1712 {
1713 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
1714 return 0;
1715 }
1716
1717 static void
1718 encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
1719 enum ofp_version ofp_version, enum ofp_raw_action_type raw,
1720 enum mf_field_id field, struct ofpbuf *out)
1721 {
1722 uint16_t port = l4_port->port;
1723
1724 if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
1725 ofpact_put_set_field(out, ofp_version, field, port);
1726 } else {
1727 ofpact_put_raw(out, ofp_version, raw, port);
1728 }
1729 }
1730
1731 static void
1732 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
1733 enum ofp_version ofp_version, struct ofpbuf *out)
1734 {
1735 uint8_t proto = l4_port->flow_ip_proto;
1736 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
1737 : proto == IPPROTO_UDP ? MFF_UDP_SRC
1738 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
1739 : MFF_N_IDS);
1740
1741 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
1742 }
1743
1744 static void
1745 encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
1746 enum ofp_version ofp_version,
1747 struct ofpbuf *out)
1748 {
1749 uint8_t proto = l4_port->flow_ip_proto;
1750 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
1751 : proto == IPPROTO_UDP ? MFF_UDP_DST
1752 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
1753 : MFF_N_IDS);
1754
1755 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
1756 }
1757
1758 static char * OVS_WARN_UNUSED_RESULT
1759 parse_SET_L4_SRC_PORT(char *arg, struct ofpbuf *ofpacts,
1760 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1761 {
1762 return str_to_u16(arg, "source port",
1763 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
1764 }
1765
1766 static char * OVS_WARN_UNUSED_RESULT
1767 parse_SET_L4_DST_PORT(char *arg, struct ofpbuf *ofpacts,
1768 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1769 {
1770 return str_to_u16(arg, "destination port",
1771 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
1772 }
1773
1774 static void
1775 format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a, struct ds *s)
1776 {
1777 ds_put_format(s, "mod_tp_src:%d", a->port);
1778 }
1779
1780 static void
1781 format_SET_L4_DST_PORT(const struct ofpact_l4_port *a, struct ds *s)
1782 {
1783 ds_put_format(s, "mod_tp_dst:%d", a->port);
1784 }
1785 \f
1786 /* Action structure for OFPAT_COPY_FIELD. */
1787 struct ofp15_action_copy_field {
1788 ovs_be16 type; /* OFPAT_COPY_FIELD. */
1789 ovs_be16 len; /* Length is padded to 64 bits. */
1790 ovs_be16 n_bits; /* Number of bits to copy. */
1791 ovs_be16 src_offset; /* Starting bit offset in source. */
1792 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1793 uint8_t pad[2];
1794 /* Followed by:
1795 * - OXM header for source field.
1796 * - OXM header for destination field.
1797 * - Padding with 0-bytes to a multiple of 8 bytes.
1798 * The "pad2" member is the beginning of the above. */
1799 uint8_t pad2[4];
1800 };
1801 OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
1802
1803 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
1804 struct onf_action_copy_field {
1805 ovs_be16 type; /* OFPAT_EXPERIMENTER. */
1806 ovs_be16 len; /* Length is padded to 64 bits. */
1807 ovs_be32 experimenter; /* ONF_VENDOR_ID. */
1808 ovs_be16 exp_type; /* 3200. */
1809 uint8_t pad[2]; /* Not used. */
1810 ovs_be16 n_bits; /* Number of bits to copy. */
1811 ovs_be16 src_offset; /* Starting bit offset in source. */
1812 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1813 uint8_t pad2[2]; /* Not used. */
1814 /* Followed by:
1815 * - OXM header for source field.
1816 * - OXM header for destination field.
1817 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
1818 * The "pad3" member is the beginning of the above. */
1819 uint8_t pad3[4]; /* Not used. */
1820 };
1821 OFP_ASSERT(sizeof(struct onf_action_copy_field) == 24);
1822
1823 /* Action structure for NXAST_REG_MOVE.
1824 *
1825 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
1826 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
1827 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
1828 * the next most significant bit, and so on.
1829 *
1830 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
1831 * sense to use nxm_hasmask=1 because the action does not do any kind of
1832 * matching; it uses the actual value of a field.)
1833 *
1834 * The following nxm_header values are potentially acceptable as 'src':
1835 *
1836 * - NXM_OF_IN_PORT
1837 * - NXM_OF_ETH_DST
1838 * - NXM_OF_ETH_SRC
1839 * - NXM_OF_ETH_TYPE
1840 * - NXM_OF_VLAN_TCI
1841 * - NXM_OF_IP_TOS
1842 * - NXM_OF_IP_PROTO
1843 * - NXM_OF_IP_SRC
1844 * - NXM_OF_IP_DST
1845 * - NXM_OF_TCP_SRC
1846 * - NXM_OF_TCP_DST
1847 * - NXM_OF_UDP_SRC
1848 * - NXM_OF_UDP_DST
1849 * - NXM_OF_ICMP_TYPE
1850 * - NXM_OF_ICMP_CODE
1851 * - NXM_OF_ARP_OP
1852 * - NXM_OF_ARP_SPA
1853 * - NXM_OF_ARP_TPA
1854 * - NXM_NX_TUN_ID
1855 * - NXM_NX_ARP_SHA
1856 * - NXM_NX_ARP_THA
1857 * - NXM_NX_ICMPV6_TYPE
1858 * - NXM_NX_ICMPV6_CODE
1859 * - NXM_NX_ND_SLL
1860 * - NXM_NX_ND_TLL
1861 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1862 * - NXM_NX_PKT_MARK
1863 * - NXM_NX_TUN_IPV4_SRC
1864 * - NXM_NX_TUN_IPV4_DST
1865 *
1866 * The following nxm_header values are potentially acceptable as 'dst':
1867 *
1868 * - NXM_OF_ETH_DST
1869 * - NXM_OF_ETH_SRC
1870 * - NXM_OF_IP_TOS
1871 * - NXM_OF_IP_SRC
1872 * - NXM_OF_IP_DST
1873 * - NXM_OF_TCP_SRC
1874 * - NXM_OF_TCP_DST
1875 * - NXM_OF_UDP_SRC
1876 * - NXM_OF_UDP_DST
1877 * - NXM_NX_ARP_SHA
1878 * - NXM_NX_ARP_THA
1879 * - NXM_OF_ARP_OP
1880 * - NXM_OF_ARP_SPA
1881 * - NXM_OF_ARP_TPA
1882 * Modifying any of the above fields changes the corresponding packet
1883 * header.
1884 *
1885 * - NXM_OF_IN_PORT
1886 *
1887 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1888 *
1889 * - NXM_NX_PKT_MARK
1890 *
1891 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
1892 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
1893 * header (if any), ignoring the other bits. Setting a value with CFI=1
1894 * adds or modifies the 802.1Q header appropriately, setting the TCI field
1895 * to the field's new value (with the CFI bit masked out).
1896 *
1897 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
1898 * any of these values modifies the corresponding tunnel header field used
1899 * for the packet's next tunnel encapsulation, if allowed by the
1900 * configuration of the output tunnel port.
1901 *
1902 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
1903 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
1904 * used only if the flow's nx_match includes an nxm_entry that specifies
1905 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
1906 *
1907 * The switch will reject actions for which src_ofs+n_bits is greater than the
1908 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
1909 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
1910 *
1911 * This action behaves properly when 'src' overlaps with 'dst', that is, it
1912 * behaves as if 'src' were copied out to a temporary buffer, then the
1913 * temporary buffer copied to 'dst'.
1914 */
1915 struct nx_action_reg_move {
1916 ovs_be16 type; /* OFPAT_VENDOR. */
1917 ovs_be16 len; /* Length is 24. */
1918 ovs_be32 vendor; /* NX_VENDOR_ID. */
1919 ovs_be16 subtype; /* NXAST_REG_MOVE. */
1920 ovs_be16 n_bits; /* Number of bits. */
1921 ovs_be16 src_ofs; /* Starting bit offset in source. */
1922 ovs_be16 dst_ofs; /* Starting bit offset in destination. */
1923 /* Followed by:
1924 * - OXM/NXM header for source field (4 or 8 bytes).
1925 * - OXM/NXM header for destination field (4 or 8 bytes).
1926 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
1927 };
1928 OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
1929
1930 static enum ofperr
1931 decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
1932 const void *action, ovs_be16 action_len, size_t oxm_offset,
1933 struct ofpbuf *ofpacts)
1934 {
1935 struct ofpact_reg_move *move;
1936 enum ofperr error;
1937 struct ofpbuf b;
1938
1939 move = ofpact_put_REG_MOVE(ofpacts);
1940 move->ofpact.raw = ONFACT_RAW13_COPY_FIELD;
1941 move->src.ofs = ntohs(src_offset);
1942 move->src.n_bits = ntohs(n_bits);
1943 move->dst.ofs = ntohs(dst_offset);
1944 move->dst.n_bits = ntohs(n_bits);
1945
1946 ofpbuf_use_const(&b, action, ntohs(action_len));
1947 ofpbuf_pull(&b, oxm_offset);
1948 error = nx_pull_header(&b, &move->src.field, NULL);
1949 if (error) {
1950 return error;
1951 }
1952 error = nx_pull_header(&b, &move->dst.field, NULL);
1953 if (error) {
1954 return error;
1955 }
1956
1957 if (!is_all_zeros(b.data, b.size)) {
1958 return OFPERR_NXBRC_MUST_BE_ZERO;
1959 }
1960
1961 return nxm_reg_move_check(move, NULL);
1962 }
1963
1964 static enum ofperr
1965 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
1966 enum ofp_version ofp_version OVS_UNUSED,
1967 struct ofpbuf *ofpacts)
1968 {
1969 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
1970 oacf->n_bits, oacf, oacf->len,
1971 OBJECT_OFFSETOF(oacf, pad2), ofpacts);
1972 }
1973
1974 static enum ofperr
1975 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field *oacf,
1976 enum ofp_version ofp_version OVS_UNUSED,
1977 struct ofpbuf *ofpacts)
1978 {
1979 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
1980 oacf->n_bits, oacf, oacf->len,
1981 OBJECT_OFFSETOF(oacf, pad3), ofpacts);
1982 }
1983
1984 static enum ofperr
1985 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
1986 enum ofp_version ofp_version OVS_UNUSED,
1987 struct ofpbuf *ofpacts)
1988 {
1989 struct ofpact_reg_move *move;
1990 enum ofperr error;
1991 struct ofpbuf b;
1992
1993 move = ofpact_put_REG_MOVE(ofpacts);
1994 move->ofpact.raw = NXAST_RAW_REG_MOVE;
1995 move->src.ofs = ntohs(narm->src_ofs);
1996 move->src.n_bits = ntohs(narm->n_bits);
1997 move->dst.ofs = ntohs(narm->dst_ofs);
1998 move->dst.n_bits = ntohs(narm->n_bits);
1999
2000 ofpbuf_use_const(&b, narm, ntohs(narm->len));
2001 ofpbuf_pull(&b, sizeof *narm);
2002 error = nx_pull_header(&b, &move->src.field, NULL);
2003 if (error) {
2004 return error;
2005 }
2006 error = nx_pull_header(&b, &move->dst.field, NULL);
2007 if (error) {
2008 return error;
2009 }
2010 if (!is_all_zeros(b.data, b.size)) {
2011 return OFPERR_NXBRC_MUST_BE_ZERO;
2012 }
2013
2014 return nxm_reg_move_check(move, NULL);
2015 }
2016
2017 static void
2018 encode_REG_MOVE(const struct ofpact_reg_move *move,
2019 enum ofp_version ofp_version, struct ofpbuf *out)
2020 {
2021 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2022 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
2023 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
2024 * probe for support. Until we have that ability, we currently prefer
2025 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2026 * versions. */
2027 size_t start_ofs = out->size;
2028 if (ofp_version >= OFP15_VERSION) {
2029 struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
2030 copy->n_bits = htons(move->dst.n_bits);
2031 copy->src_offset = htons(move->src.ofs);
2032 copy->dst_offset = htons(move->dst.ofs);
2033 out->size = out->size - sizeof copy->pad2;
2034 nx_put_header(out, move->src.field->id, ofp_version, false);
2035 nx_put_header(out, move->dst.field->id, ofp_version, false);
2036 } else if (ofp_version == OFP13_VERSION
2037 && move->ofpact.raw == ONFACT_RAW13_COPY_FIELD) {
2038 struct onf_action_copy_field *copy = put_ONFACT13_COPY_FIELD(out);
2039 copy->n_bits = htons(move->dst.n_bits);
2040 copy->src_offset = htons(move->src.ofs);
2041 copy->dst_offset = htons(move->dst.ofs);
2042 out->size = out->size - sizeof copy->pad3;
2043 nx_put_header(out, move->src.field->id, ofp_version, false);
2044 nx_put_header(out, move->dst.field->id, ofp_version, false);
2045 } else {
2046 struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
2047 narm->n_bits = htons(move->dst.n_bits);
2048 narm->src_ofs = htons(move->src.ofs);
2049 narm->dst_ofs = htons(move->dst.ofs);
2050 nx_put_header(out, move->src.field->id, 0, false);
2051 nx_put_header(out, move->dst.field->id, 0, false);
2052 }
2053 pad_ofpat(out, start_ofs);
2054 }
2055
2056 static char * OVS_WARN_UNUSED_RESULT
2057 parse_REG_MOVE(const char *arg, struct ofpbuf *ofpacts,
2058 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2059 {
2060 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2061 const char *full_arg = arg;
2062 char *error;
2063
2064 error = mf_parse_subfield__(&move->src, &arg);
2065 if (error) {
2066 return error;
2067 }
2068 if (strncmp(arg, "->", 2)) {
2069 return xasprintf("%s: missing `->' following source", full_arg);
2070 }
2071 arg += 2;
2072 error = mf_parse_subfield(&move->dst, arg);
2073 if (error) {
2074 return error;
2075 }
2076
2077 if (move->src.n_bits != move->dst.n_bits) {
2078 return xasprintf("%s: source field is %d bits wide but destination is "
2079 "%d bits wide", full_arg,
2080 move->src.n_bits, move->dst.n_bits);
2081 }
2082 return NULL;
2083 }
2084
2085 static void
2086 format_REG_MOVE(const struct ofpact_reg_move *a, struct ds *s)
2087 {
2088 nxm_format_reg_move(a, s);
2089 }
2090 \f
2091 /* Action structure for OFPAT12_SET_FIELD. */
2092 struct ofp12_action_set_field {
2093 ovs_be16 type; /* OFPAT12_SET_FIELD. */
2094 ovs_be16 len; /* Length is padded to 64 bits. */
2095
2096 /* Followed by:
2097 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2098 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2099 *
2100 * The "pad" member is the beginning of the above. */
2101 uint8_t pad[4];
2102 };
2103 OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
2104
2105 /* Action structure for NXAST_REG_LOAD.
2106 *
2107 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2108 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2109 * starts at 0 for the least-significant bit, 1 for the next most significant
2110 * bit, and so on.
2111 *
2112 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2113 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2114 * loading them.
2115 *
2116 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2117 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2118 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2119 * also stored as one less than its true value:
2120 *
2121 * 15 6 5 0
2122 * +------------------------------+------------------+
2123 * | ofs | n_bits - 1 |
2124 * +------------------------------+------------------+
2125 *
2126 * The switch will reject actions for which ofs+n_bits is greater than the
2127 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2128 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2129 * OFPBAC_BAD_ARGUMENT.
2130 */
2131 struct nx_action_reg_load {
2132 ovs_be16 type; /* OFPAT_VENDOR. */
2133 ovs_be16 len; /* Length is 24. */
2134 ovs_be32 vendor; /* NX_VENDOR_ID. */
2135 ovs_be16 subtype; /* NXAST_REG_LOAD. */
2136 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
2137 ovs_be32 dst; /* Destination register. */
2138 ovs_be64 value; /* Immediate value. */
2139 };
2140 OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
2141
2142 /* Action structure for NXAST_REG_LOAD2.
2143 *
2144 * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2145 * in any OpenFlow version. Compared to NXAST_REG_LOAD, we can use this to set
2146 * OXM experimenter fields. */
2147 struct nx_action_reg_load2 {
2148 ovs_be16 type; /* OFPAT_VENDOR. */
2149 ovs_be16 len; /* At least 16. */
2150 ovs_be32 vendor; /* NX_VENDOR_ID. */
2151 ovs_be16 subtype; /* NXAST_SET_FIELD. */
2152
2153 /* Followed by:
2154 * - An NXM/OXM header, value, and optionally a mask.
2155 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2156 *
2157 * The "pad" member is the beginning of the above. */
2158 uint8_t pad[6];
2159 };
2160 OFP_ASSERT(sizeof(struct nx_action_reg_load2) == 16);
2161
2162 static enum ofperr
2163 decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2164 bool may_mask, struct ofpbuf *ofpacts)
2165 {
2166 struct ofpact_set_field *sf;
2167 enum ofperr error;
2168 struct ofpbuf b;
2169
2170 sf = ofpact_put_SET_FIELD(ofpacts);
2171
2172 ofpbuf_use_const(&b, oasf, ntohs(oasf->len));
2173 ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
2174 error = nx_pull_entry(&b, &sf->field, &sf->value,
2175 may_mask ? &sf->mask : NULL);
2176 if (error) {
2177 return (error == OFPERR_OFPBMC_BAD_MASK
2178 ? OFPERR_OFPBAC_BAD_SET_MASK
2179 : error);
2180 }
2181 if (!may_mask) {
2182 memset(&sf->mask, 0xff, sf->field->n_bytes);
2183 }
2184
2185 if (!is_all_zeros(b.data, b.size)) {
2186 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2187 }
2188
2189 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2190 * Set-Field. */
2191 if (sf->field->id == MFF_IN_PORT_OXM) {
2192 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2193 }
2194
2195 /* oxm_length is now validated to be compatible with mf_value. */
2196 if (!sf->field->writable) {
2197 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2198 sf->field->name);
2199 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2200 }
2201
2202 /* The value must be valid for match. OpenFlow 1.5 also says,
2203 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2204 * a 1-bit in oxm_value and in oxm_mask." */
2205 if (!mf_is_value_valid(sf->field, &sf->value)
2206 || (sf->field->id == MFF_VLAN_VID
2207 && (!(sf->mask.be16 & htons(OFPVID12_PRESENT))
2208 || !(sf->value.be16 & htons(OFPVID12_PRESENT))))) {
2209 struct ds ds = DS_EMPTY_INITIALIZER;
2210 mf_format(sf->field, &sf->value, NULL, &ds);
2211 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
2212 sf->field->name, ds_cstr(&ds));
2213 ds_destroy(&ds);
2214
2215 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2216 }
2217 return 0;
2218 }
2219
2220 static enum ofperr
2221 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
2222 enum ofp_version ofp_version OVS_UNUSED,
2223 struct ofpbuf *ofpacts)
2224 {
2225 return decode_ofpat_set_field(oasf, false, ofpacts);
2226 }
2227
2228 static enum ofperr
2229 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
2230 enum ofp_version ofp_version OVS_UNUSED,
2231 struct ofpbuf *ofpacts)
2232 {
2233 return decode_ofpat_set_field(oasf, true, ofpacts);
2234 }
2235
2236 static enum ofperr
2237 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2238 enum ofp_version ofp_version OVS_UNUSED,
2239 struct ofpbuf *out)
2240 {
2241 struct ofpact_set_field *sf = ofpact_put_reg_load(out);
2242 struct mf_subfield dst;
2243 enum ofperr error;
2244
2245 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2246
2247 dst.field = mf_from_nxm_header(ntohl(narl->dst));
2248 dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2249 dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2250 error = mf_check_dst(&dst, NULL);
2251 if (error) {
2252 return error;
2253 }
2254
2255 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2256 * narl->value. */
2257 if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2258 return OFPERR_OFPBAC_BAD_ARGUMENT;
2259 }
2260
2261 sf->field = dst.field;
2262 bitwise_put(ntohll(narl->value),
2263 &sf->value, dst.field->n_bytes, dst.ofs,
2264 dst.n_bits);
2265 bitwise_put(UINT64_MAX,
2266 &sf->mask, dst.field->n_bytes, dst.ofs,
2267 dst.n_bits);
2268
2269 return 0;
2270 }
2271
2272 static enum ofperr
2273 decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2 *narl,
2274 enum ofp_version ofp_version OVS_UNUSED,
2275 struct ofpbuf *out)
2276 {
2277 struct ofpact_set_field *sf;
2278 enum ofperr error;
2279 struct ofpbuf b;
2280
2281 sf = ofpact_put_SET_FIELD(out);
2282 sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
2283
2284 ofpbuf_use_const(&b, narl, ntohs(narl->len));
2285 ofpbuf_pull(&b, OBJECT_OFFSETOF(narl, pad));
2286 error = nx_pull_entry(&b, &sf->field, &sf->value, &sf->mask);
2287 if (error) {
2288 return error;
2289 }
2290 if (!is_all_zeros(b.data, b.size)) {
2291 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2292 }
2293
2294 if (!sf->field->writable) {
2295 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2296 sf->field->name);
2297 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2298 }
2299 return 0;
2300 }
2301
2302 static void
2303 ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2304 enum mf_field_id field, uint64_t value_)
2305 {
2306 struct ofp12_action_set_field *oasf OVS_UNUSED;
2307 int n_bytes = mf_from_id(field)->n_bytes;
2308 size_t start_ofs = openflow->size;
2309 union mf_value value;
2310
2311 value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2312
2313 oasf = put_OFPAT12_SET_FIELD(openflow);
2314 openflow->size = openflow->size - sizeof oasf->pad;
2315 nx_put_entry(openflow, field, ofp_version, &value, NULL);
2316 pad_ofpat(openflow, start_ofs);
2317 }
2318
2319 static bool
2320 next_load_segment(const struct ofpact_set_field *sf,
2321 struct mf_subfield *dst, uint64_t *value)
2322 {
2323 int n_bits = sf->field->n_bits;
2324 int n_bytes = sf->field->n_bytes;
2325 int start = dst->ofs + dst->n_bits;
2326
2327 if (start < n_bits) {
2328 dst->field = sf->field;
2329 dst->ofs = bitwise_scan(&sf->mask, n_bytes, 1, start, n_bits);
2330 if (dst->ofs < n_bits) {
2331 dst->n_bits = bitwise_scan(&sf->mask, n_bytes, 0, dst->ofs + 1,
2332 MIN(dst->ofs + 64, n_bits)) - dst->ofs;
2333 *value = bitwise_get(&sf->value, n_bytes, dst->ofs, dst->n_bits);
2334 return true;
2335 }
2336 }
2337 return false;
2338 }
2339
2340 /* Convert 'sf' to a series of REG_LOADs. */
2341 static void
2342 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2343 {
2344 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2345 * experimenter OXM or is variable length (or if it came in as
2346 * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2. Otherwise use
2347 * NXAST_REG_LOAD, which is backward compatible. */
2348 if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
2349 || !mf_nxm_header(sf->field->id) || sf->field->variable_len) {
2350 struct nx_action_reg_load2 *narl OVS_UNUSED;
2351 size_t start_ofs = openflow->size;
2352
2353 narl = put_NXAST_REG_LOAD2(openflow);
2354 openflow->size = openflow->size - sizeof narl->pad;
2355 nx_put_entry(openflow, sf->field->id, 0, &sf->value, &sf->mask);
2356 pad_ofpat(openflow, start_ofs);
2357 } else {
2358 struct mf_subfield dst;
2359 uint64_t value;
2360
2361 dst.ofs = dst.n_bits = 0;
2362 while (next_load_segment(sf, &dst, &value)) {
2363 struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2364 narl->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
2365 narl->dst = htonl(mf_nxm_header(dst.field->id));
2366 narl->value = htonll(value);
2367 }
2368 }
2369 }
2370
2371 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2372 * actions, if we can, falling back to Nicira extensions if we must.
2373 *
2374 * We check only meta-flow types that can appear within set field actions and
2375 * that have a mapping to compatible action types. These struct mf_field
2376 * definitions have a defined OXM or NXM header value and specify the field as
2377 * writable. */
2378 static void
2379 set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
2380 enum ofp_version ofp_version,
2381 struct ofpbuf *out)
2382 {
2383 switch ((int) sf->field->id) {
2384 case MFF_VLAN_TCI: {
2385 ovs_be16 tci = sf->value.be16;
2386 bool cfi = (tci & htons(VLAN_CFI)) != 0;
2387 uint16_t vid = vlan_tci_to_vid(tci);
2388 uint8_t pcp = vlan_tci_to_pcp(tci);
2389
2390 if (ofp_version < OFP11_VERSION) {
2391 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2392 *
2393 * If CFI=1, Add or modify VLAN VID & PCP.
2394 * If CFI=0, strip VLAN header, if any.
2395 */
2396 if (cfi) {
2397 put_OFPAT10_SET_VLAN_VID(out, vid);
2398 put_OFPAT10_SET_VLAN_PCP(out, pcp);
2399 } else {
2400 put_OFPAT10_STRIP_VLAN(out);
2401 }
2402 } else {
2403 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2404 *
2405 * If CFI=1, Add or modify VLAN VID & PCP.
2406 * OpenFlow 1.1 set actions only apply if the packet
2407 * already has VLAN tags. To be sure that is the case
2408 * we have to push a VLAN header. As we do not support
2409 * multiple layers of VLANs, this is a no-op, if a VLAN
2410 * header already exists. This may backfire, however,
2411 * when we start supporting multiple layers of VLANs.
2412 * If CFI=0, strip VLAN header, if any.
2413 */
2414 if (cfi) {
2415 /* Push a VLAN tag, if one was not seen at action validation
2416 * time. */
2417 if (!sf->flow_has_vlan) {
2418 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
2419 }
2420 put_OFPAT11_SET_VLAN_VID(out, vid);
2421 put_OFPAT11_SET_VLAN_PCP(out, pcp);
2422 } else {
2423 /* If the flow did not match on vlan, we have no way of
2424 * knowing if the vlan tag exists, so we must POP just to be
2425 * sure. */
2426 put_OFPAT11_POP_VLAN(out);
2427 }
2428 }
2429 break;
2430 }
2431
2432 case MFF_VLAN_VID: {
2433 uint16_t vid = ntohs(sf->value.be16) & VLAN_VID_MASK;
2434 if (ofp_version == OFP10_VERSION) {
2435 put_OFPAT10_SET_VLAN_VID(out, vid);
2436 } else {
2437 put_OFPAT11_SET_VLAN_VID(out, vid);
2438 }
2439 break;
2440 }
2441
2442 case MFF_VLAN_PCP:
2443 if (ofp_version == OFP10_VERSION) {
2444 put_OFPAT10_SET_VLAN_PCP(out, sf->value.u8);
2445 } else {
2446 put_OFPAT11_SET_VLAN_PCP(out, sf->value.u8);
2447 }
2448 break;
2449
2450 case MFF_ETH_SRC:
2451 put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr = sf->value.mac;
2452 break;
2453
2454 case MFF_ETH_DST:
2455 put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr = sf->value.mac;
2456 break;
2457
2458 case MFF_IPV4_SRC:
2459 put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value.be32);
2460 break;
2461
2462 case MFF_IPV4_DST:
2463 put_OFPAT_SET_NW_DST(out, ofp_version, sf->value.be32);
2464 break;
2465
2466 case MFF_IP_DSCP:
2467 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8);
2468 break;
2469
2470 case MFF_IP_DSCP_SHIFTED:
2471 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8 << 2);
2472 break;
2473
2474 case MFF_TCP_SRC:
2475 case MFF_UDP_SRC:
2476 put_OFPAT_SET_TP_SRC(out, sf->value.be16);
2477 break;
2478
2479 case MFF_TCP_DST:
2480 case MFF_UDP_DST:
2481 put_OFPAT_SET_TP_DST(out, sf->value.be16);
2482 break;
2483
2484 default:
2485 set_field_to_nxast(sf, out);
2486 break;
2487 }
2488 }
2489
2490 static void
2491 set_field_to_set_field(const struct ofpact_set_field *sf,
2492 enum ofp_version ofp_version, struct ofpbuf *out)
2493 {
2494 struct ofp12_action_set_field *oasf OVS_UNUSED;
2495 size_t start_ofs = out->size;
2496
2497 oasf = put_OFPAT12_SET_FIELD(out);
2498 out->size = out->size - sizeof oasf->pad;
2499 nx_put_entry(out, sf->field->id, ofp_version, &sf->value, &sf->mask);
2500 pad_ofpat(out, start_ofs);
2501 }
2502
2503 static void
2504 encode_SET_FIELD(const struct ofpact_set_field *sf,
2505 enum ofp_version ofp_version, struct ofpbuf *out)
2506 {
2507 if (ofp_version >= OFP15_VERSION) {
2508 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2509 * entirely). */
2510 set_field_to_set_field(sf, ofp_version, out);
2511 } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
2512 sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
2513 /* It came in as reg_load, send it out the same way. */
2514 set_field_to_nxast(sf, out);
2515 } else if (ofp_version < OFP12_VERSION) {
2516 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2517 set_field_to_legacy_openflow(sf, ofp_version, out);
2518 } else if (is_all_ones((const uint8_t *) &sf->mask, sf->field->n_bytes)) {
2519 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2520 * entire field, so encode it as OFPAT_SET_FIELD. */
2521 set_field_to_set_field(sf, ofp_version, out);
2522 } else {
2523 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2524 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2525 * so encode it as reg_load. */
2526 set_field_to_nxast(sf, out);
2527 }
2528 }
2529
2530 /* Parses the input argument 'arg' into the key, value, and delimiter
2531 * components that are common across the reg_load and set_field action format.
2532 *
2533 * With an argument like "1->metadata", sets the following pointers to
2534 * point within 'arg':
2535 * key: "metadata"
2536 * value: "1"
2537 * delim: "->"
2538 *
2539 * Returns NULL if successful, otherwise a malloc()'d string describing the
2540 * error. The caller is responsible for freeing the returned string. */
2541 static char * OVS_WARN_UNUSED_RESULT
2542 set_field_split_str(char *arg, char **key, char **value, char **delim)
2543 {
2544 char *value_end;
2545
2546 *value = arg;
2547 value_end = strstr(arg, "->");
2548 *key = value_end + strlen("->");
2549 if (delim) {
2550 *delim = value_end;
2551 }
2552
2553 if (!value_end) {
2554 return xasprintf("%s: missing `->'", arg);
2555 }
2556 if (strlen(value_end) <= strlen("->")) {
2557 return xasprintf("%s: missing field name following `->'", arg);
2558 }
2559
2560 return NULL;
2561 }
2562
2563 /* Parses a "set_field" action with argument 'arg', appending the parsed
2564 * action to 'ofpacts'.
2565 *
2566 * Returns NULL if successful, otherwise a malloc()'d string describing the
2567 * error. The caller is responsible for freeing the returned string. */
2568 static char * OVS_WARN_UNUSED_RESULT
2569 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
2570 enum ofputil_protocol *usable_protocols)
2571 {
2572 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2573 char *value;
2574 char *delim;
2575 char *key;
2576 const struct mf_field *mf;
2577 char *error;
2578
2579 error = set_field_split_str(arg, &key, &value, &delim);
2580 if (error) {
2581 return error;
2582 }
2583
2584 mf = mf_from_name(key);
2585 if (!mf) {
2586 return xasprintf("%s is not a valid OXM field name", key);
2587 }
2588 if (!mf->writable) {
2589 return xasprintf("%s is read-only", key);
2590 }
2591 sf->field = mf;
2592 delim[0] = '\0';
2593 error = mf_parse(mf, value, &sf->value, &sf->mask);
2594 if (error) {
2595 return error;
2596 }
2597
2598 if (!mf_is_value_valid(mf, &sf->value)) {
2599 return xasprintf("%s is not a valid value for field %s", value, key);
2600 }
2601
2602 *usable_protocols &= mf->usable_protocols_exact;
2603 return NULL;
2604 }
2605
2606 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2607 * action to 'ofpacts'.
2608 *
2609 * Returns NULL if successful, otherwise a malloc()'d string describing the
2610 * error. The caller is responsible for freeing the returned string. */
2611 static char * OVS_WARN_UNUSED_RESULT
2612 parse_SET_FIELD(const char *arg, struct ofpbuf *ofpacts,
2613 enum ofputil_protocol *usable_protocols)
2614 {
2615 char *copy = xstrdup(arg);
2616 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
2617 free(copy);
2618 return error;
2619 }
2620
2621 static char * OVS_WARN_UNUSED_RESULT
2622 parse_reg_load(char *arg, struct ofpbuf *ofpacts)
2623 {
2624 struct ofpact_set_field *sf = ofpact_put_reg_load(ofpacts);
2625 struct mf_subfield dst;
2626 char *key, *value_str;
2627 union mf_value value;
2628 char *error;
2629
2630 error = set_field_split_str(arg, &key, &value_str, NULL);
2631 if (error) {
2632 return error;
2633 }
2634
2635 error = mf_parse_subfield(&dst, key);
2636 if (error) {
2637 return error;
2638 }
2639
2640 if (parse_int_string(value_str, (uint8_t *)&value, dst.field->n_bytes,
2641 &key)) {
2642 return xasprintf("%s: cannot parse integer value", arg);
2643 }
2644
2645 if (!bitwise_is_all_zeros(&value, dst.field->n_bytes, dst.n_bits,
2646 dst.field->n_bytes * 8 - dst.n_bits)) {
2647 struct ds ds;
2648
2649 ds_init(&ds);
2650 mf_format(dst.field, &value, NULL, &ds);
2651 error = xasprintf("%s: value %s does not fit into %d bits",
2652 arg, ds_cstr(&ds), dst.n_bits);
2653 ds_destroy(&ds);
2654 return error;
2655 }
2656
2657 sf->field = dst.field;
2658 memset(&sf->value, 0, sizeof sf->value);
2659 bitwise_copy(&value, dst.field->n_bytes, 0, &sf->value,
2660 dst.field->n_bytes, dst.ofs, dst.n_bits);
2661 bitwise_one(&sf->mask, dst.field->n_bytes, dst.ofs, dst.n_bits);
2662
2663 return NULL;
2664 }
2665
2666 static void
2667 format_SET_FIELD(const struct ofpact_set_field *a, struct ds *s)
2668 {
2669 if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
2670 struct mf_subfield dst;
2671 uint64_t value;
2672
2673 dst.ofs = dst.n_bits = 0;
2674 while (next_load_segment(a, &dst, &value)) {
2675 ds_put_format(s, "load:%#"PRIx64"->", value);
2676 mf_format_subfield(&dst, s);
2677 ds_put_char(s, ',');
2678 }
2679 ds_chomp(s, ',');
2680 } else {
2681 ds_put_cstr(s, "set_field:");
2682 mf_format(a->field, &a->value, &a->mask, s);
2683 ds_put_format(s, "->%s", a->field->name);
2684 }
2685 }
2686
2687 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2688 * is marked such that, if possible, it will be translated to OpenFlow as
2689 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2690 * that was the way that the action was expressed when it came into OVS or for
2691 * backward compatibility. */
2692 struct ofpact_set_field *
2693 ofpact_put_reg_load(struct ofpbuf *ofpacts)
2694 {
2695 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2696 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2697 return sf;
2698 }
2699 \f
2700 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2701 *
2702 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2703 * top of the stack.
2704 */
2705 struct nx_action_stack {
2706 ovs_be16 type; /* OFPAT_VENDOR. */
2707 ovs_be16 len; /* Length is 16. */
2708 ovs_be32 vendor; /* NX_VENDOR_ID. */
2709 ovs_be16 subtype; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2710 ovs_be16 offset; /* Bit offset into the field. */
2711 /* Followed by:
2712 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2713 * - ovs_be16 'n_bits', the number of bits to extract from the field.
2714 * - Enough 0-bytes to pad out the action to 24 bytes. */
2715 uint8_t pad[12]; /* See above. */
2716 };
2717 OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
2718
2719 static enum ofperr
2720 decode_stack_action(const struct nx_action_stack *nasp,
2721 struct ofpact_stack *stack_action)
2722 {
2723 enum ofperr error;
2724 struct ofpbuf b;
2725
2726 stack_action->subfield.ofs = ntohs(nasp->offset);
2727
2728 ofpbuf_use_const(&b, nasp, sizeof *nasp);
2729 ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
2730 error = nx_pull_header(&b, &stack_action->subfield.field, NULL);
2731 if (error) {
2732 return error;
2733 }
2734 stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
2735 ofpbuf_pull(&b, 2);
2736 if (!is_all_zeros(b.data, b.size)) {
2737 return OFPERR_NXBRC_MUST_BE_ZERO;
2738 }
2739
2740 return 0;
2741 }
2742
2743 static enum ofperr
2744 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
2745 enum ofp_version ofp_version OVS_UNUSED,
2746 struct ofpbuf *ofpacts)
2747 {
2748 struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
2749 enum ofperr error = decode_stack_action(nasp, push);
2750 return error ? error : nxm_stack_push_check(push, NULL);
2751 }
2752
2753 static enum ofperr
2754 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
2755 enum ofp_version ofp_version OVS_UNUSED,
2756 struct ofpbuf *ofpacts)
2757 {
2758 struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
2759 enum ofperr error = decode_stack_action(nasp, pop);
2760 return error ? error : nxm_stack_pop_check(pop, NULL);
2761 }
2762
2763 static void
2764 encode_STACK_op(const struct ofpact_stack *stack_action,
2765 struct nx_action_stack *nasp)
2766 {
2767 struct ofpbuf b;
2768 ovs_be16 n_bits;
2769
2770 nasp->offset = htons(stack_action->subfield.ofs);
2771
2772 ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
2773 ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
2774 nx_put_header(&b, stack_action->subfield.field->id, 0, false);
2775 n_bits = htons(stack_action->subfield.n_bits);
2776 ofpbuf_put(&b, &n_bits, sizeof n_bits);
2777 }
2778
2779 static void
2780 encode_STACK_PUSH(const struct ofpact_stack *stack,
2781 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2782 {
2783 encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
2784 }
2785
2786 static void
2787 encode_STACK_POP(const struct ofpact_stack *stack,
2788 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2789 {
2790 encode_STACK_op(stack, put_NXAST_STACK_POP(out));
2791 }
2792
2793 static char * OVS_WARN_UNUSED_RESULT
2794 parse_STACK_PUSH(char *arg, struct ofpbuf *ofpacts,
2795 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2796 {
2797 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
2798 }
2799
2800 static char * OVS_WARN_UNUSED_RESULT
2801 parse_STACK_POP(char *arg, struct ofpbuf *ofpacts,
2802 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2803 {
2804 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
2805 }
2806
2807 static void
2808 format_STACK_PUSH(const struct ofpact_stack *a, struct ds *s)
2809 {
2810 nxm_format_stack_push(a, s);
2811 }
2812
2813 static void
2814 format_STACK_POP(const struct ofpact_stack *a, struct ds *s)
2815 {
2816 nxm_format_stack_pop(a, s);
2817 }
2818 \f
2819 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
2820 *
2821 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
2822 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
2823 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
2824 * 'n_controllers' controller IDs specified in 'cnt_ids'.
2825 *
2826 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
2827 * sent only to controllers with id 0.)
2828 */
2829 struct nx_action_cnt_ids {
2830 ovs_be16 type; /* OFPAT_VENDOR. */
2831 ovs_be16 len; /* Length including slaves. */
2832 ovs_be32 vendor; /* NX_VENDOR_ID. */
2833 ovs_be16 subtype; /* NXAST_DEC_TTL_CNT_IDS. */
2834
2835 ovs_be16 n_controllers; /* Number of controllers. */
2836 uint8_t zeros[4]; /* Must be zero. */
2837
2838 /* Followed by 1 or more controller ids.
2839 *
2840 * uint16_t cnt_ids[]; // Controller ids.
2841 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
2842 */
2843 };
2844 OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
2845
2846 static enum ofperr
2847 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
2848 {
2849 uint16_t id = 0;
2850 struct ofpact_cnt_ids *ids;
2851 enum ofperr error = 0;
2852
2853 ids = ofpact_put_DEC_TTL(out);
2854 ids->n_controllers = 1;
2855 ofpbuf_put(out, &id, sizeof id);
2856 ids = out->header;
2857 ofpact_update_len(out, &ids->ofpact);
2858 return error;
2859 }
2860
2861 static enum ofperr
2862 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
2863 enum ofp_version ofp_version OVS_UNUSED,
2864 struct ofpbuf *out)
2865 {
2866 struct ofpact_cnt_ids *ids;
2867 size_t ids_size;
2868 int i;
2869
2870 ids = ofpact_put_DEC_TTL(out);
2871 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2872 ids->n_controllers = ntohs(nac_ids->n_controllers);
2873 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
2874
2875 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
2876 return OFPERR_NXBRC_MUST_BE_ZERO;
2877 }
2878
2879 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
2880 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
2881 "bytes allocated for controller ids. %"PRIuSIZE" bytes "
2882 "are required for %"PRIu16" controllers.",
2883 ids_size, ids->n_controllers * sizeof(ovs_be16),
2884 ids->n_controllers);
2885 return OFPERR_OFPBAC_BAD_LEN;
2886 }
2887
2888 for (i = 0; i < ids->n_controllers; i++) {
2889 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
2890 ofpbuf_put(out, &id, sizeof id);
2891 ids = out->header;
2892 }
2893
2894 ofpact_update_len(out, &ids->ofpact);
2895
2896 return 0;
2897 }
2898
2899 static void
2900 encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
2901 enum ofp_version ofp_version, struct ofpbuf *out)
2902 {
2903 if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
2904 || dec_ttl->n_controllers != 1
2905 || dec_ttl->cnt_ids[0] != 0) {
2906 struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
2907 int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
2908 ovs_be16 *ids;
2909 size_t i;
2910
2911 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2912 nac_ids->n_controllers = htons(dec_ttl->n_controllers);
2913
2914 ids = ofpbuf_put_zeros(out, ids_len);
2915 for (i = 0; i < dec_ttl->n_controllers; i++) {
2916 ids[i] = htons(dec_ttl->cnt_ids[i]);
2917 }
2918 } else {
2919 put_OFPAT_DEC_NW_TTL(out, ofp_version);
2920 }
2921 }
2922
2923 static void
2924 parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
2925 {
2926 struct ofpact_cnt_ids *ids;
2927 uint16_t id = 0;
2928
2929 ofpact_put_DEC_TTL(ofpacts);
2930 ofpbuf_put(ofpacts, &id, sizeof id);
2931 ids = ofpacts->header;
2932 ids->n_controllers++;
2933 ofpact_update_len(ofpacts, &ids->ofpact);
2934 }
2935
2936 static char * OVS_WARN_UNUSED_RESULT
2937 parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
2938 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2939 {
2940 if (*arg == '\0') {
2941 parse_noargs_dec_ttl(ofpacts);
2942 } else {
2943 struct ofpact_cnt_ids *ids;
2944 char *cntr;
2945
2946 ids = ofpact_put_DEC_TTL(ofpacts);
2947 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2948 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
2949 cntr = strtok_r(NULL, ", ", &arg)) {
2950 uint16_t id = atoi(cntr);
2951
2952 ofpbuf_put(ofpacts, &id, sizeof id);
2953 ids = ofpacts->header;
2954 ids->n_controllers++;
2955 }
2956 if (!ids->n_controllers) {
2957 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
2958 "id.");
2959 }
2960 ofpact_update_len(ofpacts, &ids->ofpact);
2961 }
2962 return NULL;
2963 }
2964
2965 static void
2966 format_DEC_TTL(const struct ofpact_cnt_ids *a, struct ds *s)
2967 {
2968 size_t i;
2969
2970 ds_put_cstr(s, "dec_ttl");
2971 if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
2972 ds_put_cstr(s, "(");
2973 for (i = 0; i < a->n_controllers; i++) {
2974 if (i) {
2975 ds_put_cstr(s, ",");
2976 }
2977 ds_put_format(s, "%"PRIu16, a->cnt_ids[i]);
2978 }
2979 ds_put_cstr(s, ")");
2980 }
2981 }
2982 \f
2983 /* Set MPLS label actions. */
2984
2985 static enum ofperr
2986 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label,
2987 enum ofp_version ofp_version OVS_UNUSED,
2988 struct ofpbuf *out)
2989 {
2990 ofpact_put_SET_MPLS_LABEL(out)->label = label;
2991 return 0;
2992 }
2993
2994 static void
2995 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
2996 enum ofp_version ofp_version,
2997 struct ofpbuf *out)
2998 {
2999 if (ofp_version < OFP12_VERSION) {
3000 put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
3001 } else {
3002 ofpact_put_set_field(out, ofp_version, MFF_MPLS_LABEL,
3003 ntohl(label->label));
3004 }
3005 }
3006
3007 static char * OVS_WARN_UNUSED_RESULT
3008 parse_SET_MPLS_LABEL(char *arg, struct ofpbuf *ofpacts,
3009 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3010 {
3011 struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(ofpacts);
3012 if (*arg == '\0') {
3013 return xstrdup("set_mpls_label: expected label.");
3014 }
3015
3016 mpls_label->label = htonl(atoi(arg));
3017 return NULL;
3018 }
3019
3020 static void
3021 format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a, struct ds *s)
3022 {
3023 ds_put_format(s, "set_mpls_label(%"PRIu32")", ntohl(a->label));
3024 }
3025 \f
3026 /* Set MPLS TC actions. */
3027
3028 static enum ofperr
3029 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc,
3030 enum ofp_version ofp_version OVS_UNUSED,
3031 struct ofpbuf *out)
3032 {
3033 ofpact_put_SET_MPLS_TC(out)->tc = tc;
3034 return 0;
3035 }
3036
3037 static void
3038 encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
3039 enum ofp_version ofp_version, struct ofpbuf *out)
3040 {
3041 if (ofp_version < OFP12_VERSION) {
3042 put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
3043 } else {
3044 ofpact_put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
3045 }
3046 }
3047
3048 static char * OVS_WARN_UNUSED_RESULT
3049 parse_SET_MPLS_TC(char *arg, struct ofpbuf *ofpacts,
3050 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3051 {
3052 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(ofpacts);
3053
3054 if (*arg == '\0') {
3055 return xstrdup("set_mpls_tc: expected tc.");
3056 }
3057
3058 mpls_tc->tc = atoi(arg);
3059 return NULL;
3060 }
3061
3062 static void
3063 format_SET_MPLS_TC(const struct ofpact_mpls_tc *a, struct ds *s)
3064 {
3065 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->tc);
3066 }
3067 \f
3068 /* Set MPLS TTL actions. */
3069
3070 static enum ofperr
3071 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl,
3072 enum ofp_version ofp_version OVS_UNUSED,
3073 struct ofpbuf *out)
3074 {
3075 ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
3076 return 0;
3077 }
3078
3079 static void
3080 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
3081 enum ofp_version ofp_version, struct ofpbuf *out)
3082 {
3083 put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
3084 }
3085
3086 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3087 * action to 'ofpacts'.
3088 *
3089 * Returns NULL if successful, otherwise a malloc()'d string describing the
3090 * error. The caller is responsible for freeing the returned string. */
3091 static char * OVS_WARN_UNUSED_RESULT
3092 parse_SET_MPLS_TTL(char *arg, struct ofpbuf *ofpacts,
3093 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3094 {
3095 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(ofpacts);
3096
3097 if (*arg == '\0') {
3098 return xstrdup("set_mpls_ttl: expected ttl.");
3099 }
3100
3101 mpls_ttl->ttl = atoi(arg);
3102 return NULL;
3103 }
3104
3105 static void
3106 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a, struct ds *s)
3107 {
3108 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->ttl);
3109 }
3110 \f
3111 /* Decrement MPLS TTL actions. */
3112
3113 static enum ofperr
3114 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
3115 {
3116 ofpact_put_DEC_MPLS_TTL(out);
3117 return 0;
3118 }
3119
3120 static void
3121 encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
3122 enum ofp_version ofp_version, struct ofpbuf *out)
3123 {
3124 put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
3125 }
3126
3127 static char * OVS_WARN_UNUSED_RESULT
3128 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3129 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3130 {
3131 ofpact_put_DEC_MPLS_TTL(ofpacts);
3132 return NULL;
3133 }
3134
3135 static void
3136 format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3137 {
3138 ds_put_cstr(s, "dec_mpls_ttl");
3139 }
3140 \f
3141 /* Push MPLS label action. */
3142
3143 static enum ofperr
3144 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype,
3145 enum ofp_version ofp_version OVS_UNUSED,
3146 struct ofpbuf *out)
3147 {
3148 struct ofpact_push_mpls *oam;
3149
3150 if (!eth_type_mpls(ethertype)) {
3151 return OFPERR_OFPBAC_BAD_ARGUMENT;
3152 }
3153 oam = ofpact_put_PUSH_MPLS(out);
3154 oam->ethertype = ethertype;
3155
3156 return 0;
3157 }
3158
3159 static void
3160 encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
3161 enum ofp_version ofp_version, struct ofpbuf *out)
3162 {
3163 put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
3164 }
3165
3166 static char * OVS_WARN_UNUSED_RESULT
3167 parse_PUSH_MPLS(char *arg, struct ofpbuf *ofpacts,
3168 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3169 {
3170 uint16_t ethertype;
3171 char *error;
3172
3173 error = str_to_u16(arg, "push_mpls", &ethertype);
3174 if (!error) {
3175 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
3176 }
3177 return error;
3178 }
3179
3180 static void
3181 format_PUSH_MPLS(const struct ofpact_push_mpls *a, struct ds *s)
3182 {
3183 ds_put_format(s, "push_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3184 }
3185 \f
3186 /* Pop MPLS label action. */
3187
3188 static enum ofperr
3189 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype,
3190 enum ofp_version ofp_version OVS_UNUSED,
3191 struct ofpbuf *out)
3192 {
3193 ofpact_put_POP_MPLS(out)->ethertype = ethertype;
3194 return 0;
3195 }
3196
3197 static void
3198 encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
3199 enum ofp_version ofp_version, struct ofpbuf *out)
3200 {
3201 put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
3202 }
3203
3204 static char * OVS_WARN_UNUSED_RESULT
3205 parse_POP_MPLS(char *arg, struct ofpbuf *ofpacts,
3206 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3207 {
3208 uint16_t ethertype;
3209 char *error;
3210
3211 error = str_to_u16(arg, "pop_mpls", &ethertype);
3212 if (!error) {
3213 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
3214 }
3215 return error;
3216 }
3217
3218 static void
3219 format_POP_MPLS(const struct ofpact_pop_mpls *a, struct ds *s)
3220 {
3221 ds_put_format(s, "pop_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3222 }
3223 \f
3224 /* Set tunnel ID actions. */
3225
3226 static enum ofperr
3227 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id,
3228 enum ofp_version ofp_version OVS_UNUSED,
3229 struct ofpbuf *out)
3230 {
3231 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3232 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
3233 tunnel->tun_id = tun_id;
3234 return 0;
3235 }
3236
3237 static enum ofperr
3238 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id,
3239 enum ofp_version ofp_version OVS_UNUSED,
3240 struct ofpbuf *out)
3241 {
3242 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3243 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
3244 tunnel->tun_id = tun_id;
3245 return 0;
3246 }
3247
3248 static void
3249 encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
3250 enum ofp_version ofp_version, struct ofpbuf *out)
3251 {
3252 uint64_t tun_id = tunnel->tun_id;
3253
3254 if (ofp_version < OFP12_VERSION) {
3255 if (tun_id <= UINT32_MAX
3256 && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
3257 put_NXAST_SET_TUNNEL(out, tun_id);
3258 } else {
3259 put_NXAST_SET_TUNNEL64(out, tun_id);
3260 }
3261 } else {
3262 ofpact_put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
3263 }
3264 }
3265
3266 static char * OVS_WARN_UNUSED_RESULT
3267 parse_set_tunnel(char *arg, struct ofpbuf *ofpacts,
3268 enum ofp_raw_action_type raw)
3269 {
3270 struct ofpact_tunnel *tunnel;
3271
3272 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
3273 tunnel->ofpact.raw = raw;
3274 return str_to_u64(arg, &tunnel->tun_id);
3275 }
3276
3277 static char * OVS_WARN_UNUSED_RESULT
3278 parse_SET_TUNNEL(char *arg, struct ofpbuf *ofpacts,
3279 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3280 {
3281 return parse_set_tunnel(arg, ofpacts, NXAST_RAW_SET_TUNNEL);
3282 }
3283
3284 static void
3285 format_SET_TUNNEL(const struct ofpact_tunnel *a, struct ds *s)
3286 {
3287 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3288 (a->tun_id > UINT32_MAX
3289 || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
3290 a->tun_id);
3291 }
3292 \f
3293 /* Set queue action. */
3294
3295 static enum ofperr
3296 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id,
3297 enum ofp_version ofp_version OVS_UNUSED,
3298 struct ofpbuf *out)
3299 {
3300 ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
3301 return 0;
3302 }
3303
3304 static void
3305 encode_SET_QUEUE(const struct ofpact_queue *queue,
3306 enum ofp_version ofp_version, struct ofpbuf *out)
3307 {
3308 put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
3309 }
3310
3311 static char * OVS_WARN_UNUSED_RESULT
3312 parse_SET_QUEUE(char *arg, struct ofpbuf *ofpacts,
3313 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3314 {
3315 return str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
3316 }
3317
3318 static void
3319 format_SET_QUEUE(const struct ofpact_queue *a, struct ds *s)
3320 {
3321 ds_put_format(s, "set_queue:%"PRIu32, a->queue_id);
3322 }
3323 \f
3324 /* Pop queue action. */
3325
3326 static enum ofperr
3327 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
3328 {
3329 ofpact_put_POP_QUEUE(out);
3330 return 0;
3331 }
3332
3333 static void
3334 encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
3335 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3336 {
3337 put_NXAST_POP_QUEUE(out);
3338 }
3339
3340 static char * OVS_WARN_UNUSED_RESULT
3341 parse_POP_QUEUE(const char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3342 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3343 {
3344 ofpact_put_POP_QUEUE(ofpacts);
3345 return NULL;
3346 }
3347
3348 static void
3349 format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3350 {
3351 ds_put_cstr(s, "pop_queue");
3352 }
3353 \f
3354 /* Action structure for NXAST_FIN_TIMEOUT.
3355 *
3356 * This action changes the idle timeout or hard timeout, or both, of this
3357 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3358 * When such a packet is observed, the action reduces the rule's idle timeout
3359 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3360 * action has no effect on an existing timeout that is already shorter than the
3361 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3362 * of zero has no effect on the respective timeout.
3363 *
3364 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3365 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3366 * receipt of the FIN or RST.
3367 *
3368 * This is useful for quickly discarding learned TCP flows that otherwise will
3369 * take a long time to expire.
3370 *
3371 * This action is intended for use with an OpenFlow rule that matches only a
3372 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3373 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3374 * RST in any of those flows will cause the entire OpenFlow rule to expire
3375 * early, which is not normally desirable.
3376 */
3377 struct nx_action_fin_timeout {
3378 ovs_be16 type; /* OFPAT_VENDOR. */
3379 ovs_be16 len; /* 16. */
3380 ovs_be32 vendor; /* NX_VENDOR_ID. */
3381 ovs_be16 subtype; /* NXAST_FIN_TIMEOUT. */
3382 ovs_be16 fin_idle_timeout; /* New idle timeout, if nonzero. */
3383 ovs_be16 fin_hard_timeout; /* New hard timeout, if nonzero. */
3384 ovs_be16 pad; /* Must be zero. */
3385 };
3386 OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
3387
3388 static enum ofperr
3389 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
3390 enum ofp_version ofp_version OVS_UNUSED,
3391 struct ofpbuf *out)
3392 {
3393 struct ofpact_fin_timeout *oft;
3394
3395 oft = ofpact_put_FIN_TIMEOUT(out);
3396 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
3397 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
3398 return 0;
3399 }
3400
3401 static void
3402 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
3403 enum ofp_version ofp_version OVS_UNUSED,
3404 struct ofpbuf *out)
3405 {
3406 struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
3407 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
3408 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
3409 }
3410
3411 static char * OVS_WARN_UNUSED_RESULT
3412 parse_FIN_TIMEOUT(char *arg, struct ofpbuf *ofpacts,
3413 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3414 {
3415 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(ofpacts);
3416 char *key, *value;
3417
3418 while (ofputil_parse_key_value(&arg, &key, &value)) {
3419 char *error;
3420
3421 if (!strcmp(key, "idle_timeout")) {
3422 error = str_to_u16(value, key, &oft->fin_idle_timeout);
3423 } else if (!strcmp(key, "hard_timeout")) {
3424 error = str_to_u16(value, key, &oft->fin_hard_timeout);
3425 } else {
3426 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
3427 key);
3428 }
3429
3430 if (error) {
3431 return error;
3432 }
3433 }
3434 return NULL;
3435 }
3436
3437 static void
3438 format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a, struct ds *s)
3439 {
3440 ds_put_cstr(s, "fin_timeout(");
3441 if (a->fin_idle_timeout) {
3442 ds_put_format(s, "idle_timeout=%"PRIu16",", a->fin_idle_timeout);
3443 }
3444 if (a->fin_hard_timeout) {
3445 ds_put_format(s, "hard_timeout=%"PRIu16",", a->fin_hard_timeout);
3446 }
3447 ds_chomp(s, ',');
3448 ds_put_char(s, ')');
3449 }
3450 \f
3451 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3452 *
3453 * These actions search one of the switch's flow tables:
3454 *
3455 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3456 * it specifies the table to search.
3457 *
3458 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3459 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3460 * table, that is, the OpenFlow flow table that contains the flow from
3461 * which this action was obtained. If this action did not come from a
3462 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3463 * is the current table.
3464 *
3465 * The flow table lookup uses a flow that may be slightly modified from the
3466 * original lookup:
3467 *
3468 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3469 * is used as the flow's in_port.
3470 *
3471 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3472 * then its value is used as the flow's in_port. Otherwise, the original
3473 * in_port is used.
3474 *
3475 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3476 * resubmit action, then the flow is updated with the new values.
3477 *
3478 * Following the lookup, the original in_port is restored.
3479 *
3480 * If the modified flow matched in the flow table, then the corresponding
3481 * actions are executed. Afterward, actions following the resubmit in the
3482 * original set of actions, if any, are executed; any changes made to the
3483 * packet (e.g. changes to VLAN) by secondary actions persist when those
3484 * actions are executed, although the original in_port is restored.
3485 *
3486 * Resubmit actions may be used any number of times within a set of actions.
3487 *
3488 * Resubmit actions may nest to an implementation-defined depth. Beyond this
3489 * implementation-defined depth, further resubmit actions are simply ignored.
3490 *
3491 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3492 * 'pad' to be all-bits-zero.
3493 *
3494 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3495 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3496 */
3497 struct nx_action_resubmit {
3498 ovs_be16 type; /* OFPAT_VENDOR. */
3499 ovs_be16 len; /* Length is 16. */
3500 ovs_be32 vendor; /* NX_VENDOR_ID. */
3501 ovs_be16 subtype; /* NXAST_RESUBMIT. */
3502 ovs_be16 in_port; /* New in_port for checking flow table. */
3503 uint8_t table; /* NXAST_RESUBMIT_TABLE: table to use. */
3504 uint8_t pad[3];
3505 };
3506 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
3507
3508 static enum ofperr
3509 decode_NXAST_RAW_RESUBMIT(uint16_t port,
3510 enum ofp_version ofp_version OVS_UNUSED,
3511 struct ofpbuf *out)
3512 {
3513 struct ofpact_resubmit *resubmit;
3514
3515 resubmit = ofpact_put_RESUBMIT(out);
3516 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
3517 resubmit->in_port = u16_to_ofp(port);
3518 resubmit->table_id = 0xff;
3519 return 0;
3520 }
3521
3522 static enum ofperr
3523 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
3524 enum ofp_version ofp_version OVS_UNUSED,
3525 struct ofpbuf *out)
3526 {
3527 struct ofpact_resubmit *resubmit;
3528
3529 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3530 return OFPERR_OFPBAC_BAD_ARGUMENT;
3531 }
3532
3533 resubmit = ofpact_put_RESUBMIT(out);
3534 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
3535 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
3536 resubmit->table_id = nar->table;
3537 return 0;
3538 }
3539
3540 static void
3541 encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
3542 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3543 {
3544 uint16_t in_port = ofp_to_u16(resubmit->in_port);
3545
3546 if (resubmit->table_id == 0xff
3547 && resubmit->ofpact.raw != NXAST_RAW_RESUBMIT_TABLE) {
3548 put_NXAST_RESUBMIT(out, in_port);
3549 } else {
3550 struct nx_action_resubmit *nar = put_NXAST_RESUBMIT_TABLE(out);
3551 nar->table = resubmit->table_id;
3552 nar->in_port = htons(in_port);
3553 }
3554 }
3555
3556 static char * OVS_WARN_UNUSED_RESULT
3557 parse_RESUBMIT(char *arg, struct ofpbuf *ofpacts,
3558 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3559 {
3560 struct ofpact_resubmit *resubmit;
3561 char *in_port_s, *table_s;
3562
3563 resubmit = ofpact_put_RESUBMIT(ofpacts);
3564
3565 in_port_s = strsep(&arg, ",");
3566 if (in_port_s && in_port_s[0]) {
3567 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
3568 return xasprintf("%s: resubmit to unknown port", in_port_s);
3569 }
3570 } else {
3571 resubmit->in_port = OFPP_IN_PORT;
3572 }
3573
3574 table_s = strsep(&arg, ",");
3575 if (table_s && table_s[0]) {
3576 uint32_t table_id = 0;
3577 char *error;
3578
3579 error = str_to_u32(table_s, &table_id);
3580 if (error) {
3581 return error;
3582 }
3583 resubmit->table_id = table_id;
3584 } else {
3585 resubmit->table_id = 255;
3586 }
3587
3588 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
3589 return xstrdup("at least one \"in_port\" or \"table\" must be "
3590 "specified on resubmit");
3591 }
3592 return NULL;
3593 }
3594
3595 static void
3596 format_RESUBMIT(const struct ofpact_resubmit *a, struct ds *s)
3597 {
3598 if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
3599 ds_put_cstr(s, "resubmit:");
3600 ofputil_format_port(a->in_port, s);
3601 } else {
3602 ds_put_format(s, "resubmit(");
3603 if (a->in_port != OFPP_IN_PORT) {
3604 ofputil_format_port(a->in_port, s);
3605 }
3606 ds_put_char(s, ',');
3607 if (a->table_id != 255) {
3608 ds_put_format(s, "%"PRIu8, a->table_id);
3609 }
3610 ds_put_char(s, ')');
3611 }
3612 }
3613 \f
3614 /* Action structure for NXAST_LEARN.
3615 *
3616 * This action adds or modifies a flow in an OpenFlow table, similar to
3617 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3618 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3619 * flow's match criteria and actions are built by applying each of the series
3620 * of flow_mod_spec elements included as part of the action.
3621 *
3622 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3623 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3624 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3625 * bits from a source to a destination. In this case, the header contains
3626 * multiple fields:
3627 *
3628 * 15 14 13 12 11 10 0
3629 * +------+---+------+---------------------------------+
3630 * | 0 |src| dst | n_bits |
3631 * +------+---+------+---------------------------------+
3632 *
3633 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3634 * following table summarizes the meaning of each possible combination.
3635 * Details follow the table:
3636 *
3637 * src dst meaning
3638 * --- --- ----------------------------------------------------------
3639 * 0 0 Add match criteria based on value in a field.
3640 * 1 0 Add match criteria based on an immediate value.
3641 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3642 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3643 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3644 * All other combinations are undefined and not allowed.
3645 *
3646 * The flow_mod_spec header is followed by a source specification and a
3647 * destination specification. The format and meaning of the source
3648 * specification depends on 'src':
3649 *
3650 * - If 'src' is 0, the source bits are taken from a field in the flow to
3651 * which this action is attached. (This should be a wildcarded field. If
3652 * its value is fully specified then the source bits being copied have
3653 * constant values.)
3654 *
3655 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3656 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3657 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3658 * 'field' and 'ofs' are subject to the same restrictions as the source
3659 * field in NXAST_REG_MOVE.
3660 *
3661 * - If 'src' is 1, the source bits are a constant value. The source
3662 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3663 * number in network order, the source bits are the 'n_bits'
3664 * least-significant bits. The switch will report an error if other bits
3665 * in the constant are nonzero.
3666 *
3667 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3668 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3669 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3670 * meaning of the flow_mod_spec depends on 'dst':
3671 *
3672 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3673 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3674 * packet equal the source bits. 'field' may be any nxm_header with
3675 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3676 *
3677 * Order is significant. Earlier flow_mod_specs must satisfy any
3678 * prerequisites for matching fields specified later, by copying constant
3679 * values into prerequisite fields.
3680 *
3681 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3682 * restrictions.
3683 *
3684 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3685 * the new flow. The new flow copies the source bits into
3686 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3687 * flow_mod_specs.
3688 *
3689 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3690 * greater than 64 yields multiple NXAST_REG_LOAD actions.
3691 *
3692 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3693 * empty. It has the following meaning:
3694 *
3695 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3696 * The new flow outputs to the OpenFlow port specified by the source field.
3697 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3698 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
3699 * may not be used.
3700 *
3701 * Resource Management
3702 * -------------------
3703 *
3704 * A switch has a finite amount of flow table space available for learning.
3705 * When this space is exhausted, no new learning table entries will be learned
3706 * until some existing flow table entries expire. The controller should be
3707 * prepared to handle this by flooding (which can be implemented as a
3708 * low-priority flow).
3709 *
3710 * If a learned flow matches a single TCP stream with a relatively long
3711 * timeout, one may make the best of resource constraints by setting
3712 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3713 * both, to shorter timeouts. When either of these is specified as a nonzero
3714 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3715 * the learned flow.
3716 *
3717 * Examples
3718 * --------
3719 *
3720 * The following examples give a prose description of the flow_mod_specs along
3721 * with informal notation for how those would be represented and a hex dump of
3722 * the bytes that would be required.
3723 *
3724 * These examples could work with various nx_action_learn parameters. Typical
3725 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3726 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3727 *
3728 * 1. Learn input port based on the source MAC, with lookup into
3729 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
3730 *
3731 * Match on in_port=99:
3732 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
3733 * ovs_be16(99), 00 63
3734 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3735 *
3736 * Match Ethernet destination on Ethernet source from packet:
3737 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3738 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3739 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3740 *
3741 * Set NXM_NX_REG1[16:31] to the packet's input port:
3742 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
3743 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3744 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
3745 *
3746 * Given a packet that arrived on port A with Ethernet source address B,
3747 * this would set up the flow "in_port=99, dl_dst=B,
3748 * actions=load:A->NXM_NX_REG1[16..31]".
3749 *
3750 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
3751 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3752 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
3753 *
3754 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
3755 * into NXM_NX_REG1[16:31]:
3756 *
3757 * Match on same VLAN ID as packet:
3758 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
3759 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3760 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3761 *
3762 * Match Ethernet destination on Ethernet source from packet:
3763 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3764 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3765 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3766 *
3767 * Output to the packet's input port:
3768 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
3769 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3770 *
3771 * Given a packet that arrived on port A with Ethernet source address B in
3772 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
3773 * actions=output:A".
3774 *
3775 * In syntax accepted by ovs-ofctl, this action is:
3776 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3777 * output:NXM_OF_IN_PORT[])
3778 *
3779 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
3780 * 10-second MAC expiration time to make it easier to see what's going on
3781 *
3782 * ovs-vsctl del-controller br0
3783 * ovs-ofctl del-flows br0
3784 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
3785 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
3786 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
3787 output:NXM_OF_IN_PORT[]), resubmit(,1)"
3788 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
3789 *
3790 * You can then dump the MAC learning table with:
3791 *
3792 * ovs-ofctl dump-flows br0 table=1
3793 *
3794 * Usage Advice
3795 * ------------
3796 *
3797 * For best performance, segregate learned flows into a table that is not used
3798 * for any other flows except possibly for a lowest-priority "catch-all" flow
3799 * (a flow with no match criteria). If different learning actions specify
3800 * different match criteria, use different tables for the learned flows.
3801 *
3802 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
3803 * These timeouts apply to the flow that is added, which means that a flow with
3804 * an idle timeout will expire when no traffic has been sent *to* the learned
3805 * address. This is not usually the intent in MAC learning; instead, we want
3806 * the MAC learn entry to expire when no traffic has been sent *from* the
3807 * learned address. Use a hard timeout for that.
3808 *
3809 *
3810 * Visibility of Changes
3811 * ---------------------
3812 *
3813 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
3814 * flow translation are visible to flow table lookups made later in the flow
3815 * translation. This means that, in the example above, a MAC learned by the
3816 * learn action in table 0 would be found in table 1 (if the packet being
3817 * processed had the same source and destination MAC address).
3818 *
3819 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
3820 * modify a flow) by a "learn" action are visible only for later flow
3821 * translations, not for later lookups within the same flow translation. In
3822 * the MAC learning example, a MAC learned by the learn action in table 0 would
3823 * not be found in table 1 if the flow translation would resubmit to table 1
3824 * after the processing of the learn action, meaning that if this MAC had not
3825 * been learned before then the packet would be flooded. */
3826 struct nx_action_learn {
3827 ovs_be16 type; /* OFPAT_VENDOR. */
3828 ovs_be16 len; /* At least 24. */
3829 ovs_be32 vendor; /* NX_VENDOR_ID. */
3830 ovs_be16 subtype; /* NXAST_LEARN. */
3831 ovs_be16 idle_timeout; /* Idle time before discarding (seconds). */
3832 ovs_be16 hard_timeout; /* Max time before discarding (seconds). */
3833 ovs_be16 priority; /* Priority level of flow entry. */
3834 ovs_be64 cookie; /* Cookie for new flow. */
3835 ovs_be16 flags; /* NX_LEARN_F_*. */
3836 uint8_t table_id; /* Table to insert flow entry. */
3837 uint8_t pad; /* Must be zero. */
3838 ovs_be16 fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
3839 ovs_be16 fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
3840 /* Followed by a sequence of flow_mod_spec elements, as described above,
3841 * until the end of the action is reached. */
3842 };
3843 OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
3844
3845 static ovs_be16
3846 get_be16(const void **pp)
3847 {
3848 const ovs_be16 *p = *pp;
3849 ovs_be16 value = *p;
3850 *pp = p + 1;
3851 return value;
3852 }
3853
3854 static ovs_be32
3855 get_be32(const void **pp)
3856 {
3857 const ovs_be32 *p = *pp;
3858 ovs_be32 value = get_unaligned_be32(p);
3859 *pp = p + 1;
3860 return value;
3861 }
3862
3863 static void
3864 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
3865 {
3866 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
3867 sf->ofs = ntohs(get_be16(p));
3868 sf->n_bits = n_bits;
3869 }
3870
3871 static unsigned int
3872 learn_min_len(uint16_t header)
3873 {
3874 int n_bits = header & NX_LEARN_N_BITS_MASK;
3875 int src_type = header & NX_LEARN_SRC_MASK;
3876 int dst_type = header & NX_LEARN_DST_MASK;
3877 unsigned int min_len;
3878
3879 min_len = 0;
3880 if (src_type == NX_LEARN_SRC_FIELD) {
3881 min_len += sizeof(ovs_be32); /* src_field */
3882 min_len += sizeof(ovs_be16); /* src_ofs */
3883 } else {
3884 min_len += DIV_ROUND_UP(n_bits, 16);
3885 }
3886 if (dst_type == NX_LEARN_DST_MATCH ||
3887 dst_type == NX_LEARN_DST_LOAD) {
3888 min_len += sizeof(ovs_be32); /* dst_field */
3889 min_len += sizeof(ovs_be16); /* dst_ofs */
3890 }
3891 return min_len;
3892 }
3893
3894 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
3895 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
3896 static enum ofperr
3897 decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
3898 enum ofp_version ofp_version OVS_UNUSED,
3899 struct ofpbuf *ofpacts)
3900 {
3901 struct ofpact_learn *learn;
3902 const void *p, *end;
3903
3904 if (nal->pad) {
3905 return OFPERR_OFPBAC_BAD_ARGUMENT;
3906 }
3907
3908 learn = ofpact_put_LEARN(ofpacts);
3909
3910 learn->idle_timeout = ntohs(nal->idle_timeout);
3911 learn->hard_timeout = ntohs(nal->hard_timeout);
3912 learn->priority = ntohs(nal->priority);
3913 learn->cookie = nal->cookie;
3914 learn->table_id = nal->table_id;
3915 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
3916 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
3917
3918 learn->flags = ntohs(nal->flags);
3919 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
3920 NX_LEARN_F_DELETE_LEARNED)) {
3921 return OFPERR_OFPBAC_BAD_ARGUMENT;
3922 }
3923
3924 if (learn->table_id == 0xff) {
3925 return OFPERR_OFPBAC_BAD_ARGUMENT;
3926 }
3927
3928 end = (char *) nal + ntohs(nal->len);
3929 for (p = nal + 1; p != end; ) {
3930 struct ofpact_learn_spec *spec;
3931 uint16_t header = ntohs(get_be16(&p));
3932
3933 if (!header) {
3934 break;
3935 }
3936
3937 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
3938 learn = ofpacts->header;
3939 learn->n_specs++;
3940
3941 spec->src_type = header & NX_LEARN_SRC_MASK;
3942 spec->dst_type = header & NX_LEARN_DST_MASK;
3943 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
3944
3945 /* Check for valid src and dst type combination. */
3946 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3947 spec->dst_type == NX_LEARN_DST_LOAD ||
3948 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
3949 spec->src_type == NX_LEARN_SRC_FIELD)) {
3950 /* OK. */
3951 } else {
3952 return OFPERR_OFPBAC_BAD_ARGUMENT;
3953 }
3954
3955 /* Check that the arguments don't overrun the end of the action. */
3956 if ((char *) end - (char *) p < learn_min_len(header)) {
3957 return OFPERR_OFPBAC_BAD_LEN;
3958 }
3959
3960 /* Get the source. */
3961 if (spec->src_type == NX_LEARN_SRC_FIELD) {
3962 get_subfield(spec->n_bits, &p, &spec->src);
3963 } else {
3964 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
3965
3966 bitwise_copy(p, p_bytes, 0,
3967 &spec->src_imm, sizeof spec->src_imm, 0,
3968 spec->n_bits);
3969 p = (const uint8_t *) p + p_bytes;
3970 }
3971
3972 /* Get the destination. */
3973 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3974 spec->dst_type == NX_LEARN_DST_LOAD) {
3975 get_subfield(spec->n_bits, &p, &spec->dst);
3976 }
3977 }
3978 ofpact_update_len(ofpacts, &learn->ofpact);
3979
3980 if (!is_all_zeros(p, (char *) end - (char *) p)) {
3981 return OFPERR_OFPBAC_BAD_ARGUMENT;
3982 }
3983
3984 return 0;
3985 }
3986
3987 static void
3988 put_be16(struct ofpbuf *b, ovs_be16 x)
3989 {
3990 ofpbuf_put(b, &x, sizeof x);
3991 }
3992
3993 static void
3994 put_be32(struct ofpbuf *b, ovs_be32 x)
3995 {
3996 ofpbuf_put(b, &x, sizeof x);
3997 }
3998
3999 static void
4000 put_u16(struct ofpbuf *b, uint16_t x)
4001 {
4002 put_be16(b, htons(x));
4003 }
4004
4005 static void
4006 put_u32(struct ofpbuf *b, uint32_t x)
4007 {
4008 put_be32(b, htonl(x));
4009 }
4010
4011 static void
4012 encode_LEARN(const struct ofpact_learn *learn,
4013 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4014 {
4015 const struct ofpact_learn_spec *spec;
4016 struct nx_action_learn *nal;
4017 size_t start_ofs;
4018
4019 start_ofs = out->size;
4020 nal = put_NXAST_LEARN(out);
4021 nal->idle_timeout = htons(learn->idle_timeout);
4022 nal->hard_timeout = htons(learn->hard_timeout);
4023 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
4024 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
4025 nal->priority = htons(learn->priority);
4026 nal->cookie = learn->cookie;
4027 nal->flags = htons(learn->flags);
4028 nal->table_id = learn->table_id;
4029
4030 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
4031 put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
4032
4033 if (spec->src_type == NX_LEARN_SRC_FIELD) {
4034 put_u32(out, mf_nxm_header(spec->src.field->id));
4035 put_u16(out, spec->src.ofs);
4036 } else {
4037 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4038 uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
4039 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
4040 bits, n_dst_bytes, 0,
4041 spec->n_bits);
4042 }
4043
4044 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4045 spec->dst_type == NX_LEARN_DST_LOAD) {
4046 put_u32(out, mf_nxm_header(spec->dst.field->id));
4047 put_u16(out, spec->dst.ofs);
4048 }
4049 }
4050
4051 pad_ofpat(out, start_ofs);
4052 }
4053
4054 static char * OVS_WARN_UNUSED_RESULT
4055 parse_LEARN(char *arg, struct ofpbuf *ofpacts,
4056 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4057 {
4058 return learn_parse(arg, ofpacts);
4059 }
4060
4061 static void
4062 format_LEARN(const struct ofpact_learn *a, struct ds *s)
4063 {
4064 learn_format(a, s);
4065 }
4066 \f
4067 /* Action structure for NXAST_CONJUNCTION. */
4068 struct nx_action_conjunction {
4069 ovs_be16 type; /* OFPAT_VENDOR. */
4070 ovs_be16 len; /* At least 16. */
4071 ovs_be32 vendor; /* NX_VENDOR_ID. */
4072 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
4073 uint8_t clause;
4074 uint8_t n_clauses;
4075 ovs_be32 id;
4076 };
4077 OFP_ASSERT(sizeof(struct nx_action_conjunction) == 16);
4078
4079 static void
4080 add_conjunction(struct ofpbuf *out,
4081 uint32_t id, uint8_t clause, uint8_t n_clauses)
4082 {
4083 struct ofpact_conjunction *oc;
4084
4085 oc = ofpact_put_CONJUNCTION(out);
4086 oc->id = id;
4087 oc->clause = clause;
4088 oc->n_clauses = n_clauses;
4089 }
4090
4091 static enum ofperr
4092 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction *nac,
4093 enum ofp_version ofp_version OVS_UNUSED,
4094 struct ofpbuf *out)
4095 {
4096 if (nac->n_clauses < 2 || nac->n_clauses > 64
4097 || nac->clause >= nac->n_clauses) {
4098 return OFPERR_NXBAC_BAD_CONJUNCTION;
4099 } else {
4100 add_conjunction(out, ntohl(nac->id), nac->clause, nac->n_clauses);
4101 return 0;
4102 }
4103 }
4104
4105 static void
4106 encode_CONJUNCTION(const struct ofpact_conjunction *oc,
4107 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4108 {
4109 struct nx_action_conjunction *nac = put_NXAST_CONJUNCTION(out);
4110 nac->clause = oc->clause;
4111 nac->n_clauses = oc->n_clauses;
4112 nac->id = htonl(oc->id);
4113 }
4114
4115 static void
4116 format_CONJUNCTION(const struct ofpact_conjunction *oc, struct ds *s)
4117 {
4118 ds_put_format(s, "conjunction(%"PRIu32",%"PRIu8"/%"PRIu8")",
4119 oc->id, oc->clause + 1, oc->n_clauses);
4120 }
4121
4122 static char * OVS_WARN_UNUSED_RESULT
4123 parse_CONJUNCTION(const char *arg, struct ofpbuf *ofpacts,
4124 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4125 {
4126 uint8_t n_clauses;
4127 uint8_t clause;
4128 uint32_t id;
4129 int n;
4130
4131 if (!ovs_scan(arg, "%"SCNi32" , %"SCNu8" / %"SCNu8" %n",
4132 &id, &clause, &n_clauses, &n) || n != strlen(arg)) {
4133 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
4134 }
4135
4136 if (n_clauses < 2) {
4137 return xstrdup("conjunction must have at least 2 clauses");
4138 } else if (n_clauses > 64) {
4139 return xstrdup("conjunction must have at most 64 clauses");
4140 } else if (clause < 1) {
4141 return xstrdup("clause index must be positive");
4142 } else if (clause > n_clauses) {
4143 return xstrdup("clause index must be less than or equal to "
4144 "number of clauses");
4145 }
4146
4147 add_conjunction(ofpacts, id, clause - 1, n_clauses);
4148 return NULL;
4149 }
4150 \f
4151 /* Action structure for NXAST_MULTIPATH.
4152 *
4153 * This action performs the following steps in sequence:
4154 *
4155 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4156 * Refer to the definition of "enum nx_mp_fields" for details.
4157 *
4158 * The 'basis' value is used as a universal hash parameter, that is,
4159 * different values of 'basis' yield different hash functions. The
4160 * particular universal hash function used is implementation-defined.
4161 *
4162 * The hashed fields' values are drawn from the current state of the
4163 * flow, including all modifications that have been made by actions up to
4164 * this point.
4165 *
4166 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
4167 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
4168 * for details.
4169 *
4170 * The output of the algorithm is 'link', an unsigned integer less than
4171 * or equal to 'max_link'.
4172 *
4173 * Some algorithms use 'arg' as an additional argument.
4174 *
4175 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
4176 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4177 * action.
4178 *
4179 * The switch will reject actions that have an unknown 'fields', or an unknown
4180 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4181 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4182 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4183 */
4184 struct nx_action_multipath {
4185 ovs_be16 type; /* OFPAT_VENDOR. */
4186 ovs_be16 len; /* Length is 32. */
4187 ovs_be32 vendor; /* NX_VENDOR_ID. */
4188 ovs_be16 subtype; /* NXAST_MULTIPATH. */
4189
4190 /* What fields to hash and how. */
4191 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
4192 ovs_be16 basis; /* Universal hash parameter. */
4193 ovs_be16 pad0;
4194
4195 /* Multipath link choice algorithm to apply to hash value. */
4196 ovs_be16 algorithm; /* One of NX_MP_ALG_*. */
4197 ovs_be16 max_link; /* Number of output links, minus 1. */
4198 ovs_be32 arg; /* Algorithm-specific argument. */
4199 ovs_be16 pad1;
4200
4201 /* Where to store the result. */
4202 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
4203 ovs_be32 dst; /* Destination. */
4204 };
4205 OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
4206
4207 static enum ofperr
4208 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
4209 enum ofp_version ofp_version OVS_UNUSED,
4210 struct ofpbuf *out)
4211 {
4212 uint32_t n_links = ntohs(nam->max_link) + 1;
4213 size_t min_n_bits = log_2_ceil(n_links);
4214 struct ofpact_multipath *mp;
4215
4216 mp = ofpact_put_MULTIPATH(out);
4217 mp->fields = ntohs(nam->fields);
4218 mp->basis = ntohs(nam->basis);
4219 mp->algorithm = ntohs(nam->algorithm);
4220 mp->max_link = ntohs(nam->max_link);
4221 mp->arg = ntohl(nam->arg);
4222 mp->dst.field = mf_from_nxm_header(ntohl(nam->dst));
4223 mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
4224 mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
4225
4226 if (!flow_hash_fields_valid(mp->fields)) {
4227 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
4228 return OFPERR_OFPBAC_BAD_ARGUMENT;
4229 } else if (mp->algorithm != NX_MP_ALG_MODULO_N
4230 && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
4231 && mp->algorithm != NX_MP_ALG_HRW
4232 && mp->algorithm != NX_MP_ALG_ITER_HASH) {
4233 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
4234 return OFPERR_OFPBAC_BAD_ARGUMENT;
4235 } else if (mp->dst.n_bits < min_n_bits) {
4236 VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
4237 "%"PRIu32" links", min_n_bits, n_links);
4238 return OFPERR_OFPBAC_BAD_ARGUMENT;
4239 }
4240
4241 return multipath_check(mp, NULL);
4242 }
4243
4244 static void
4245 encode_MULTIPATH(const struct ofpact_multipath *mp,
4246 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4247 {
4248 struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
4249
4250 nam->fields = htons(mp->fields);
4251 nam->basis = htons(mp->basis);
4252 nam->algorithm = htons(mp->algorithm);
4253 nam->max_link = htons(mp->max_link);
4254 nam->arg = htonl(mp->arg);
4255 nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
4256 nam->dst = htonl(mf_nxm_header(mp->dst.field->id));
4257 }
4258
4259 static char * OVS_WARN_UNUSED_RESULT
4260 parse_MULTIPATH(const char *arg, struct ofpbuf *ofpacts,
4261 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4262 {
4263 return multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
4264 }
4265
4266 static void
4267 format_MULTIPATH(const struct ofpact_multipath *a, struct ds *s)
4268 {
4269 multipath_format(a, s);
4270 }
4271 \f
4272 /* Action structure for NXAST_NOTE.
4273 *
4274 * This action has no effect. It is variable length. The switch does not
4275 * attempt to interpret the user-defined 'note' data in any way. A controller
4276 * can use this action to attach arbitrary metadata to a flow.
4277 *
4278 * This action might go away in the future.
4279 */
4280 struct nx_action_note {
4281 ovs_be16 type; /* OFPAT_VENDOR. */
4282 ovs_be16 len; /* A multiple of 8, but at least 16. */
4283 ovs_be32 vendor; /* NX_VENDOR_ID. */
4284 ovs_be16 subtype; /* NXAST_NOTE. */
4285 uint8_t note[6]; /* Start of user-defined data. */
4286 /* Possibly followed by additional user-defined data. */
4287 };
4288 OFP_ASSERT(sizeof(struct nx_action_note) == 16);
4289
4290 static enum ofperr
4291 decode_NXAST_RAW_NOTE(const struct nx_action_note *nan,
4292 enum ofp_version ofp_version OVS_UNUSED,
4293 struct ofpbuf *out)
4294 {
4295 struct ofpact_note *note;
4296 unsigned int length;
4297
4298 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
4299 note = ofpact_put(out, OFPACT_NOTE,
4300 offsetof(struct ofpact_note, data) + length);
4301 note->length = length;
4302 memcpy(note->data, nan->note, length);
4303
4304 return 0;
4305 }
4306
4307 static void
4308 encode_NOTE(const struct ofpact_note *note,
4309 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4310 {
4311 size_t start_ofs = out->size;
4312 struct nx_action_note *nan;
4313 unsigned int remainder;
4314 unsigned int len;
4315
4316 put_NXAST_NOTE(out);
4317 out->size = out->size - sizeof nan->note;
4318
4319 ofpbuf_put(out, note->data, note->length);
4320
4321 len = out->size - start_ofs;
4322 remainder = len % OFP_ACTION_ALIGN;
4323 if (remainder) {
4324 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
4325 }
4326 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
4327 nan->len = htons(out->size - start_ofs);
4328 }
4329
4330 static char * OVS_WARN_UNUSED_RESULT
4331 parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
4332 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4333 {
4334 struct ofpact_note *note;
4335
4336 note = ofpact_put_NOTE(ofpacts);
4337 while (*arg != '\0') {
4338 uint8_t byte;
4339 bool ok;
4340
4341 if (*arg == '.') {
4342 arg++;
4343 }
4344 if (*arg == '\0') {
4345 break;
4346 }
4347
4348 byte = hexits_value(arg, 2, &ok);
4349 if (!ok) {
4350 return xstrdup("bad hex digit in `note' argument");
4351 }
4352 ofpbuf_put(ofpacts, &byte, 1);
4353
4354 note = ofpacts->header;
4355 note->length++;
4356
4357 arg += 2;
4358 }
4359 ofpact_update_len(ofpacts, &note->ofpact);
4360 return NULL;
4361 }
4362
4363 static void
4364 format_NOTE(const struct ofpact_note *a, struct ds *s)
4365 {
4366 size_t i;
4367
4368 ds_put_cstr(s, "note:");
4369 for (i = 0; i < a->length; i++) {
4370 if (i) {
4371 ds_put_char(s, '.');
4372 }
4373 ds_put_format(s, "%02"PRIx8, a->data[i]);
4374 }
4375 }
4376 \f
4377 /* Exit action. */
4378
4379 static enum ofperr
4380 decode_NXAST_RAW_EXIT(struct ofpbuf *out)
4381 {
4382 ofpact_put_EXIT(out);
4383 return 0;
4384 }
4385
4386 static void
4387 encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
4388 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4389 {
4390 put_NXAST_EXIT(out);
4391 }
4392
4393 static char * OVS_WARN_UNUSED_RESULT
4394 parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4395 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4396 {
4397 ofpact_put_EXIT(ofpacts);
4398 return NULL;
4399 }
4400
4401 static void
4402 format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4403 {
4404 ds_put_cstr(s, "exit");
4405 }
4406 \f
4407 /* Unroll xlate action. */
4408
4409 static void
4410 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
4411 enum ofp_version ofp_version OVS_UNUSED,
4412 struct ofpbuf *out OVS_UNUSED)
4413 {
4414 OVS_NOT_REACHED();
4415 }
4416
4417 static char * OVS_WARN_UNUSED_RESULT
4418 parse_UNROLL_XLATE(char *arg OVS_UNUSED, struct ofpbuf *ofpacts OVS_UNUSED,
4419 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4420 {
4421 OVS_NOT_REACHED();
4422 return NULL;
4423 }
4424
4425 static void
4426 format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a OVS_UNUSED,
4427 struct ds *s)
4428 {
4429 ds_put_cstr(s, "unroll_xlate");
4430 }
4431 \f
4432 /* Action structure for NXAST_SAMPLE.
4433 *
4434 * Samples matching packets with the given probability and sends them
4435 * each to the set of collectors identified with the given ID. The
4436 * probability is expressed as a number of packets to be sampled out
4437 * of USHRT_MAX packets, and must be >0.
4438 *
4439 * When sending packet samples to IPFIX collectors, the IPFIX flow
4440 * record sent for each sampled packet is associated with the given
4441 * observation domain ID and observation point ID. Each IPFIX flow
4442 * record contain the sampled packet's headers when executing this
4443 * rule. If a sampled packet's headers are modified by previous
4444 * actions in the flow, those modified headers are sent. */
4445 struct nx_action_sample {
4446 ovs_be16 type; /* OFPAT_VENDOR. */
4447 ovs_be16 len; /* Length is 24. */
4448 ovs_be32 vendor; /* NX_VENDOR_ID. */
4449 ovs_be16 subtype; /* NXAST_SAMPLE. */
4450 ovs_be16 probability; /* Fraction of packets to sample. */
4451 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
4452 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
4453 ovs_be32 obs_point_id; /* ID of sampling observation point. */
4454 };
4455 OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
4456
4457 static enum ofperr
4458 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas,
4459 enum ofp_version ofp_version OVS_UNUSED,
4460 struct ofpbuf *out)
4461 {
4462 struct ofpact_sample *sample;
4463
4464 sample = ofpact_put_SAMPLE(out);
4465 sample->probability = ntohs(nas->probability);
4466 sample->collector_set_id = ntohl(nas->collector_set_id);
4467 sample->obs_domain_id = ntohl(nas->obs_domain_id);
4468 sample->obs_point_id = ntohl(nas->obs_point_id);
4469
4470 if (sample->probability == 0) {
4471 return OFPERR_OFPBAC_BAD_ARGUMENT;
4472 }
4473
4474 return 0;
4475 }
4476
4477 static void
4478 encode_SAMPLE(const struct ofpact_sample *sample,
4479 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4480 {
4481 struct nx_action_sample *nas;
4482
4483 nas = put_NXAST_SAMPLE(out);
4484 nas->probability = htons(sample->probability);
4485 nas->collector_set_id = htonl(sample->collector_set_id);
4486 nas->obs_domain_id = htonl(sample->obs_domain_id);
4487 nas->obs_point_id = htonl(sample->obs_point_id);
4488 }
4489
4490 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4491 * action to 'ofpacts'.
4492 *
4493 * Returns NULL if successful, otherwise a malloc()'d string describing the
4494 * error. The caller is responsible for freeing the returned string. */
4495 static char * OVS_WARN_UNUSED_RESULT
4496 parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
4497 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4498 {
4499 struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
4500 char *key, *value;
4501
4502 while (ofputil_parse_key_value(&arg, &key, &value)) {
4503 char *error = NULL;
4504
4505 if (!strcmp(key, "probability")) {
4506 error = str_to_u16(value, "probability", &os->probability);
4507 if (!error && os->probability == 0) {
4508 error = xasprintf("invalid probability value \"%s\"", value);
4509 }
4510 } else if (!strcmp(key, "collector_set_id")) {
4511 error = str_to_u32(value, &os->collector_set_id);
4512 } else if (!strcmp(key, "obs_domain_id")) {
4513 error = str_to_u32(value, &os->obs_domain_id);
4514 } else if (!strcmp(key, "obs_point_id")) {
4515 error = str_to_u32(value, &os->obs_point_id);
4516 } else {
4517 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4518 key);
4519 }
4520 if (error) {
4521 return error;
4522 }
4523 }
4524 if (os->probability == 0) {
4525 return xstrdup("non-zero \"probability\" must be specified on sample");
4526 }
4527 return NULL;
4528 }
4529
4530 static void
4531 format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4532 {
4533 ds_put_format(s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
4534 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
4535 a->probability, a->collector_set_id,
4536 a->obs_domain_id, a->obs_point_id);
4537 }
4538 \f
4539 /* debug_recirc instruction. */
4540
4541 static bool enable_debug;
4542
4543 void
4544 ofpact_dummy_enable(void)
4545 {
4546 enable_debug = true;
4547 }
4548
4549 static enum ofperr
4550 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
4551 {
4552 if (!enable_debug) {
4553 return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
4554 }
4555
4556 ofpact_put_DEBUG_RECIRC(out);
4557 return 0;
4558 }
4559
4560 static void
4561 encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
4562 enum ofp_version ofp_version OVS_UNUSED,
4563 struct ofpbuf *out)
4564 {
4565 put_NXAST_DEBUG_RECIRC(out);
4566 }
4567
4568 static char * OVS_WARN_UNUSED_RESULT
4569 parse_DEBUG_RECIRC(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4570 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4571 {
4572 ofpact_put_DEBUG_RECIRC(ofpacts);
4573 return NULL;
4574 }
4575
4576 static void
4577 format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4578 {
4579 ds_put_cstr(s, "debug_recirc");
4580 }
4581
4582 /* Action structure for NXAST_CT.
4583 *
4584 * Pass traffic to the connection tracker.
4585 *
4586 * There are two important concepts to understanding the connection tracking
4587 * interface: Packet state and Connection state. Packets may be "Untracked" or
4588 * "Tracked". Connections may be "Uncommitted" or "Committed".
4589 *
4590 * - Packet State:
4591 *
4592 * Untracked packets have not yet passed through the connection tracker,
4593 * and the connection state for such packets is unknown. In most cases,
4594 * packets entering the OpenFlow pipeline will initially be in the
4595 * untracked state. Untracked packets may become tracked by executing
4596 * NXAST_CT with a "recirc_table" specified. This makes various aspects
4597 * about the connection available, in particular the connection state.
4598 *
4599 * Tracked packets have previously passed through the connection tracker.
4600 * These packets will remain tracked through until the end of the OpenFlow
4601 * pipeline. Tracked packets which have NXAST_CT executed with a
4602 * "recirc_table" specified will return to the tracked state.
4603 *
4604 * The packet state is only significant for the duration of packet
4605 * processing within the OpenFlow pipeline.
4606 *
4607 * - Connection State:
4608 *
4609 * Multiple packets may be associated with a single connection. Initially,
4610 * all connections are uncommitted. The connection state corresponding to
4611 * a packet is available in the NXM_NX_CT_STATE field for tracked packets.
4612 *
4613 * Uncommitted connections have no state stored about them. Uncommitted
4614 * connections may transition into the committed state by executing
4615 * NXAST_CT with the NX_CT_F_COMMIT flag.
4616 *
4617 * Once a connection becomes committed, information may be gathered about
4618 * the connection by passing subsequent packets through the connection
4619 * tracker, and the state of the connection will be stored beyond the
4620 * lifetime of packet processing.
4621 *
4622 * Connections may transition back into the uncommitted state due to
4623 * external timers, or due to the contents of packets that are sent to the
4624 * connection tracker. This behaviour is outside of the scope of the
4625 * OpenFlow interface.
4626 *
4627 * The "zone" specifies a context within which the tracking is done:
4628 *
4629 * The connection tracking zone is a 16-bit number. Each zone is an
4630 * independent connection tracking context. The connection state for each
4631 * connection is completely separate for each zone, so if a connection
4632 * is committed to zone A, then it will remain uncommitted in zone B.
4633 * If NXAST_CT is executed with the same zone multiple times, later
4634 * executions have no effect.
4635 *
4636 * If 'zone_src' is nonzero, this specifies that the zone should be
4637 * sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
4638 * of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
4639 * NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
4640 * are the same as the acceptable nxm_header values for the 'src' field of
4641 * NXAST_REG_MOVE.
4642 *
4643 * If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
4644 * connection tracking zone.
4645 *
4646 * The "recirc_table" allows NXM_NX_CT_* fields to become available:
4647 *
4648 * If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
4649 * packet will be logically cloned prior to executing this action. One
4650 * copy will be sent to the connection tracker, then will be re-injected
4651 * into the OpenFlow pipeline beginning at the OpenFlow table specified in
4652 * this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
4653 * fields will be populated. The original instance of the packet will
4654 * continue the current actions list. This can be thought of as similar to
4655 * the effect of the "output" action: One copy is sent out (in this case,
4656 * to the connection tracker), but the current copy continues processing.
4657 *
4658 * It is strongly recommended that this table is later than the current
4659 * table, to prevent loops.
4660 */
4661 struct nx_action_conntrack {
4662 ovs_be16 type; /* OFPAT_VENDOR. */
4663 ovs_be16 len; /* At least 24. */
4664 ovs_be32 vendor; /* NX_VENDOR_ID. */
4665 ovs_be16 subtype; /* NXAST_CT. */
4666 ovs_be16 flags; /* Zero or more NX_CT_F_* flags.
4667 * Unspecified flag bits must be zero. */
4668 ovs_be32 zone_src; /* Connection tracking context. */
4669 union {
4670 ovs_be16 zone_ofs_nbits;/* Range to use from source field. */
4671 ovs_be16 zone_imm; /* Immediate value for zone. */
4672 };
4673 uint8_t recirc_table; /* Recirculate to a specific table, or
4674 NX_CT_RECIRC_NONE for no recirculation. */
4675 uint8_t pad[5]; /* Zeroes */
4676 /* Followed by a sequence of zero or more OpenFlow actions. The length of
4677 * these is included in 'len'. */
4678 };
4679 OFP_ASSERT(sizeof(struct nx_action_conntrack) == 24);
4680
4681 static enum ofperr
4682 decode_ct_zone(const struct nx_action_conntrack *nac,
4683 struct ofpact_conntrack *out)
4684 {
4685 if (nac->zone_src) {
4686 enum ofperr error;
4687
4688 out->zone_src.field = mf_from_nxm_header(ntohl(nac->zone_src));
4689 out->zone_src.ofs = nxm_decode_ofs(nac->zone_ofs_nbits);
4690 out->zone_src.n_bits = nxm_decode_n_bits(nac->zone_ofs_nbits);
4691 error = mf_check_src(&out->zone_src, NULL);
4692 if (error) {
4693 return error;
4694 }
4695
4696 if (out->zone_src.n_bits != 16) {
4697 VLOG_WARN_RL(&rl, "zone n_bits %d not within valid range [16..16]",
4698 out->zone_src.n_bits);
4699 return OFPERR_OFPBAC_BAD_SET_LEN;
4700 }
4701 } else {
4702 out->zone_src.field = NULL;
4703 out->zone_imm = ntohs(nac->zone_imm);
4704 }
4705
4706 return 0;
4707 }
4708
4709 static enum ofperr
4710 decode_NXAST_RAW_CT(const struct nx_action_conntrack *nac,
4711 enum ofp_version ofp_version OVS_UNUSED,
4712 struct ofpbuf *out)
4713 {
4714 struct ofpact_conntrack *conntrack;
4715 int error = 0;
4716
4717 conntrack = ofpact_put_CT(out);
4718 conntrack->flags = ntohs(nac->flags);
4719 error = decode_ct_zone(nac, conntrack);
4720 if (error) {
4721 goto out;
4722 }
4723 conntrack->recirc_table = nac->recirc_table;
4724
4725 out:
4726 return error;
4727 }
4728
4729 static void
4730 encode_CT(const struct ofpact_conntrack *conntrack,
4731 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4732 {
4733 struct nx_action_conntrack *nac;
4734
4735 nac = put_NXAST_CT(out);
4736 nac->flags = htons(conntrack->flags);
4737 if (conntrack->zone_src.field) {
4738 nac->zone_src = htonl(mf_nxm_header(conntrack->zone_src.field->id));
4739 nac->zone_ofs_nbits = nxm_encode_ofs_nbits(conntrack->zone_src.ofs,
4740 conntrack->zone_src.n_bits);
4741 } else {
4742 nac->zone_src = htonl(0);
4743 nac->zone_imm = htons(conntrack->zone_imm);
4744 }
4745 nac->recirc_table = conntrack->recirc_table;
4746 }
4747
4748 /* Parses 'arg' as the argument to a "ct" action, and appends such an
4749 * action to 'ofpacts'.
4750 *
4751 * Returns NULL if successful, otherwise a malloc()'d string describing the
4752 * error. The caller is responsible for freeing the returned string. */
4753 static char * OVS_WARN_UNUSED_RESULT
4754 parse_CT(char *arg, struct ofpbuf *ofpacts,
4755 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4756 {
4757 struct ofpact_conntrack *oc;
4758 char *error = NULL;
4759 char *key, *value;
4760
4761 oc = ofpact_put_CT(ofpacts);
4762 oc->flags = 0;
4763 oc->recirc_table = NX_CT_RECIRC_NONE;
4764 while (ofputil_parse_key_value(&arg, &key, &value)) {
4765 if (!strcmp(key, "commit")) {
4766 oc->flags |= NX_CT_F_COMMIT;
4767 } else if (!strcmp(key, "table")) {
4768 error = str_to_u8(value, "recirc_table", &oc->recirc_table);
4769 if (!error && oc->recirc_table == NX_CT_RECIRC_NONE) {
4770 error = xasprintf("invalid table %#"PRIx16, oc->recirc_table);
4771 }
4772 } else if (!strcmp(key, "zone")) {
4773 error = str_to_u16(value, "zone", &oc->zone_imm);
4774
4775 if (error) {
4776 free(error);
4777 error = mf_parse_subfield(&oc->zone_src, value);
4778 if (error) {
4779 return error;
4780 }
4781 }
4782 } else {
4783 error = xasprintf("invalid argument to \"ct\" action: `%s'", key);
4784 }
4785 if (error) {
4786 break;
4787 }
4788 }
4789
4790 return error;
4791 }
4792
4793 static void
4794 format_CT(const struct ofpact_conntrack *a, struct ds *s)
4795 {
4796 ds_put_cstr(s, "ct(");
4797 if (a->flags & NX_CT_F_COMMIT) {
4798 ds_put_cstr(s, "commit,");
4799 }
4800 if (a->recirc_table != NX_CT_RECIRC_NONE) {
4801 ds_put_format(s, "table=%"PRIu8",", a->recirc_table);
4802 }
4803 if (a->zone_src.field) {
4804 ds_put_format(s, "zone=");
4805 mf_format_subfield(&a->zone_src, s);
4806 ds_put_char(s, ',');
4807 } else if (a->zone_imm) {
4808 ds_put_format(s, "zone=%"PRIu16",", a->zone_imm);
4809 }
4810 ds_chomp(s, ',');
4811 ds_put_char(s, ')');
4812 }
4813 \f
4814 /* Meter instruction. */
4815
4816 static void
4817 encode_METER(const struct ofpact_meter *meter,
4818 enum ofp_version ofp_version, struct ofpbuf *out)
4819 {
4820 if (ofp_version >= OFP13_VERSION) {
4821 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
4822 }
4823 }
4824
4825 static char * OVS_WARN_UNUSED_RESULT
4826 parse_METER(char *arg, struct ofpbuf *ofpacts,
4827 enum ofputil_protocol *usable_protocols)
4828 {
4829 *usable_protocols &= OFPUTIL_P_OF13_UP;
4830 return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
4831 }
4832
4833 static void
4834 format_METER(const struct ofpact_meter *a, struct ds *s)
4835 {
4836 ds_put_format(s, "meter:%"PRIu32, a->meter_id);
4837 }
4838 \f
4839 /* Clear-Actions instruction. */
4840
4841 static void
4842 encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
4843 enum ofp_version ofp_version OVS_UNUSED,
4844 struct ofpbuf *out OVS_UNUSED)
4845 {
4846 if (ofp_version > OFP10_VERSION) {
4847 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
4848 }
4849 }
4850
4851 static char * OVS_WARN_UNUSED_RESULT
4852 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4853 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4854 {
4855 ofpact_put_CLEAR_ACTIONS(ofpacts);
4856 return NULL;
4857 }
4858
4859 static void
4860 format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4861 {
4862 ds_put_cstr(s, "clear_actions");
4863 }
4864 \f
4865 /* Write-Actions instruction. */
4866
4867 static void
4868 encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
4869 enum ofp_version ofp_version, struct ofpbuf *out)
4870 {
4871 if (ofp_version > OFP10_VERSION) {
4872 const size_t ofs = out->size;
4873
4874 instruction_put_OFPIT11_WRITE_ACTIONS(out);
4875 ofpacts_put_openflow_actions(actions->actions,
4876 ofpact_nest_get_action_len(actions),
4877 out, ofp_version);
4878 ofpacts_update_instruction_actions(out, ofs);
4879 }
4880 }
4881
4882 static char * OVS_WARN_UNUSED_RESULT
4883 parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
4884 enum ofputil_protocol *usable_protocols)
4885 {
4886 size_t ofs = ofpacts_pull(ofpacts);
4887 struct ofpact_nest *on;
4888 char *error;
4889
4890 /* Add a Write-Actions instruction and then pull it off. */
4891 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
4892 ofpbuf_pull(ofpacts, sizeof *on);
4893
4894 /* Parse nested actions.
4895 *
4896 * We pulled off "write-actions" and the previous actions because the
4897 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
4898 * that it doesn't actually include the nested actions. That means that
4899 * ofpacts_parse() would reject them as being part of an Apply-Actions that
4900 * follows a Write-Actions, which is an invalid order. */
4901 error = ofpacts_parse(arg, ofpacts, usable_protocols, false,
4902 OFPACT_WRITE_ACTIONS);
4903
4904 /* Put the Write-Actions back on and update its length. */
4905 on = ofpbuf_push_uninit(ofpacts, sizeof *on);
4906 on->ofpact.len = ofpacts->size;
4907
4908 /* Put any previous actions or instructions back on. */
4909 ofpbuf_push_uninit(ofpacts, ofs);
4910
4911 return error;
4912 }
4913
4914 static void
4915 format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
4916 {
4917 ds_put_cstr(s, "write_actions(");
4918 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
4919 ds_put_char(s, ')');
4920 }
4921 \f
4922 /* Action structure for NXAST_WRITE_METADATA.
4923 *
4924 * Modifies the 'mask' bits of the metadata value. */
4925 struct nx_action_write_metadata {
4926 ovs_be16 type; /* OFPAT_VENDOR. */
4927 ovs_be16 len; /* Length is 32. */
4928 ovs_be32 vendor; /* NX_VENDOR_ID. */
4929 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
4930 uint8_t zeros[6]; /* Must be zero. */
4931 ovs_be64 metadata; /* Metadata register. */
4932 ovs_be64 mask; /* Metadata mask. */
4933 };
4934 OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
4935
4936 static enum ofperr
4937 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
4938 enum ofp_version ofp_version OVS_UNUSED,
4939 struct ofpbuf *out)
4940 {
4941 struct ofpact_metadata *om;
4942
4943 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
4944 return OFPERR_NXBRC_MUST_BE_ZERO;
4945 }
4946
4947 om = ofpact_put_WRITE_METADATA(out);
4948 om->metadata = nawm->metadata;
4949 om->mask = nawm->mask;
4950
4951 return 0;
4952 }
4953
4954 static void
4955 encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
4956 enum ofp_version ofp_version, struct ofpbuf *out)
4957 {
4958 if (ofp_version == OFP10_VERSION) {
4959 struct nx_action_write_metadata *nawm;
4960
4961 nawm = put_NXAST_WRITE_METADATA(out);
4962 nawm->metadata = metadata->metadata;
4963 nawm->mask = metadata->mask;
4964 } else {
4965 struct ofp11_instruction_write_metadata *oiwm;
4966
4967 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
4968 oiwm->metadata = metadata->metadata;
4969 oiwm->metadata_mask = metadata->mask;
4970 }
4971 }
4972
4973 static char * OVS_WARN_UNUSED_RESULT
4974 parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
4975 enum ofputil_protocol *usable_protocols)
4976 {
4977 struct ofpact_metadata *om;
4978 char *mask = strchr(arg, '/');
4979
4980 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
4981
4982 om = ofpact_put_WRITE_METADATA(ofpacts);
4983 if (mask) {
4984 char *error;
4985
4986 *mask = '\0';
4987 error = str_to_be64(mask + 1, &om->mask);
4988 if (error) {
4989 return error;
4990 }
4991 } else {
4992 om->mask = OVS_BE64_MAX;
4993 }
4994
4995 return str_to_be64(arg, &om->metadata);
4996 }
4997
4998 static void
4999 format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
5000 {
5001 ds_put_format(s, "write_metadata:%#"PRIx64, ntohll(a->metadata));
5002 if (a->mask != OVS_BE64_MAX) {
5003 ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
5004 }
5005 }
5006 \f
5007 /* Goto-Table instruction. */
5008
5009 static void
5010 encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
5011 enum ofp_version ofp_version, struct ofpbuf *out)
5012 {
5013 if (ofp_version == OFP10_VERSION) {
5014 struct nx_action_resubmit *nar;
5015
5016 nar = put_NXAST_RESUBMIT_TABLE(out);
5017 nar->table = goto_table->table_id;
5018 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
5019 } else {
5020 struct ofp11_instruction_goto_table *oigt;
5021
5022 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
5023 oigt->table_id = goto_table->table_id;
5024 memset(oigt->pad, 0, sizeof oigt->pad);
5025 }
5026 }
5027
5028 static char * OVS_WARN_UNUSED_RESULT
5029 parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
5030 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5031 {
5032 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
5033 char *table_s = strsep(&arg, ",");
5034 if (!table_s || !table_s[0]) {
5035 return xstrdup("instruction goto-table needs table id");
5036 }
5037 return str_to_u8(table_s, "table", &ogt->table_id);
5038 }
5039
5040 static void
5041 format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
5042 {
5043 ds_put_format(s, "goto_table:%"PRIu8, a->table_id);
5044 }
5045 \f
5046 static void
5047 log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
5048 const struct ofp_action_header *bad_action, enum ofperr error)
5049 {
5050 if (!VLOG_DROP_WARN(&rl)) {
5051 struct ds s;
5052
5053 ds_init(&s);
5054 ds_put_hex_dump(&s, actions, actions_len, 0, false);
5055 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
5056 (char *)bad_action - (char *)actions,
5057 ofperr_get_name(error), ds_cstr(&s));
5058 ds_destroy(&s);
5059 }
5060 }
5061
5062 static enum ofperr
5063 ofpacts_decode(const void *actions, size_t actions_len,
5064 enum ofp_version ofp_version, struct ofpbuf *ofpacts)
5065 {
5066 struct ofpbuf openflow;
5067
5068 ofpbuf_use_const(&openflow, actions, actions_len);
5069 while (openflow.size) {
5070 const struct ofp_action_header *action = openflow.data;
5071 enum ofp_raw_action_type raw;
5072 enum ofperr error;
5073 uint64_t arg;
5074
5075 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
5076 if (!error) {
5077 error = ofpact_decode(action, raw, ofp_version, arg, ofpacts);
5078 }
5079
5080 if (error) {
5081 log_bad_action(actions, actions_len, action, error);
5082 return error;
5083 }
5084 }
5085
5086 ofpact_pad(ofpacts);
5087 return 0;
5088 }
5089
5090 static enum ofperr
5091 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
5092 unsigned int actions_len,
5093 enum ofp_version version,
5094 uint32_t allowed_ovsinsts,
5095 struct ofpbuf *ofpacts,
5096 enum ofpact_type outer_action)
5097 {
5098 const struct ofp_action_header *actions;
5099 enum ofperr error;
5100
5101 if (!outer_action) {
5102 ofpbuf_clear(ofpacts);
5103 }
5104
5105 if (actions_len % OFP_ACTION_ALIGN != 0) {
5106 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
5107 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
5108 return OFPERR_OFPBRC_BAD_LEN;
5109 }
5110
5111 actions = ofpbuf_try_pull(openflow, actions_len);
5112 if (actions == NULL) {
5113 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
5114 "remaining message length (%"PRIu32")",
5115 actions_len, openflow->size);
5116 return OFPERR_OFPBRC_BAD_LEN;
5117 }
5118
5119 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5120 if (error) {
5121 ofpbuf_clear(ofpacts);
5122 return error;
5123 }
5124
5125 error = ofpacts_verify(ofpacts->data, ofpacts->size, allowed_ovsinsts,
5126 outer_action);
5127 if (error) {
5128 ofpbuf_clear(ofpacts);
5129 }
5130 return error;
5131 }
5132
5133 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
5134 * front of 'openflow' into ofpacts. On success, replaces any existing content
5135 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
5136 * Returns 0 if successful, otherwise an OpenFlow error.
5137 *
5138 * Actions are processed according to their OpenFlow version which
5139 * is provided in the 'version' parameter.
5140 *
5141 * In most places in OpenFlow, actions appear encapsulated in instructions, so
5142 * you should call ofpacts_pull_openflow_instructions() instead of this
5143 * function.
5144 *
5145 * The parsed actions are valid generically, but they may not be valid in a
5146 * specific context. For example, port numbers up to OFPP_MAX are valid
5147 * generically, but specific datapaths may only support port numbers in a
5148 * smaller range. Use ofpacts_check() to additional check whether actions are
5149 * valid in a specific context. */
5150 enum ofperr
5151 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
5152 unsigned int actions_len,
5153 enum ofp_version version,
5154 struct ofpbuf *ofpacts)
5155 {
5156 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
5157 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
5158 ofpacts, 0);
5159 }
5160 \f
5161 /* OpenFlow 1.1 actions. */
5162
5163
5164 /* True if an action sets the value of a field
5165 * in a way that is compatibile with the action set.
5166 * The field can be set via either a set or a move action.
5167 * False otherwise. */
5168 static bool
5169 ofpact_is_set_or_move_action(const struct ofpact *a)
5170 {
5171 switch (a->type) {
5172 case OFPACT_SET_FIELD:
5173 case OFPACT_REG_MOVE:
5174 case OFPACT_SET_ETH_DST:
5175 case OFPACT_SET_ETH_SRC:
5176 case OFPACT_SET_IP_DSCP:
5177 case OFPACT_SET_IP_ECN:
5178 case OFPACT_SET_IP_TTL:
5179 case OFPACT_SET_IPV4_DST:
5180 case OFPACT_SET_IPV4_SRC:
5181 case OFPACT_SET_L4_DST_PORT:
5182 case OFPACT_SET_L4_SRC_PORT:
5183 case OFPACT_SET_MPLS_LABEL:
5184 case OFPACT_SET_MPLS_TC:
5185 case OFPACT_SET_MPLS_TTL:
5186 case OFPACT_SET_QUEUE:
5187 case OFPACT_SET_TUNNEL:
5188 case OFPACT_SET_VLAN_PCP:
5189 case OFPACT_SET_VLAN_VID:
5190 return true;
5191 case OFPACT_BUNDLE:
5192 case OFPACT_CLEAR_ACTIONS:
5193 case OFPACT_CT:
5194 case OFPACT_CONTROLLER:
5195 case OFPACT_DEC_MPLS_TTL:
5196 case OFPACT_DEC_TTL:
5197 case OFPACT_ENQUEUE:
5198 case OFPACT_EXIT:
5199 case OFPACT_UNROLL_XLATE:
5200 case OFPACT_FIN_TIMEOUT:
5201 case OFPACT_GOTO_TABLE:
5202 case OFPACT_GROUP:
5203 case OFPACT_LEARN:
5204 case OFPACT_CONJUNCTION:
5205 case OFPACT_METER:
5206 case OFPACT_MULTIPATH:
5207 case OFPACT_NOTE:
5208 case OFPACT_OUTPUT:
5209 case OFPACT_OUTPUT_REG:
5210 case OFPACT_POP_MPLS:
5211 case OFPACT_POP_QUEUE:
5212 case OFPACT_PUSH_MPLS:
5213 case OFPACT_PUSH_VLAN:
5214 case OFPACT_RESUBMIT:
5215 case OFPACT_SAMPLE:
5216 case OFPACT_STACK_POP:
5217 case OFPACT_STACK_PUSH:
5218 case OFPACT_STRIP_VLAN:
5219 case OFPACT_WRITE_ACTIONS:
5220 case OFPACT_WRITE_METADATA:
5221 case OFPACT_DEBUG_RECIRC:
5222 return false;
5223 default:
5224 OVS_NOT_REACHED();
5225 }
5226 }
5227
5228 /* True if an action is allowed in the action set.
5229 * False otherwise. */
5230 static bool
5231 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
5232 {
5233 switch (a->type) {
5234 case OFPACT_DEC_MPLS_TTL:
5235 case OFPACT_DEC_TTL:
5236 case OFPACT_GROUP:
5237 case OFPACT_OUTPUT:
5238 case OFPACT_POP_MPLS:
5239 case OFPACT_PUSH_MPLS:
5240 case OFPACT_PUSH_VLAN:
5241 case OFPACT_REG_MOVE:
5242 case OFPACT_SET_FIELD:
5243 case OFPACT_SET_ETH_DST:
5244 case OFPACT_SET_ETH_SRC:
5245 case OFPACT_SET_IP_DSCP:
5246 case OFPACT_SET_IP_ECN:
5247 case OFPACT_SET_IP_TTL:
5248 case OFPACT_SET_IPV4_DST:
5249 case OFPACT_SET_IPV4_SRC:
5250 case OFPACT_SET_L4_DST_PORT:
5251 case OFPACT_SET_L4_SRC_PORT:
5252 case OFPACT_SET_MPLS_LABEL:
5253 case OFPACT_SET_MPLS_TC:
5254 case OFPACT_SET_MPLS_TTL:
5255 case OFPACT_SET_QUEUE:
5256 case OFPACT_SET_TUNNEL:
5257 case OFPACT_SET_VLAN_PCP:
5258 case OFPACT_SET_VLAN_VID:
5259 case OFPACT_STRIP_VLAN:
5260 return true;
5261
5262 /* In general these actions are excluded because they are not part of
5263 * the OpenFlow specification nor map to actions that are defined in
5264 * the specification. Thus the order in which they should be applied
5265 * in the action set is undefined. */
5266 case OFPACT_BUNDLE:
5267 case OFPACT_CONTROLLER:
5268 case OFPACT_CT:
5269 case OFPACT_ENQUEUE:
5270 case OFPACT_EXIT:
5271 case OFPACT_UNROLL_XLATE:
5272 case OFPACT_FIN_TIMEOUT:
5273 case OFPACT_LEARN:
5274 case OFPACT_CONJUNCTION:
5275 case OFPACT_MULTIPATH:
5276 case OFPACT_NOTE:
5277 case OFPACT_OUTPUT_REG:
5278 case OFPACT_POP_QUEUE:
5279 case OFPACT_RESUBMIT:
5280 case OFPACT_SAMPLE:
5281 case OFPACT_STACK_POP:
5282 case OFPACT_STACK_PUSH:
5283 case OFPACT_DEBUG_RECIRC:
5284
5285 /* The action set may only include actions and thus
5286 * may not include any instructions */
5287 case OFPACT_CLEAR_ACTIONS:
5288 case OFPACT_GOTO_TABLE:
5289 case OFPACT_METER:
5290 case OFPACT_WRITE_ACTIONS:
5291 case OFPACT_WRITE_METADATA:
5292 return false;
5293 default:
5294 OVS_NOT_REACHED();
5295 }
5296 }
5297
5298 /* Append ofpact 'a' onto the tail of 'out' */
5299 static void
5300 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
5301 {
5302 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
5303 }
5304
5305 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
5306 static bool
5307 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
5308 enum ofpact_type filter)
5309 {
5310 const struct ofpact *target;
5311 const struct ofpact *a;
5312
5313 target = NULL;
5314 OFPACT_FOR_EACH (a, in->data, in->size) {
5315 if (a->type == filter) {
5316 target = a;
5317 }
5318 }
5319 if (target) {
5320 ofpact_copy(out, target);
5321 }
5322 return target != NULL;
5323 }
5324
5325 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
5326 * The order of appended ofpacts is preserved between 'in' and 'out' */
5327 static void
5328 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
5329 bool (*filter)(const struct ofpact *))
5330 {
5331 const struct ofpact *a;
5332
5333 OFPACT_FOR_EACH (a, in->data, in->size) {
5334 if (filter(a)) {
5335 ofpact_copy(out, a);
5336 }
5337 }
5338 }
5339
5340 /* Reads 'action_set', which contains ofpacts accumulated by
5341 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
5342 * executed directly into 'action_list'. (These names correspond to the
5343 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
5344 *
5345 * In general this involves appending the last instance of each action that is
5346 * admissible in the action set in the order described in the OpenFlow
5347 * specification.
5348 *
5349 * Exceptions:
5350 * + output action is only appended if no group action was present in 'in'.
5351 * + As a simplification all set actions are copied in the order the are
5352 * provided in 'in' as many set actions applied to a field has the same
5353 * affect as only applying the last action that sets a field and
5354 * duplicates are removed by do_xlate_actions().
5355 * This has an unwanted side-effect of compsoting multiple
5356 * LOAD_REG actions that touch different regions of the same field. */
5357 void
5358 ofpacts_execute_action_set(struct ofpbuf *action_list,
5359 const struct ofpbuf *action_set)
5360 {
5361 /* The OpenFlow spec "Action Set" section specifies this order. */
5362 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
5363 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
5364 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
5365 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
5366 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
5367 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
5368 ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
5369 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
5370
5371 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
5372 * we should execute only OFPACT_GROUP.
5373 *
5374 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
5375 * all the actions because there's no point in modifying a packet that will
5376 * not be sent anywhere. */
5377 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
5378 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
5379 !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT)) {
5380 ofpbuf_clear(action_list);
5381 }
5382 }
5383
5384
5385 static enum ofperr
5386 ofpacts_decode_for_action_set(const struct ofp_action_header *in,
5387 size_t n_in, enum ofp_version version,
5388 struct ofpbuf *out)
5389 {
5390 enum ofperr error;
5391 struct ofpact *a;
5392 size_t start = out->size;
5393
5394 error = ofpacts_decode(in, n_in, version, out);
5395
5396 if (error) {
5397 return error;
5398 }
5399
5400 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
5401 if (!ofpact_is_allowed_in_actions_set(a)) {
5402 VLOG_WARN_RL(&rl, "disallowed action in action set");
5403 return OFPERR_OFPBAC_BAD_TYPE;
5404 }
5405 }
5406
5407 return 0;
5408 }
5409 \f
5410 /* OpenFlow 1.1 instructions. */
5411
5412 struct instruction_type_info {
5413 enum ovs_instruction_type type;
5414 const char *name;
5415 };
5416
5417 static const struct instruction_type_info inst_info[] = {
5418 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
5419 OVS_INSTRUCTIONS
5420 #undef DEFINE_INST
5421 };
5422
5423 const char *
5424 ovs_instruction_name_from_type(enum ovs_instruction_type type)
5425 {
5426 return inst_info[type].name;
5427 }
5428
5429 int
5430 ovs_instruction_type_from_name(const char *name)
5431 {
5432 const struct instruction_type_info *p;
5433 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
5434 if (!strcasecmp(name, p->name)) {
5435 return p->type;
5436 }
5437 }
5438 return -1;
5439 }
5440
5441 enum ovs_instruction_type
5442 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
5443 {
5444 switch (type) {
5445 case OFPACT_METER:
5446 return OVSINST_OFPIT13_METER;
5447 case OFPACT_CLEAR_ACTIONS:
5448 return OVSINST_OFPIT11_CLEAR_ACTIONS;
5449 case OFPACT_WRITE_ACTIONS:
5450 return OVSINST_OFPIT11_WRITE_ACTIONS;
5451 case OFPACT_WRITE_METADATA:
5452 return OVSINST_OFPIT11_WRITE_METADATA;
5453 case OFPACT_GOTO_TABLE:
5454 return OVSINST_OFPIT11_GOTO_TABLE;
5455 case OFPACT_OUTPUT:
5456 case OFPACT_GROUP:
5457 case OFPACT_CONTROLLER:
5458 case OFPACT_ENQUEUE:
5459 case OFPACT_OUTPUT_REG:
5460 case OFPACT_BUNDLE:
5461 case OFPACT_SET_VLAN_VID:
5462 case OFPACT_SET_VLAN_PCP:
5463 case OFPACT_STRIP_VLAN:
5464 case OFPACT_PUSH_VLAN:
5465 case OFPACT_SET_ETH_SRC:
5466 case OFPACT_SET_ETH_DST:
5467 case OFPACT_SET_IPV4_SRC:
5468 case OFPACT_SET_IPV4_DST:
5469 case OFPACT_SET_IP_DSCP:
5470 case OFPACT_SET_IP_ECN:
5471 case OFPACT_SET_IP_TTL:
5472 case OFPACT_SET_L4_SRC_PORT:
5473 case OFPACT_SET_L4_DST_PORT:
5474 case OFPACT_REG_MOVE:
5475 case OFPACT_SET_FIELD:
5476 case OFPACT_STACK_PUSH:
5477 case OFPACT_STACK_POP:
5478 case OFPACT_DEC_TTL:
5479 case OFPACT_SET_MPLS_LABEL:
5480 case OFPACT_SET_MPLS_TC:
5481 case OFPACT_SET_MPLS_TTL:
5482 case OFPACT_DEC_MPLS_TTL:
5483 case OFPACT_PUSH_MPLS:
5484 case OFPACT_POP_MPLS:
5485 case OFPACT_SET_TUNNEL:
5486 case OFPACT_SET_QUEUE:
5487 case OFPACT_POP_QUEUE:
5488 case OFPACT_FIN_TIMEOUT:
5489 case OFPACT_RESUBMIT:
5490 case OFPACT_LEARN:
5491 case OFPACT_CONJUNCTION:
5492 case OFPACT_MULTIPATH:
5493 case OFPACT_NOTE:
5494 case OFPACT_EXIT:
5495 case OFPACT_UNROLL_XLATE:
5496 case OFPACT_SAMPLE:
5497 case OFPACT_DEBUG_RECIRC:
5498 case OFPACT_CT:
5499 default:
5500 return OVSINST_OFPIT11_APPLY_ACTIONS;
5501 }
5502 }
5503
5504 enum ofperr
5505 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
5506 const uint16_t inst_type)
5507 {
5508 switch (inst_type) {
5509
5510 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5511 case ENUM: \
5512 *instruction_type = OVSINST_##ENUM; \
5513 return 0;
5514 OVS_INSTRUCTIONS
5515 #undef DEFINE_INST
5516
5517 default:
5518 return OFPERR_OFPBIC_UNKNOWN_INST;
5519 }
5520 }
5521
5522 /* Two-way translation between OVS's internal "OVSINST_*" representation of
5523 * instructions and the "OFPIT_*" representation used in OpenFlow. */
5524 struct ovsinst_map {
5525 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
5526 int ofpit; /* OFPIT_* number from OpenFlow spec. */
5527 };
5528
5529 static const struct ovsinst_map *
5530 get_ovsinst_map(enum ofp_version version)
5531 {
5532 /* OpenFlow 1.1 and 1.2 instructions. */
5533 static const struct ovsinst_map of11[] = {
5534 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5535 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5536 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5537 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5538 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5539 { 0, -1 },
5540 };
5541
5542 /* OpenFlow 1.3+ instructions. */
5543 static const struct ovsinst_map of13[] = {
5544 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5545 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5546 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5547 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5548 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5549 { OVSINST_OFPIT13_METER, 6 },
5550 { 0, -1 },
5551 };
5552
5553 return version < OFP13_VERSION ? of11 : of13;
5554 }
5555
5556 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
5557 * values, into a bitmap of instructions suitable for OpenFlow 'version'
5558 * (OFP11_VERSION or later), and returns the result. */
5559 ovs_be32
5560 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
5561 {
5562 uint32_t ofpit_bitmap = 0;
5563 const struct ovsinst_map *x;
5564
5565 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5566 if (ovsinst_bitmap & (1u << x->ovsinst)) {
5567 ofpit_bitmap |= 1u << x->ofpit;
5568 }
5569 }
5570 return htonl(ofpit_bitmap);
5571 }
5572
5573 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
5574 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
5575 * correspond to OVSINST_* values, and returns the result. */
5576 uint32_t
5577 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
5578 {
5579 uint32_t ovsinst_bitmap = 0;
5580 const struct ovsinst_map *x;
5581
5582 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5583 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
5584 ovsinst_bitmap |= 1u << x->ovsinst;
5585 }
5586 }
5587 return ovsinst_bitmap;
5588 }
5589
5590 static inline struct ofp11_instruction *
5591 instruction_next(const struct ofp11_instruction *inst)
5592 {
5593 return ((struct ofp11_instruction *) (void *)
5594 ((uint8_t *) inst + ntohs(inst->len)));
5595 }
5596
5597 static inline bool
5598 instruction_is_valid(const struct ofp11_instruction *inst,
5599 size_t n_instructions)
5600 {
5601 uint16_t len = ntohs(inst->len);
5602 return (!(len % OFP11_INSTRUCTION_ALIGN)
5603 && len >= sizeof *inst
5604 && len / sizeof *inst <= n_instructions);
5605 }
5606
5607 /* This macro is careful to check for instructions with bad lengths. */
5608 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
5609 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
5610 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
5611 ((LEFT) -= (ntohs((ITER)->len) \
5612 / sizeof(struct ofp11_instruction)), \
5613 (ITER) = instruction_next(ITER)))
5614
5615 static enum ofperr
5616 decode_openflow11_instruction(const struct ofp11_instruction *inst,
5617 enum ovs_instruction_type *type)
5618 {
5619 uint16_t len = ntohs(inst->len);
5620
5621 switch (inst->type) {
5622 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
5623 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
5624
5625 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5626 case CONSTANT_HTONS(ENUM): \
5627 if (EXTENSIBLE \
5628 ? len >= sizeof(struct STRUCT) \
5629 : len == sizeof(struct STRUCT)) { \
5630 *type = OVSINST_##ENUM; \
5631 return 0; \
5632 } else { \
5633 return OFPERR_OFPBIC_BAD_LEN; \
5634 }
5635 OVS_INSTRUCTIONS
5636 #undef DEFINE_INST
5637
5638 default:
5639 return OFPERR_OFPBIC_UNKNOWN_INST;
5640 }
5641 }
5642
5643 static enum ofperr
5644 decode_openflow11_instructions(const struct ofp11_instruction insts[],
5645 size_t n_insts,
5646 const struct ofp11_instruction *out[])
5647 {
5648 const struct ofp11_instruction *inst;
5649 size_t left;
5650
5651 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
5652 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
5653 enum ovs_instruction_type type;
5654 enum ofperr error;
5655
5656 error = decode_openflow11_instruction(inst, &type);
5657 if (error) {
5658 return error;
5659 }
5660
5661 if (out[type]) {
5662 return OFPERR_OFPBIC_DUP_INST;
5663 }
5664 out[type] = inst;
5665 }
5666
5667 if (left) {
5668 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
5669 (n_insts - left) * sizeof *inst);
5670 return OFPERR_OFPBIC_BAD_LEN;
5671 }
5672 return 0;
5673 }
5674
5675 static void
5676 get_actions_from_instruction(const struct ofp11_instruction *inst,
5677 const struct ofp_action_header **actions,
5678 size_t *actions_len)
5679 {
5680 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
5681 *actions_len = ntohs(inst->len) - sizeof *inst;
5682 }
5683
5684 enum ofperr
5685 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
5686 unsigned int instructions_len,
5687 enum ofp_version version,
5688 struct ofpbuf *ofpacts)
5689 {
5690 const struct ofp11_instruction *instructions;
5691 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
5692 enum ofperr error;
5693
5694 if (version == OFP10_VERSION) {
5695 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
5696 version,
5697 (1u << N_OVS_INSTRUCTIONS) - 1,
5698 ofpacts, 0);
5699 }
5700
5701 ofpbuf_clear(ofpacts);
5702
5703 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
5704 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
5705 "multiple of %d",
5706 instructions_len, OFP11_INSTRUCTION_ALIGN);
5707 error = OFPERR_OFPBIC_BAD_LEN;
5708 goto exit;
5709 }
5710
5711 instructions = ofpbuf_try_pull(openflow, instructions_len);
5712 if (instructions == NULL) {
5713 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
5714 "remaining message length (%"PRIu32")",
5715 instructions_len, openflow->size);
5716 error = OFPERR_OFPBIC_BAD_LEN;
5717 goto exit;
5718 }
5719
5720 error = decode_openflow11_instructions(
5721 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
5722 insts);
5723 if (error) {
5724 goto exit;
5725 }
5726
5727 if (insts[OVSINST_OFPIT13_METER]) {
5728 const struct ofp13_instruction_meter *oim;
5729 struct ofpact_meter *om;
5730
5731 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
5732 insts[OVSINST_OFPIT13_METER]);
5733
5734 om = ofpact_put_METER(ofpacts);
5735 om->meter_id = ntohl(oim->meter_id);
5736 }
5737 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
5738 const struct ofp_action_header *actions;
5739 size_t actions_len;
5740
5741 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
5742 &actions, &actions_len);
5743 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5744 if (error) {
5745 goto exit;
5746 }
5747 }
5748 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
5749 instruction_get_OFPIT11_CLEAR_ACTIONS(
5750 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
5751 ofpact_put_CLEAR_ACTIONS(ofpacts);
5752 }
5753 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
5754 struct ofpact_nest *on;
5755 const struct ofp_action_header *actions;
5756 size_t actions_len;
5757 size_t start;
5758
5759 ofpact_pad(ofpacts);
5760 start = ofpacts->size;
5761 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
5762 offsetof(struct ofpact_nest, actions));
5763 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
5764 &actions, &actions_len);
5765 error = ofpacts_decode_for_action_set(actions, actions_len,
5766 version, ofpacts);
5767 if (error) {
5768 goto exit;
5769 }
5770 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
5771 on->ofpact.len = ofpacts->size - start;
5772 }
5773 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
5774 const struct ofp11_instruction_write_metadata *oiwm;
5775 struct ofpact_metadata *om;
5776
5777 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
5778 insts[OVSINST_OFPIT11_WRITE_METADATA]);
5779
5780 om = ofpact_put_WRITE_METADATA(ofpacts);
5781 om->metadata = oiwm->metadata;
5782 om->mask = oiwm->metadata_mask;
5783 }
5784 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
5785 const struct ofp11_instruction_goto_table *oigt;
5786 struct ofpact_goto_table *ogt;
5787
5788 oigt = instruction_get_OFPIT11_GOTO_TABLE(
5789 insts[OVSINST_OFPIT11_GOTO_TABLE]);
5790 ogt = ofpact_put_GOTO_TABLE(ofpacts);
5791 ogt->table_id = oigt->table_id;
5792 }
5793
5794 error = ofpacts_verify(ofpacts->data, ofpacts->size,
5795 (1u << N_OVS_INSTRUCTIONS) - 1, 0);
5796 exit:
5797 if (error) {
5798 ofpbuf_clear(ofpacts);
5799 }
5800 return error;
5801 }
5802
5803 /* Update the length of the instruction that begins at offset 'ofs' within
5804 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
5805 * If the instruction contains no nested actions, deletes it entirely. */
5806 static void
5807 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
5808 {
5809 struct ofp11_instruction_actions *oia;
5810
5811 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
5812 if (openflow->size > ofs + sizeof *oia) {
5813 oia->len = htons(openflow->size - ofs);
5814 } else {
5815 openflow->size = ofs;
5816 }
5817 }
5818 \f
5819 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
5820 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
5821 * valid, otherwise an OpenFlow error code. */
5822 enum ofperr
5823 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
5824 {
5825 switch (port) {
5826 case OFPP_IN_PORT:
5827 case OFPP_TABLE:
5828 case OFPP_NORMAL:
5829 case OFPP_FLOOD:
5830 case OFPP_ALL:
5831 case OFPP_CONTROLLER:
5832 case OFPP_LOCAL:
5833 return 0;
5834
5835 case OFPP_NONE:
5836 return OFPERR_OFPBAC_BAD_OUT_PORT;
5837
5838 default:
5839 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
5840 return 0;
5841 }
5842 return OFPERR_OFPBAC_BAD_OUT_PORT;
5843 }
5844 }
5845
5846 /* Removes the protocols that require consistency between match and actions
5847 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
5848 *
5849 * (An example of an inconsistency between match and actions is a flow that
5850 * does not match on an MPLS Ethertype but has an action that pops an MPLS
5851 * label.) */
5852 static void
5853 inconsistent_match(enum ofputil_protocol *usable_protocols)
5854 {
5855 *usable_protocols &= OFPUTIL_P_OF10_ANY;
5856 }
5857
5858 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
5859 * caller must restore them.
5860 *
5861 * Modifies some actions, filling in fields that could not be properly set
5862 * without context. */
5863 static enum ofperr
5864 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
5865 struct flow *flow, ofp_port_t max_ports,
5866 uint8_t table_id, uint8_t n_tables)
5867 {
5868 const struct ofpact_enqueue *enqueue;
5869 const struct mf_field *mf;
5870
5871 switch (a->type) {
5872 case OFPACT_OUTPUT:
5873 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
5874 max_ports);
5875
5876 case OFPACT_CONTROLLER:
5877 return 0;
5878
5879 case OFPACT_ENQUEUE:
5880 enqueue = ofpact_get_ENQUEUE(a);
5881 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
5882 && enqueue->port != OFPP_IN_PORT
5883 && enqueue->port != OFPP_LOCAL) {
5884 return OFPERR_OFPBAC_BAD_OUT_PORT;
5885 }
5886 return 0;
5887
5888 case OFPACT_OUTPUT_REG:
5889 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
5890
5891 case OFPACT_BUNDLE:
5892 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
5893
5894 case OFPACT_SET_VLAN_VID:
5895 /* Remember if we saw a vlan tag in the flow to aid translating to
5896 * OpenFlow 1.1+ if need be. */
5897 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
5898 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5899 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5900 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
5901 inconsistent_match(usable_protocols);
5902 }
5903 /* Temporary mark that we have a vlan tag. */
5904 flow->vlan_tci |= htons(VLAN_CFI);
5905 return 0;
5906
5907 case OFPACT_SET_VLAN_PCP:
5908 /* Remember if we saw a vlan tag in the flow to aid translating to
5909 * OpenFlow 1.1+ if need be. */
5910 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
5911 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5912 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5913 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
5914 inconsistent_match(usable_protocols);
5915 }
5916 /* Temporary mark that we have a vlan tag. */
5917 flow->vlan_tci |= htons(VLAN_CFI);
5918 return 0;
5919
5920 case OFPACT_STRIP_VLAN:
5921 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
5922 inconsistent_match(usable_protocols);
5923 }
5924 /* Temporary mark that we have no vlan tag. */
5925 flow->vlan_tci = htons(0);
5926 return 0;
5927
5928 case OFPACT_PUSH_VLAN:
5929 if (flow->vlan_tci & htons(VLAN_CFI)) {
5930 /* Multiple VLAN headers not supported. */
5931 return OFPERR_OFPBAC_BAD_TAG;
5932 }
5933 /* Temporary mark that we have a vlan tag. */
5934 flow->vlan_tci |= htons(VLAN_CFI);
5935 return 0;
5936
5937 case OFPACT_SET_ETH_SRC:
5938 case OFPACT_SET_ETH_DST:
5939 return 0;
5940
5941 case OFPACT_SET_IPV4_SRC:
5942 case OFPACT_SET_IPV4_DST:
5943 if (flow->dl_type != htons(ETH_TYPE_IP)) {
5944 inconsistent_match(usable_protocols);
5945 }
5946 return 0;
5947
5948 case OFPACT_SET_IP_DSCP:
5949 case OFPACT_SET_IP_ECN:
5950 case OFPACT_SET_IP_TTL:
5951 case OFPACT_DEC_TTL:
5952 if (!is_ip_any(flow)) {
5953 inconsistent_match(usable_protocols);
5954 }
5955 return 0;
5956
5957 case OFPACT_SET_L4_SRC_PORT:
5958 case OFPACT_SET_L4_DST_PORT:
5959 if (!is_ip_any(flow) || (flow->nw_frag & FLOW_NW_FRAG_LATER) ||
5960 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
5961 && flow->nw_proto != IPPROTO_SCTP)) {
5962 inconsistent_match(usable_protocols);
5963 }
5964 /* Note on which transport protocol the port numbers are set.
5965 * This allows this set action to be converted to an OF1.2 set field
5966 * action. */
5967 if (a->type == OFPACT_SET_L4_SRC_PORT) {
5968 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
5969 } else {
5970 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
5971 }
5972 return 0;
5973
5974 case OFPACT_REG_MOVE:
5975 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
5976
5977 case OFPACT_SET_FIELD:
5978 mf = ofpact_get_SET_FIELD(a)->field;
5979 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
5980 if (!mf_are_prereqs_ok(mf, flow) ||
5981 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
5982 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
5983 mf->name);
5984 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
5985 }
5986 /* Remember if we saw a vlan tag in the flow to aid translating to
5987 * OpenFlow 1.1 if need be. */
5988 ofpact_get_SET_FIELD(a)->flow_has_vlan =
5989 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5990 if (mf->id == MFF_VLAN_TCI) {
5991 /* The set field may add or remove the vlan tag,
5992 * Mark the status temporarily. */
5993 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
5994 }
5995 return 0;
5996
5997 case OFPACT_STACK_PUSH:
5998 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
5999
6000 case OFPACT_STACK_POP:
6001 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
6002
6003 case OFPACT_SET_MPLS_LABEL:
6004 case OFPACT_SET_MPLS_TC:
6005 case OFPACT_SET_MPLS_TTL:
6006 case OFPACT_DEC_MPLS_TTL:
6007 if (!eth_type_mpls(flow->dl_type)) {
6008 inconsistent_match(usable_protocols);
6009 }
6010 return 0;
6011
6012 case OFPACT_SET_TUNNEL:
6013 case OFPACT_SET_QUEUE:
6014 case OFPACT_POP_QUEUE:
6015 case OFPACT_RESUBMIT:
6016 return 0;
6017
6018 case OFPACT_FIN_TIMEOUT:
6019 if (flow->nw_proto != IPPROTO_TCP) {
6020 inconsistent_match(usable_protocols);
6021 }
6022 return 0;
6023
6024 case OFPACT_LEARN:
6025 return learn_check(ofpact_get_LEARN(a), flow);
6026
6027 case OFPACT_CONJUNCTION:
6028 return 0;
6029
6030 case OFPACT_MULTIPATH:
6031 return multipath_check(ofpact_get_MULTIPATH(a), flow);
6032
6033 case OFPACT_NOTE:
6034 case OFPACT_EXIT:
6035 return 0;
6036
6037 case OFPACT_PUSH_MPLS:
6038 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
6039 /* The packet is now MPLS and the MPLS payload is opaque.
6040 * Thus nothing can be assumed about the network protocol.
6041 * Temporarily mark that we have no nw_proto. */
6042 flow->nw_proto = 0;
6043 return 0;
6044
6045 case OFPACT_POP_MPLS:
6046 if (!eth_type_mpls(flow->dl_type)) {
6047 inconsistent_match(usable_protocols);
6048 }
6049 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
6050 return 0;
6051
6052 case OFPACT_SAMPLE:
6053 return 0;
6054
6055 case OFPACT_CT: {
6056 struct ofpact_conntrack *oc = ofpact_get_CT(a);
6057
6058 if (!dl_type_is_ip_any(flow->dl_type)
6059 || (flow->ct_state & CS_INVALID && oc->flags & NX_CT_F_COMMIT)) {
6060 inconsistent_match(usable_protocols);
6061 }
6062
6063 if (oc->zone_src.field) {
6064 return mf_check_src(&oc->zone_src, flow);
6065 }
6066 return 0;
6067 }
6068
6069 case OFPACT_CLEAR_ACTIONS:
6070 return 0;
6071
6072 case OFPACT_WRITE_ACTIONS: {
6073 /* Use a temporary copy of 'usable_protocols' because we can't check
6074 * consistency of an action set. */
6075 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
6076 enum ofputil_protocol p = *usable_protocols;
6077 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
6078 flow, max_ports, table_id, n_tables, &p);
6079 }
6080
6081 case OFPACT_WRITE_METADATA:
6082 return 0;
6083
6084 case OFPACT_METER: {
6085 uint32_t mid = ofpact_get_METER(a)->meter_id;
6086 if (mid == 0 || mid > OFPM13_MAX) {
6087 return OFPERR_OFPMMFC_INVALID_METER;
6088 }
6089 return 0;
6090 }
6091
6092 case OFPACT_GOTO_TABLE: {
6093 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
6094 if ((table_id != 255 && goto_table <= table_id)
6095 || (n_tables != 255 && goto_table >= n_tables)) {
6096 return OFPERR_OFPBIC_BAD_TABLE_ID;
6097 }
6098 return 0;
6099 }
6100
6101 case OFPACT_GROUP:
6102 return 0;
6103
6104 case OFPACT_UNROLL_XLATE:
6105 /* UNROLL is an internal action that should never be seen via
6106 * OpenFlow. */
6107 return OFPERR_OFPBAC_BAD_TYPE;
6108
6109 case OFPACT_DEBUG_RECIRC:
6110 return 0;
6111
6112 default:
6113 OVS_NOT_REACHED();
6114 }
6115 }
6116
6117 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
6118 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
6119 * switch with no more than 'max_ports' ports.
6120 *
6121 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
6122 * '*usable_protocols' the protocols that forbid the inconsistency. (An
6123 * example of an inconsistency between match and actions is a flow that does
6124 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
6125 *
6126 * May annotate ofpacts with information gathered from the 'flow'.
6127 *
6128 * May temporarily modify 'flow', but restores the changes before returning. */
6129 enum ofperr
6130 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
6131 struct flow *flow, ofp_port_t max_ports,
6132 uint8_t table_id, uint8_t n_tables,
6133 enum ofputil_protocol *usable_protocols)
6134 {
6135 struct ofpact *a;
6136 ovs_be16 dl_type = flow->dl_type;
6137 ovs_be16 vlan_tci = flow->vlan_tci;
6138 uint8_t nw_proto = flow->nw_proto;
6139 enum ofperr error = 0;
6140
6141 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6142 error = ofpact_check__(usable_protocols, a, flow,
6143 max_ports, table_id, n_tables);
6144 if (error) {
6145 break;
6146 }
6147 }
6148 /* Restore fields that may have been modified. */
6149 flow->dl_type = dl_type;
6150 flow->vlan_tci = vlan_tci;
6151 flow->nw_proto = nw_proto;
6152 return error;
6153 }
6154
6155 /* Like ofpacts_check(), but reports inconsistencies as
6156 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
6157 enum ofperr
6158 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
6159 struct flow *flow, ofp_port_t max_ports,
6160 uint8_t table_id, uint8_t n_tables,
6161 enum ofputil_protocol usable_protocols)
6162 {
6163 enum ofputil_protocol p = usable_protocols;
6164 enum ofperr error;
6165
6166 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
6167 table_id, n_tables, &p);
6168 return (error ? error
6169 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
6170 : 0);
6171 }
6172
6173 static enum ofperr
6174 ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action)
6175 {
6176 if (outer_action != OFPACT_WRITE_ACTIONS) {
6177 VLOG_WARN("\"%s\" action doesn't support nested action \"%s\"",
6178 ofpact_name(outer_action), ofpact_name(a->type));
6179 return OFPERR_OFPBAC_BAD_ARGUMENT;
6180 }
6181
6182 return 0;
6183 }
6184
6185 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
6186 * appropriate order as defined by the OpenFlow spec and as required by Open
6187 * vSwitch.
6188 *
6189 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
6190 * instructions that are allowed within 'ofpacts[]'.
6191 *
6192 * If 'outer_action' is not zero, it specifies that the actions are nested
6193 * within another action of type 'outer_action'. */
6194 static enum ofperr
6195 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
6196 uint32_t allowed_ovsinsts, enum ofpact_type outer_action)
6197 {
6198 const struct ofpact *a;
6199 enum ovs_instruction_type inst;
6200
6201 inst = OVSINST_OFPIT13_METER;
6202 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6203 enum ovs_instruction_type next;
6204
6205 if (a->type == OFPACT_CONJUNCTION) {
6206 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6207 if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
6208 VLOG_WARN("\"conjunction\" actions may be used along with "
6209 "\"note\" but not any other kind of action "
6210 "(such as the \"%s\" action used here)",
6211 ofpact_name(a->type));
6212 return OFPERR_NXBAC_BAD_CONJUNCTION;
6213 }
6214 }
6215 return 0;
6216 }
6217
6218 if (outer_action) {
6219 enum ofperr error = ofpacts_verify_nested(a, outer_action);
6220
6221 if (error) {
6222 return error;
6223 }
6224 }
6225
6226 next = ovs_instruction_type_from_ofpact_type(a->type);
6227 if (a > ofpacts
6228 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
6229 ? next < inst
6230 : next <= inst)) {
6231 const char *name = ovs_instruction_name_from_type(inst);
6232 const char *next_name = ovs_instruction_name_from_type(next);
6233
6234 if (next == inst) {
6235 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
6236 "1.1+ compatibility", name);
6237 } else {
6238 VLOG_WARN("invalid instruction ordering: %s must appear "
6239 "before %s, for OpenFlow 1.1+ compatibility",
6240 next_name, name);
6241 }
6242 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
6243 }
6244 if (!((1u << next) & allowed_ovsinsts)) {
6245 const char *name = ovs_instruction_name_from_type(next);
6246
6247 VLOG_WARN("%s instruction not allowed here", name);
6248 return OFPERR_OFPBIC_UNSUP_INST;
6249 }
6250
6251 inst = next;
6252 }
6253
6254 return 0;
6255 }
6256 \f
6257 /* Converting ofpacts to OpenFlow. */
6258
6259 static void
6260 encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
6261 struct ofpbuf *out)
6262 {
6263 switch (a->type) {
6264 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6265 case OFPACT_##ENUM: \
6266 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
6267 return;
6268 OFPACTS
6269 #undef OFPACT
6270 default:
6271 OVS_NOT_REACHED();
6272 }
6273 }
6274
6275 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
6276 * actions in 'openflow', appending the actions to any existing data in
6277 * 'openflow'. */
6278 size_t
6279 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
6280 struct ofpbuf *openflow,
6281 enum ofp_version ofp_version)
6282 {
6283 const struct ofpact *a;
6284 size_t start_size = openflow->size;
6285
6286 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6287 encode_ofpact(a, ofp_version, openflow);
6288 }
6289 return openflow->size - start_size;
6290 }
6291
6292 static enum ovs_instruction_type
6293 ofpact_is_apply_actions(const struct ofpact *a)
6294 {
6295 return (ovs_instruction_type_from_ofpact_type(a->type)
6296 == OVSINST_OFPIT11_APPLY_ACTIONS);
6297 }
6298
6299 void
6300 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
6301 size_t ofpacts_len,
6302 struct ofpbuf *openflow,
6303 enum ofp_version ofp_version)
6304 {
6305 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
6306 const struct ofpact *a;
6307
6308 if (ofp_version == OFP10_VERSION) {
6309 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
6310 ofp_version);
6311 return;
6312 }
6313
6314 a = ofpacts;
6315 while (a < end) {
6316 if (ofpact_is_apply_actions(a)) {
6317 size_t ofs = openflow->size;
6318
6319 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
6320 do {
6321 encode_ofpact(a, ofp_version, openflow);
6322 a = ofpact_next(a);
6323 } while (a < end && ofpact_is_apply_actions(a));
6324 ofpacts_update_instruction_actions(openflow, ofs);
6325 } else {
6326 encode_ofpact(a, ofp_version, openflow);
6327 a = ofpact_next(a);
6328 }
6329 }
6330 }
6331 \f
6332 /* Sets of supported actions. */
6333
6334 /* Two-way translation between OVS's internal "OFPACT_*" representation of
6335 * actions and the "OFPAT_*" representation used in some OpenFlow version.
6336 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
6337 * instance is specific to one OpenFlow version.) */
6338 struct ofpact_map {
6339 enum ofpact_type ofpact; /* Internal name for action type. */
6340 int ofpat; /* OFPAT_* number from OpenFlow spec. */
6341 };
6342
6343 static const struct ofpact_map *
6344 get_ofpact_map(enum ofp_version version)
6345 {
6346 /* OpenFlow 1.0 actions. */
6347 static const struct ofpact_map of10[] = {
6348 { OFPACT_OUTPUT, 0 },
6349 { OFPACT_SET_VLAN_VID, 1 },
6350 { OFPACT_SET_VLAN_PCP, 2 },
6351 { OFPACT_STRIP_VLAN, 3 },
6352 { OFPACT_SET_ETH_SRC, 4 },
6353 { OFPACT_SET_ETH_DST, 5 },
6354 { OFPACT_SET_IPV4_SRC, 6 },
6355 { OFPACT_SET_IPV4_DST, 7 },
6356 { OFPACT_SET_IP_DSCP, 8 },
6357 { OFPACT_SET_L4_SRC_PORT, 9 },
6358 { OFPACT_SET_L4_DST_PORT, 10 },
6359 { OFPACT_ENQUEUE, 11 },
6360 { 0, -1 },
6361 };
6362
6363 /* OpenFlow 1.1 actions. */
6364 static const struct ofpact_map of11[] = {
6365 { OFPACT_OUTPUT, 0 },
6366 { OFPACT_SET_VLAN_VID, 1 },
6367 { OFPACT_SET_VLAN_PCP, 2 },
6368 { OFPACT_SET_ETH_SRC, 3 },
6369 { OFPACT_SET_ETH_DST, 4 },
6370 { OFPACT_SET_IPV4_SRC, 5 },
6371 { OFPACT_SET_IPV4_DST, 6 },
6372 { OFPACT_SET_IP_DSCP, 7 },
6373 { OFPACT_SET_IP_ECN, 8 },
6374 { OFPACT_SET_L4_SRC_PORT, 9 },
6375 { OFPACT_SET_L4_DST_PORT, 10 },
6376 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6377 /* OFPAT_COPY_TTL_IN (12) not supported. */
6378 { OFPACT_SET_MPLS_LABEL, 13 },
6379 { OFPACT_SET_MPLS_TC, 14 },
6380 { OFPACT_SET_MPLS_TTL, 15 },
6381 { OFPACT_DEC_MPLS_TTL, 16 },
6382 { OFPACT_PUSH_VLAN, 17 },
6383 { OFPACT_STRIP_VLAN, 18 },
6384 { OFPACT_PUSH_MPLS, 19 },
6385 { OFPACT_POP_MPLS, 20 },
6386 { OFPACT_SET_QUEUE, 21 },
6387 { OFPACT_GROUP, 22 },
6388 { OFPACT_SET_IP_TTL, 23 },
6389 { OFPACT_DEC_TTL, 24 },
6390 { 0, -1 },
6391 };
6392
6393 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
6394 static const struct ofpact_map of12[] = {
6395 { OFPACT_OUTPUT, 0 },
6396 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6397 /* OFPAT_COPY_TTL_IN (12) not supported. */
6398 { OFPACT_SET_MPLS_TTL, 15 },
6399 { OFPACT_DEC_MPLS_TTL, 16 },
6400 { OFPACT_PUSH_VLAN, 17 },
6401 { OFPACT_STRIP_VLAN, 18 },
6402 { OFPACT_PUSH_MPLS, 19 },
6403 { OFPACT_POP_MPLS, 20 },
6404 { OFPACT_SET_QUEUE, 21 },
6405 { OFPACT_GROUP, 22 },
6406 { OFPACT_SET_IP_TTL, 23 },
6407 { OFPACT_DEC_TTL, 24 },
6408 { OFPACT_SET_FIELD, 25 },
6409 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
6410 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
6411 { 0, -1 },
6412 };
6413
6414 switch (version) {
6415 case OFP10_VERSION:
6416 return of10;
6417
6418 case OFP11_VERSION:
6419 return of11;
6420
6421 case OFP12_VERSION:
6422 case OFP13_VERSION:
6423 case OFP14_VERSION:
6424 case OFP15_VERSION:
6425 default:
6426 return of12;
6427 }
6428 }
6429
6430 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
6431 * values, into a bitmap of actions suitable for OpenFlow 'version', and
6432 * returns the result. */
6433 ovs_be32
6434 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
6435 {
6436 uint32_t openflow_bitmap = 0;
6437 const struct ofpact_map *x;
6438
6439 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6440 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
6441 openflow_bitmap |= 1u << x->ofpat;
6442 }
6443 }
6444 return htonl(openflow_bitmap);
6445 }
6446
6447 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
6448 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
6449 * and returns the result. */
6450 uint64_t
6451 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
6452 {
6453 uint64_t ofpact_bitmap = 0;
6454 const struct ofpact_map *x;
6455
6456 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6457 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
6458 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
6459 }
6460 }
6461 return ofpact_bitmap;
6462 }
6463
6464 /* Appends to 's' a string representation of the set of OFPACT_* represented
6465 * by 'ofpacts_bitmap'. */
6466 void
6467 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
6468 {
6469 if (!ofpacts_bitmap) {
6470 ds_put_cstr(s, "<none>");
6471 } else {
6472 while (ofpacts_bitmap) {
6473 ds_put_format(s, "%s ",
6474 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
6475 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
6476 }
6477 ds_chomp(s, ' ');
6478 }
6479 }
6480 \f
6481 /* Returns true if 'action' outputs to 'port', false otherwise. */
6482 static bool
6483 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
6484 {
6485 switch (ofpact->type) {
6486 case OFPACT_OUTPUT:
6487 return ofpact_get_OUTPUT(ofpact)->port == port;
6488 case OFPACT_ENQUEUE:
6489 return ofpact_get_ENQUEUE(ofpact)->port == port;
6490 case OFPACT_CONTROLLER:
6491 return port == OFPP_CONTROLLER;
6492
6493 case OFPACT_OUTPUT_REG:
6494 case OFPACT_BUNDLE:
6495 case OFPACT_SET_VLAN_VID:
6496 case OFPACT_SET_VLAN_PCP:
6497 case OFPACT_STRIP_VLAN:
6498 case OFPACT_PUSH_VLAN:
6499 case OFPACT_SET_ETH_SRC:
6500 case OFPACT_SET_ETH_DST:
6501 case OFPACT_SET_IPV4_SRC:
6502 case OFPACT_SET_IPV4_DST:
6503 case OFPACT_SET_IP_DSCP:
6504 case OFPACT_SET_IP_ECN:
6505 case OFPACT_SET_IP_TTL:
6506 case OFPACT_SET_L4_SRC_PORT:
6507 case OFPACT_SET_L4_DST_PORT:
6508 case OFPACT_REG_MOVE:
6509 case OFPACT_SET_FIELD:
6510 case OFPACT_STACK_PUSH:
6511 case OFPACT_STACK_POP:
6512 case OFPACT_DEC_TTL:
6513 case OFPACT_SET_MPLS_LABEL:
6514 case OFPACT_SET_MPLS_TC:
6515 case OFPACT_SET_MPLS_TTL:
6516 case OFPACT_DEC_MPLS_TTL:
6517 case OFPACT_SET_TUNNEL:
6518 case OFPACT_WRITE_METADATA:
6519 case OFPACT_SET_QUEUE:
6520 case OFPACT_POP_QUEUE:
6521 case OFPACT_FIN_TIMEOUT:
6522 case OFPACT_RESUBMIT:
6523 case OFPACT_LEARN:
6524 case OFPACT_CONJUNCTION:
6525 case OFPACT_MULTIPATH:
6526 case OFPACT_NOTE:
6527 case OFPACT_EXIT:
6528 case OFPACT_UNROLL_XLATE:
6529 case OFPACT_PUSH_MPLS:
6530 case OFPACT_POP_MPLS:
6531 case OFPACT_SAMPLE:
6532 case OFPACT_CLEAR_ACTIONS:
6533 case OFPACT_WRITE_ACTIONS:
6534 case OFPACT_GOTO_TABLE:
6535 case OFPACT_METER:
6536 case OFPACT_GROUP:
6537 case OFPACT_DEBUG_RECIRC:
6538 case OFPACT_CT:
6539 default:
6540 return false;
6541 }
6542 }
6543
6544 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6545 * to 'port', false otherwise. */
6546 bool
6547 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
6548 ofp_port_t port)
6549 {
6550 const struct ofpact *a;
6551
6552 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6553 if (ofpact_outputs_to_port(a, port)) {
6554 return true;
6555 }
6556 }
6557
6558 return false;
6559 }
6560
6561 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6562 * to 'group', false otherwise. */
6563 bool
6564 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
6565 uint32_t group_id)
6566 {
6567 const struct ofpact *a;
6568
6569 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6570 if (a->type == OFPACT_GROUP
6571 && ofpact_get_GROUP(a)->group_id == group_id) {
6572 return true;
6573 }
6574 }
6575
6576 return false;
6577 }
6578
6579 bool
6580 ofpacts_equal(const struct ofpact *a, size_t a_len,
6581 const struct ofpact *b, size_t b_len)
6582 {
6583 return a_len == b_len && !memcmp(a, b, a_len);
6584 }
6585
6586 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
6587 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
6588 *
6589 * This function relies on the order of 'ofpacts' being correct (as checked by
6590 * ofpacts_verify()). */
6591 uint32_t
6592 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
6593 {
6594 const struct ofpact *a;
6595
6596 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6597 enum ovs_instruction_type inst;
6598
6599 inst = ovs_instruction_type_from_ofpact_type(a->type);
6600 if (a->type == OFPACT_METER) {
6601 return ofpact_get_METER(a)->meter_id;
6602 } else if (inst > OVSINST_OFPIT13_METER) {
6603 break;
6604 }
6605 }
6606
6607 return 0;
6608 }
6609 \f
6610 /* Formatting ofpacts. */
6611
6612 static void
6613 ofpact_format(const struct ofpact *a, struct ds *s)
6614 {
6615 switch (a->type) {
6616 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6617 case OFPACT_##ENUM: \
6618 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
6619 break;
6620 OFPACTS
6621 #undef OFPACT
6622 default:
6623 OVS_NOT_REACHED();
6624 }
6625 }
6626
6627 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
6628 * 'ofpacts' to 'string'. */
6629 void
6630 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
6631 struct ds *string)
6632 {
6633 if (!ofpacts_len) {
6634 ds_put_cstr(string, "drop");
6635 } else {
6636 const struct ofpact *a;
6637
6638 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6639 if (a != ofpacts) {
6640 ds_put_cstr(string, ",");
6641 }
6642
6643 /* XXX write-actions */
6644 ofpact_format(a, string);
6645 }
6646 }
6647 }
6648 \f
6649 /* Internal use by helpers. */
6650
6651 void *
6652 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
6653 {
6654 struct ofpact *ofpact;
6655
6656 ofpact_pad(ofpacts);
6657 ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
6658 ofpact = ofpacts->header;
6659 ofpact_init(ofpact, type, len);
6660 return ofpact;
6661 }
6662
6663 void
6664 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
6665 {
6666 memset(ofpact, 0, len);
6667 ofpact->type = type;
6668 ofpact->raw = -1;
6669 ofpact->len = len;
6670 }
6671 \f
6672 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
6673 * starting at 'ofpact'.
6674 *
6675 * This is the correct way to update a variable-length ofpact's length after
6676 * adding the variable-length part of the payload. (See the large comment
6677 * near the end of ofp-actions.h for more information.) */
6678 void
6679 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
6680 {
6681 ovs_assert(ofpact == ofpacts->header);
6682 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
6683 }
6684
6685 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
6686 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
6687 * client must call this itself after adding the final ofpact to an array of
6688 * them.
6689 *
6690 * (The consequences of failing to call this function are probably not dire.
6691 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
6692 * not dereference it. That's undefined behavior, technically, but it will not
6693 * cause a real problem on common systems. Still, it seems better to call
6694 * it.) */
6695 void
6696 ofpact_pad(struct ofpbuf *ofpacts)
6697 {
6698 unsigned int pad = PAD_SIZE(ofpacts->size, OFPACT_ALIGNTO);
6699 if (pad) {
6700 ofpbuf_put_zeros(ofpacts, pad);
6701 }
6702 }
6703 \f
6704
6705
6706
6707 static char * OVS_WARN_UNUSED_RESULT
6708 ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
6709 enum ofputil_protocol *usable_protocols)
6710 {
6711 switch (type) {
6712 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6713 case OFPACT_##ENUM: \
6714 return parse_##ENUM(value, ofpacts, usable_protocols);
6715 OFPACTS
6716 #undef OFPACT
6717 default:
6718 OVS_NOT_REACHED();
6719 }
6720 }
6721
6722 static bool
6723 ofpact_type_from_name(const char *name, enum ofpact_type *type)
6724 {
6725 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6726 if (!strcasecmp(name, NAME)) { \
6727 *type = OFPACT_##ENUM; \
6728 return true; \
6729 }
6730 OFPACTS
6731 #undef OFPACT
6732
6733 return false;
6734 }
6735
6736 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
6737 *
6738 * Returns NULL if successful, otherwise a malloc()'d string describing the
6739 * error. The caller is responsible for freeing the returned string.
6740 *
6741 * If 'outer_action' is specified, indicates that the actions being parsed
6742 * are nested within another action of the type specified in 'outer_action'. */
6743 static char * OVS_WARN_UNUSED_RESULT
6744 ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
6745 enum ofputil_protocol *usable_protocols,
6746 bool allow_instructions, enum ofpact_type outer_action)
6747 {
6748 int prev_inst = -1;
6749 enum ofperr retval;
6750 char *key, *value;
6751 bool drop = false;
6752 char *pos;
6753
6754 pos = str;
6755 while (ofputil_parse_key_value(&pos, &key, &value)) {
6756 enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
6757 enum ofpact_type type;
6758 char *error = NULL;
6759 ofp_port_t port;
6760
6761 if (ofpact_type_from_name(key, &type)) {
6762 error = ofpact_parse(type, value, ofpacts, usable_protocols);
6763 inst = ovs_instruction_type_from_ofpact_type(type);
6764 } else if (!strcasecmp(key, "mod_vlan_vid")) {
6765 error = parse_set_vlan_vid(value, ofpacts, true);
6766 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
6767 error = parse_set_vlan_pcp(value, ofpacts, true);
6768 } else if (!strcasecmp(key, "set_nw_ttl")) {
6769 error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
6770 } else if (!strcasecmp(key, "pop_vlan")) {
6771 error = parse_pop_vlan(ofpacts);
6772 } else if (!strcasecmp(key, "set_tunnel64")) {
6773 error = parse_set_tunnel(value, ofpacts,
6774 NXAST_RAW_SET_TUNNEL64);
6775 } else if (!strcasecmp(key, "load")) {
6776 error = parse_reg_load(value, ofpacts);
6777 } else if (!strcasecmp(key, "bundle_load")) {
6778 error = parse_bundle_load(value, ofpacts);
6779 } else if (!strcasecmp(key, "drop")) {
6780 drop = true;
6781 } else if (!strcasecmp(key, "apply_actions")) {
6782 return xstrdup("apply_actions is the default instruction");
6783 } else if (ofputil_port_from_string(key, &port)) {
6784 ofpact_put_OUTPUT(ofpacts)->port = port;
6785 } else {
6786 return xasprintf("unknown action %s", key);
6787 }
6788 if (error) {
6789 return error;
6790 }
6791
6792 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
6793 if (!allow_instructions) {
6794 return xasprintf("only actions are allowed here (not "
6795 "instruction %s)",
6796 ovs_instruction_name_from_type(inst));
6797 }
6798 if (inst == prev_inst) {
6799 return xasprintf("instruction %s may be specified only once",
6800 ovs_instruction_name_from_type(inst));
6801 }
6802 }
6803 if (prev_inst != -1 && inst < prev_inst) {
6804 return xasprintf("instruction %s must be specified before %s",
6805 ovs_instruction_name_from_type(inst),
6806 ovs_instruction_name_from_type(prev_inst));
6807 }
6808 prev_inst = inst;
6809 }
6810 ofpact_pad(ofpacts);
6811
6812 if (drop && ofpacts->size) {
6813 return xstrdup("\"drop\" must not be accompanied by any other action "
6814 "or instruction");
6815 }
6816
6817 retval = ofpacts_verify(ofpacts->data, ofpacts->size,
6818 (allow_instructions
6819 ? (1u << N_OVS_INSTRUCTIONS) - 1
6820 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS),
6821 outer_action);
6822 if (retval) {
6823 return xstrdup("Incorrect instruction ordering");
6824 }
6825
6826 return NULL;
6827 }
6828
6829 static char * OVS_WARN_UNUSED_RESULT
6830 ofpacts_parse(char *str, struct ofpbuf *ofpacts,
6831 enum ofputil_protocol *usable_protocols, bool allow_instructions,
6832 enum ofpact_type outer_action)
6833 {
6834 uint32_t orig_size = ofpacts->size;
6835 char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
6836 allow_instructions, outer_action);
6837 if (error) {
6838 ofpacts->size = orig_size;
6839 }
6840 return error;
6841 }
6842
6843 static char * OVS_WARN_UNUSED_RESULT
6844 ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
6845 enum ofputil_protocol *usable_protocols,
6846 bool allow_instructions, enum ofpact_type outer_action)
6847 {
6848 char *error, *s;
6849
6850 *usable_protocols = OFPUTIL_P_ANY;
6851
6852 s = xstrdup(s_);
6853 error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions,
6854 outer_action);
6855 free(s);
6856
6857 return error;
6858 }
6859
6860 /* Parses 's' as a set of OpenFlow actions and appends the actions to
6861 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
6862 * that are nested within the action of type 'outer_action'.
6863 *
6864 * Returns NULL if successful, otherwise a malloc()'d string describing the
6865 * error. The caller is responsible for freeing the returned string. */
6866 char * OVS_WARN_UNUSED_RESULT
6867 ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
6868 enum ofputil_protocol *usable_protocols)
6869 {
6870 return ofpacts_parse_copy(s, ofpacts, usable_protocols, false, 0);
6871 }
6872
6873 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
6874 * 'ofpacts'.
6875 *
6876 * Returns NULL if successful, otherwise a malloc()'d string describing the
6877 * error. The caller is responsible for freeing the returned string. */
6878 char * OVS_WARN_UNUSED_RESULT
6879 ofpacts_parse_instructions(const char *s, struct ofpbuf *ofpacts,
6880 enum ofputil_protocol *usable_protocols)
6881 {
6882 return ofpacts_parse_copy(s, ofpacts, usable_protocols, true, 0);
6883 }
6884
6885 const char *
6886 ofpact_name(enum ofpact_type type)
6887 {
6888 switch (type) {
6889 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
6890 OFPACTS
6891 #undef OFPACT
6892 }
6893 return "<unknown>";
6894 }
6895 \f
6896 /* Low-level action decoding and encoding functions. */
6897
6898 /* Everything needed to identify a particular OpenFlow action. */
6899 struct ofpact_hdrs {
6900 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
6901 uint16_t type; /* Type if standard, otherwise subtype. */
6902 uint8_t ofp_version; /* From ofp_header. */
6903 };
6904
6905 /* Information about a particular OpenFlow action. */
6906 struct ofpact_raw_instance {
6907 /* The action's identity. */
6908 struct ofpact_hdrs hdrs;
6909 enum ofp_raw_action_type raw;
6910
6911 /* Looking up the action. */
6912 struct hmap_node decode_node; /* Based on 'hdrs'. */
6913 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
6914
6915 /* The action's encoded size.
6916 *
6917 * If this action is fixed-length, 'min_length' == 'max_length'.
6918 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
6919 * OFP_ACTION_ALIGN) == 65528. */
6920 unsigned short int min_length;
6921 unsigned short int max_length;
6922
6923 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
6924 * offset of that argument from the beginning of the action and 'arg_len'
6925 * its length, both in bytes.
6926 *
6927 * For actions that take other forms, these are both zero. */
6928 unsigned short int arg_ofs;
6929 unsigned short int arg_len;
6930
6931 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
6932 const char *name;
6933
6934 /* If this action is deprecated, a human-readable string with a brief
6935 * explanation. */
6936 const char *deprecation;
6937 };
6938
6939 /* Action header. */
6940 struct ofp_action_header {
6941 /* The meaning of other values of 'type' generally depends on the OpenFlow
6942 * version (see enum ofp_raw_action_type).
6943 *
6944 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
6945 * designates an OpenFlow vendor ID and that the remainder of the action
6946 * structure has a vendor-defined meaning.
6947 */
6948 #define OFPAT_VENDOR 0xffff
6949 ovs_be16 type;
6950
6951 /* Always a multiple of 8. */
6952 ovs_be16 len;
6953
6954 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
6955 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
6956 ovs_be32 vendor;
6957 };
6958 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
6959
6960 /* Header for Nicira-defined actions and for ONF vendor extensions.
6961 *
6962 * This cannot be used as an entirely generic vendor extension action header,
6963 * because OpenFlow does not specify the location or size of the action
6964 * subtype; it just happens that ONF extensions and Nicira extensions share
6965 * this format. */
6966 struct ext_action_header {
6967 ovs_be16 type; /* OFPAT_VENDOR. */
6968 ovs_be16 len; /* At least 16. */
6969 ovs_be32 vendor; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
6970 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
6971 uint8_t pad[6];
6972 };
6973 OFP_ASSERT(sizeof(struct ext_action_header) == 16);
6974
6975 static bool
6976 ofpact_hdrs_equal(const struct ofpact_hdrs *a,
6977 const struct ofpact_hdrs *b)
6978 {
6979 return (a->vendor == b->vendor
6980 && a->type == b->type
6981 && a->ofp_version == b->ofp_version);
6982 }
6983
6984 static uint32_t
6985 ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
6986 {
6987 return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
6988 }
6989
6990 #include "ofp-actions.inc2"
6991
6992 static struct hmap *
6993 ofpact_decode_hmap(void)
6994 {
6995 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6996 static struct hmap hmap;
6997
6998 if (ovsthread_once_start(&once)) {
6999 struct ofpact_raw_instance *inst;
7000
7001 hmap_init(&hmap);
7002 for (inst = all_raw_instances;
7003 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7004 inst++) {
7005 hmap_insert(&hmap, &inst->decode_node,
7006 ofpact_hdrs_hash(&inst->hdrs));
7007 }
7008 ovsthread_once_done(&once);
7009 }
7010 return &hmap;
7011 }
7012
7013 static struct hmap *
7014 ofpact_encode_hmap(void)
7015 {
7016 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7017 static struct hmap hmap;
7018
7019 if (ovsthread_once_start(&once)) {
7020 struct ofpact_raw_instance *inst;
7021
7022 hmap_init(&hmap);
7023 for (inst = all_raw_instances;
7024 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7025 inst++) {
7026 hmap_insert(&hmap, &inst->encode_node,
7027 hash_2words(inst->raw, inst->hdrs.ofp_version));
7028 }
7029 ovsthread_once_done(&once);
7030 }
7031 return &hmap;
7032 }
7033
7034 static enum ofperr
7035 ofpact_decode_raw(enum ofp_version ofp_version,
7036 const struct ofp_action_header *oah, size_t length,
7037 const struct ofpact_raw_instance **instp)
7038 {
7039 const struct ofpact_raw_instance *inst;
7040 struct ofpact_hdrs hdrs;
7041
7042 *instp = NULL;
7043 if (length < sizeof *oah) {
7044 return OFPERR_OFPBAC_BAD_LEN;
7045 }
7046
7047 /* Get base action type. */
7048 if (oah->type == htons(OFPAT_VENDOR)) {
7049 /* Get vendor. */
7050 hdrs.vendor = ntohl(oah->vendor);
7051 if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
7052 /* Get extension subtype. */
7053 const struct ext_action_header *nah;
7054
7055 nah = ALIGNED_CAST(const struct ext_action_header *, oah);
7056 if (length < sizeof *nah) {
7057 return OFPERR_OFPBAC_BAD_LEN;
7058 }
7059 hdrs.type = ntohs(nah->subtype);
7060 } else {
7061 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
7062 hdrs.vendor);
7063 return OFPERR_OFPBAC_BAD_VENDOR;
7064 }
7065 } else {
7066 hdrs.vendor = 0;
7067 hdrs.type = ntohs(oah->type);
7068 }
7069
7070 hdrs.ofp_version = ofp_version;
7071 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
7072 ofpact_decode_hmap()) {
7073 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
7074 *instp = inst;
7075 return 0;
7076 }
7077 }
7078
7079 return (hdrs.vendor
7080 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
7081 : OFPERR_OFPBAC_BAD_TYPE);
7082 }
7083
7084 static enum ofperr
7085 ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7086 enum ofp_raw_action_type *raw, uint64_t *arg)
7087 {
7088 const struct ofp_action_header *oah = buf->data;
7089 const struct ofpact_raw_instance *action;
7090 unsigned int length;
7091 enum ofperr error;
7092
7093 *raw = *arg = 0;
7094 error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
7095 if (error) {
7096 return error;
7097 }
7098
7099 if (action->deprecation) {
7100 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
7101 action->name, ofputil_version_to_string(ofp_version),
7102 action->deprecation);
7103 }
7104
7105 length = ntohs(oah->len);
7106 if (length > buf->size) {
7107 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
7108 "length %"PRIu32, action->name, length, buf->size);
7109 return OFPERR_OFPBAC_BAD_LEN;
7110 }
7111 if (length < action->min_length || length > action->max_length) {
7112 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
7113 "[%hu,%hu]", action->name, length,
7114 action->min_length, action->max_length);
7115 return OFPERR_OFPBAC_BAD_LEN;
7116 }
7117 if (length % 8) {
7118 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
7119 "of 8", action->name, length);
7120 return OFPERR_OFPBAC_BAD_LEN;
7121 }
7122
7123 *raw = action->raw;
7124 *arg = 0;
7125 if (action->arg_len) {
7126 const uint8_t *p;
7127 int i;
7128
7129 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
7130 for (i = 0; i < action->arg_len; i++) {
7131 *arg = (*arg << 8) | p[i];
7132 }
7133 }
7134
7135 ofpbuf_pull(buf, length);
7136
7137 return 0;
7138 }
7139
7140 static const struct ofpact_raw_instance *
7141 ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
7142 {
7143 const struct ofpact_raw_instance *inst;
7144
7145 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
7146 ofpact_encode_hmap()) {
7147 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
7148 return inst;
7149 }
7150 }
7151 OVS_NOT_REACHED();
7152 }
7153
7154 static void *
7155 ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7156 enum ofp_raw_action_type raw, uint64_t arg)
7157 {
7158 const struct ofpact_raw_instance *inst;
7159 struct ofp_action_header *oah;
7160 const struct ofpact_hdrs *hdrs;
7161
7162 inst = ofpact_raw_lookup(ofp_version, raw);
7163 hdrs = &inst->hdrs;
7164
7165 oah = ofpbuf_put_zeros(buf, inst->min_length);
7166 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
7167 oah->len = htons(inst->min_length);
7168 oah->vendor = htonl(hdrs->vendor);
7169
7170 switch (hdrs->vendor) {
7171 case 0:
7172 break;
7173
7174 case NX_VENDOR_ID:
7175 case ONF_VENDOR_ID: {
7176 struct ext_action_header *nah = (struct ext_action_header *) oah;
7177 nah->subtype = htons(hdrs->type);
7178 break;
7179 }
7180
7181 default:
7182 OVS_NOT_REACHED();
7183 }
7184
7185 if (inst->arg_len) {
7186 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
7187 int i;
7188
7189 for (i = 0; i < inst->arg_len; i++) {
7190 *--p = arg;
7191 arg >>= 8;
7192 }
7193 } else {
7194 ovs_assert(!arg);
7195 }
7196
7197 return oah;
7198 }
7199
7200 static void
7201 pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
7202 {
7203 struct ofp_action_header *oah;
7204
7205 ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs, 8));
7206
7207 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
7208 oah->len = htons(openflow->size - start_ofs);
7209 }
7210