]> git.proxmox.com Git - ovs.git/blob - include/openvswitch/ofp-actions.h
ofp-actions: Correct member name for write_actions.
[ovs.git] / include / openvswitch / ofp-actions.h
1 /*
2 * Copyright (c) 2012, 2013, 2014, 2015, 2016 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 #ifndef OPENVSWITCH_OFP_ACTIONS_H
18 #define OPENVSWITCH_OFP_ACTIONS_H 1
19
20 #include <stddef.h>
21 #include <stdint.h>
22 #include "openflow/openflow.h"
23 #include "openflow/nicira-ext.h"
24 #include "openvswitch/meta-flow.h"
25 #include "openvswitch/ofp-util.h"
26 #include "openvswitch/ofp-errors.h"
27 #include "openvswitch/types.h"
28
29 /* List of OVS abstracted actions.
30 *
31 * This macro is used directly only internally by this header, but the list is
32 * still of interest to developers.
33 *
34 * Each OFPACT invocation has the following parameters:
35 *
36 * 1. <ENUM>, used below in the enum definition of OFPACT_<ENUM>, and
37 * elsewhere.
38 *
39 * 2. <STRUCT> corresponding to a structure "struct <STRUCT>", that must be
40 * defined below. This structure must be an abstract definition of the
41 * action. Its first member must have type "struct ofpact" and name
42 * "ofpact". It may be fixed length or end with a flexible array member
43 * (e.g. "int member[];").
44 *
45 * 3. <MEMBER>, which has one of two possible values:
46 *
47 * - If "struct <STRUCT>" is fixed-length, it must be "ofpact".
48 *
49 * - If "struct <STRUCT>" is variable-length, it must be the name of the
50 * flexible array member.
51 *
52 * 4. <NAME>, a quoted string that gives the name of the action, for use in
53 * parsing actions from text.
54 */
55 #define OFPACTS \
56 /* Output. */ \
57 OFPACT(OUTPUT, ofpact_output, ofpact, "output") \
58 OFPACT(GROUP, ofpact_group, ofpact, "group") \
59 OFPACT(CONTROLLER, ofpact_controller, userdata, "controller") \
60 OFPACT(ENQUEUE, ofpact_enqueue, ofpact, "enqueue") \
61 OFPACT(OUTPUT_REG, ofpact_output_reg, ofpact, "output_reg") \
62 OFPACT(BUNDLE, ofpact_bundle, slaves, "bundle") \
63 \
64 /* Header changes. */ \
65 OFPACT(SET_FIELD, ofpact_set_field, ofpact, "set_field") \
66 OFPACT(SET_VLAN_VID, ofpact_vlan_vid, ofpact, "set_vlan_vid") \
67 OFPACT(SET_VLAN_PCP, ofpact_vlan_pcp, ofpact, "set_vlan_pcp") \
68 OFPACT(STRIP_VLAN, ofpact_null, ofpact, "strip_vlan") \
69 OFPACT(PUSH_VLAN, ofpact_null, ofpact, "push_vlan") \
70 OFPACT(SET_ETH_SRC, ofpact_mac, ofpact, "mod_dl_src") \
71 OFPACT(SET_ETH_DST, ofpact_mac, ofpact, "mod_dl_dst") \
72 OFPACT(SET_IPV4_SRC, ofpact_ipv4, ofpact, "mod_nw_src") \
73 OFPACT(SET_IPV4_DST, ofpact_ipv4, ofpact, "mod_nw_dst") \
74 OFPACT(SET_IP_DSCP, ofpact_dscp, ofpact, "mod_nw_tos") \
75 OFPACT(SET_IP_ECN, ofpact_ecn, ofpact, "mod_nw_ecn") \
76 OFPACT(SET_IP_TTL, ofpact_ip_ttl, ofpact, "mod_nw_ttl") \
77 OFPACT(SET_L4_SRC_PORT, ofpact_l4_port, ofpact, "mod_tp_src") \
78 OFPACT(SET_L4_DST_PORT, ofpact_l4_port, ofpact, "mod_tp_dst") \
79 OFPACT(REG_MOVE, ofpact_reg_move, ofpact, "move") \
80 OFPACT(STACK_PUSH, ofpact_stack, ofpact, "push") \
81 OFPACT(STACK_POP, ofpact_stack, ofpact, "pop") \
82 OFPACT(DEC_TTL, ofpact_cnt_ids, cnt_ids, "dec_ttl") \
83 OFPACT(SET_MPLS_LABEL, ofpact_mpls_label, ofpact, "set_mpls_label") \
84 OFPACT(SET_MPLS_TC, ofpact_mpls_tc, ofpact, "set_mpls_tc") \
85 OFPACT(SET_MPLS_TTL, ofpact_mpls_ttl, ofpact, "set_mpls_ttl") \
86 OFPACT(DEC_MPLS_TTL, ofpact_null, ofpact, "dec_mpls_ttl") \
87 OFPACT(PUSH_MPLS, ofpact_push_mpls, ofpact, "push_mpls") \
88 OFPACT(POP_MPLS, ofpact_pop_mpls, ofpact, "pop_mpls") \
89 \
90 /* Metadata. */ \
91 OFPACT(SET_TUNNEL, ofpact_tunnel, ofpact, "set_tunnel") \
92 OFPACT(SET_QUEUE, ofpact_queue, ofpact, "set_queue") \
93 OFPACT(POP_QUEUE, ofpact_null, ofpact, "pop_queue") \
94 OFPACT(FIN_TIMEOUT, ofpact_fin_timeout, ofpact, "fin_timeout") \
95 \
96 /* Flow table interaction. */ \
97 OFPACT(RESUBMIT, ofpact_resubmit, ofpact, "resubmit") \
98 OFPACT(LEARN, ofpact_learn, specs, "learn") \
99 OFPACT(CONJUNCTION, ofpact_conjunction, ofpact, "conjunction") \
100 \
101 /* Arithmetic. */ \
102 OFPACT(MULTIPATH, ofpact_multipath, ofpact, "multipath") \
103 \
104 /* Other. */ \
105 OFPACT(NOTE, ofpact_note, data, "note") \
106 OFPACT(EXIT, ofpact_null, ofpact, "exit") \
107 OFPACT(SAMPLE, ofpact_sample, ofpact, "sample") \
108 OFPACT(UNROLL_XLATE, ofpact_unroll_xlate, ofpact, "unroll_xlate") \
109 OFPACT(CT, ofpact_conntrack, ofpact, "ct") \
110 OFPACT(NAT, ofpact_nat, ofpact, "nat") \
111 OFPACT(OUTPUT_TRUNC, ofpact_output_trunc,ofpact, "output_trunc") \
112 \
113 /* Debugging actions. \
114 * \
115 * These are intentionally undocumented, subject to change, and \
116 * only accepted if ovs-vswitchd is started with --enable-dummy. */ \
117 OFPACT(DEBUG_RECIRC, ofpact_null, ofpact, "debug_recirc") \
118 \
119 /* Instructions. */ \
120 OFPACT(METER, ofpact_meter, ofpact, "meter") \
121 OFPACT(CLEAR_ACTIONS, ofpact_null, ofpact, "clear_actions") \
122 OFPACT(WRITE_ACTIONS, ofpact_nest, actions, "write_actions") \
123 OFPACT(WRITE_METADATA, ofpact_metadata, ofpact, "write_metadata") \
124 OFPACT(GOTO_TABLE, ofpact_goto_table, ofpact, "goto_table")
125
126 /* enum ofpact_type, with a member OFPACT_<ENUM> for each action. */
127 enum OVS_PACKED_ENUM ofpact_type {
128 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) OFPACT_##ENUM,
129 OFPACTS
130 #undef OFPACT
131 };
132
133 /* Define N_OFPACTS to the number of types of ofpacts. */
134 enum {
135 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) + 1
136 N_OFPACTS = OFPACTS
137 #undef OFPACT
138 };
139
140 /* Header for an action.
141 *
142 * Each action is a structure "struct ofpact_*" that begins with "struct
143 * ofpact", usually followed by other data that describes the action. Actions
144 * are padded out to a multiple of OFPACT_ALIGNTO bytes in length.
145 *
146 * The 'raw' member is special:
147 *
148 * - Most "struct ofpact"s correspond to one particular kind of OpenFlow
149 * action, at least in a given OpenFlow version. For example,
150 * OFPACT_SET_VLAN_VID corresponds to OFPAT10_SET_VLAN_VID in OpenFlow
151 * 1.0.
152 *
153 * For such actions, the 'raw' member is not meaningful and generally
154 * should be zero.
155 *
156 * - A few "struct ofpact"s correspond to multiple OpenFlow actions. For
157 * example, OFPACT_SET_TUNNEL can be NXAST_SET_TUNNEL or
158 * NXAST_SET_TUNNEL64. In these cases, if the "struct ofpact" originated
159 * from OpenFlow, then we want to make sure that, if it gets translated
160 * back to OpenFlow later, it is translated back to the same action type.
161 * (Otherwise, we'd violate the promise made in DESIGN, in the "Action
162 * Reproduction" section.)
163 *
164 * For such actions, the 'raw' member should be the "enum ofp_raw_action"
165 * originally extracted from the OpenFlow action. (If the action didn't
166 * originate from OpenFlow, then setting 'raw' to zero should be fine:
167 * code to translate the ofpact to OpenFlow must tolerate this case.)
168 */
169 struct ofpact {
170 /* We want the space advantage of an 8-bit type here on every
171 * implementation, without giving up the advantage of having a useful type
172 * on implementations that support packed enums. */
173 #ifdef HAVE_PACKED_ENUM
174 enum ofpact_type type; /* OFPACT_*. */
175 #else
176 uint8_t type; /* OFPACT_* */
177 #endif
178
179 uint8_t raw; /* Original type when added, if any. */
180 uint16_t len; /* Length of the action, in bytes, including
181 * struct ofpact, excluding padding. */
182 };
183 BUILD_ASSERT_DECL(sizeof(struct ofpact) == 4);
184
185 /* Alignment. */
186 #define OFPACT_ALIGNTO 8
187 #define OFPACT_ALIGN(SIZE) ROUND_UP(SIZE, OFPACT_ALIGNTO)
188
189 /* Expands to an anonymous union that contains:
190 *
191 * - MEMBERS in a nested anonymous struct.
192 *
193 * - An array as large as MEMBERS plus padding to a multiple of 8 bytes.
194 *
195 * The effect is to pad MEMBERS to a multiple of 8 bytes. */
196 #define OFPACT_PADDED_MEMBERS(MEMBERS) \
197 union { \
198 struct { MEMBERS }; \
199 uint8_t pad[OFPACT_ALIGN(sizeof(struct { MEMBERS }))]; \
200 }
201
202 /* Returns the ofpact following 'ofpact'. */
203 static inline struct ofpact *
204 ofpact_next(const struct ofpact *ofpact)
205 {
206 return (void *) ((uint8_t *) ofpact + OFPACT_ALIGN(ofpact->len));
207 }
208
209 struct ofpact *ofpact_next_flattened(const struct ofpact *);
210
211 static inline struct ofpact *
212 ofpact_end(const struct ofpact *ofpacts, size_t ofpacts_len)
213 {
214 return (void *) ((uint8_t *) ofpacts + ofpacts_len);
215 }
216
217 static inline const struct ofpact *
218 ofpact_find_type(const struct ofpact *a, enum ofpact_type type,
219 const struct ofpact * const end)
220 {
221 while (a < end) {
222 if (a->type == type) {
223 return a;
224 }
225 a = ofpact_next(a);
226 }
227 return NULL;
228 }
229
230 #define OFPACT_FIND_TYPE(A, TYPE, END) \
231 ofpact_get_##TYPE##_nullable(ofpact_find_type(A, OFPACT_##TYPE, END))
232
233 static inline const struct ofpact *
234 ofpact_find_type_flattened(const struct ofpact *a, enum ofpact_type type,
235 const struct ofpact * const end)
236 {
237 while (a < end) {
238 if (a->type == type) {
239 return a;
240 }
241 a = ofpact_next_flattened(a);
242 }
243 return NULL;
244 }
245
246 #define OFPACT_FIND_TYPE_FLATTENED(A, TYPE, END) \
247 ofpact_get_##TYPE##_nullable( \
248 ofpact_find_type_flattened(A, OFPACT_##TYPE, END))
249
250 /* Assigns POS to each ofpact, in turn, in the OFPACTS_LEN bytes of ofpacts
251 * starting at OFPACTS. */
252 #define OFPACT_FOR_EACH(POS, OFPACTS, OFPACTS_LEN) \
253 for ((POS) = (OFPACTS); (POS) < ofpact_end(OFPACTS, OFPACTS_LEN); \
254 (POS) = ofpact_next(POS))
255
256 #define OFPACT_FOR_EACH_TYPE(POS, TYPE, OFPACTS, OFPACTS_LEN) \
257 for ((POS) = OFPACT_FIND_TYPE(OFPACTS, TYPE, \
258 ofpact_end(OFPACTS, OFPACTS_LEN)); \
259 (POS); \
260 (POS) = OFPACT_FIND_TYPE(ofpact_next(&(POS)->ofpact), TYPE, \
261 ofpact_end(OFPACTS, OFPACTS_LEN)))
262
263 /* Assigns POS to each ofpact, in turn, in the OFPACTS_LEN bytes of ofpacts
264 * starting at OFPACTS.
265 *
266 * For ofpacts that contain nested ofpacts, this visits each of the inner
267 * ofpacts as well. */
268 #define OFPACT_FOR_EACH_FLATTENED(POS, OFPACTS, OFPACTS_LEN) \
269 for ((POS) = (OFPACTS); (POS) < ofpact_end(OFPACTS, OFPACTS_LEN); \
270 (POS) = ofpact_next_flattened(POS))
271
272 #define OFPACT_FOR_EACH_TYPE_FLATTENED(POS, TYPE, OFPACTS, OFPACTS_LEN) \
273 for ((POS) = OFPACT_FIND_TYPE_FLATTENED(OFPACTS, TYPE, \
274 ofpact_end(OFPACTS, OFPACTS_LEN)); \
275 (POS); \
276 (POS) = OFPACT_FIND_TYPE_FLATTENED( \
277 ofpact_next_flattened(&(POS)->ofpact), TYPE, \
278 ofpact_end(OFPACTS, OFPACTS_LEN)))
279 \f
280 /* Action structure for each OFPACT_*. */
281
282 /* OFPACT_STRIP_VLAN, OFPACT_POP_QUEUE, OFPACT_EXIT, OFPACT_CLEAR_ACTIONS.
283 *
284 * Used for OFPAT10_STRIP_VLAN, NXAST_POP_QUEUE, NXAST_EXIT,
285 * OFPAT11_POP_VLAN, OFPIT11_CLEAR_ACTIONS.
286 *
287 * Action structure for actions that do not have any extra data beyond the
288 * action type. */
289 struct ofpact_null {
290 struct ofpact ofpact;
291 };
292
293 /* OFPACT_OUTPUT.
294 *
295 * Used for OFPAT10_OUTPUT. */
296 struct ofpact_output {
297 struct ofpact ofpact;
298 ofp_port_t port; /* Output port. */
299 uint16_t max_len; /* Max send len, for port OFPP_CONTROLLER. */
300 };
301
302 /* OFPACT_CONTROLLER.
303 *
304 * Used for NXAST_CONTROLLER. */
305 struct ofpact_controller {
306 OFPACT_PADDED_MEMBERS(
307 struct ofpact ofpact;
308 uint16_t max_len; /* Max length to send to controller. */
309 uint16_t controller_id; /* Controller ID to send packet-in. */
310 enum ofp_packet_in_reason reason; /* Reason to put in packet-in. */
311
312 /* If true, this action freezes packet traversal of the OpenFlow
313 * tables and adds a continuation to the packet-in message, that
314 * a controller can use to resume that traversal. */
315 bool pause;
316
317 /* Arbitrary data to include in the packet-in message (currently,
318 * only in NXT_PACKET_IN2). */
319 uint16_t userdata_len;
320 );
321 uint8_t userdata[0];
322 };
323
324 /* OFPACT_ENQUEUE.
325 *
326 * Used for OFPAT10_ENQUEUE. */
327 struct ofpact_enqueue {
328 struct ofpact ofpact;
329 ofp_port_t port;
330 uint32_t queue;
331 };
332
333 /* OFPACT_OUTPUT_REG.
334 *
335 * Used for NXAST_OUTPUT_REG. */
336 struct ofpact_output_reg {
337 struct ofpact ofpact;
338 uint16_t max_len;
339 struct mf_subfield src;
340 };
341
342 /* OFPACT_OUTPUT_TRUNC.
343 *
344 * Used for NXAST_OUTPUT_TRUNC. */
345 struct ofpact_output_trunc {
346 struct ofpact ofpact;
347 ofp_port_t port; /* Output port. */
348 uint32_t max_len; /* Max send len. */
349 };
350
351 /* Bundle slave choice algorithm to apply.
352 *
353 * In the descriptions below, 'slaves' is the list of possible slaves in the
354 * order they appear in the OpenFlow action. */
355 enum nx_bd_algorithm {
356 /* Chooses the first live slave listed in the bundle.
357 *
358 * O(n_slaves) performance. */
359 NX_BD_ALG_ACTIVE_BACKUP = 0,
360
361 /* Highest Random Weight.
362 *
363 * for i in [0,n_slaves):
364 * weights[i] = hash(flow, i)
365 * slave = { slaves[i] such that weights[i] >= weights[j] for all j != i }
366 *
367 * Redistributes 1/n_slaves of traffic when a slave's liveness changes.
368 * O(n_slaves) performance.
369 *
370 * Uses the 'fields' and 'basis' parameters. */
371 NX_BD_ALG_HRW = 1
372 };
373
374 /* OFPACT_BUNDLE.
375 *
376 * Used for NXAST_BUNDLE. */
377 struct ofpact_bundle {
378 struct ofpact ofpact;
379
380 /* Slave choice algorithm to apply to hash value. */
381 enum nx_bd_algorithm algorithm;
382
383 /* What fields to hash and how. */
384 enum nx_hash_fields fields;
385 uint16_t basis; /* Universal hash parameter. */
386
387 struct mf_subfield dst;
388
389 /* Slaves for output. */
390 unsigned int n_slaves;
391 ofp_port_t slaves[];
392 };
393
394 /* OFPACT_SET_VLAN_VID.
395 *
396 * We keep track if vlan was present at action validation time to avoid a
397 * PUSH_VLAN when translating to OpenFlow 1.1+.
398 *
399 * We also keep the originating OFPUTIL action code in ofpact.compat.
400 *
401 * Used for OFPAT10_SET_VLAN_VID and OFPAT11_SET_VLAN_VID. */
402 struct ofpact_vlan_vid {
403 struct ofpact ofpact;
404 uint16_t vlan_vid; /* VLAN VID in low 12 bits, 0 in other bits. */
405 bool push_vlan_if_needed; /* OF 1.0 semantics if true. */
406 bool flow_has_vlan; /* VLAN present at action validation time? */
407 };
408
409 /* OFPACT_SET_VLAN_PCP.
410 *
411 * We keep track if vlan was present at action validation time to avoid a
412 * PUSH_VLAN when translating to OpenFlow 1.1+.
413 *
414 * We also keep the originating OFPUTIL action code in ofpact.compat.
415 *
416 * Used for OFPAT10_SET_VLAN_PCP and OFPAT11_SET_VLAN_PCP. */
417 struct ofpact_vlan_pcp {
418 struct ofpact ofpact;
419 uint8_t vlan_pcp; /* VLAN PCP in low 3 bits, 0 in other bits. */
420 bool push_vlan_if_needed; /* OF 1.0 semantics if true. */
421 bool flow_has_vlan; /* VLAN present at action validation time? */
422 };
423
424 /* OFPACT_SET_ETH_SRC, OFPACT_SET_ETH_DST.
425 *
426 * Used for OFPAT10_SET_DL_SRC, OFPAT10_SET_DL_DST. */
427 struct ofpact_mac {
428 struct ofpact ofpact;
429 struct eth_addr mac;
430 };
431
432 /* OFPACT_SET_IPV4_SRC, OFPACT_SET_IPV4_DST.
433 *
434 * Used for OFPAT10_SET_NW_SRC, OFPAT10_SET_NW_DST. */
435 struct ofpact_ipv4 {
436 struct ofpact ofpact;
437 ovs_be32 ipv4;
438 };
439
440 /* OFPACT_SET_IP_DSCP.
441 *
442 * Used for OFPAT10_SET_NW_TOS. */
443 struct ofpact_dscp {
444 struct ofpact ofpact;
445 uint8_t dscp; /* DSCP in high 6 bits, rest ignored. */
446 };
447
448 /* OFPACT_SET_IP_ECN.
449 *
450 * Used for OFPAT11_SET_NW_ECN. */
451 struct ofpact_ecn {
452 struct ofpact ofpact;
453 uint8_t ecn; /* ECN in low 2 bits, rest ignored. */
454 };
455
456 /* OFPACT_SET_IP_TTL.
457 *
458 * Used for OFPAT11_SET_NW_TTL. */
459 struct ofpact_ip_ttl {
460 struct ofpact ofpact;
461 uint8_t ttl;
462 };
463
464 /* OFPACT_SET_L4_SRC_PORT, OFPACT_SET_L4_DST_PORT.
465 *
466 * Used for OFPAT10_SET_TP_SRC, OFPAT10_SET_TP_DST. */
467 struct ofpact_l4_port {
468 struct ofpact ofpact;
469 uint16_t port; /* TCP, UDP or SCTP port number. */
470 uint8_t flow_ip_proto; /* IP proto from corresponding match, or 0 */
471 };
472
473 /* OFPACT_REG_MOVE.
474 *
475 * Used for NXAST_REG_MOVE. */
476 struct ofpact_reg_move {
477 struct ofpact ofpact;
478 struct mf_subfield src;
479 struct mf_subfield dst;
480 };
481
482 /* OFPACT_STACK_PUSH.
483 *
484 * Used for NXAST_STACK_PUSH and NXAST_STACK_POP. */
485 struct ofpact_stack {
486 struct ofpact ofpact;
487 struct mf_subfield subfield;
488 };
489
490 /* OFPACT_SET_FIELD.
491 *
492 * Used for NXAST_REG_LOAD and OFPAT12_SET_FIELD. */
493 struct ofpact_set_field {
494 struct ofpact ofpact;
495 const struct mf_field *field;
496 bool flow_has_vlan; /* VLAN present at action validation time. */
497 union mf_value value;
498 union mf_value mask;
499 };
500
501 /* OFPACT_PUSH_VLAN/MPLS/PBB
502 *
503 * Used for NXAST_PUSH_MPLS, OFPAT11_PUSH_MPLS. */
504 struct ofpact_push_mpls {
505 struct ofpact ofpact;
506 ovs_be16 ethertype;
507 };
508
509 /* OFPACT_POP_MPLS
510 *
511 * Used for NXAST_POP_MPLS, OFPAT11_POP_MPLS.. */
512 struct ofpact_pop_mpls {
513 struct ofpact ofpact;
514 ovs_be16 ethertype;
515 };
516
517 /* OFPACT_SET_TUNNEL.
518 *
519 * Used for NXAST_SET_TUNNEL, NXAST_SET_TUNNEL64. */
520 struct ofpact_tunnel {
521 struct ofpact ofpact;
522 uint64_t tun_id;
523 };
524
525 /* OFPACT_SET_QUEUE.
526 *
527 * Used for NXAST_SET_QUEUE. */
528 struct ofpact_queue {
529 struct ofpact ofpact;
530 uint32_t queue_id;
531 };
532
533 /* OFPACT_FIN_TIMEOUT.
534 *
535 * Used for NXAST_FIN_TIMEOUT. */
536 struct ofpact_fin_timeout {
537 struct ofpact ofpact;
538 uint16_t fin_idle_timeout;
539 uint16_t fin_hard_timeout;
540 };
541
542 /* OFPACT_WRITE_METADATA.
543 *
544 * Used for NXAST_WRITE_METADATA. */
545 struct ofpact_metadata {
546 struct ofpact ofpact;
547 ovs_be64 metadata;
548 ovs_be64 mask;
549 };
550
551 /* OFPACT_METER.
552 *
553 * Used for OFPIT13_METER. */
554 struct ofpact_meter {
555 struct ofpact ofpact;
556 uint32_t meter_id;
557 };
558
559 /* OFPACT_WRITE_ACTIONS.
560 *
561 * Used for OFPIT11_WRITE_ACTIONS. */
562 struct ofpact_nest {
563 OFPACT_PADDED_MEMBERS(struct ofpact ofpact;);
564 struct ofpact actions[];
565 };
566 BUILD_ASSERT_DECL(offsetof(struct ofpact_nest, actions) % OFPACT_ALIGNTO == 0);
567 BUILD_ASSERT_DECL(offsetof(struct ofpact_nest, actions)
568 == sizeof(struct ofpact_nest));
569
570 /* Bits for 'flags' in struct nx_action_conntrack.
571 *
572 * If NX_CT_F_COMMIT is set, then the connection entry is moved from the
573 * unconfirmed to confirmed list in the tracker. */
574 enum nx_conntrack_flags {
575 NX_CT_F_COMMIT = 1 << 0,
576 };
577
578 /* Magic value for struct nx_action_conntrack 'recirc_table' field, to specify
579 * that the packet should not be recirculated. */
580 #define NX_CT_RECIRC_NONE OFPTT_ALL
581
582 #if !defined(IPPORT_FTP)
583 #define IPPORT_FTP 21
584 #endif
585
586 /* OFPACT_CT.
587 *
588 * Used for NXAST_CT. */
589 struct ofpact_conntrack {
590 OFPACT_PADDED_MEMBERS(
591 struct ofpact ofpact;
592 uint16_t flags;
593 uint16_t zone_imm;
594 struct mf_subfield zone_src;
595 uint16_t alg;
596 uint8_t recirc_table;
597 );
598 struct ofpact actions[0];
599 };
600 BUILD_ASSERT_DECL(offsetof(struct ofpact_conntrack, actions)
601 % OFPACT_ALIGNTO == 0);
602 BUILD_ASSERT_DECL(offsetof(struct ofpact_conntrack, actions)
603 == sizeof(struct ofpact_conntrack));
604
605 static inline size_t
606 ofpact_ct_get_action_len(const struct ofpact_conntrack *oc)
607 {
608 return oc->ofpact.len - offsetof(struct ofpact_conntrack, actions);
609 }
610
611 static inline size_t
612 ofpact_nest_get_action_len(const struct ofpact_nest *on)
613 {
614 return on->ofpact.len - offsetof(struct ofpact_nest, actions);
615 }
616
617 void ofpacts_execute_action_set(struct ofpbuf *action_list,
618 const struct ofpbuf *action_set);
619
620 /* Bits for 'flags' in struct nx_action_nat.
621 */
622 enum nx_nat_flags {
623 NX_NAT_F_SRC = 1 << 0, /* Mutually exclusive with NX_NAT_F_DST. */
624 NX_NAT_F_DST = 1 << 1,
625 NX_NAT_F_PERSISTENT = 1 << 2,
626 NX_NAT_F_PROTO_HASH = 1 << 3, /* Mutually exclusive with PROTO_RANDOM. */
627 NX_NAT_F_PROTO_RANDOM = 1 << 4,
628 NX_NAT_F_MASK = (NX_NAT_F_SRC | NX_NAT_F_DST | NX_NAT_F_PERSISTENT | NX_NAT_F_PROTO_HASH | NX_NAT_F_PROTO_RANDOM)
629 };
630
631 /* OFPACT_NAT.
632 *
633 * Used for NXAST_NAT. */
634 struct ofpact_nat {
635 struct ofpact ofpact;
636 uint8_t range_af; /* AF_UNSPEC, AF_INET, or AF_INET6 */
637 uint16_t flags; /* NX_NAT_F_* */
638 struct {
639 struct {
640 uint16_t min;
641 uint16_t max;
642 } proto;
643 union {
644 struct {
645 ovs_be32 min;
646 ovs_be32 max;
647 } ipv4;
648 struct {
649 struct in6_addr min;
650 struct in6_addr max;
651 } ipv6;
652 } addr;
653 } range;
654 };
655
656
657 /* OFPACT_RESUBMIT.
658 *
659 * Used for NXAST_RESUBMIT, NXAST_RESUBMIT_TABLE. */
660 struct ofpact_resubmit {
661 struct ofpact ofpact;
662 ofp_port_t in_port;
663 uint8_t table_id;
664 };
665
666 /* Part of struct ofpact_learn, below. */
667 struct ofpact_learn_spec {
668 int n_bits; /* Number of bits in source and dest. */
669
670 int src_type; /* One of NX_LEARN_SRC_*. */
671 struct mf_subfield src; /* NX_LEARN_SRC_FIELD only. */
672 union mf_subvalue src_imm; /* NX_LEARN_SRC_IMMEDIATE only. */
673
674 int dst_type; /* One of NX_LEARN_DST_*. */
675 struct mf_subfield dst; /* NX_LEARN_DST_MATCH, NX_LEARN_DST_LOAD only. */
676 };
677
678
679 /* Bits for 'flags' in struct nx_action_learn.
680 *
681 * If NX_LEARN_F_SEND_FLOW_REM is set, then the learned flows will have their
682 * OFPFF_SEND_FLOW_REM flag set.
683 *
684 * If NX_LEARN_F_DELETE_LEARNED is set, then removing this action will delete
685 * all the flows from the learn action's 'table_id' that have the learn
686 * action's 'cookie'. Important points:
687 *
688 * - The deleted flows include those created by this action, those created
689 * by other learn actions with the same 'table_id' and 'cookie', those
690 * created by flow_mod requests by a controller in the specified table
691 * with the specified cookie, and those created through any other
692 * means.
693 *
694 * - If multiple flows specify "learn" actions with
695 * NX_LEARN_F_DELETE_LEARNED with the same 'table_id' and 'cookie', then
696 * no deletion occurs until all of those "learn" actions are deleted.
697 *
698 * - Deleting a flow that contains a learn action is the most obvious way
699 * to delete a learn action. Modifying a flow's actions, or replacing it
700 * by a new flow, can also delete a learn action. Finally, replacing a
701 * learn action with NX_LEARN_F_DELETE_LEARNED with a learn action
702 * without that flag also effectively deletes the learn action and can
703 * trigger flow deletion.
704 *
705 * NX_LEARN_F_DELETE_LEARNED was added in Open vSwitch 2.4. */
706 enum nx_learn_flags {
707 NX_LEARN_F_SEND_FLOW_REM = 1 << 0,
708 NX_LEARN_F_DELETE_LEARNED = 1 << 1,
709 };
710
711 #define NX_LEARN_N_BITS_MASK 0x3ff
712
713 #define NX_LEARN_SRC_FIELD (0 << 13) /* Copy from field. */
714 #define NX_LEARN_SRC_IMMEDIATE (1 << 13) /* Copy from immediate value. */
715 #define NX_LEARN_SRC_MASK (1 << 13)
716
717 #define NX_LEARN_DST_MATCH (0 << 11) /* Add match criterion. */
718 #define NX_LEARN_DST_LOAD (1 << 11) /* Add NXAST_REG_LOAD action. */
719 #define NX_LEARN_DST_OUTPUT (2 << 11) /* Add OFPAT_OUTPUT action. */
720 #define NX_LEARN_DST_RESERVED (3 << 11) /* Not yet defined. */
721 #define NX_LEARN_DST_MASK (3 << 11)
722
723 /* OFPACT_LEARN.
724 *
725 * Used for NXAST_LEARN. */
726 struct ofpact_learn {
727 struct ofpact ofpact;
728
729 uint16_t idle_timeout; /* Idle time before discarding (seconds). */
730 uint16_t hard_timeout; /* Max time before discarding (seconds). */
731 uint16_t priority; /* Priority level of flow entry. */
732 uint8_t table_id; /* Table to insert flow entry. */
733 ovs_be64 cookie; /* Cookie for new flow. */
734 enum nx_learn_flags flags; /* NX_LEARN_F_*. */
735 uint16_t fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
736 uint16_t fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
737
738 unsigned int n_specs;
739 struct ofpact_learn_spec specs[];
740 };
741
742 /* Multipath link choice algorithm to apply.
743 *
744 * In the descriptions below, 'n_links' is max_link + 1. */
745 enum nx_mp_algorithm {
746 /* link = hash(flow) % n_links.
747 *
748 * Redistributes all traffic when n_links changes. O(1) performance. See
749 * RFC 2992.
750 *
751 * Use UINT16_MAX for max_link to get a raw hash value. */
752 NX_MP_ALG_MODULO_N = 0,
753
754 /* link = hash(flow) / (MAX_HASH / n_links).
755 *
756 * Redistributes between one-quarter and one-half of traffic when n_links
757 * changes. O(1) performance. See RFC 2992.
758 */
759 NX_MP_ALG_HASH_THRESHOLD = 1,
760
761 /* Highest Random Weight.
762 *
763 * for i in [0,n_links):
764 * weights[i] = hash(flow, i)
765 * link = { i such that weights[i] >= weights[j] for all j != i }
766 *
767 * Redistributes 1/n_links of traffic when n_links changes. O(n_links)
768 * performance. If n_links is greater than a threshold (currently 64, but
769 * subject to change), Open vSwitch will substitute another algorithm
770 * automatically. See RFC 2992. */
771 NX_MP_ALG_HRW = 2,
772
773 /* Iterative Hash.
774 *
775 * i = 0
776 * repeat:
777 * i = i + 1
778 * link = hash(flow, i) % arg
779 * while link > max_link
780 *
781 * Redistributes 1/n_links of traffic when n_links changes. O(1)
782 * performance when arg/max_link is bounded by a constant.
783 *
784 * Redistributes all traffic when arg changes.
785 *
786 * arg must be greater than max_link and for best performance should be no
787 * more than approximately max_link * 2. If arg is outside the acceptable
788 * range, Open vSwitch will automatically substitute the least power of 2
789 * greater than max_link.
790 *
791 * This algorithm is specific to Open vSwitch.
792 */
793 NX_MP_ALG_ITER_HASH = 3,
794 };
795
796 /* OFPACT_CONJUNCTION.
797 *
798 * Used for NXAST_CONJUNCTION. */
799 struct ofpact_conjunction {
800 struct ofpact ofpact;
801 uint8_t clause;
802 uint8_t n_clauses;
803 uint32_t id;
804 };
805
806 /* OFPACT_MULTIPATH.
807 *
808 * Used for NXAST_MULTIPATH. */
809 struct ofpact_multipath {
810 struct ofpact ofpact;
811
812 /* What fields to hash and how. */
813 enum nx_hash_fields fields;
814 uint16_t basis; /* Universal hash parameter. */
815
816 /* Multipath link choice algorithm to apply to hash value. */
817 enum nx_mp_algorithm algorithm;
818 uint16_t max_link; /* Number of output links, minus 1. */
819 uint32_t arg; /* Algorithm-specific argument. */
820
821 /* Where to store the result. */
822 struct mf_subfield dst;
823 };
824
825 /* OFPACT_NOTE.
826 *
827 * Used for NXAST_NOTE. */
828 struct ofpact_note {
829 struct ofpact ofpact;
830 size_t length;
831 uint8_t data[];
832 };
833
834 /* OFPACT_SAMPLE.
835 *
836 * Used for NXAST_SAMPLE and NXAST_SAMPLE2. */
837 struct ofpact_sample {
838 struct ofpact ofpact;
839 uint16_t probability; /* Always positive. */
840 uint32_t collector_set_id;
841 uint32_t obs_domain_id;
842 uint32_t obs_point_id;
843 ofp_port_t sampling_port;
844 };
845
846 /* OFPACT_DEC_TTL.
847 *
848 * Used for OFPAT11_DEC_NW_TTL, NXAST_DEC_TTL and NXAST_DEC_TTL_CNT_IDS. */
849 struct ofpact_cnt_ids {
850 struct ofpact ofpact;
851
852 /* Controller ids. */
853 unsigned int n_controllers;
854 uint16_t cnt_ids[];
855 };
856
857 /* OFPACT_SET_MPLS_LABEL.
858 *
859 * Used for OFPAT11_SET_MPLS_LABEL and NXAST_SET_MPLS_LABEL */
860 struct ofpact_mpls_label {
861 struct ofpact ofpact;
862
863 ovs_be32 label;
864 };
865
866 /* OFPACT_SET_MPLS_TC.
867 *
868 * Used for OFPAT11_SET_MPLS_TC and NXAST_SET_MPLS_TC */
869 struct ofpact_mpls_tc {
870 struct ofpact ofpact;
871
872 uint8_t tc;
873 };
874
875 /* OFPACT_SET_MPLS_TTL.
876 *
877 * Used for OFPAT11_SET_MPLS_TTL and NXAST_SET_MPLS_TTL */
878 struct ofpact_mpls_ttl {
879 struct ofpact ofpact;
880
881 uint8_t ttl;
882 };
883
884 /* OFPACT_GOTO_TABLE
885 *
886 * Used for OFPIT11_GOTO_TABLE */
887 struct ofpact_goto_table {
888 struct ofpact ofpact;
889 uint8_t table_id;
890 };
891
892 /* OFPACT_GROUP.
893 *
894 * Used for OFPAT11_GROUP. */
895 struct ofpact_group {
896 struct ofpact ofpact;
897 uint32_t group_id;
898 };
899
900 /* OFPACT_UNROLL_XLATE.
901 *
902 * Used only internally. */
903 struct ofpact_unroll_xlate {
904 struct ofpact ofpact;
905
906 /* Metadata in xlate context, visible to controller via PACKET_INs. */
907 uint8_t rule_table_id; /* 0xFF if none. */
908 ovs_be64 rule_cookie; /* OVS_BE64_MAX if none. */
909 };
910
911 /* Converting OpenFlow to ofpacts. */
912 enum ofperr ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
913 unsigned int actions_len,
914 enum ofp_version version,
915 struct ofpbuf *ofpacts);
916 enum ofperr ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
917 unsigned int instructions_len,
918 enum ofp_version version,
919 struct ofpbuf *ofpacts);
920 enum ofperr ofpacts_check(struct ofpact[], size_t ofpacts_len,
921 struct flow *, ofp_port_t max_ports,
922 uint8_t table_id, uint8_t n_tables,
923 enum ofputil_protocol *usable_protocols);
924 enum ofperr ofpacts_check_consistency(struct ofpact[], size_t ofpacts_len,
925 struct flow *, ofp_port_t max_ports,
926 uint8_t table_id, uint8_t n_tables,
927 enum ofputil_protocol usable_protocols);
928 enum ofperr ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports);
929
930 /* Converting ofpacts to OpenFlow. */
931 size_t ofpacts_put_openflow_actions(const struct ofpact[], size_t ofpacts_len,
932 struct ofpbuf *openflow, enum ofp_version);
933 void ofpacts_put_openflow_instructions(const struct ofpact[],
934 size_t ofpacts_len,
935 struct ofpbuf *openflow,
936 enum ofp_version ofp_version);
937
938 /* Sets of supported actions. */
939 ovs_be32 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version);
940 uint64_t ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version);
941 void ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *);
942
943 /* Working with ofpacts. */
944 bool ofpacts_output_to_port(const struct ofpact[], size_t ofpacts_len,
945 ofp_port_t port);
946 bool ofpacts_output_to_group(const struct ofpact[], size_t ofpacts_len,
947 uint32_t group_id);
948 bool ofpacts_equal(const struct ofpact a[], size_t a_len,
949 const struct ofpact b[], size_t b_len);
950 const struct mf_field *ofpact_get_mf_dst(const struct ofpact *ofpact);
951 uint32_t ofpacts_get_meter(const struct ofpact[], size_t ofpacts_len);
952
953 /* Formatting and parsing ofpacts. */
954 void ofpacts_format(const struct ofpact[], size_t ofpacts_len, struct ds *);
955 char *ofpacts_parse_actions(const char *, struct ofpbuf *ofpacts,
956 enum ofputil_protocol *usable_protocols)
957 OVS_WARN_UNUSED_RESULT;
958 char *ofpacts_parse_instructions(const char *, struct ofpbuf *ofpacts,
959 enum ofputil_protocol *usable_protocols)
960 OVS_WARN_UNUSED_RESULT;
961 const char *ofpact_name(enum ofpact_type);
962
963 /* Internal use by the helpers below. */
964 void ofpact_init(struct ofpact *, enum ofpact_type, size_t len);
965 void *ofpact_put(struct ofpbuf *, enum ofpact_type, size_t len);
966 void *ofpact_finish(struct ofpbuf *, struct ofpact *);
967
968 /* For each OFPACT_<ENUM> with a corresponding struct <STRUCT>, this defines
969 * the following commonly useful functions:
970 *
971 * struct <STRUCT> *ofpact_put_<ENUM>(struct ofpbuf *ofpacts);
972 *
973 * Appends a new 'ofpact', of length OFPACT_<ENUM>_SIZE, to 'ofpacts',
974 * initializes it with ofpact_init_<ENUM>(), and returns it. Also sets
975 * 'ofpacts->header' to the returned action.
976 *
977 * After using this function to add a variable-length action, add the
978 * elements of the flexible array (e.g. with ofpbuf_put()), then use
979 * ofpact_finish() to pad the action to a multiple of OFPACT_ALIGNTO bytes
980 * and update its embedded length field. (Keep in mind the need to refresh
981 * the structure from 'ofpacts->header' after adding data to 'ofpacts'.)
982 *
983 * struct <STRUCT> *ofpact_get_<ENUM>(const struct ofpact *ofpact);
984 *
985 * Returns 'ofpact' cast to "struct <STRUCT> *". 'ofpact->type' must be
986 * OFPACT_<ENUM>.
987 *
988 * void ofpact_finish_<ENUM>(struct ofpbuf *ofpacts, struct <STRUCT> **ap);
989 *
990 * Finishes composing variable-length action '*ap' (begun using
991 * ofpact_put_<NAME>() on 'ofpacts'), by padding the action to a multiple
992 * of OFPACT_ALIGNTO bytes and updating its embedded length field.
993 *
994 * May reallocate 'ofpacts', and so as a convenience automatically updates
995 * '*ap' to point to the new location. If the caller has other pointers
996 * within 'ap' or 'ofpacts', it needs to update them manually.
997 *
998 * as well as the following more rarely useful definitions:
999 *
1000 * void ofpact_init_<ENUM>(struct <STRUCT> *ofpact);
1001 *
1002 * Initializes the parts of 'ofpact' that identify it as having type
1003 * OFPACT_<ENUM> and length OFPACT_<ENUM>_SIZE and zeros the rest.
1004 *
1005 * <ENUM>_SIZE
1006 *
1007 * The size of the action structure. For a fixed-length action, this is
1008 * sizeof(struct <STRUCT>) rounded up to a multiple of OFPACT_ALIGNTO. For
1009 * a variable-length action, this is the offset to the variable-length
1010 * part.
1011 */
1012 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
1013 BUILD_ASSERT_DECL(offsetof(struct STRUCT, ofpact) == 0); \
1014 \
1015 enum { OFPACT_##ENUM##_SIZE \
1016 = (offsetof(struct STRUCT, MEMBER) \
1017 ? offsetof(struct STRUCT, MEMBER) \
1018 : OFPACT_ALIGN(sizeof(struct STRUCT))) }; \
1019 \
1020 static inline struct STRUCT * \
1021 ofpact_get_##ENUM(const struct ofpact *ofpact) \
1022 { \
1023 ovs_assert(ofpact->type == OFPACT_##ENUM); \
1024 return ALIGNED_CAST(struct STRUCT *, ofpact); \
1025 } \
1026 \
1027 static inline struct STRUCT * \
1028 ofpact_get_##ENUM##_nullable(const struct ofpact *ofpact) \
1029 { \
1030 ovs_assert(!ofpact || ofpact->type == OFPACT_##ENUM); \
1031 return ALIGNED_CAST(struct STRUCT *, ofpact); \
1032 } \
1033 \
1034 static inline struct STRUCT * \
1035 ofpact_put_##ENUM(struct ofpbuf *ofpacts) \
1036 { \
1037 return ofpact_put(ofpacts, OFPACT_##ENUM, \
1038 OFPACT_##ENUM##_SIZE); \
1039 } \
1040 \
1041 static inline void \
1042 ofpact_init_##ENUM(struct STRUCT *ofpact) \
1043 { \
1044 ofpact_init(&ofpact->ofpact, OFPACT_##ENUM, \
1045 OFPACT_##ENUM##_SIZE); \
1046 } \
1047 \
1048 static inline void \
1049 ofpact_finish_##ENUM(struct ofpbuf *ofpbuf, struct STRUCT **ofpactp) \
1050 { \
1051 struct ofpact *ofpact = &(*ofpactp)->ofpact; \
1052 ovs_assert(ofpact->type == OFPACT_##ENUM); \
1053 *ofpactp = ofpact_finish(ofpbuf, ofpact); \
1054 }
1055 OFPACTS
1056 #undef OFPACT
1057
1058 /* Additional functions for composing ofpacts. */
1059 struct ofpact_set_field *ofpact_put_reg_load(struct ofpbuf *ofpacts);
1060
1061 /* OpenFlow 1.1 instructions.
1062 * The order is sorted in execution order. Not in the value of OFPIT11_xxx.
1063 * It is enforced on parser from text string.
1064 */
1065 #define OVS_INSTRUCTIONS \
1066 DEFINE_INST(OFPIT13_METER, \
1067 ofp13_instruction_meter, false, \
1068 "meter") \
1069 \
1070 DEFINE_INST(OFPIT11_APPLY_ACTIONS, \
1071 ofp11_instruction_actions, true, \
1072 "apply_actions") \
1073 \
1074 DEFINE_INST(OFPIT11_CLEAR_ACTIONS, \
1075 ofp11_instruction, false, \
1076 "clear_actions") \
1077 \
1078 DEFINE_INST(OFPIT11_WRITE_ACTIONS, \
1079 ofp11_instruction_actions, true, \
1080 "write_actions") \
1081 \
1082 DEFINE_INST(OFPIT11_WRITE_METADATA, \
1083 ofp11_instruction_write_metadata, false, \
1084 "write_metadata") \
1085 \
1086 DEFINE_INST(OFPIT11_GOTO_TABLE, \
1087 ofp11_instruction_goto_table, false, \
1088 "goto_table")
1089
1090 enum ovs_instruction_type {
1091 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) OVSINST_##ENUM,
1092 OVS_INSTRUCTIONS
1093 #undef DEFINE_INST
1094 };
1095
1096 enum {
1097 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
1098 N_OVS_INSTRUCTIONS = OVS_INSTRUCTIONS
1099 #undef DEFINE_INST
1100 };
1101
1102 const char *ovs_instruction_name_from_type(enum ovs_instruction_type type);
1103 int ovs_instruction_type_from_name(const char *name);
1104 enum ovs_instruction_type ovs_instruction_type_from_ofpact_type(
1105 enum ofpact_type);
1106 enum ofperr ovs_instruction_type_from_inst_type(
1107 enum ovs_instruction_type *instruction_type, const uint16_t inst_type);
1108
1109 ovs_be32 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version);
1110 uint32_t ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap,
1111 enum ofp_version);
1112
1113 #endif /* ofp-actions.h */