Coverage Report

Created: 2025-11-11 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/ofp-actions.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2008-2017, 2019-2020 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
19
#include <sys/types.h>
20
#include <netinet/in.h>
21
22
#include "bundle.h"
23
#include "byte-order.h"
24
#include "colors.h"
25
#include "compiler.h"
26
#include "dummy.h"
27
#include "openvswitch/hmap.h"
28
#include "learn.h"
29
#include "multipath.h"
30
#include "nx-match.h"
31
#include "odp-netlink.h"
32
#include "openvswitch/dynamic-string.h"
33
#include "openvswitch/meta-flow.h"
34
#include "openvswitch/ofp-actions.h"
35
#include "openvswitch/ofp-packet.h"
36
#include "openvswitch/ofp-parse.h"
37
#include "openvswitch/ofp-port.h"
38
#include "openvswitch/ofp-prop.h"
39
#include "openvswitch/ofp-table.h"
40
#include "openvswitch/ofpbuf.h"
41
#include "openvswitch/vlog.h"
42
#include "unaligned.h"
43
#include "util.h"
44
#include "vl-mff-map.h"
45
46
VLOG_DEFINE_THIS_MODULE(ofp_actions);
47
48
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
49
50
struct ofp_action_header;
51
52
/* Header for Open vSwitch and ONF vendor extension actions.
53
 *
54
 * This is the entire header for a few Open vSwitch vendor extension actions,
55
 * the ones that either have no arguments or for which variable-length
56
 * arguments follow the header.
57
 *
58
 * This cannot be used as an entirely generic vendor extension action header,
59
 * because OpenFlow does not specify the location or size of the action
60
 * subtype; it just happens that ONF extensions and Nicira extensions share
61
 * this format. */
62
struct ext_action_header {
63
    ovs_be16 type;                  /* OFPAT_VENDOR. */
64
    ovs_be16 len;                   /* At least 16. */
65
    ovs_be32 vendor;                /* NX_VENDOR_ID or ONF_VENDOR_ID. */
66
    ovs_be16 subtype;               /* See enum ofp_raw_action_type. */
67
    uint8_t pad[6];
68
};
69
OFP_ASSERT(sizeof(struct ext_action_header) == 16);
70
71
/* Raw identifiers for OpenFlow actions.
72
 *
73
 * Decoding and encoding OpenFlow actions across multiple versions is difficult
74
 * to do in a clean, consistent way.  This enumeration lays out all of the
75
 * forms of actions that Open vSwitch supports.
76
 *
77
 * The comments here must follow a stylized form because the
78
 * "extract-ofp-actions" program parses them at build time to generate data
79
 * tables.
80
 *
81
 *   - The first part of each comment specifies the vendor, OpenFlow versions,
82
 *     and type for each protocol that supports the action:
83
 *
84
 *         # The vendor is OF for standard OpenFlow actions, NX for Nicira
85
 *           extension actions.  (Support for other vendors can be added, but
86
 *           it can't be done just based on a vendor ID definition alone
87
 *           because OpenFlow doesn't define a standard way to specify a
88
 *           subtype for vendor actions, so other vendors might do it different
89
 *           from Nicira.)
90
 *
91
 *         # The version can specify a specific OpenFlow version, a version
92
 *           range delimited by "-", or an open-ended range with "+".
93
 *
94
 *         # The type, in parentheses, is the action type number (for standard
95
 *           OpenFlow actions) or subtype (for vendor extension actions).
96
 *
97
 *         # Optionally one may add "is deprecated" followed by a
98
 *           human-readable reason in parentheses (which will be used in log
99
 *           messages), if a particular action should no longer be used.
100
 *
101
 *     Multiple such specifications may be separated by commas.
102
 *
103
 *   - The second part describes the action's wire format.  It may be:
104
 *
105
 *         # "struct <name>": The struct fully specifies the wire format.  The
106
 *           action is exactly the size of the struct.  (Thus, the struct must
107
 *           be an exact multiple of 8 bytes in size.)
108
 *
109
 *         # "struct <name>, ...": The struct specifies the beginning of the
110
 *           wire format.  An instance of the action is either the struct's
111
 *           exact size, or a multiple of 8 bytes longer.
112
 *
113
 *         # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
114
 *           vendor extension) header, followed by 0 or more pad bytes to align
115
 *           to a multiple of <N> bits, followed by an argument of the given
116
 *           type, followed by 0 or more pad bytes to bring the total action up
117
 *           to a multiple of 8 bytes.
118
 *
119
 *         # "void": The action is just a (standard or vendor extension)
120
 *           header.
121
 *
122
 *         # Optionally, one may add "VLMFF" in the end of the second part if
123
 *           the Openflow action may use a variable length meta-flow field
124
 *           (i.e. tun_metadata). Adding "VLMFF" will pass the per-switch based
125
 *           variable length meta-flow field mapping map (struct vl_mff_map) to
126
 *           the corresponding action decoding function.
127
 *
128
 *   - Optional additional text enclosed in square brackets is commentary for
129
 *     the human reader.
130
 */
131
enum ofp_raw_action_type {
132
/* ## ----------------- ## */
133
/* ## Standard actions. ## */
134
/* ## ----------------- ## */
135
136
    /* OF1.0(0): struct ofp10_action_output. */
137
    OFPAT_RAW10_OUTPUT,
138
    /* OF1.1+(0): struct ofp11_action_output. */
139
    OFPAT_RAW11_OUTPUT,
140
141
    /* OF1.0(1): uint16_t. */
142
    OFPAT_RAW10_SET_VLAN_VID,
143
    /* OF1.0(2): uint8_t. */
144
    OFPAT_RAW10_SET_VLAN_PCP,
145
146
    /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
147
     *
148
     * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
149
     * modification actions: the 1.0 versions push a VLAN header if none is
150
     * present, but the 1.1 versions do not.  That is the only reason that we
151
     * distinguish their raw action types.] */
152
    OFPAT_RAW11_SET_VLAN_VID,
153
    /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
154
    OFPAT_RAW11_SET_VLAN_PCP,
155
156
    /* OF1.1+(17): ovs_be16.
157
     *
158
     * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
159
     * TCI.] */
160
    OFPAT_RAW11_PUSH_VLAN,
161
162
    /* OF1.0(3): void. */
163
    OFPAT_RAW10_STRIP_VLAN,
164
    /* OF1.1+(18): void. */
165
    OFPAT_RAW11_POP_VLAN,
166
167
    /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
168
     * ofp_action_dl_addr. */
169
    OFPAT_RAW_SET_DL_SRC,
170
171
    /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
172
     * ofp_action_dl_addr. */
173
    OFPAT_RAW_SET_DL_DST,
174
175
    /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
176
     * ovs_be32. */
177
    OFPAT_RAW_SET_NW_SRC,
178
179
    /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
180
     * ovs_be32. */
181
    OFPAT_RAW_SET_NW_DST,
182
183
    /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
184
    OFPAT_RAW_SET_NW_TOS,
185
186
    /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
187
    OFPAT_RAW11_SET_NW_ECN,
188
189
    /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
190
     * ovs_be16. */
191
    OFPAT_RAW_SET_TP_SRC,
192
193
    /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
194
     * ovs_be16. */
195
    OFPAT_RAW_SET_TP_DST,
196
197
    /* OF1.0(11): struct ofp10_action_enqueue. */
198
    OFPAT_RAW10_ENQUEUE,
199
200
    /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
201
     * ovs_be32. */
202
    OFPAT_RAW_SET_MPLS_LABEL,
203
204
    /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
205
     * uint8_t. */
206
    OFPAT_RAW_SET_MPLS_TC,
207
208
    /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
209
     * uint8_t. */
210
    OFPAT_RAW_SET_MPLS_TTL,
211
212
    /* NX1.0(26), OF1.1+(16): void. */
213
    OFPAT_RAW_DEC_MPLS_TTL,
214
215
    /* NX1.0(23), OF1.1+(19): ovs_be16.
216
     *
217
     * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
218
    OFPAT_RAW_PUSH_MPLS,
219
220
    /* NX1.0(24), OF1.1+(20): ovs_be16.
221
     *
222
     * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
223
     * ETH_TYPE_MPLS otherwise, not the label.] */
224
    OFPAT_RAW_POP_MPLS,
225
226
    /* NX1.0(4), OF1.1+(21): uint32_t. */
227
    OFPAT_RAW_SET_QUEUE,
228
229
    /* NX1.0(40), OF1.1+(22): uint32_t. */
230
    OFPAT_RAW_GROUP,
231
232
    /* OF1.1+(23): uint8_t. */
233
    OFPAT_RAW11_SET_NW_TTL,
234
235
    /* NX1.0(18), OF1.1+(24): void. */
236
    OFPAT_RAW_DEC_NW_TTL,
237
    /* NX1.0+(21): struct nx_action_cnt_ids, ... */
238
    NXAST_RAW_DEC_TTL_CNT_IDS,
239
240
    /* OF1.2-1.4(25): struct ofp12_action_set_field, ... VLMFF */
241
    OFPAT_RAW12_SET_FIELD,
242
    /* OF1.5+(25): struct ofp12_action_set_field, ... VLMFF */
243
    OFPAT_RAW15_SET_FIELD,
244
    /* NX1.0-1.4(7): struct nx_action_reg_load. VLMFF
245
     *
246
     * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
247
     * we drop reg_load.] */
248
    NXAST_RAW_REG_LOAD,
249
    /* NX1.0-1.4(33): struct ext_action_header, ... VLMFF
250
     *
251
     * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
252
     * we drop reg_load2.] */
253
    NXAST_RAW_REG_LOAD2,
254
255
    /* OF1.5+(28): struct ofp15_action_copy_field, ... VLMFF */
256
    OFPAT_RAW15_COPY_FIELD,
257
    /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... VLMFF */
258
    ONFACT_RAW13_COPY_FIELD,
259
    /* NX1.0-1.4(6): struct nx_action_reg_move, ... VLMFF */
260
    NXAST_RAW_REG_MOVE,
261
262
    /* OF1.5+(29): uint32_t. */
263
    OFPAT_RAW15_METER,
264
265
/* ## ------------------------- ## */
266
/* ## Nicira extension actions. ## */
267
/* ## ------------------------- ## */
268
269
/* Actions similar to standard actions are listed with the standard actions. */
270
271
    /* NX1.0+(1): uint16_t. */
272
    NXAST_RAW_RESUBMIT,
273
    /* NX1.0+(14): struct nx_action_resubmit. */
274
    NXAST_RAW_RESUBMIT_TABLE,
275
    /* NX1.0+(44): struct nx_action_resubmit. */
276
    NXAST_RAW_RESUBMIT_TABLE_CT,
277
278
    /* NX1.0+(2): uint32_t. */
279
    NXAST_RAW_SET_TUNNEL,
280
    /* NX1.0+(9): uint64_t. */
281
    NXAST_RAW_SET_TUNNEL64,
282
283
    /* NX1.0+(5): void. */
284
    NXAST_RAW_POP_QUEUE,
285
286
    /* NX1.0+(8): struct nx_action_note, ... */
287
    NXAST_RAW_NOTE,
288
289
    /* NX1.0+(10): struct nx_action_multipath. VLMFF */
290
    NXAST_RAW_MULTIPATH,
291
292
    /* NX1.0+(12): struct nx_action_bundle, ... */
293
    NXAST_RAW_BUNDLE,
294
    /* NX1.0+(13): struct nx_action_bundle, ... VLMFF */
295
    NXAST_RAW_BUNDLE_LOAD,
296
297
    /* NX1.0+(15): struct nx_action_output_reg. VLMFF */
298
    NXAST_RAW_OUTPUT_REG,
299
    /* NX1.0+(32): struct nx_action_output_reg2. VLMFF */
300
    NXAST_RAW_OUTPUT_REG2,
301
302
    /* NX1.0+(16): struct nx_action_learn, ... VLMFF */
303
    NXAST_RAW_LEARN,
304
    /* NX1.0+(45): struct nx_action_learn2, ... VLMFF */
305
    NXAST_RAW_LEARN2,
306
307
    /* NX1.0+(17): void. */
308
    NXAST_RAW_EXIT,
309
310
    /* NX1.0+(19): struct nx_action_fin_timeout. */
311
    NXAST_RAW_FIN_TIMEOUT,
312
313
    /* NX1.0+(20): struct nx_action_controller. */
314
    NXAST_RAW_CONTROLLER,
315
    /* NX1.0+(37): struct ext_action_header, ... */
316
    NXAST_RAW_CONTROLLER2,
317
318
    /* NX1.0+(22): struct nx_action_write_metadata. */
319
    NXAST_RAW_WRITE_METADATA,
320
321
    /* NX1.0+(27): struct nx_action_stack. VLMFF */
322
    NXAST_RAW_STACK_PUSH,
323
324
    /* NX1.0+(28): struct nx_action_stack. VLMFF */
325
    NXAST_RAW_STACK_POP,
326
327
    /* NX1.0+(29): struct nx_action_sample. */
328
    NXAST_RAW_SAMPLE,
329
    /* NX1.0+(38): struct nx_action_sample2. */
330
    NXAST_RAW_SAMPLE2,
331
    /* NX1.0+(41): struct nx_action_sample2. */
332
    NXAST_RAW_SAMPLE3,
333
    /* NX1.0+(51): struct nx_action_sample4. VLMFF */
334
    NXAST_RAW_SAMPLE4,
335
336
    /* NX1.0+(34): struct nx_action_conjunction. */
337
    NXAST_RAW_CONJUNCTION,
338
339
    /* NX1.0+(35): struct nx_action_conntrack, ... VLMFF */
340
    NXAST_RAW_CT,
341
342
    /* NX1.0+(36): struct nx_action_nat, ... */
343
    NXAST_RAW_NAT,
344
345
    /* NX1.0+(39): struct nx_action_output_trunc. */
346
    NXAST_RAW_OUTPUT_TRUNC,
347
348
    /* NX1.0+(42): struct ext_action_header, ... VLMFF */
349
    NXAST_RAW_CLONE,
350
351
    /* NX1.0+(43): void. */
352
    NXAST_RAW_CT_CLEAR,
353
354
    /* NX1.3+(46): struct nx_action_encap, ... */
355
    NXAST_RAW_ENCAP,
356
357
    /* NX1.3+(47): struct nx_action_decap, ... */
358
    NXAST_RAW_DECAP,
359
360
    /* NX1.3+(48): void. */
361
    NXAST_RAW_DEC_NSH_TTL,
362
363
    /* NX1.0+(49): struct nx_action_check_pkt_larger, ... VLMFF */
364
    NXAST_RAW_CHECK_PKT_LARGER,
365
366
    /* NX1.0+(50): struct nx_action_delete_field. VLMFF */
367
    NXAST_RAW_DELETE_FIELD,
368
369
/* ## ------------------ ## */
370
/* ## Debugging actions. ## */
371
/* ## ------------------ ## */
372
373
/* These are intentionally undocumented, subject to change, and ovs-vswitchd */
374
/* accepts them only if started with --enable-dummy. */
375
376
    /* NX1.0+(254): void. */
377
    NXAST_RAW_DEBUG_SLOW,
378
379
    /* NX1.0+(255): void. */
380
    NXAST_RAW_DEBUG_RECIRC,
381
};
382
383
/* OpenFlow actions are always a multiple of 8 bytes in length. */
384
240k
#define OFP_ACTION_ALIGN 8
385
386
/* Define a few functions for working with instructions. */
387
#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)             \
388
    static inline const struct STRUCT * OVS_UNUSED              \
389
    instruction_get_##ENUM(const struct ofp11_instruction *inst)\
390
169
    {                                                           \
391
169
        ovs_assert(inst->type == htons(ENUM));                  \
392
169
        return ALIGNED_CAST(struct STRUCT *, inst);             \
393
169
    }                                                           \
Unexecuted instantiation: ofp-actions.c:instruction_get_OFPIT13_METER
Unexecuted instantiation: ofp-actions.c:instruction_get_OFPIT11_APPLY_ACTIONS
Unexecuted instantiation: ofp-actions.c:instruction_get_OFPIT11_WRITE_ACTIONS
Unexecuted instantiation: ofp-actions.c:instruction_get_OFPIT11_WRITE_METADATA
394
                                                                \
395
    static inline void OVS_UNUSED                               \
396
    instruction_init_##ENUM(struct STRUCT *s)                   \
397
724
    {                                                           \
398
724
        memset(s, 0, sizeof *s);                                \
399
724
        s->type = htons(ENUM);                                  \
400
724
        s->len = htons(sizeof *s);                              \
401
724
    }                                                           \
402
                                                                \
403
    static inline struct STRUCT * OVS_UNUSED                    \
404
    instruction_put_##ENUM(struct ofpbuf *buf)                  \
405
724
    {                                                           \
406
724
        struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
407
724
        instruction_init_##ENUM(s);                             \
408
724
        return s;                                               \
409
724
    }
ofp-actions.c:instruction_put_OFPIT13_METER
Line
Count
Source
405
138
    {                                                           \
406
138
        struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
407
138
        instruction_init_##ENUM(s);                             \
408
138
        return s;                                               \
409
138
    }
ofp-actions.c:instruction_put_OFPIT11_CLEAR_ACTIONS
Line
Count
Source
405
2
    {                                                           \
406
2
        struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
407
2
        instruction_init_##ENUM(s);                             \
408
2
        return s;                                               \
409
2
    }
ofp-actions.c:instruction_put_OFPIT11_WRITE_ACTIONS
Line
Count
Source
405
38
    {                                                           \
406
38
        struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
407
38
        instruction_init_##ENUM(s);                             \
408
38
        return s;                                               \
409
38
    }
ofp-actions.c:instruction_put_OFPIT11_WRITE_METADATA
Line
Count
Source
405
2
    {                                                           \
406
2
        struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
407
2
        instruction_init_##ENUM(s);                             \
408
2
        return s;                                               \
409
2
    }
ofp-actions.c:instruction_put_OFPIT11_GOTO_TABLE
Line
Count
Source
405
1
    {                                                           \
406
1
        struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
407
1
        instruction_init_##ENUM(s);                             \
408
1
        return s;                                               \
409
1
    }
ofp-actions.c:instruction_put_OFPIT11_APPLY_ACTIONS
Line
Count
Source
405
543
    {                                                           \
406
543
        struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
407
543
        instruction_init_##ENUM(s);                             \
408
543
        return s;                                               \
409
543
    }
410
1.61k
OVS_INSTRUCTIONS
ofp-actions.c:instruction_get_OFPIT11_CLEAR_ACTIONS
Line
Count
Source
410
OVS_INSTRUCTIONS
ofp-actions.c:instruction_get_OFPIT11_GOTO_TABLE
Line
Count
Source
410
OVS_INSTRUCTIONS
ofp-actions.c:instruction_init_OFPIT13_METER
Line
Count
Source
410
OVS_INSTRUCTIONS
ofp-actions.c:instruction_init_OFPIT11_CLEAR_ACTIONS
Line
Count
Source
410
OVS_INSTRUCTIONS
ofp-actions.c:instruction_init_OFPIT11_WRITE_ACTIONS
Line
Count
Source
410
OVS_INSTRUCTIONS
ofp-actions.c:instruction_init_OFPIT11_WRITE_METADATA
Line
Count
Source
410
OVS_INSTRUCTIONS
ofp-actions.c:instruction_init_OFPIT11_GOTO_TABLE
Line
Count
Source
410
OVS_INSTRUCTIONS
ofp-actions.c:instruction_init_OFPIT11_APPLY_ACTIONS
Line
Count
Source
410
OVS_INSTRUCTIONS
411
1.61k
#undef DEFINE_INST
412
1.61k
413
1.61k
static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
414
1.61k
                                               size_t ofs);
415
1.61k
static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
416
1.61k
417
1.61k
static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
418
1.61k
                                  enum ofp_version, uint32_t allowed_ovsinsts,
419
1.61k
                                  enum ofpact_type outer_action,
420
1.61k
                                  char **errorp);
421
1.61k
422
1.61k
static void put_set_field(struct ofpbuf *openflow, enum ofp_version,
423
1.61k
                          enum mf_field_id, uint64_t value);
424
1.61k
425
1.61k
static void put_reg_load(struct ofpbuf *openflow,
426
1.61k
                         const struct mf_subfield *, uint64_t value);
427
1.61k
428
1.61k
static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
429
1.61k
                                   enum ofp_raw_action_type *, uint64_t *arg,
430
1.61k
                                   size_t *raw_len);
431
1.61k
static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
432
1.61k
                            enum ofp_raw_action_type, uint64_t arg);
433
1.61k
434
1.61k
static char *OVS_WARN_UNUSED_RESULT ofpacts_parse(
435
1.61k
    char *str, const struct ofpact_parse_params *pp,
436
1.61k
    bool allow_instructions, enum ofpact_type outer_action);
437
1.61k
static enum ofperr ofpacts_pull_openflow_actions__(
438
1.61k
    struct ofpbuf *openflow, unsigned int actions_len,
439
1.61k
    enum ofp_version version, uint32_t allowed_ovsinsts,
440
1.61k
    struct ofpbuf *ofpacts, enum ofpact_type outer_action,
441
1.61k
    const struct vl_mff_map *vl_mff_map, uint64_t *ofpacts_tlv_bitmap);
442
1.61k
static char * OVS_WARN_UNUSED_RESULT ofpacts_parse_copy(
443
1.61k
    const char *s_, const struct ofpact_parse_params *pp,
444
1.61k
    bool allow_instructions, enum ofpact_type outer_action);
445
1.61k
446
1.61k
static void inconsistent_match(enum ofputil_protocol *usable_protocols);
447
1.61k
448
1.61k
/* Returns the ofpact following 'ofpact', except that if 'ofpact' contains
449
1.61k
 * nested ofpacts it returns the first one. */
450
1.61k
struct ofpact *
451
1.61k
ofpact_next_flattened(const struct ofpact *ofpact)
452
1.61k
{
453
0
    switch (ofpact->type) {
454
0
    case OFPACT_OUTPUT:
455
0
    case OFPACT_GROUP:
456
0
    case OFPACT_CONTROLLER:
457
0
    case OFPACT_ENQUEUE:
458
0
    case OFPACT_OUTPUT_REG:
459
0
    case OFPACT_OUTPUT_TRUNC:
460
0
    case OFPACT_BUNDLE:
461
0
    case OFPACT_SET_FIELD:
462
0
    case OFPACT_SET_VLAN_VID:
463
0
    case OFPACT_SET_VLAN_PCP:
464
0
    case OFPACT_STRIP_VLAN:
465
0
    case OFPACT_PUSH_VLAN:
466
0
    case OFPACT_SET_ETH_SRC:
467
0
    case OFPACT_SET_ETH_DST:
468
0
    case OFPACT_SET_IPV4_SRC:
469
0
    case OFPACT_SET_IPV4_DST:
470
0
    case OFPACT_SET_IP_DSCP:
471
0
    case OFPACT_SET_IP_ECN:
472
0
    case OFPACT_SET_IP_TTL:
473
0
    case OFPACT_SET_L4_SRC_PORT:
474
0
    case OFPACT_SET_L4_DST_PORT:
475
0
    case OFPACT_REG_MOVE:
476
0
    case OFPACT_STACK_PUSH:
477
0
    case OFPACT_STACK_POP:
478
0
    case OFPACT_DEC_TTL:
479
0
    case OFPACT_SET_MPLS_LABEL:
480
0
    case OFPACT_SET_MPLS_TC:
481
0
    case OFPACT_SET_MPLS_TTL:
482
0
    case OFPACT_DEC_MPLS_TTL:
483
0
    case OFPACT_PUSH_MPLS:
484
0
    case OFPACT_POP_MPLS:
485
0
    case OFPACT_SET_TUNNEL:
486
0
    case OFPACT_SET_QUEUE:
487
0
    case OFPACT_POP_QUEUE:
488
0
    case OFPACT_FIN_TIMEOUT:
489
0
    case OFPACT_RESUBMIT:
490
0
    case OFPACT_LEARN:
491
0
    case OFPACT_CONJUNCTION:
492
0
    case OFPACT_MULTIPATH:
493
0
    case OFPACT_NOTE:
494
0
    case OFPACT_EXIT:
495
0
    case OFPACT_SAMPLE:
496
0
    case OFPACT_UNROLL_XLATE:
497
0
    case OFPACT_CT_CLEAR:
498
0
    case OFPACT_DEBUG_RECIRC:
499
0
    case OFPACT_DEBUG_SLOW:
500
0
    case OFPACT_METER:
501
0
    case OFPACT_CLEAR_ACTIONS:
502
0
    case OFPACT_WRITE_METADATA:
503
0
    case OFPACT_GOTO_TABLE:
504
0
    case OFPACT_NAT:
505
0
    case OFPACT_ENCAP:
506
0
    case OFPACT_DECAP:
507
0
    case OFPACT_DEC_NSH_TTL:
508
0
    case OFPACT_CHECK_PKT_LARGER:
509
0
    case OFPACT_DELETE_FIELD:
510
0
        return ofpact_next(ofpact);
511
512
0
    case OFPACT_CLONE:
513
0
        return ofpact_get_CLONE(ofpact)->actions;
514
515
0
    case OFPACT_CT:
516
0
        return ofpact_get_CT(ofpact)->actions;
517
518
0
    case OFPACT_WRITE_ACTIONS:
519
0
        return ofpact_get_WRITE_ACTIONS(ofpact)->actions;
520
0
    }
521
522
0
    OVS_NOT_REACHED();
523
0
}
524
525
/* Pull off existing actions or instructions. Used by nesting actions to keep
526
 * ofpacts_parse() oblivious of actions nesting.
527
 *
528
 * Push the actions back on after nested parsing, e.g.:
529
 *
530
 *     size_t ofs = ofpacts_pull(ofpacts);
531
 *     ...nested parsing...
532
 *     ofpbuf_push_uninit(ofpacts, ofs);
533
 */
534
static size_t
535
ofpacts_pull(struct ofpbuf *ofpacts)
536
143k
{
537
143k
    size_t ofs;
538
539
143k
    ofs = ofpacts->size;
540
143k
    ofpbuf_pull(ofpacts, ofs);
541
542
143k
    return ofs;
543
143k
}
544
545
#include "ofp-actions.inc1"
546

547
/* Output actions. */
548
549
/* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
550
 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
551
 * number of bytes to send.  A 'max_len' of zero means no bytes of the
552
 * packet should be sent. */
553
struct ofp10_action_output {
554
    ovs_be16 type;                  /* OFPAT10_OUTPUT. */
555
    ovs_be16 len;                   /* Length is 8. */
556
    ovs_be16 port;                  /* Output port. */
557
    ovs_be16 max_len;               /* Max length to send to controller. */
558
};
559
OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
560
561
/* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
562
   * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
563
   * number of bytes to send. A 'max_len' of zero means no bytes of the
564
   * packet should be sent.*/
565
struct ofp11_action_output {
566
    ovs_be16 type;                    /* OFPAT11_OUTPUT. */
567
    ovs_be16 len;                     /* Length is 16. */
568
    ovs_be32 port;                    /* Output port. */
569
    ovs_be16 max_len;                 /* Max length to send to controller. */
570
    uint8_t pad[6];                   /* Pad to 64 bits. */
571
};
572
OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
573
574
static enum ofperr
575
decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
576
                          enum ofp_version ofp_version OVS_UNUSED,
577
                          struct ofpbuf *out)
578
13.1k
{
579
13.1k
    struct ofpact_output *output;
580
581
13.1k
    output = ofpact_put_OUTPUT(out);
582
13.1k
    output->port = u16_to_ofp(ntohs(oao->port));
583
13.1k
    output->max_len = ntohs(oao->max_len);
584
585
13.1k
    return ofpact_check_output_port(output->port, OFPP_MAX);
586
13.1k
}
587
588
static enum ofperr
589
decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
590
                          enum ofp_version ofp_version OVS_UNUSED,
591
                          struct ofpbuf *out)
592
3.04k
{
593
3.04k
    struct ofpact_output *output;
594
3.04k
    enum ofperr error;
595
596
3.04k
    output = ofpact_put_OUTPUT(out);
597
3.04k
    output->max_len = ntohs(oao->max_len);
598
599
3.04k
    error = ofputil_port_from_ofp11(oao->port, &output->port);
600
3.04k
    if (error) {
601
507
        return error;
602
507
    }
603
604
2.54k
    return ofpact_check_output_port(output->port, OFPP_MAX);
605
3.04k
}
606
607
static void
608
encode_OUTPUT(const struct ofpact_output *output,
609
              enum ofp_version ofp_version, struct ofpbuf *out)
610
120k
{
611
120k
    if (ofp_version == OFP10_VERSION) {
612
94.6k
        struct ofp10_action_output *oao;
613
614
94.6k
        oao = put_OFPAT10_OUTPUT(out);
615
94.6k
        oao->port = htons(ofp_to_u16(output->port));
616
94.6k
        oao->max_len = htons(output->max_len);
617
94.6k
    } else {
618
26.2k
        struct ofp11_action_output *oao;
619
620
26.2k
        oao = put_OFPAT11_OUTPUT(out);
621
26.2k
        oao->port = ofputil_port_to_ofp11(output->port);
622
26.2k
        oao->max_len = htons(output->max_len);
623
26.2k
    }
624
120k
}
625
626
static char * OVS_WARN_UNUSED_RESULT
627
parse_truncate_subfield(const char *arg_,
628
                        const struct ofpact_parse_params *pp,
629
                        struct ofpact_output_trunc *output_trunc)
630
1.57k
{
631
1.57k
    char *key, *value;
632
1.57k
    char *arg = CONST_CAST(char *, arg_);
633
634
4.84k
    while (ofputil_parse_key_value(&arg, &key, &value)) {
635
3.42k
        if (!strcmp(key, "port")) {
636
1.63k
            if (!ofputil_port_from_string(value, pp->port_map,
637
1.63k
                                          &output_trunc->port)) {
638
35
                return xasprintf("output to unknown truncate port: %s",
639
35
                                  value);
640
35
            }
641
1.59k
            if (ofp_to_u16(output_trunc->port) > ofp_to_u16(OFPP_MAX)) {
642
871
                if (output_trunc->port != OFPP_LOCAL &&
643
861
                    output_trunc->port != OFPP_IN_PORT)
644
10
                return xasprintf("output to unsupported truncate port: %s",
645
10
                                 value);
646
871
            }
647
1.79k
        } else if (!strcmp(key, "max_len")) {
648
1.72k
            char *err;
649
650
1.72k
            err = str_to_u32(value, &output_trunc->max_len);
651
1.72k
            if (err) {
652
29
                return err;
653
29
            }
654
655
1.69k
            if (output_trunc->max_len < ETH_HEADER_LEN) {
656
1
                return xasprintf("max_len %"PRIu32" is less than the minimum "
657
1
                                 "value %d",
658
1
                                 output_trunc->max_len, ETH_HEADER_LEN);
659
1
            }
660
1.69k
        } else {
661
74
            return xasprintf("invalid key '%s' in output_trunc argument",
662
74
                                key);
663
74
        }
664
3.42k
    }
665
1.42k
    return NULL;
666
1.57k
}
667
668
static char * OVS_WARN_UNUSED_RESULT
669
parse_OUTPUT(const char *arg, const struct ofpact_parse_params *pp)
670
3.05k
{
671
3.05k
    if (strstr(arg, "port") && strstr(arg, "max_len")) {
672
1.57k
        struct ofpact_output_trunc *output_trunc;
673
674
1.57k
        output_trunc = ofpact_put_OUTPUT_TRUNC(pp->ofpacts);
675
1.57k
        return parse_truncate_subfield(arg, pp, output_trunc);
676
1.57k
    }
677
678
1.48k
    ofp_port_t port;
679
1.48k
    if (ofputil_port_from_string(arg, pp->port_map, &port)) {
680
831
        struct ofpact_output *output = ofpact_put_OUTPUT(pp->ofpacts);
681
831
        output->port = port;
682
831
        output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
683
831
        return NULL;
684
831
    }
685
686
651
    struct mf_subfield src;
687
651
    char *error = mf_parse_subfield(&src, arg);
688
651
    if (!error) {
689
638
        struct ofpact_output_reg *output_reg;
690
691
638
        output_reg = ofpact_put_OUTPUT_REG(pp->ofpacts);
692
638
        output_reg->max_len = UINT16_MAX;
693
638
        output_reg->src = src;
694
638
        return NULL;
695
638
    }
696
13
    free(error);
697
698
13
    return xasprintf("%s: output to unknown port", arg);
699
651
}
700
701
static void
702
format_OUTPUT(const struct ofpact_output *a,
703
              const struct ofpact_format_params *fp)
704
10.0k
{
705
10.0k
    if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
706
5.95k
        ds_put_format(fp->s, "%soutput:%s", colors.special, colors.end);
707
5.95k
    }
708
10.0k
    ofputil_format_port(a->port, fp->port_map, fp->s);
709
10.0k
    if (a->port == OFPP_CONTROLLER) {
710
140
        ds_put_format(fp->s, ":%"PRIu16, a->max_len);
711
140
    }
712
10.0k
}
713
714
static enum ofperr
715
check_OUTPUT(const struct ofpact_output *a,
716
             const struct ofpact_check_params *cp)
717
131k
{
718
131k
    return ofpact_check_output_port(a->port, cp->max_ports);
719
131k
}
720

721
/* Group actions. */
722
723
static enum ofperr
724
decode_OFPAT_RAW_GROUP(uint32_t group_id,
725
                         enum ofp_version ofp_version OVS_UNUSED,
726
                         struct ofpbuf *out)
727
2.32k
{
728
2.32k
    ofpact_put_GROUP(out)->group_id = group_id;
729
2.32k
    return 0;
730
2.32k
}
731
732
static void
733
encode_GROUP(const struct ofpact_group *group,
734
             enum ofp_version ofp_version, struct ofpbuf *out)
735
275
{
736
275
    put_OFPAT_GROUP(out, ofp_version, group->group_id);
737
275
}
738
739
static char * OVS_WARN_UNUSED_RESULT
740
parse_GROUP(char *arg, const struct ofpact_parse_params *pp)
741
313
{
742
313
    return str_to_u32(arg, &ofpact_put_GROUP(pp->ofpacts)->group_id);
743
313
}
744
745
static void
746
format_GROUP(const struct ofpact_group *a,
747
             const struct ofpact_format_params *fp)
748
1.29k
{
749
1.29k
    ds_put_format(fp->s, "%sgroup:%s%"PRIu32,
750
1.29k
                  colors.special, colors.end, a->group_id);
751
1.29k
}
752
753
static enum ofperr
754
check_GROUP(const struct ofpact_group *a OVS_UNUSED,
755
            const struct ofpact_check_params *cp OVS_UNUSED)
756
1.80k
{
757
1.80k
    return 0;
758
1.80k
}
759

760
/* Action structure for NXAST_CONTROLLER.
761
 *
762
 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER.  In
763
 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
764
 * specifying:
765
 *
766
 *    - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
767
 *
768
 *    - 'controller_id': The ID of the controller connection to which the
769
 *      ofp_packet_in should be sent.  The ofp_packet_in or nx_packet_in is
770
 *      sent only to controllers that have the specified controller connection
771
 *      ID.  See "struct nx_controller_id" for more information. */
772
struct nx_action_controller {
773
    ovs_be16 type;                  /* OFPAT_VENDOR. */
774
    ovs_be16 len;                   /* Length is 16. */
775
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
776
    ovs_be16 subtype;               /* NXAST_CONTROLLER. */
777
    ovs_be16 max_len;               /* Maximum length to send to controller. */
778
    ovs_be16 controller_id;         /* Controller ID to send packet-in. */
779
    uint8_t reason;                 /* enum ofp_packet_in_reason (OFPR_*). */
780
    uint8_t zero;                   /* Must be zero. */
781
};
782
OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
783
784
/* Properties for NXAST_CONTROLLER2.
785
 *
786
 * For more information on the effect of NXAC2PT_PAUSE, see the large comment
787
 * on NXT_PACKET_IN2 in nicira-ext.h */
788
enum nx_action_controller2_prop_type {
789
    NXAC2PT_MAX_LEN,            /* ovs_be16 max bytes to send (default all). */
790
    NXAC2PT_CONTROLLER_ID,      /* ovs_be16 dest controller ID (default 0). */
791
    NXAC2PT_REASON,             /* uint8_t reason (OFPR_*), default 0. */
792
    NXAC2PT_USERDATA,           /* Data to copy into NXPINT_USERDATA. */
793
    NXAC2PT_PAUSE,              /* Flag to pause pipeline to resume later. */
794
    NXAC2PT_METER_ID,           /* ovs_b32 meter (default NX_CTLR_NO_METER). */
795
};
796
797
/* The action structure for NXAST_CONTROLLER2 is "struct ext_action_header",
798
 * followed by NXAC2PT_* properties. */
799
800
static enum ofperr
801
decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
802
                            enum ofp_version ofp_version OVS_UNUSED,
803
                            struct ofpbuf *out)
804
2.54k
{
805
2.54k
    struct ofpact_controller *oc;
806
807
2.54k
    oc = ofpact_put_CONTROLLER(out);
808
2.54k
    oc->ofpact.raw = NXAST_RAW_CONTROLLER;
809
2.54k
    oc->max_len = ntohs(nac->max_len);
810
2.54k
    oc->controller_id = ntohs(nac->controller_id);
811
2.54k
    oc->reason = nac->reason;
812
2.54k
    oc->meter_id = NX_CTLR_NO_METER;
813
2.54k
    ofpact_finish_CONTROLLER(out, &oc);
814
815
2.54k
    return 0;
816
2.54k
}
817
818
static enum ofperr
819
decode_NXAST_RAW_CONTROLLER2(const struct ext_action_header *eah,
820
                             enum ofp_version ofp_version OVS_UNUSED,
821
                             struct ofpbuf *out)
822
1.94k
{
823
1.94k
    if (!is_all_zeros(eah->pad, sizeof eah->pad)) {
824
329
        return OFPERR_NXBRC_MUST_BE_ZERO;
825
329
    }
826
827
1.61k
    size_t start_ofs = out->size;
828
1.61k
    struct ofpact_controller *oc = ofpact_put_CONTROLLER(out);
829
1.61k
    oc->ofpact.raw = NXAST_RAW_CONTROLLER2;
830
1.61k
    oc->max_len = UINT16_MAX;
831
1.61k
    oc->reason = OFPR_ACTION;
832
1.61k
    oc->meter_id = NX_CTLR_NO_METER;
833
834
1.61k
    struct ofpbuf properties;
835
1.61k
    ofpbuf_use_const(&properties, eah, ntohs(eah->len));
836
1.61k
    ofpbuf_pull(&properties, sizeof *eah);
837
838
2.96k
    while (properties.size > 0) {
839
1.89k
        struct ofpbuf payload;
840
1.89k
        uint64_t type;
841
842
1.89k
        enum ofperr error = ofpprop_pull(&properties, &payload, &type);
843
1.89k
        if (error) {
844
493
            return error;
845
493
        }
846
847
1.40k
        switch (type) {
848
227
        case NXAC2PT_MAX_LEN:
849
227
            error = ofpprop_parse_u16(&payload, &oc->max_len);
850
227
            break;
851
852
18
        case NXAC2PT_CONTROLLER_ID:
853
18
            error = ofpprop_parse_u16(&payload, &oc->controller_id);
854
18
            break;
855
856
36
        case NXAC2PT_REASON: {
857
36
            uint8_t u8;
858
36
            error = ofpprop_parse_u8(&payload, &u8);
859
36
            if (!error) {
860
18
                oc->reason = u8;
861
18
            }
862
36
            break;
863
0
        }
864
865
827
        case NXAC2PT_USERDATA:
866
827
            out->size = start_ofs + sizeof(struct ofpact_controller);
867
827
            ofpbuf_put(out, payload.msg, ofpbuf_msgsize(&payload));
868
827
            oc = ofpbuf_at_assert(out, start_ofs, sizeof *oc);
869
827
            oc->userdata_len = ofpbuf_msgsize(&payload);
870
827
            break;
871
872
234
        case NXAC2PT_PAUSE:
873
234
            oc->pause = true;
874
234
            break;
875
876
39
        case NXAC2PT_METER_ID:
877
39
            error = ofpprop_parse_u32(&payload, &oc->meter_id);
878
39
            break;
879
880
20
        default:
881
20
            error = OFPPROP_UNKNOWN(false, "NXAST_RAW_CONTROLLER2", type);
882
20
            break;
883
1.40k
        }
884
1.40k
        if (error) {
885
49
            return error;
886
49
        }
887
1.40k
    }
888
889
1.07k
    ofpact_finish_CONTROLLER(out, &oc);
890
891
1.07k
    return 0;
892
1.61k
}
893
894
static void
895
encode_CONTROLLER(const struct ofpact_controller *controller,
896
                  enum ofp_version ofp_version OVS_UNUSED,
897
                  struct ofpbuf *out)
898
6.66k
{
899
6.66k
    if (controller->userdata_len
900
4.65k
        || controller->pause
901
1.15k
        || controller->meter_id != NX_CTLR_NO_METER
902
6.31k
        || controller->ofpact.raw == NXAST_RAW_CONTROLLER2) {
903
6.31k
        size_t start_ofs = out->size;
904
6.31k
        put_NXAST_CONTROLLER2(out);
905
6.31k
        if (controller->max_len != UINT16_MAX) {
906
79
            ofpprop_put_u16(out, NXAC2PT_MAX_LEN, controller->max_len);
907
79
        }
908
6.31k
        if (controller->controller_id != 0) {
909
45
            ofpprop_put_u16(out, NXAC2PT_CONTROLLER_ID,
910
45
                            controller->controller_id);
911
45
        }
912
6.31k
        if (controller->reason != OFPR_ACTION) {
913
194
            ofpprop_put_u8(out, NXAC2PT_REASON, controller->reason);
914
194
        }
915
6.31k
        if (controller->userdata_len != 0) {
916
2.00k
            ofpprop_put(out, NXAC2PT_USERDATA, controller->userdata,
917
2.00k
                        controller->userdata_len);
918
2.00k
        }
919
6.31k
        if (controller->pause) {
920
3.49k
            ofpprop_put_flag(out, NXAC2PT_PAUSE);
921
3.49k
        }
922
6.31k
        if (controller->meter_id != NX_CTLR_NO_METER) {
923
808
            ofpprop_put_u32(out, NXAC2PT_METER_ID, controller->meter_id);
924
808
        }
925
6.31k
        pad_ofpat(out, start_ofs);
926
6.31k
    } else {
927
349
        struct nx_action_controller *nac;
928
929
349
        nac = put_NXAST_CONTROLLER(out);
930
349
        nac->max_len = htons(controller->max_len);
931
349
        nac->controller_id = htons(controller->controller_id);
932
349
        nac->reason = controller->reason;
933
349
    }
934
6.66k
}
935
936
static char * OVS_WARN_UNUSED_RESULT
937
parse_CONTROLLER(char *arg, const struct ofpact_parse_params *pp)
938
8.76k
{
939
8.76k
    enum ofp_packet_in_reason reason = OFPR_ACTION;
940
8.76k
    uint16_t controller_id = 0;
941
8.76k
    uint16_t max_len = UINT16_MAX;
942
8.76k
    uint32_t meter_id = NX_CTLR_NO_METER;
943
8.76k
    const char *userdata = NULL;
944
8.76k
    bool pause = false;
945
946
8.76k
    if (!arg[0]) {
947
        /* Use defaults. */
948
8.15k
    } else if (strspn(arg, "0123456789") == strlen(arg)) {
949
597
        char *error = str_to_u16(arg, "max_len", &max_len);
950
597
        if (error) {
951
1
            return error;
952
1
        }
953
7.55k
    } else {
954
7.55k
        char *name, *value;
955
956
16.7k
        while (ofputil_parse_key_value(&arg, &name, &value)) {
957
9.36k
            if (!strcmp(name, "reason")) {
958
758
                if (!ofputil_packet_in_reason_from_string(value, &reason)) {
959
83
                    return xasprintf("unknown reason \"%s\"", value);
960
83
                }
961
8.60k
            } else if (!strcmp(name, "max_len")) {
962
302
                char *error = str_to_u16(value, "max_len", &max_len);
963
302
                if (error) {
964
1
                    return error;
965
1
                }
966
8.30k
            } else if (!strcmp(name, "id")) {
967
687
                char *error = str_to_u16(value, "id", &controller_id);
968
687
                if (error) {
969
1
                    return error;
970
1
                }
971
7.62k
            } else if (!strcmp(name, "userdata")) {
972
2.19k
                userdata = value;
973
5.42k
            } else if (!strcmp(name, "pause")) {
974
4.39k
                pause = true;
975
4.39k
            } else if (!strcmp(name, "meter_id")) {
976
993
                char *error = str_to_u32(value, &meter_id);
977
993
                if (error) {
978
1
                    return error;
979
1
                }
980
993
            } else {
981
41
                return xasprintf("unknown key \"%s\" parsing controller "
982
41
                                 "action", name);
983
41
            }
984
9.36k
        }
985
7.55k
    }
986
987
8.63k
    if (reason == OFPR_ACTION && controller_id == 0 && !userdata && !pause
988
2.48k
        && meter_id == NX_CTLR_NO_METER) {
989
1.54k
        struct ofpact_output *output;
990
991
1.54k
        output = ofpact_put_OUTPUT(pp->ofpacts);
992
1.54k
        output->port = OFPP_CONTROLLER;
993
1.54k
        output->max_len = max_len;
994
7.09k
    } else {
995
7.09k
        struct ofpact_controller *controller;
996
997
7.09k
        controller = ofpact_put_CONTROLLER(pp->ofpacts);
998
7.09k
        controller->max_len = max_len;
999
7.09k
        controller->reason = reason;
1000
7.09k
        controller->controller_id = controller_id;
1001
7.09k
        controller->pause = pause;
1002
7.09k
        controller->meter_id = meter_id;
1003
1004
7.09k
        if (userdata) {
1005
2.13k
            size_t start_ofs = pp->ofpacts->size;
1006
2.13k
            const char *end = ofpbuf_put_hex(pp->ofpacts, userdata, NULL);
1007
2.13k
            if (*end) {
1008
4
                return xstrdup("bad hex digit in `controller' "
1009
4
                               "action `userdata'");
1010
4
            }
1011
2.13k
            size_t userdata_len = pp->ofpacts->size - start_ofs;
1012
2.13k
            controller = pp->ofpacts->header;
1013
2.13k
            controller->userdata_len = userdata_len;
1014
2.13k
        }
1015
1016
7.09k
        if (ofpbuf_oversized(pp->ofpacts)) {
1017
2
            return xasprintf("input too big");
1018
2
        }
1019
1020
7.08k
        ofpact_finish_CONTROLLER(pp->ofpacts, &controller);
1021
7.08k
    }
1022
1023
8.63k
    return NULL;
1024
8.63k
}
1025
1026
static void
1027
format_CONTROLLER(const struct ofpact_controller *a,
1028
                  const struct ofpact_format_params *fp)
1029
3.58k
{
1030
3.58k
    if (a->reason == OFPR_ACTION && !a->controller_id && !a->userdata_len
1031
251
        && !a->pause && a->meter_id == NX_CTLR_NO_METER) {
1032
55
        ds_put_format(fp->s, "%sCONTROLLER:%s%"PRIu16,
1033
55
                      colors.special, colors.end, a->max_len);
1034
3.52k
    } else {
1035
3.52k
        enum ofp_packet_in_reason reason = a->reason;
1036
1037
3.52k
        ds_put_format(fp->s, "%scontroller(%s", colors.paren, colors.end);
1038
3.52k
        if (reason != OFPR_ACTION) {
1039
2.05k
            char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
1040
1041
2.05k
            ds_put_format(fp->s, "%sreason=%s%s,", colors.param, colors.end,
1042
2.05k
                          ofputil_packet_in_reason_to_string(
1043
2.05k
                              reason, reasonbuf, sizeof reasonbuf));
1044
2.05k
        }
1045
3.52k
        if (a->max_len != UINT16_MAX) {
1046
2.60k
            ds_put_format(fp->s, "%smax_len=%s%"PRIu16",",
1047
2.60k
                          colors.param, colors.end, a->max_len);
1048
2.60k
        }
1049
3.52k
        if (a->controller_id != 0) {
1050
1.68k
            ds_put_format(fp->s, "%sid=%s%"PRIu16",",
1051
1.68k
                          colors.param, colors.end, a->controller_id);
1052
1.68k
        }
1053
3.52k
        if (a->userdata_len) {
1054
825
            ds_put_format(fp->s, "%suserdata=%s", colors.param, colors.end);
1055
825
            ds_put_hex_with_delimiter(fp->s, a->userdata, a->userdata_len,
1056
825
                                      ".");
1057
825
            ds_put_char(fp->s, ',');
1058
825
        }
1059
3.52k
        if (a->pause) {
1060
157
            ds_put_format(fp->s, "%spause%s,", colors.value, colors.end);
1061
157
        }
1062
3.52k
        if (a->meter_id != NX_CTLR_NO_METER) {
1063
39
            ds_put_format(fp->s, "%smeter_id=%s%"PRIu32",",
1064
39
                          colors.param, colors.end, a->meter_id);
1065
39
        }
1066
3.52k
        ds_chomp(fp->s, ',');
1067
3.52k
        ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
1068
3.52k
    }
1069
3.58k
}
1070
1071
static enum ofperr
1072
check_CONTROLLER(const struct ofpact_controller *a OVS_UNUSED,
1073
                 const struct ofpact_check_params *cp OVS_UNUSED)
1074
6.69k
{
1075
6.69k
    return 0;
1076
6.69k
}
1077

1078
/* Enqueue action. */
1079
struct ofp10_action_enqueue {
1080
    ovs_be16 type;            /* OFPAT10_ENQUEUE. */
1081
    ovs_be16 len;             /* Len is 16. */
1082
    ovs_be16 port;            /* Port that queue belongs. Should
1083
                                 refer to a valid physical port
1084
                                 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
1085
    uint8_t pad[6];           /* Pad for 64-bit alignment. */
1086
    ovs_be32 queue_id;        /* Where to enqueue the packets. */
1087
};
1088
OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
1089
1090
static enum ofperr
1091
decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
1092
                           enum ofp_version ofp_version OVS_UNUSED,
1093
                           struct ofpbuf *out)
1094
289
{
1095
289
    struct ofpact_enqueue *enqueue;
1096
1097
289
    enqueue = ofpact_put_ENQUEUE(out);
1098
289
    enqueue->port = u16_to_ofp(ntohs(oae->port));
1099
289
    enqueue->queue = ntohl(oae->queue_id);
1100
289
    if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
1101
289
        && enqueue->port != OFPP_IN_PORT
1102
62
        && enqueue->port != OFPP_LOCAL) {
1103
34
        return OFPERR_OFPBAC_BAD_OUT_PORT;
1104
34
    }
1105
255
    return 0;
1106
289
}
1107
1108
static void
1109
encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
1110
               enum ofp_version ofp_version, struct ofpbuf *out)
1111
946
{
1112
946
    if (ofp_version == OFP10_VERSION) {
1113
571
        struct ofp10_action_enqueue *oae;
1114
1115
571
        oae = put_OFPAT10_ENQUEUE(out);
1116
571
        oae->port = htons(ofp_to_u16(enqueue->port));
1117
571
        oae->queue_id = htonl(enqueue->queue);
1118
571
    } else {
1119
375
        put_OFPAT_SET_QUEUE(out, ofp_version, enqueue->queue);
1120
1121
375
        struct ofp11_action_output *oao = put_OFPAT11_OUTPUT(out);
1122
375
        oao->port = ofputil_port_to_ofp11(enqueue->port);
1123
375
        oao->max_len = OVS_BE16_MAX;
1124
1125
375
        put_NXAST_POP_QUEUE(out);
1126
375
    }
1127
946
}
1128
1129
static char * OVS_WARN_UNUSED_RESULT
1130
parse_ENQUEUE(char *arg, const struct ofpact_parse_params *pp)
1131
1.11k
{
1132
1.11k
    char *sp = NULL;
1133
1.11k
    char *port = strtok_r(arg, ":q,", &sp);
1134
1.11k
    char *queue = strtok_r(NULL, "", &sp);
1135
1.11k
    struct ofpact_enqueue *enqueue;
1136
1137
1.11k
    if (port == NULL || queue == NULL) {
1138
5
        return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
1139
5
                       "\"enqueue(PORT,QUEUE)\"");
1140
5
    }
1141
1142
1.10k
    enqueue = ofpact_put_ENQUEUE(pp->ofpacts);
1143
1.10k
    if (!ofputil_port_from_string(port, pp->port_map, &enqueue->port)) {
1144
1
        return xasprintf("%s: enqueue to unknown port", port);
1145
1
    }
1146
1.10k
    return str_to_u32(queue, &enqueue->queue);
1147
1.10k
}
1148
1149
static void
1150
format_ENQUEUE(const struct ofpact_enqueue *a,
1151
               const struct ofpact_format_params *fp)
1152
255
{
1153
255
    ds_put_format(fp->s, "%senqueue:%s", colors.param, colors.end);
1154
255
    ofputil_format_port(a->port, fp->port_map, fp->s);
1155
255
    ds_put_format(fp->s, ":%"PRIu32, a->queue);
1156
255
}
1157
1158
static enum ofperr
1159
check_ENQUEUE(const struct ofpact_enqueue *a,
1160
              const struct ofpact_check_params *cp)
1161
1.08k
{
1162
1.08k
    if (ofp_to_u16(a->port) >= ofp_to_u16(cp->max_ports)
1163
1.08k
        && a->port != OFPP_IN_PORT
1164
221
        && a->port != OFPP_LOCAL) {
1165
16
        return OFPERR_OFPBAC_BAD_OUT_PORT;
1166
16
    }
1167
1.06k
    return 0;
1168
1.08k
}
1169

1170
/* Action structure for NXAST_OUTPUT_REG.
1171
 *
1172
 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
1173
 *
1174
 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
1175
 * the NXAST_REG_LOAD action.
1176
 *
1177
 * The acceptable nxm_header values for 'src' are the same as the acceptable
1178
 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
1179
 *
1180
 * The 'max_len' field indicates the number of bytes to send when the chosen
1181
 * port is OFPP_CONTROLLER.  Its semantics are equivalent to the 'max_len'
1182
 * field of OFPAT_OUTPUT.
1183
 *
1184
 * The 'zero' field is required to be zeroed for forward compatibility. */
1185
struct nx_action_output_reg {
1186
    ovs_be16 type;              /* OFPAT_VENDOR. */
1187
    ovs_be16 len;               /* 24. */
1188
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
1189
    ovs_be16 subtype;           /* NXAST_OUTPUT_REG. */
1190
1191
    ovs_be16 ofs_nbits;         /* (ofs << 6) | (n_bits - 1). */
1192
    ovs_be32 src;               /* Source. */
1193
1194
    ovs_be16 max_len;           /* Max length to send to controller. */
1195
1196
    uint8_t zero[6];            /* Reserved, must be zero. */
1197
};
1198
OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
1199
1200
/* Action structure for NXAST_OUTPUT_REG2.
1201
 *
1202
 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
1203
 * experimenter OXM as 'src'.
1204
 */
1205
struct nx_action_output_reg2 {
1206
    ovs_be16 type;              /* OFPAT_VENDOR. */
1207
    ovs_be16 len;               /* 24. */
1208
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
1209
    ovs_be16 subtype;           /* NXAST_OUTPUT_REG2. */
1210
1211
    ovs_be16 ofs_nbits;         /* (ofs << 6) | (n_bits - 1). */
1212
    ovs_be16 max_len;           /* Max length to send to controller. */
1213
1214
    /* Followed by:
1215
     * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
1216
     * - Enough 0-bytes to pad the action out to 24 bytes. */
1217
    uint8_t pad[10];
1218
};
1219
OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
1220
1221
static enum ofperr
1222
decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
1223
                            enum ofp_version ofp_version OVS_UNUSED,
1224
                            const struct vl_mff_map *vl_mff_map,
1225
                            uint64_t *tlv_bitmap, struct ofpbuf *out)
1226
1.22k
{
1227
1.22k
    struct ofpact_output_reg *output_reg;
1228
1.22k
    enum ofperr error;
1229
1230
1.22k
    if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
1231
193
        return OFPERR_OFPBAC_BAD_ARGUMENT;
1232
193
    }
1233
1234
1.03k
    output_reg = ofpact_put_OUTPUT_REG(out);
1235
1.03k
    output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
1236
1.03k
    output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
1237
1.03k
    output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
1238
1.03k
    output_reg->max_len = ntohs(naor->max_len);
1239
1.03k
    error = mf_vl_mff_mf_from_nxm_header(ntohl(naor->src), vl_mff_map,
1240
1.03k
                                         &output_reg->src.field, tlv_bitmap);
1241
1.03k
    if (error) {
1242
69
        return error;
1243
69
    }
1244
1245
967
    return mf_check_src(&output_reg->src, NULL);
1246
1.03k
}
1247
1248
static enum ofperr
1249
decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
1250
                             enum ofp_version ofp_version OVS_UNUSED,
1251
                             const struct vl_mff_map *vl_mff_map,
1252
                             uint64_t *tlv_bitmap, struct ofpbuf *out)
1253
1.02k
{
1254
1.02k
    struct ofpact_output_reg *output_reg;
1255
1.02k
    enum ofperr error;
1256
1257
1.02k
    output_reg = ofpact_put_OUTPUT_REG(out);
1258
1.02k
    output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
1259
1.02k
    output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
1260
1.02k
    output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
1261
1.02k
    output_reg->max_len = ntohs(naor->max_len);
1262
1263
1.02k
    struct ofpbuf b = ofpbuf_const_initializer(naor, ntohs(naor->len));
1264
1.02k
    ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
1265
1266
1.02k
    error = mf_vl_mff_nx_pull_header(&b, vl_mff_map, &output_reg->src.field,
1267
1.02k
                                     NULL, tlv_bitmap);
1268
1.02k
    if (error) {
1269
750
        return error;
1270
750
    }
1271
1272
279
    if (!is_all_zeros(b.data, b.size)) {
1273
18
        return OFPERR_NXBRC_MUST_BE_ZERO;
1274
18
    }
1275
1276
261
    return mf_check_src(&output_reg->src, NULL);
1277
279
}
1278
1279
static void
1280
encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
1281
                  enum ofp_version ofp_version OVS_UNUSED,
1282
                  struct ofpbuf *out)
1283
518
{
1284
    /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
1285
     * cannot be encoded in the older form, encode it as
1286
     * NXAST_RAW_OUTPUT_REG2. */
1287
518
    if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
1288
518
        || !mf_nxm_header(output_reg->src.field->id)) {
1289
289
        struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
1290
289
        size_t size = out->size;
1291
1292
289
        naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1293
289
                                               output_reg->src.n_bits);
1294
289
        naor->max_len = htons(output_reg->max_len);
1295
1296
289
        out->size = size - sizeof naor->pad;
1297
289
        nx_put_mff_header(out, output_reg->src.field, 0, false);
1298
289
        out->size = size;
1299
289
    } else {
1300
229
        struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
1301
1302
229
        naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1303
229
                                               output_reg->src.n_bits);
1304
229
        naor->src = htonl(nxm_header_from_mff(output_reg->src.field));
1305
229
        naor->max_len = htons(output_reg->max_len);
1306
229
    }
1307
518
}
1308
1309
static char * OVS_WARN_UNUSED_RESULT
1310
parse_OUTPUT_REG(const char *arg, const struct ofpact_parse_params *pp)
1311
194
{
1312
194
    return parse_OUTPUT(arg, pp);
1313
194
}
1314
1315
static void
1316
format_OUTPUT_REG(const struct ofpact_output_reg *a,
1317
                  const struct ofpact_format_params *fp)
1318
361
{
1319
361
    ds_put_format(fp->s, "%soutput:%s", colors.special, colors.end);
1320
361
    mf_format_subfield(&a->src, fp->s);
1321
361
}
1322
1323
static enum ofperr
1324
check_OUTPUT_REG(const struct ofpact_output_reg *a,
1325
                 const struct ofpact_check_params *cp)
1326
1.40k
{
1327
1.40k
    return mf_check_src(&a->src, cp->match);
1328
1.40k
}
1329

1330
/* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
1331
 *
1332
 * The bundle actions choose a member from a supplied list of options.
1333
 * NXAST_BUNDLE outputs to its selection.  NXAST_BUNDLE_LOAD writes its
1334
 * selection to a register.
1335
 *
1336
 * The list of possible members follows the nx_action_bundle structure. The
1337
 * size of each member is governed by its type as indicated by the
1338
 * 'member_type' parameter. The list of members should be padded at its end
1339
 * with zeros to make the total length of the action a multiple of 8.
1340
 *
1341
 * Switches infer from the 'member_type' parameter the size of each member.
1342
 * All implementations must support the NXM_OF_IN_PORT 'member_type' which
1343
 * indicates that the members are OpenFlow port numbers with
1344
 * NXM_LENGTH(NXM_OF_IN_PORT) == 2 byte width.  Switches should reject actions
1345
 * which indicate unknown or unsupported member types.
1346
 *
1347
 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
1348
 * member.  If the switch does not support the specified 'algorithm' parameter,
1349
 * it should reject the action.
1350
 *
1351
 * Several algorithms take into account liveness when selecting members.  The
1352
 * liveness of a member is implementation defined (with one exception), but
1353
 * will generally take into account things like its carrier status and the
1354
 * results of any link monitoring protocols which happen to be running on it.
1355
 * In order to give controllers a place-holder value, the OFPP_NONE port is
1356
 * always considered live, that is, NXAST_BUNDLE_LOAD stores OFPP_NONE in the
1357
 * output register if no member is live.
1358
 *
1359
 * Some member selection strategies require the use of a hash function, in
1360
 * which case the 'fields' and 'basis' parameters should be populated.  The
1361
 * 'fields' parameter (one of NX_HASH_FIELDS_*) designates which parts of the
1362
 * flow to hash.  Refer to the definition of "enum nx_hash_fields" for details.
1363
 * The 'basis' parameter is used as a universal hash parameter.  Different
1364
 * values of 'basis' yield different hash results.
1365
 *
1366
 * The 'zero' parameter at the end of the action structure is reserved for
1367
 * future use.  Switches are required to reject actions which have nonzero
1368
 * bytes in the 'zero' field.
1369
 *
1370
 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed.  Switches
1371
 * should reject actions which have nonzero bytes in either of these fields.
1372
 *
1373
 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected member in
1374
 * dst[ofs:ofs+n_bits].  The format and semantics of 'dst' and 'ofs_nbits' are
1375
 * similar to those for the NXAST_REG_LOAD action. */
1376
struct nx_action_bundle {
1377
    ovs_be16 type;              /* OFPAT_VENDOR. */
1378
    ovs_be16 len;               /* Length including members. */
1379
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
1380
    ovs_be16 subtype;           /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
1381
1382
    /* Member choice algorithm to apply to hash value. */
1383
    ovs_be16 algorithm;         /* One of NX_BD_ALG_*. */
1384
1385
    /* What fields to hash and how. */
1386
    ovs_be16 fields;            /* One of NX_HASH_FIELDS_*. */
1387
    ovs_be16 basis;             /* Universal hash parameter. */
1388
1389
    ovs_be32 member_type;       /* NXM_OF_IN_PORT. */
1390
    ovs_be16 n_members;         /* Number of members. */
1391
1392
    ovs_be16 ofs_nbits;         /* (ofs << 6) | (n_bits - 1). */
1393
    ovs_be32 dst;               /* Destination. */
1394
1395
    uint8_t zero[4];            /* Reserved. Must be zero. */
1396
};
1397
OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
1398
1399
static enum ofperr
1400
decode_bundle(bool load, const struct nx_action_bundle *nab,
1401
              const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap,
1402
              struct ofpbuf *ofpacts)
1403
3.64k
{
1404
3.64k
    static struct vlog_rate_limit rll = VLOG_RATE_LIMIT_INIT(1, 5);
1405
3.64k
    struct ofpact_bundle *bundle;
1406
3.64k
    uint32_t member_type;
1407
3.64k
    size_t members_size, i;
1408
3.64k
    enum ofperr error;
1409
1410
3.64k
    bundle = ofpact_put_BUNDLE(ofpacts);
1411
1412
3.64k
    bundle->n_members = ntohs(nab->n_members);
1413
3.64k
    bundle->basis = ntohs(nab->basis);
1414
3.64k
    bundle->fields = ntohs(nab->fields);
1415
3.64k
    bundle->algorithm = ntohs(nab->algorithm);
1416
3.64k
    member_type = ntohl(nab->member_type);
1417
3.64k
    members_size = ntohs(nab->len) - sizeof *nab;
1418
1419
3.64k
    error = OFPERR_OFPBAC_BAD_ARGUMENT;
1420
3.64k
    if (!flow_hash_fields_valid(bundle->fields)) {
1421
507
        VLOG_WARN_RL(&rll, "unsupported fields %d", (int) bundle->fields);
1422
3.14k
    } else if (bundle->n_members > BUNDLE_MAX_MEMBERS) {
1423
284
        VLOG_WARN_RL(&rll, "too many members");
1424
2.85k
    } else if (bundle->algorithm != NX_BD_ALG_HRW
1425
2.20k
               && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
1426
233
        VLOG_WARN_RL(&rll, "unsupported algorithm %d", (int) bundle->algorithm);
1427
2.62k
    } else if (member_type != mf_nxm_header(MFF_IN_PORT)) {
1428
1.33k
        VLOG_WARN_RL(&rll, "unsupported member type %"PRIu32, member_type);
1429
1.33k
    } else {
1430
1.28k
        error = 0;
1431
1.28k
    }
1432
1433
3.64k
    if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
1434
2.27k
        VLOG_WARN_RL(&rll, "reserved field is nonzero");
1435
2.27k
        error = OFPERR_OFPBAC_BAD_ARGUMENT;
1436
2.27k
    }
1437
1438
3.64k
    if (load) {
1439
1.19k
        bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
1440
1.19k
        bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
1441
1.19k
        error = mf_vl_mff_mf_from_nxm_header(ntohl(nab->dst), vl_mff_map,
1442
1.19k
                                             &bundle->dst.field, tlv_bitmap);
1443
1.19k
        if (error) {
1444
109
            return error;
1445
109
        }
1446
1447
1.08k
        if (bundle->dst.n_bits < 16) {
1448
78
            VLOG_WARN_RL(&rll, "bundle_load action requires at least 16 bit "
1449
78
                         "destination.");
1450
78
            error = OFPERR_OFPBAC_BAD_ARGUMENT;
1451
78
        }
1452
2.45k
    } else {
1453
2.45k
        if (nab->ofs_nbits || nab->dst) {
1454
1.18k
            VLOG_WARN_RL(&rll, "bundle action has nonzero reserved fields");
1455
1.18k
            error = OFPERR_OFPBAC_BAD_ARGUMENT;
1456
1.18k
        }
1457
2.45k
    }
1458
1459
3.53k
    if (members_size < bundle->n_members * sizeof(ovs_be16)) {
1460
1.16k
        VLOG_WARN_RL(&rll, "Nicira action %s only has %"PRIuSIZE" bytes "
1461
1.16k
                     "allocated for members.  %"PRIuSIZE" bytes are "
1462
1.16k
                     "required for %u members.",
1463
1.16k
                     load ? "bundle_load" : "bundle", members_size,
1464
1.16k
                     bundle->n_members * sizeof(ovs_be16), bundle->n_members);
1465
1.16k
        error = OFPERR_OFPBAC_BAD_LEN;
1466
2.37k
    } else {
1467
20.6k
        for (i = 0; i < bundle->n_members; i++) {
1468
18.2k
            ofp_port_t ofp_port
1469
18.2k
                = u16_to_ofp(ntohs(((ovs_be16 *)(nab + 1))[i]));
1470
18.2k
            ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
1471
18.2k
            bundle = ofpacts->header;
1472
18.2k
        }
1473
2.37k
    }
1474
1475
3.53k
    ofpact_finish_BUNDLE(ofpacts, &bundle);
1476
3.53k
    if (!error) {
1477
2.12k
        error = bundle_check(bundle, OFPP_MAX, NULL);
1478
2.12k
    }
1479
3.53k
    return error;
1480
3.64k
}
1481
1482
static enum ofperr
1483
decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab,
1484
                        enum ofp_version ofp_version OVS_UNUSED,
1485
                        struct ofpbuf *out)
1486
2.45k
{
1487
2.45k
    return decode_bundle(false, nab, NULL, NULL, out);
1488
2.45k
}
1489
1490
static enum ofperr
1491
decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
1492
                             enum ofp_version ofp_version OVS_UNUSED,
1493
                             const struct vl_mff_map *vl_mff_map,
1494
                             uint64_t *tlv_bitmap, struct ofpbuf *out)
1495
1.19k
{
1496
1.19k
    return decode_bundle(true, nab, vl_mff_map, tlv_bitmap, out);
1497
1.19k
}
1498
1499
static void
1500
encode_BUNDLE(const struct ofpact_bundle *bundle,
1501
              enum ofp_version ofp_version OVS_UNUSED,
1502
              struct ofpbuf *out)
1503
663
{
1504
663
    int members_len = ROUND_UP(2 * bundle->n_members, OFP_ACTION_ALIGN);
1505
663
    struct nx_action_bundle *nab;
1506
663
    ovs_be16 *members;
1507
663
    size_t i;
1508
1509
663
    nab = (bundle->dst.field
1510
663
           ? put_NXAST_BUNDLE_LOAD(out)
1511
663
           : put_NXAST_BUNDLE(out));
1512
663
    nab->len = htons(ntohs(nab->len) + members_len);
1513
663
    nab->algorithm = htons(bundle->algorithm);
1514
663
    nab->fields = htons(bundle->fields);
1515
663
    nab->basis = htons(bundle->basis);
1516
663
    nab->member_type = htonl(mf_nxm_header(MFF_IN_PORT));
1517
663
    nab->n_members = htons(bundle->n_members);
1518
663
    if (bundle->dst.field) {
1519
0
        nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1520
0
                                              bundle->dst.n_bits);
1521
0
        nab->dst = htonl(nxm_header_from_mff(bundle->dst.field));
1522
0
    }
1523
1524
663
    members = ofpbuf_put_zeros(out, members_len);
1525
38.6k
    for (i = 0; i < bundle->n_members; i++) {
1526
37.9k
        members[i] = htons(ofp_to_u16(bundle->members[i]));
1527
37.9k
    }
1528
663
}
1529
1530
static char * OVS_WARN_UNUSED_RESULT
1531
parse_BUNDLE(const char *arg, const struct ofpact_parse_params *pp)
1532
1.93k
{
1533
1.93k
    return bundle_parse(arg, pp->port_map, pp->ofpacts);
1534
1.93k
}
1535
1536
static char * OVS_WARN_UNUSED_RESULT
1537
parse_bundle_load(const char *arg, const struct ofpact_parse_params *pp)
1538
1
{
1539
1
    return bundle_parse_load(arg, pp->port_map, pp->ofpacts);
1540
1
}
1541
1542
static void
1543
format_BUNDLE(const struct ofpact_bundle *a,
1544
              const struct ofpact_format_params *fp)
1545
1.87k
{
1546
1.87k
    bundle_format(a, fp->port_map, fp->s);
1547
1.87k
}
1548
1549
static enum ofperr
1550
check_BUNDLE(const struct ofpact_bundle *a,
1551
             const struct ofpact_check_params *cp)
1552
1.68k
{
1553
1.68k
    return bundle_check(a, cp->max_ports, cp->match);
1554
1.68k
}
1555

1556
/* Set VLAN actions. */
1557
1558
static enum ofperr
1559
decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1560
11.1k
{
1561
11.1k
    if (vid & ~0xfff) {
1562
116
        return OFPERR_OFPBAC_BAD_ARGUMENT;
1563
11.0k
    } else {
1564
11.0k
        struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1565
11.0k
        vlan_vid->vlan_vid = vid;
1566
11.0k
        vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1567
11.0k
        return 0;
1568
11.0k
    }
1569
11.1k
}
1570
1571
static enum ofperr
1572
decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid,
1573
                                enum ofp_version ofp_version OVS_UNUSED,
1574
                                struct ofpbuf *out)
1575
8.99k
{
1576
8.99k
    return decode_set_vlan_vid(vid, true, out);
1577
8.99k
}
1578
1579
static enum ofperr
1580
decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid,
1581
                                enum ofp_version ofp_version OVS_UNUSED,
1582
                                struct ofpbuf *out)
1583
2.13k
{
1584
2.13k
    return decode_set_vlan_vid(vid, false, out);
1585
2.13k
}
1586
1587
static void
1588
encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1589
                    enum ofp_version ofp_version, struct ofpbuf *out)
1590
1.69k
{
1591
1.69k
    uint16_t vid = vlan_vid->vlan_vid;
1592
1593
    /* Push a VLAN tag, if none is present and this form of the action calls
1594
     * for such a feature. */
1595
1.69k
    if (ofp_version > OFP10_VERSION
1596
841
        && vlan_vid->push_vlan_if_needed
1597
604
        && !vlan_vid->flow_has_vlan) {
1598
244
        put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1599
244
    }
1600
1601
1.69k
    if (ofp_version == OFP10_VERSION) {
1602
851
        put_OFPAT10_SET_VLAN_VID(out, vid);
1603
851
    } else if (ofp_version == OFP11_VERSION) {
1604
416
        put_OFPAT11_SET_VLAN_VID(out, vid);
1605
425
    } else {
1606
425
        put_set_field(out, ofp_version, MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1607
425
    }
1608
1.69k
}
1609
1610
static char * OVS_WARN_UNUSED_RESULT
1611
parse_set_vlan_vid(char *arg, bool push_vlan_if_needed,
1612
                   const struct ofpact_parse_params *pp)
1613
1.79k
{
1614
1.79k
    struct ofpact_vlan_vid *vlan_vid;
1615
1.79k
    uint16_t vid;
1616
1.79k
    char *error;
1617
1618
1.79k
    error = str_to_u16(arg, "VLAN VID", &vid);
1619
1.79k
    if (error) {
1620
5
        return error;
1621
5
    }
1622
1623
1.78k
    if (vid & ~VLAN_VID_MASK) {
1624
8
        return xasprintf("%s: not a valid VLAN VID", arg);
1625
8
    }
1626
1.77k
    vlan_vid = ofpact_put_SET_VLAN_VID(pp->ofpacts);
1627
1.77k
    vlan_vid->vlan_vid = vid;
1628
1.77k
    vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1629
1.77k
    return NULL;
1630
1.78k
}
1631
1632
static char * OVS_WARN_UNUSED_RESULT
1633
parse_SET_VLAN_VID(char *arg, const struct ofpact_parse_params *pp)
1634
724
{
1635
724
    return parse_set_vlan_vid(arg, false, pp);
1636
724
}
1637
1638
static void
1639
format_SET_VLAN_VID(const struct ofpact_vlan_vid *a,
1640
                    const struct ofpact_format_params *fp)
1641
6.51k
{
1642
6.51k
    ds_put_format(fp->s, "%s%s:%s%"PRIu16, colors.param,
1643
6.51k
                  a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1644
6.51k
                  colors.end, a->vlan_vid);
1645
6.51k
}
1646
1647
static enum ofperr
1648
check_SET_VLAN_VID(struct ofpact_vlan_vid *a, struct ofpact_check_params *cp)
1649
7.80k
{
1650
    /* Remember if we saw a vlan tag in the flow to aid translating to OpenFlow
1651
     * 1.1+ if need be. */
1652
7.80k
    ovs_be16 *tci = &cp->match->flow.vlans[0].tci;
1653
7.80k
    a->flow_has_vlan = (*tci & htons(VLAN_CFI)) != 0;
1654
7.80k
    if (!a->flow_has_vlan && !a->push_vlan_if_needed) {
1655
953
        inconsistent_match(&cp->usable_protocols);
1656
953
    }
1657
1658
    /* Temporarily mark that we have a vlan tag. */
1659
7.80k
    *tci |= htons(VLAN_CFI);
1660
1661
7.80k
    return 0;
1662
7.80k
}
1663

1664
/* Set PCP actions. */
1665
1666
static enum ofperr
1667
decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1668
5.93k
{
1669
5.93k
    if (pcp & ~7) {
1670
64
        return OFPERR_OFPBAC_BAD_ARGUMENT;
1671
5.87k
    } else {
1672
5.87k
        struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1673
5.87k
        vlan_pcp->vlan_pcp = pcp;
1674
5.87k
        vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1675
5.87k
        return 0;
1676
5.87k
    }
1677
5.93k
}
1678
1679
static enum ofperr
1680
decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp,
1681
                                enum ofp_version ofp_version OVS_UNUSED,
1682
                                struct ofpbuf *out)
1683
4.23k
{
1684
4.23k
    return decode_set_vlan_pcp(pcp, true, out);
1685
4.23k
}
1686
1687
static enum ofperr
1688
decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp,
1689
                                enum ofp_version ofp_version OVS_UNUSED,
1690
                                struct ofpbuf *out)
1691
1.70k
{
1692
1.70k
    return decode_set_vlan_pcp(pcp, false, out);
1693
1.70k
}
1694
1695
static void
1696
encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1697
                    enum ofp_version ofp_version, struct ofpbuf *out)
1698
1.89k
{
1699
1.89k
    uint8_t pcp = vlan_pcp->vlan_pcp;
1700
1701
    /* Push a VLAN tag, if none is present and this form of the action calls
1702
     * for such a feature. */
1703
1.89k
    if (ofp_version > OFP10_VERSION
1704
1.24k
        && vlan_pcp->push_vlan_if_needed
1705
912
        && !vlan_pcp->flow_has_vlan) {
1706
587
        put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1707
587
    }
1708
1709
1.89k
    if (ofp_version == OFP10_VERSION) {
1710
654
        put_OFPAT10_SET_VLAN_PCP(out, pcp);
1711
1.24k
    } else if (ofp_version == OFP11_VERSION) {
1712
546
        put_OFPAT11_SET_VLAN_PCP(out, pcp);
1713
697
    } else {
1714
697
        put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1715
697
    }
1716
1.89k
}
1717
1718
static char * OVS_WARN_UNUSED_RESULT
1719
parse_set_vlan_pcp(char *arg, bool push_vlan_if_needed,
1720
                   const struct ofpact_parse_params *pp)
1721
2.01k
{
1722
2.01k
    struct ofpact_vlan_pcp *vlan_pcp;
1723
2.01k
    uint8_t pcp;
1724
2.01k
    char *error;
1725
1726
2.01k
    error = str_to_u8(arg, "VLAN PCP", &pcp);
1727
2.01k
    if (error) {
1728
5
        return error;
1729
5
    }
1730
1731
2.01k
    if (pcp & ~7) {
1732
3
        return xasprintf("%s: not a valid VLAN PCP", arg);
1733
3
    }
1734
2.00k
    vlan_pcp = ofpact_put_SET_VLAN_PCP(pp->ofpacts);
1735
2.00k
    vlan_pcp->vlan_pcp = pcp;
1736
2.00k
    vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1737
2.00k
    return NULL;
1738
2.01k
}
1739
1740
static char * OVS_WARN_UNUSED_RESULT
1741
parse_SET_VLAN_PCP(char *arg, const struct ofpact_parse_params *pp)
1742
710
{
1743
710
    return parse_set_vlan_pcp(arg, false, pp);
1744
710
}
1745
1746
static void
1747
format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a,
1748
                    const struct ofpact_format_params *fp)
1749
2.52k
{
1750
2.52k
    ds_put_format(fp->s, "%s%s:%s%"PRIu8, colors.param,
1751
2.52k
                  a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1752
2.52k
                  colors.end, a->vlan_pcp);
1753
2.52k
}
1754
1755
static enum ofperr
1756
check_SET_VLAN_PCP(struct ofpact_vlan_pcp *a, struct ofpact_check_params *cp)
1757
5.21k
{
1758
    /* Remember if we saw a vlan tag in the flow to aid translating to OpenFlow
1759
     * 1.1+ if need be. */
1760
5.21k
    ovs_be16 *tci = &cp->match->flow.vlans[0].tci;
1761
5.21k
    a->flow_has_vlan = (*tci & htons(VLAN_CFI)) != 0;
1762
5.21k
    if (!a->flow_has_vlan && !a->push_vlan_if_needed) {
1763
402
        inconsistent_match(&cp->usable_protocols);
1764
402
    }
1765
1766
    /* Temporarily mark that we have a vlan tag. */
1767
5.21k
    *tci |= htons(VLAN_CFI);
1768
1769
5.21k
    return 0;
1770
5.21k
}
1771

1772
/* Strip VLAN actions. */
1773
1774
static enum ofperr
1775
decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1776
9.32k
{
1777
9.32k
    ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1778
9.32k
    return 0;
1779
9.32k
}
1780
1781
static enum ofperr
1782
decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1783
2.62k
{
1784
2.62k
    ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1785
2.62k
    return 0;
1786
2.62k
}
1787
1788
static void
1789
encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1790
                  enum ofp_version ofp_version, struct ofpbuf *out)
1791
2.73k
{
1792
2.73k
    if (ofp_version == OFP10_VERSION) {
1793
1.54k
        put_OFPAT10_STRIP_VLAN(out);
1794
1.54k
    } else {
1795
1.19k
        put_OFPAT11_POP_VLAN(out);
1796
1.19k
    }
1797
2.73k
}
1798
1799
static char * OVS_WARN_UNUSED_RESULT
1800
parse_STRIP_VLAN(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
1801
477
{
1802
477
    ofpact_put_STRIP_VLAN(pp->ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1803
477
    return NULL;
1804
477
}
1805
1806
static char * OVS_WARN_UNUSED_RESULT
1807
parse_pop_vlan(const struct ofpact_parse_params *pp)
1808
2.69k
{
1809
2.69k
    ofpact_put_STRIP_VLAN(pp->ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1810
2.69k
    return NULL;
1811
2.69k
}
1812
1813
static void
1814
format_STRIP_VLAN(const struct ofpact_null *a,
1815
                  const struct ofpact_format_params *fp)
1816
6.11k
{
1817
6.11k
    ds_put_format(fp->s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1818
6.11k
                    ? "%spop_vlan%s"
1819
6.11k
                    : "%sstrip_vlan%s"),
1820
6.11k
                  colors.value, colors.end);
1821
6.11k
}
1822
1823
static enum ofperr
1824
check_STRIP_VLAN(const struct ofpact_null *a OVS_UNUSED,
1825
                 struct ofpact_check_params *cp)
1826
9.73k
{
1827
9.73k
    if (!(cp->match->flow.vlans[0].tci & htons(VLAN_CFI))) {
1828
2.24k
        inconsistent_match(&cp->usable_protocols);
1829
2.24k
    }
1830
9.73k
    flow_pop_vlan(&cp->match->flow, NULL);
1831
9.73k
    return 0;
1832
9.73k
}
1833

1834
/* Push VLAN action. */
1835
1836
static enum ofperr
1837
decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type,
1838
                             enum ofp_version ofp_version OVS_UNUSED,
1839
                             struct ofpbuf *out)
1840
5.71k
{
1841
5.71k
    struct ofpact_push_vlan *push_vlan;
1842
5.71k
    if (!eth_type_vlan(eth_type)) {
1843
526
        return OFPERR_OFPBAC_BAD_ARGUMENT;
1844
526
    }
1845
5.19k
    push_vlan = ofpact_put_PUSH_VLAN(out);
1846
5.19k
    push_vlan->ethertype = eth_type;
1847
5.19k
    return 0;
1848
5.71k
}
1849
1850
static void
1851
encode_PUSH_VLAN(const struct ofpact_push_vlan *push_vlan,
1852
                 enum ofp_version ofp_version, struct ofpbuf *out)
1853
1.07k
{
1854
1.07k
    if (ofp_version == OFP10_VERSION) {
1855
        /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1856
         * follow this action. */
1857
749
    } else {
1858
749
        put_OFPAT11_PUSH_VLAN(out, push_vlan->ethertype);
1859
749
    }
1860
1.07k
}
1861
1862
static char * OVS_WARN_UNUSED_RESULT
1863
parse_PUSH_VLAN(char *arg, const struct ofpact_parse_params *pp)
1864
1.36k
{
1865
1.36k
    struct ofpact_push_vlan *push_vlan;
1866
1.36k
    uint16_t ethertype;
1867
1.36k
    char *error;
1868
1869
1.36k
    *pp->usable_protocols &= OFPUTIL_P_OF11_UP;
1870
1.36k
    error = str_to_u16(arg, "ethertype", &ethertype);
1871
1.36k
    if (error) {
1872
3
        return error;
1873
3
    }
1874
1875
1.36k
    if (!eth_type_vlan(htons(ethertype))) {
1876
18
        return xasprintf("%s: not a valid VLAN ethertype", arg);
1877
18
    }
1878
1.34k
    push_vlan = ofpact_put_PUSH_VLAN(pp->ofpacts);
1879
1.34k
    push_vlan->ethertype = htons(ethertype);
1880
1.34k
    return NULL;
1881
1.36k
}
1882
1883
static void
1884
format_PUSH_VLAN(const struct ofpact_push_vlan *push_vlan,
1885
                 const struct ofpact_format_params *fp)
1886
2.06k
{
1887
2.06k
    ds_put_format(fp->s, "%spush_vlan:%s%#"PRIx16,
1888
2.06k
                  colors.param, colors.end, ntohs(push_vlan->ethertype));
1889
2.06k
}
1890
1891
static enum ofperr
1892
check_PUSH_VLAN(const struct ofpact_push_vlan *a OVS_UNUSED,
1893
                struct ofpact_check_params *cp)
1894
5.56k
{
1895
5.56k
    struct flow *flow = &cp->match->flow;
1896
5.56k
    if (flow->vlans[FLOW_MAX_VLAN_HEADERS - 1].tci & htons(VLAN_CFI)) {
1897
        /* Support maximum (FLOW_MAX_VLAN_HEADERS) VLAN headers. */
1898
827
        return OFPERR_OFPBAC_BAD_TAG;
1899
827
    }
1900
    /* Temporary mark that we have a vlan tag. */
1901
4.73k
    flow_push_vlan_uninit(flow, NULL);
1902
4.73k
    flow->vlans[0].tci |= htons(VLAN_CFI);
1903
4.73k
    return 0;
1904
5.56k
}
1905

1906
/* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1907
struct ofp_action_dl_addr {
1908
    ovs_be16 type;                  /* Type. */
1909
    ovs_be16 len;                   /* Length is 16. */
1910
    struct eth_addr dl_addr;        /* Ethernet address. */
1911
    uint8_t pad[6];
1912
};
1913
OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1914
1915
static enum ofperr
1916
decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1917
                            enum ofp_version ofp_version OVS_UNUSED,
1918
                            struct ofpbuf *out)
1919
46
{
1920
46
    ofpact_put_SET_ETH_SRC(out)->mac = a->dl_addr;
1921
46
    return 0;
1922
46
}
1923
1924
static enum ofperr
1925
decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1926
                            enum ofp_version ofp_version OVS_UNUSED,
1927
                            struct ofpbuf *out)
1928
129
{
1929
129
    ofpact_put_SET_ETH_DST(out)->mac = a->dl_addr;
1930
129
    return 0;
1931
129
}
1932
1933
static void
1934
encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1935
                    enum ofp_raw_action_type raw, enum mf_field_id field,
1936
                    struct ofpbuf *out)
1937
854
{
1938
854
    if (ofp_version < OFP12_VERSION) {
1939
400
        struct ofp_action_dl_addr *oada;
1940
1941
400
        oada = ofpact_put_raw(out, ofp_version, raw, 0);
1942
400
        oada->dl_addr = mac->mac;
1943
454
    } else {
1944
454
        put_set_field(out, ofp_version, field, eth_addr_to_uint64(mac->mac));
1945
454
    }
1946
854
}
1947
1948
static void
1949
encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1950
                   struct ofpbuf *out)
1951
391
{
1952
391
    encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1953
391
                        out);
1954
1955
391
}
1956
1957
static void
1958
encode_SET_ETH_DST(const struct ofpact_mac *mac,
1959
                               enum ofp_version ofp_version,
1960
                               struct ofpbuf *out)
1961
463
{
1962
463
    encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1963
463
                        out);
1964
1965
463
}
1966
1967
static char * OVS_WARN_UNUSED_RESULT
1968
parse_SET_ETH_SRC(char *arg, const struct ofpact_parse_params *pp)
1969
546
{
1970
546
    return str_to_mac(arg, &ofpact_put_SET_ETH_SRC(pp->ofpacts)->mac);
1971
546
}
1972
1973
static char * OVS_WARN_UNUSED_RESULT
1974
parse_SET_ETH_DST(char *arg, const struct ofpact_parse_params *pp)
1975
493
{
1976
493
    return str_to_mac(arg, &ofpact_put_SET_ETH_DST(pp->ofpacts)->mac);
1977
493
}
1978
1979
static void
1980
format_SET_ETH_SRC(const struct ofpact_mac *a,
1981
                   const struct ofpact_format_params *fp)
1982
45
{
1983
45
    ds_put_format(fp->s, "%smod_dl_src:%s"ETH_ADDR_FMT,
1984
45
                  colors.param, colors.end, ETH_ADDR_ARGS(a->mac));
1985
45
}
1986
1987
static void
1988
format_SET_ETH_DST(const struct ofpact_mac *a,
1989
                   const struct ofpact_format_params *fp)
1990
103
{
1991
103
    ds_put_format(fp->s, "%smod_dl_dst:%s"ETH_ADDR_FMT,
1992
103
                  colors.param, colors.end, ETH_ADDR_ARGS(a->mac));
1993
103
}
1994
1995
static enum ofperr
1996
check_SET_ETH_SRC(const struct ofpact_mac *a OVS_UNUSED,
1997
                  const struct ofpact_check_params *cp OVS_UNUSED)
1998
426
{
1999
426
    return 0;
2000
426
}
2001
2002
static enum ofperr
2003
check_SET_ETH_DST(const struct ofpact_mac *a OVS_UNUSED,
2004
                  const struct ofpact_check_params *cp OVS_UNUSED)
2005
547
{
2006
547
    return 0;
2007
547
}
2008

2009
/* Set IPv4 address actions. */
2010
2011
static enum ofperr
2012
decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4,
2013
                            enum ofp_version ofp_version OVS_UNUSED,
2014
                            struct ofpbuf *out)
2015
4.37k
{
2016
4.37k
    ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
2017
4.37k
    return 0;
2018
4.37k
}
2019
2020
static enum ofperr
2021
decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4,
2022
                            enum ofp_version ofp_version OVS_UNUSED,
2023
                            struct ofpbuf *out)
2024
8.54k
{
2025
8.54k
    ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
2026
8.54k
    return 0;
2027
8.54k
}
2028
2029
static void
2030
encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
2031
                     enum ofp_version ofp_version,
2032
                     enum ofp_raw_action_type raw, enum mf_field_id field,
2033
                     struct ofpbuf *out)
2034
1.56k
{
2035
1.56k
    ovs_be32 addr = ipv4->ipv4;
2036
1.56k
    if (ofp_version < OFP12_VERSION) {
2037
1.17k
        ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
2038
1.17k
    } else {
2039
389
        put_set_field(out, ofp_version, field, ntohl(addr));
2040
389
    }
2041
1.56k
}
2042
2043
static void
2044
encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
2045
                    enum ofp_version ofp_version, struct ofpbuf *out)
2046
863
{
2047
863
    encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
2048
863
                         out);
2049
863
}
2050
2051
static void
2052
encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
2053
                    enum ofp_version ofp_version, struct ofpbuf *out)
2054
705
{
2055
705
    encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
2056
705
                         out);
2057
705
}
2058
2059
static char * OVS_WARN_UNUSED_RESULT
2060
parse_SET_IPV4_SRC(char *arg, const struct ofpact_parse_params *pp)
2061
897
{
2062
897
    return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(pp->ofpacts)->ipv4);
2063
897
}
2064
2065
static char * OVS_WARN_UNUSED_RESULT
2066
parse_SET_IPV4_DST(char *arg, const struct ofpact_parse_params *pp)
2067
789
{
2068
789
    return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(pp->ofpacts)->ipv4);
2069
789
}
2070
2071
static void
2072
format_SET_IPV4_SRC(const struct ofpact_ipv4 *a,
2073
                    const struct ofpact_format_params *fp)
2074
2.87k
{
2075
2.87k
    ds_put_format(fp->s, "%smod_nw_src:%s"IP_FMT,
2076
2.87k
                  colors.param, colors.end, IP_ARGS(a->ipv4));
2077
2.87k
}
2078
2079
static void
2080
format_SET_IPV4_DST(const struct ofpact_ipv4 *a,
2081
                    const struct ofpact_format_params *fp)
2082
2.75k
{
2083
2.75k
    ds_put_format(fp->s, "%smod_nw_dst:%s"IP_FMT,
2084
2.75k
                  colors.param, colors.end, IP_ARGS(a->ipv4));
2085
2.75k
}
2086
2087
static enum ofperr
2088
check_set_ipv4(struct ofpact_check_params *cp)
2089
9.09k
{
2090
9.09k
    ovs_be16 dl_type = get_dl_type(&cp->match->flow);
2091
9.09k
    if (dl_type != htons(ETH_TYPE_IP)) {
2092
5.92k
        inconsistent_match(&cp->usable_protocols);
2093
5.92k
    }
2094
9.09k
    return 0;
2095
9.09k
}
2096
2097
static enum ofperr
2098
check_SET_IPV4_SRC(const struct ofpact_ipv4 *a OVS_UNUSED,
2099
                   struct ofpact_check_params *cp)
2100
3.98k
{
2101
3.98k
    return check_set_ipv4(cp);
2102
3.98k
}
2103
2104
static enum ofperr
2105
check_SET_IPV4_DST(const struct ofpact_ipv4 *a OVS_UNUSED,
2106
                   struct ofpact_check_params *cp)
2107
5.11k
{
2108
5.11k
    return check_set_ipv4(cp);
2109
5.11k
}
2110

2111
/* Set IPv4/v6 TOS actions. */
2112
2113
static enum ofperr
2114
decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp,
2115
                            enum ofp_version ofp_version OVS_UNUSED,
2116
                            struct ofpbuf *out)
2117
10.2k
{
2118
10.2k
    if (dscp & ~IP_DSCP_MASK) {
2119
47
        return OFPERR_OFPBAC_BAD_ARGUMENT;
2120
10.1k
    } else {
2121
10.1k
        ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
2122
10.1k
        return 0;
2123
10.1k
    }
2124
10.2k
}
2125
2126
static void
2127
encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
2128
                   enum ofp_version ofp_version, struct ofpbuf *out)
2129
960
{
2130
960
    if (ofp_version < OFP12_VERSION) {
2131
702
        put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
2132
702
    } else {
2133
258
        put_set_field(out, ofp_version, MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
2134
258
    }
2135
960
}
2136
2137
static char * OVS_WARN_UNUSED_RESULT
2138
parse_SET_IP_DSCP(char *arg, const struct ofpact_parse_params *pp)
2139
2140
1.05k
{
2141
1.05k
    uint8_t tos;
2142
1.05k
    char *error;
2143
2144
1.05k
    error = str_to_u8(arg, "TOS", &tos);
2145
1.05k
    if (error) {
2146
10
        return error;
2147
10
    }
2148
2149
1.04k
    if (tos & ~IP_DSCP_MASK) {
2150
3
        return xasprintf("%s: not a valid TOS", arg);
2151
3
    }
2152
1.04k
    ofpact_put_SET_IP_DSCP(pp->ofpacts)->dscp = tos;
2153
1.04k
    return NULL;
2154
1.04k
}
2155
2156
static void
2157
format_SET_IP_DSCP(const struct ofpact_dscp *a,
2158
                   const struct ofpact_format_params *fp)
2159
5.50k
{
2160
5.50k
    ds_put_format(fp->s, "%smod_nw_tos:%s%d",
2161
5.50k
                  colors.param, colors.end, a->dscp);
2162
5.50k
}
2163
2164
static enum ofperr
2165
check_set_ip(struct ofpact_check_params *cp)
2166
18.2k
{
2167
18.2k
    if (!is_ip_any(&cp->match->flow)) {
2168
10.9k
        inconsistent_match(&cp->usable_protocols);
2169
10.9k
    }
2170
18.2k
    return 0;
2171
18.2k
}
2172
2173
static enum ofperr
2174
check_SET_IP_DSCP(const struct ofpact_dscp *a OVS_UNUSED,
2175
                  struct ofpact_check_params *cp)
2176
6.34k
{
2177
6.34k
    return check_set_ip(cp);
2178
6.34k
}
2179

2180
/* Set IPv4/v6 ECN actions. */
2181
2182
static enum ofperr
2183
decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn,
2184
                              enum ofp_version ofp_version OVS_UNUSED,
2185
                              struct ofpbuf *out)
2186
2.25k
{
2187
2.25k
    if (ecn & ~IP_ECN_MASK) {
2188
1.51k
        return OFPERR_OFPBAC_BAD_ARGUMENT;
2189
1.51k
    } else {
2190
735
        ofpact_put_SET_IP_ECN(out)->ecn = ecn;
2191
735
        return 0;
2192
735
    }
2193
2.25k
}
2194
2195
static void
2196
encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
2197
                  enum ofp_version ofp_version, struct ofpbuf *out)
2198
1.23k
{
2199
1.23k
    uint8_t ecn = ip_ecn->ecn;
2200
1.23k
    if (ofp_version == OFP10_VERSION) {
2201
819
        struct mf_subfield dst = { .field = mf_from_id(MFF_IP_ECN),
2202
819
                                   .ofs = 0, .n_bits = 2 };
2203
819
        put_reg_load(out, &dst, ecn);
2204
819
    } else if (ofp_version == OFP11_VERSION) {
2205
195
        put_OFPAT11_SET_NW_ECN(out, ecn);
2206
217
    } else {
2207
217
        put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
2208
217
    }
2209
1.23k
}
2210
2211
static char * OVS_WARN_UNUSED_RESULT
2212
parse_SET_IP_ECN(char *arg, const struct ofpact_parse_params *pp)
2213
1.27k
{
2214
1.27k
    uint8_t ecn;
2215
1.27k
    char *error;
2216
2217
1.27k
    error = str_to_u8(arg, "ECN", &ecn);
2218
1.27k
    if (error) {
2219
10
        return error;
2220
10
    }
2221
2222
1.26k
    if (ecn & ~IP_ECN_MASK) {
2223
1
        return xasprintf("%s: not a valid ECN", arg);
2224
1
    }
2225
1.26k
    ofpact_put_SET_IP_ECN(pp->ofpacts)->ecn = ecn;
2226
1.26k
    return NULL;
2227
1.26k
}
2228
2229
static void
2230
format_SET_IP_ECN(const struct ofpact_ecn *a,
2231
                  const struct ofpact_format_params *fp)
2232
598
{
2233
598
    ds_put_format(fp->s, "%smod_nw_ecn:%s%d",
2234
598
                  colors.param, colors.end, a->ecn);
2235
598
}
2236
2237
static enum ofperr
2238
check_SET_IP_ECN(const struct ofpact_ecn *a OVS_UNUSED,
2239
                 struct ofpact_check_params *cp)
2240
1.28k
{
2241
1.28k
    return check_set_ip(cp);
2242
1.28k
}
2243

2244
/* Set IPv4/v6 TTL actions. */
2245
2246
static enum ofperr
2247
decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl,
2248
                              enum ofp_version ofp_version OVS_UNUSED,
2249
                              struct ofpbuf *out)
2250
645
{
2251
645
    ofpact_put_SET_IP_TTL(out)->ttl = ttl;
2252
645
    return 0;
2253
645
}
2254
2255
static void
2256
encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
2257
                  enum ofp_version ofp_version, struct ofpbuf *out)
2258
1.45k
{
2259
1.45k
    if (ofp_version >= OFP11_VERSION) {
2260
377
        put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
2261
1.07k
    } else {
2262
1.07k
        struct mf_subfield dst = { .field = mf_from_id(MFF_IP_TTL),
2263
1.07k
                                   .ofs = 0, .n_bits = 8 };
2264
1.07k
        put_reg_load(out, &dst, ttl->ttl);
2265
1.07k
    }
2266
1.45k
}
2267
2268
static char * OVS_WARN_UNUSED_RESULT
2269
parse_SET_IP_TTL(char *arg, const struct ofpact_parse_params *pp)
2270
2271
1.79k
{
2272
1.79k
    uint8_t ttl;
2273
1.79k
    char *error;
2274
2275
1.79k
    error = str_to_u8(arg, "TTL", &ttl);
2276
1.79k
    if (error) {
2277
21
        return error;
2278
21
    }
2279
2280
1.77k
    ofpact_put_SET_IP_TTL(pp->ofpacts)->ttl = ttl;
2281
1.77k
    return NULL;
2282
1.79k
}
2283
2284
static void
2285
format_SET_IP_TTL(const struct ofpact_ip_ttl *a,
2286
                  const struct ofpact_format_params *fp)
2287
631
{
2288
631
    ds_put_format(fp->s, "%smod_nw_ttl:%s%d",
2289
631
                  colors.param, colors.end, a->ttl);
2290
631
}
2291
2292
static enum ofperr
2293
check_SET_IP_TTL(const struct ofpact_ip_ttl *a OVS_UNUSED,
2294
                 struct ofpact_check_params *cp)
2295
2.25k
{
2296
2.25k
    return check_set_ip(cp);
2297
2.25k
}
2298

2299
/* Set TCP/UDP/SCTP port actions. */
2300
2301
static enum ofperr
2302
decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port,
2303
                            enum ofp_version ofp_version OVS_UNUSED,
2304
                            struct ofpbuf *out)
2305
11.9k
{
2306
11.9k
    ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
2307
11.9k
    return 0;
2308
11.9k
}
2309
2310
static enum ofperr
2311
decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port,
2312
                            enum ofp_version ofp_version OVS_UNUSED,
2313
                            struct ofpbuf *out)
2314
12.3k
{
2315
12.3k
    ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
2316
12.3k
    return 0;
2317
12.3k
}
2318
2319
static void
2320
encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
2321
                   enum ofp_version ofp_version, enum ofp_raw_action_type raw,
2322
                   enum mf_field_id field, struct ofpbuf *out)
2323
4.10k
{
2324
4.10k
    uint16_t port = l4_port->port;
2325
2326
4.10k
    if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
2327
521
        put_set_field(out, ofp_version, field, port);
2328
3.58k
    } else {
2329
3.58k
        ofpact_put_raw(out, ofp_version, raw, port);
2330
3.58k
    }
2331
4.10k
}
2332
2333
static void
2334
encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
2335
                       enum ofp_version ofp_version, struct ofpbuf *out)
2336
2.06k
{
2337
2.06k
    uint8_t proto = l4_port->flow_ip_proto;
2338
2.06k
    enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
2339
2.06k
                              : proto == IPPROTO_UDP ? MFF_UDP_SRC
2340
1.81k
                              : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
2341
1.41k
                              : MFF_N_IDS);
2342
2343
2.06k
    encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
2344
2.06k
}
2345
2346
static void
2347
encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
2348
                       enum ofp_version ofp_version,
2349
                       struct ofpbuf *out)
2350
2.03k
{
2351
2.03k
    uint8_t proto = l4_port->flow_ip_proto;
2352
2.03k
    enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
2353
2.03k
                              : proto == IPPROTO_UDP ? MFF_UDP_DST
2354
1.69k
                              : proto == IPPROTO_SCTP ? MFF_SCTP_DST
2355
1.45k
                              : MFF_N_IDS);
2356
2357
2.03k
    encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
2358
2.03k
}
2359
2360
static char * OVS_WARN_UNUSED_RESULT
2361
parse_SET_L4_SRC_PORT(char *arg, const struct ofpact_parse_params *pp)
2362
2.59k
{
2363
2.59k
    return str_to_u16(arg, "source port",
2364
2.59k
                      &ofpact_put_SET_L4_SRC_PORT(pp->ofpacts)->port);
2365
2.59k
}
2366
2367
static char * OVS_WARN_UNUSED_RESULT
2368
parse_SET_L4_DST_PORT(char *arg, const struct ofpact_parse_params *pp)
2369
2.28k
{
2370
2.28k
    return str_to_u16(arg, "destination port",
2371
2.28k
                      &ofpact_put_SET_L4_DST_PORT(pp->ofpacts)->port);
2372
2.28k
}
2373
2374
static void
2375
format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a,
2376
                       const struct ofpact_format_params *fp)
2377
5.72k
{
2378
5.72k
    ds_put_format(fp->s, "%smod_tp_src:%s%d",
2379
5.72k
                  colors.param, colors.end, a->port);
2380
5.72k
}
2381
2382
static void
2383
format_SET_L4_DST_PORT(const struct ofpact_l4_port *a,
2384
                       const struct ofpact_format_params *fp)
2385
10.7k
{
2386
10.7k
    ds_put_format(fp->s, "%smod_tp_dst:%s%d",
2387
10.7k
                  colors.param, colors.end, a->port);
2388
10.7k
}
2389
2390
static enum ofperr
2391
check_set_l4_port(struct ofpact_l4_port *a, struct ofpact_check_params *cp)
2392
19.5k
{
2393
19.5k
    const struct flow *flow = &cp->match->flow;
2394
19.5k
    if (!is_ip_any(flow)
2395
15.0k
        || flow->nw_frag & FLOW_NW_FRAG_LATER
2396
14.5k
        || (flow->nw_proto != IPPROTO_TCP &&
2397
9.35k
            flow->nw_proto != IPPROTO_UDP &&
2398
7.44k
            flow->nw_proto != IPPROTO_SCTP)) {
2399
7.44k
        inconsistent_match(&cp->usable_protocols);
2400
7.44k
    }
2401
2402
    /* Note the transport protocol in use, to allow this action to be converted
2403
     * to an OF1.2 set_field action later if necessary. */
2404
19.5k
    a->flow_ip_proto = flow->nw_proto;
2405
2406
19.5k
    return 0;
2407
19.5k
}
2408
2409
static enum ofperr
2410
check_SET_L4_SRC_PORT(struct ofpact_l4_port *a, struct ofpact_check_params *cp)
2411
6.68k
{
2412
6.68k
    return check_set_l4_port(a, cp);
2413
6.68k
}
2414
2415
static enum ofperr
2416
check_SET_L4_DST_PORT(struct ofpact_l4_port *a, struct ofpact_check_params *cp)
2417
12.8k
{
2418
12.8k
    return check_set_l4_port(a, cp);
2419
12.8k
}
2420

2421
/* Action structure for OFPAT_COPY_FIELD. */
2422
struct ofp15_action_copy_field {
2423
    ovs_be16 type;              /* OFPAT_COPY_FIELD. */
2424
    ovs_be16 len;               /* Length is padded to 64 bits. */
2425
    ovs_be16 n_bits;            /* Number of bits to copy. */
2426
    ovs_be16 src_offset;        /* Starting bit offset in source. */
2427
    ovs_be16 dst_offset;        /* Starting bit offset in destination. */
2428
    uint8_t pad[2];
2429
    /* Followed by:
2430
     * - OXM header for source field.
2431
     * - OXM header for destination field.
2432
     * - Padding with 0-bytes to a multiple of 8 bytes.
2433
     * The "pad2" member is the beginning of the above. */
2434
    uint8_t pad2[4];
2435
};
2436
OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
2437
2438
/* Action structure for OpenFlow 1.3 extension copy-field action.. */
2439
struct onf_action_copy_field {
2440
    ovs_be16 type;              /* OFPAT_EXPERIMENTER. */
2441
    ovs_be16 len;               /* Length is padded to 64 bits. */
2442
    ovs_be32 experimenter;      /* ONF_VENDOR_ID. */
2443
    ovs_be16 exp_type;          /* 3200. */
2444
    uint8_t pad[2];             /* Not used. */
2445
    ovs_be16 n_bits;            /* Number of bits to copy. */
2446
    ovs_be16 src_offset;        /* Starting bit offset in source. */
2447
    ovs_be16 dst_offset;        /* Starting bit offset in destination. */
2448
    uint8_t pad2[2];            /* Not used. */
2449
    /* Followed by:
2450
     * - OXM header for source field.
2451
     * - OXM header for destination field.
2452
     * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
2453
     * The "pad3" member is the beginning of the above. */
2454
    uint8_t pad3[4];            /* Not used. */
2455
};
2456
OFP_ASSERT(sizeof(struct onf_action_copy_field) == 24);
2457
2458
/* Action structure for NXAST_REG_MOVE.
2459
 *
2460
 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
2461
 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
2462
 * bit 'c').  Bit numbering starts at 0 for the least-significant bit, 1 for
2463
 * the next most significant bit, and so on.
2464
 *
2465
 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0.  (It doesn't make
2466
 * sense to use nxm_hasmask=1 because the action does not do any kind of
2467
 * matching; it uses the actual value of a field.)
2468
 *
2469
 * The following nxm_header values are potentially acceptable as 'src':
2470
 *
2471
 *   - NXM_OF_IN_PORT
2472
 *   - NXM_OF_ETH_DST
2473
 *   - NXM_OF_ETH_SRC
2474
 *   - NXM_OF_ETH_TYPE
2475
 *   - NXM_OF_VLAN_TCI
2476
 *   - NXM_OF_IP_TOS
2477
 *   - NXM_OF_IP_PROTO
2478
 *   - NXM_OF_IP_SRC
2479
 *   - NXM_OF_IP_DST
2480
 *   - NXM_OF_TCP_SRC
2481
 *   - NXM_OF_TCP_DST
2482
 *   - NXM_OF_UDP_SRC
2483
 *   - NXM_OF_UDP_DST
2484
 *   - NXM_OF_ICMP_TYPE
2485
 *   - NXM_OF_ICMP_CODE
2486
 *   - NXM_OF_ARP_OP
2487
 *   - NXM_OF_ARP_SPA
2488
 *   - NXM_OF_ARP_TPA
2489
 *   - NXM_NX_TUN_ID
2490
 *   - NXM_NX_ARP_SHA
2491
 *   - NXM_NX_ARP_THA
2492
 *   - NXM_NX_ICMPV6_TYPE
2493
 *   - NXM_NX_ICMPV6_CODE
2494
 *   - NXM_NX_ND_SLL
2495
 *   - NXM_NX_ND_TLL
2496
 *   - NXM_NX_REG(idx) for idx in the switch's accepted range.
2497
 *   - NXM_NX_PKT_MARK
2498
 *   - NXM_NX_TUN_IPV4_SRC
2499
 *   - NXM_NX_TUN_IPV4_DST
2500
 *
2501
 * The following nxm_header values are potentially acceptable as 'dst':
2502
 *
2503
 *   - NXM_OF_ETH_DST
2504
 *   - NXM_OF_ETH_SRC
2505
 *   - NXM_OF_IP_TOS
2506
 *   - NXM_OF_IP_SRC
2507
 *   - NXM_OF_IP_DST
2508
 *   - NXM_OF_TCP_SRC
2509
 *   - NXM_OF_TCP_DST
2510
 *   - NXM_OF_UDP_SRC
2511
 *   - NXM_OF_UDP_DST
2512
 *   - NXM_OF_ICMP_TYPE
2513
 *   - NXM_OF_ICMP_CODE
2514
 *   - NXM_NX_ICMPV6_TYPE
2515
 *   - NXM_NX_ICMPV6_CODE
2516
 *   - NXM_NX_ARP_SHA
2517
 *   - NXM_NX_ARP_THA
2518
 *   - NXM_OF_ARP_OP
2519
 *   - NXM_OF_ARP_SPA
2520
 *   - NXM_OF_ARP_TPA
2521
 *     Modifying any of the above fields changes the corresponding packet
2522
 *     header.
2523
 *
2524
 *   - NXM_OF_IN_PORT
2525
 *
2526
 *   - NXM_NX_REG(idx) for idx in the switch's accepted range.
2527
 *
2528
 *   - NXM_NX_PKT_MARK
2529
 *
2530
 *   - NXM_OF_VLAN_TCI.  Modifying this field's value has side effects on the
2531
 *     packet's 802.1Q header.  Setting a value with CFI=0 removes the 802.1Q
2532
 *     header (if any), ignoring the other bits.  Setting a value with CFI=1
2533
 *     adds or modifies the 802.1Q header appropriately, setting the TCI field
2534
 *     to the field's new value (with the CFI bit masked out).
2535
 *
2536
 *   - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST.  Modifying
2537
 *     any of these values modifies the corresponding tunnel header field used
2538
 *     for the packet's next tunnel encapsulation, if allowed by the
2539
 *     configuration of the output tunnel port.
2540
 *
2541
 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
2542
 * nx_match satisfies its prerequisites.  For example, NXM_OF_IP_TOS may be
2543
 * used only if the flow's nx_match includes an nxm_entry that specifies
2544
 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
2545
 *
2546
 * The switch will reject actions for which src_ofs+n_bits is greater than the
2547
 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
2548
 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
2549
 *
2550
 * This action behaves properly when 'src' overlaps with 'dst', that is, it
2551
 * behaves as if 'src' were copied out to a temporary buffer, then the
2552
 * temporary buffer copied to 'dst'.
2553
 */
2554
struct nx_action_reg_move {
2555
    ovs_be16 type;                  /* OFPAT_VENDOR. */
2556
    ovs_be16 len;                   /* Length is 24. */
2557
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
2558
    ovs_be16 subtype;               /* NXAST_REG_MOVE. */
2559
    ovs_be16 n_bits;                /* Number of bits. */
2560
    ovs_be16 src_ofs;               /* Starting bit offset in source. */
2561
    ovs_be16 dst_ofs;               /* Starting bit offset in destination. */
2562
    /* Followed by:
2563
     * - OXM/NXM header for source field (4 or 8 bytes).
2564
     * - OXM/NXM header for destination field (4 or 8 bytes).
2565
     * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
2566
};
2567
OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
2568
2569
static enum ofperr
2570
decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
2571
                    const void *action, ovs_be16 action_len, size_t oxm_offset,
2572
                    const struct vl_mff_map *vl_mff_map,
2573
                    uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2574
105
{
2575
105
    struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2576
105
    enum ofperr error;
2577
2578
105
    move->ofpact.raw = ONFACT_RAW13_COPY_FIELD;
2579
105
    move->src.ofs = ntohs(src_offset);
2580
105
    move->src.n_bits = ntohs(n_bits);
2581
105
    move->dst.ofs = ntohs(dst_offset);
2582
105
    move->dst.n_bits = ntohs(n_bits);
2583
2584
105
    struct ofpbuf b = ofpbuf_const_initializer(action, ntohs(action_len));
2585
105
    ofpbuf_pull(&b, oxm_offset);
2586
2587
105
    error = mf_vl_mff_nx_pull_header(&b, vl_mff_map, &move->src.field, NULL,
2588
105
                                     tlv_bitmap);
2589
105
    if (error) {
2590
21
        return error;
2591
21
    }
2592
84
    error = mf_vl_mff_nx_pull_header(&b, vl_mff_map, &move->dst.field, NULL,
2593
84
                                     tlv_bitmap);
2594
84
    if (error) {
2595
18
        return error;
2596
18
    }
2597
2598
66
    if (!is_all_zeros(b.data, b.size)) {
2599
16
        return OFPERR_NXBRC_MUST_BE_ZERO;
2600
16
    }
2601
2602
50
    return nxm_reg_move_check(move, NULL);
2603
66
}
2604
2605
static enum ofperr
2606
decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
2607
                              enum ofp_version ofp_version OVS_UNUSED,
2608
                              const struct vl_mff_map *vl_mff_map,
2609
                              uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2610
95
{
2611
95
    return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2612
95
                               oacf->n_bits, oacf, oacf->len,
2613
95
                               OBJECT_OFFSETOF(oacf, pad2), vl_mff_map,
2614
95
                               tlv_bitmap, ofpacts);
2615
95
}
2616
2617
static enum ofperr
2618
decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field *oacf,
2619
                               enum ofp_version ofp_version OVS_UNUSED,
2620
                               const struct vl_mff_map *vl_mff_map,
2621
                               uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2622
10
{
2623
10
    return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2624
10
                               oacf->n_bits, oacf, oacf->len,
2625
10
                               OBJECT_OFFSETOF(oacf, pad3), vl_mff_map,
2626
10
                               tlv_bitmap, ofpacts);
2627
10
}
2628
2629
static enum ofperr
2630
decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
2631
                          enum ofp_version ofp_version OVS_UNUSED,
2632
                          const struct vl_mff_map *vl_mff_map,
2633
                          uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2634
1.32k
{
2635
1.32k
    struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2636
1.32k
    enum ofperr error;
2637
2638
1.32k
    move->ofpact.raw = NXAST_RAW_REG_MOVE;
2639
1.32k
    move->src.ofs = ntohs(narm->src_ofs);
2640
1.32k
    move->src.n_bits = ntohs(narm->n_bits);
2641
1.32k
    move->dst.ofs = ntohs(narm->dst_ofs);
2642
1.32k
    move->dst.n_bits = ntohs(narm->n_bits);
2643
2644
1.32k
    struct ofpbuf b = ofpbuf_const_initializer(narm, ntohs(narm->len));
2645
1.32k
    ofpbuf_pull(&b, sizeof *narm);
2646
2647
1.32k
    error = mf_vl_mff_nx_pull_header(&b, vl_mff_map, &move->src.field, NULL,
2648
1.32k
                                     tlv_bitmap);
2649
1.32k
    if (error) {
2650
430
        return error;
2651
430
    }
2652
2653
894
    error = mf_vl_mff_nx_pull_header(&b, vl_mff_map, &move->dst.field, NULL,
2654
894
                                     tlv_bitmap);
2655
894
    if (error) {
2656
257
        return error;
2657
257
    }
2658
2659
637
    if (!is_all_zeros(b.data, b.size)) {
2660
18
        return OFPERR_NXBRC_MUST_BE_ZERO;
2661
18
    }
2662
2663
619
    return nxm_reg_move_check(move, NULL);
2664
637
}
2665
2666
static void
2667
encode_REG_MOVE(const struct ofpact_reg_move *move,
2668
                enum ofp_version ofp_version, struct ofpbuf *out)
2669
572
{
2670
    /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2671
     * NXAST_RAW_REG_MOVE is somewhat difficult.  Neither one is guaranteed to
2672
     * be supported by every OpenFlow 1.3 implementation.  It would be ideal to
2673
     * probe for support.  Until we have that ability, we currently prefer
2674
     * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2675
     * versions.  */
2676
572
    size_t start_ofs = out->size;
2677
572
    if (ofp_version >= OFP15_VERSION) {
2678
0
        struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
2679
0
        copy->n_bits = htons(move->dst.n_bits);
2680
0
        copy->src_offset = htons(move->src.ofs);
2681
0
        copy->dst_offset = htons(move->dst.ofs);
2682
0
        out->size = out->size - sizeof copy->pad2;
2683
0
        nx_put_mff_header(out, move->src.field, ofp_version, false);
2684
0
        nx_put_mff_header(out, move->dst.field, ofp_version, false);
2685
572
    } else if (ofp_version == OFP13_VERSION
2686
295
               && move->ofpact.raw == ONFACT_RAW13_COPY_FIELD) {
2687
0
        struct onf_action_copy_field *copy = put_ONFACT13_COPY_FIELD(out);
2688
0
        copy->n_bits = htons(move->dst.n_bits);
2689
0
        copy->src_offset = htons(move->src.ofs);
2690
0
        copy->dst_offset = htons(move->dst.ofs);
2691
0
        out->size = out->size - sizeof copy->pad3;
2692
0
        nx_put_mff_header(out, move->src.field, ofp_version, false);
2693
0
        nx_put_mff_header(out, move->dst.field, ofp_version, false);
2694
572
    } else {
2695
572
        struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
2696
572
        narm->n_bits = htons(move->dst.n_bits);
2697
572
        narm->src_ofs = htons(move->src.ofs);
2698
572
        narm->dst_ofs = htons(move->dst.ofs);
2699
572
        nx_put_mff_header(out, move->src.field, 0, false);
2700
572
        nx_put_mff_header(out, move->dst.field, 0, false);
2701
572
    }
2702
572
    pad_ofpat(out, start_ofs);
2703
572
}
2704
2705
static char * OVS_WARN_UNUSED_RESULT
2706
parse_REG_MOVE(const char *arg, const struct ofpact_parse_params *pp)
2707
624
{
2708
624
    struct ofpact_reg_move *move = ofpact_put_REG_MOVE(pp->ofpacts);
2709
624
    return nxm_parse_reg_move(move, arg);
2710
624
}
2711
2712
static void
2713
format_REG_MOVE(const struct ofpact_reg_move *a,
2714
                const struct ofpact_format_params *fp)
2715
476
{
2716
476
    nxm_format_reg_move(a, fp->s);
2717
476
}
2718
2719
static enum ofperr
2720
check_REG_MOVE(const struct ofpact_reg_move *a,
2721
               const struct ofpact_check_params *cp)
2722
591
{
2723
591
    return nxm_reg_move_check(a, cp->match);
2724
591
}
2725

2726
/* Action structure for OFPAT12_SET_FIELD. */
2727
struct ofp12_action_set_field {
2728
    ovs_be16 type;                  /* OFPAT12_SET_FIELD. */
2729
    ovs_be16 len;                   /* Length is padded to 64 bits. */
2730
2731
    /* Followed by:
2732
     * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2733
     * - Enough 0-bytes to pad out to a multiple of 64 bits.
2734
     *
2735
     * The "pad" member is the beginning of the above. */
2736
    uint8_t pad[4];
2737
};
2738
OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
2739
2740
/* Action structure for NXAST_REG_LOAD.
2741
 *
2742
 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2743
 * within 'a' numbered 'b' through 'c' (not including bit 'c').  Bit numbering
2744
 * starts at 0 for the least-significant bit, 1 for the next most significant
2745
 * bit, and so on.
2746
 *
2747
 * 'dst' is an nxm_header with nxm_hasmask=0.  See the documentation for
2748
 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2749
 * loading them.
2750
 *
2751
 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2752
 * to avoid enlarging the structure by another 8 bytes.  To allow 'n_bits' to
2753
 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2754
 * also stored as one less than its true value:
2755
 *
2756
 *  15                           6 5                0
2757
 * +------------------------------+------------------+
2758
 * |              ofs             |    n_bits - 1    |
2759
 * +------------------------------+------------------+
2760
 *
2761
 * The switch will reject actions for which ofs+n_bits is greater than the
2762
 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2763
 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2764
 * OFPBAC_BAD_ARGUMENT.
2765
 */
2766
struct nx_action_reg_load {
2767
    ovs_be16 type;                  /* OFPAT_VENDOR. */
2768
    ovs_be16 len;                   /* Length is 24. */
2769
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
2770
    ovs_be16 subtype;               /* NXAST_REG_LOAD. */
2771
    ovs_be16 ofs_nbits;             /* (ofs << 6) | (n_bits - 1). */
2772
    ovs_be32 dst;                   /* Destination register. */
2773
    ovs_be64 value;                 /* Immediate value. */
2774
};
2775
OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
2776
2777
/* The NXAST_REG_LOAD2 action structure is "struct ext_action_header",
2778
 * followed by:
2779
 *
2780
 * - An NXM/OXM header, value, and optionally a mask.
2781
 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2782
 *
2783
 * The "pad" member is the beginning of the above. */
2784
2785
static enum ofperr
2786
decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2787
                       bool may_mask, const struct vl_mff_map *vl_mff_map,
2788
                       uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2789
12.2k
{
2790
12.2k
    struct ofpbuf b = ofpbuf_const_initializer(oasf, ntohs(oasf->len));
2791
12.2k
    ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
2792
2793
12.2k
    union mf_value value, mask;
2794
12.2k
    const struct mf_field *field;
2795
12.2k
    enum ofperr error;
2796
12.2k
    error  = mf_vl_mff_nx_pull_entry(&b, vl_mff_map, &field, &value,
2797
12.2k
                                     may_mask ? &mask : NULL, tlv_bitmap);
2798
12.2k
    if (error) {
2799
2.68k
        return (error == OFPERR_OFPBMC_BAD_MASK
2800
2.68k
                ? OFPERR_OFPBAC_BAD_SET_MASK
2801
2.68k
                : error);
2802
2.68k
    }
2803
2804
9.53k
    if (!may_mask) {
2805
8.46k
        memset(&mask, 0xff, field->n_bytes);
2806
8.46k
    }
2807
2808
9.53k
    if (!is_all_zeros(b.data, b.size)) {
2809
1.27k
        return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2810
1.27k
    }
2811
2812
    /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2813
     * Set-Field. */
2814
8.26k
    if (field->id == MFF_IN_PORT_OXM) {
2815
34
        return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2816
34
    }
2817
2818
    /* oxm_length is now validated to be compatible with mf_value. */
2819
8.23k
    if (!field->writable) {
2820
10
        VLOG_WARN_RL(&rl, "destination field %s is not writable",
2821
10
                     field->name);
2822
10
        return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2823
10
    }
2824
2825
    /* The value must be valid for match.  OpenFlow 1.5 also says,
2826
     * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2827
     * a 1-bit in oxm_value and in oxm_mask." */
2828
8.22k
    if (!mf_is_value_valid(field, &value)
2829
7.98k
        || (field->id == MFF_VLAN_VID
2830
1.52k
            && (!(mask.be16 & htons(OFPVID12_PRESENT))
2831
1.73k
                || !(value.be16 & htons(OFPVID12_PRESENT))))) {
2832
1.73k
        struct ds ds = DS_EMPTY_INITIALIZER;
2833
1.73k
        mf_format(field, &value, NULL, NULL, &ds);
2834
1.73k
        VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
2835
1.73k
                     field->name, ds_cstr(&ds));
2836
1.73k
        ds_destroy(&ds);
2837
2838
1.73k
        return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2839
1.73k
    }
2840
2841
6.49k
    ofpact_put_set_field(ofpacts, field, &value, &mask);
2842
6.49k
    return 0;
2843
8.22k
}
2844
2845
static enum ofperr
2846
decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
2847
                             enum ofp_version ofp_version OVS_UNUSED,
2848
                             const struct vl_mff_map *vl_mff_map,
2849
                             uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2850
10.5k
{
2851
10.5k
    return decode_ofpat_set_field(oasf, false, vl_mff_map, tlv_bitmap,
2852
10.5k
                                  ofpacts);
2853
10.5k
}
2854
2855
static enum ofperr
2856
decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
2857
                             enum ofp_version ofp_version OVS_UNUSED,
2858
                             const struct vl_mff_map *vl_mff_map,
2859
                             uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
2860
1.70k
{
2861
1.70k
    return decode_ofpat_set_field(oasf, true, vl_mff_map, tlv_bitmap, ofpacts);
2862
1.70k
}
2863
2864
static enum ofperr
2865
decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2866
                          enum ofp_version ofp_version OVS_UNUSED,
2867
                          const struct vl_mff_map *vl_mff_map,
2868
                          uint64_t *tlv_bitmap, struct ofpbuf *out)
2869
4.99k
{
2870
4.99k
    struct mf_subfield dst;
2871
4.99k
    enum ofperr error;
2872
2873
4.99k
    dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2874
4.99k
    dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2875
4.99k
    error = mf_vl_mff_mf_from_nxm_header(ntohl(narl->dst), vl_mff_map,
2876
4.99k
                                         &dst.field, tlv_bitmap);
2877
4.99k
    if (error) {
2878
82
        return error;
2879
82
    }
2880
2881
4.91k
    error = mf_check_dst(&dst, NULL);
2882
4.91k
    if (error) {
2883
41
        return error;
2884
41
    }
2885
2886
    /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2887
     * narl->value. */
2888
4.87k
    if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2889
851
        return OFPERR_OFPBAC_BAD_ARGUMENT;
2890
851
    }
2891
2892
4.02k
    struct ofpact_set_field *sf = ofpact_put_reg_load(out, dst.field, NULL,
2893
4.02k
                                                      NULL);
2894
4.02k
    bitwise_put(ntohll(narl->value),
2895
4.02k
                sf->value, dst.field->n_bytes, dst.ofs,
2896
4.02k
                dst.n_bits);
2897
4.02k
    bitwise_put(UINT64_MAX,
2898
4.02k
                ofpact_set_field_mask(sf), dst.field->n_bytes, dst.ofs,
2899
4.02k
                dst.n_bits);
2900
4.02k
    return 0;
2901
4.87k
}
2902
2903
static enum ofperr
2904
decode_NXAST_RAW_REG_LOAD2(const struct ext_action_header *eah,
2905
                           enum ofp_version ofp_version OVS_UNUSED,
2906
                           const struct vl_mff_map *vl_mff_map,
2907
                           uint64_t *tlv_bitmap, struct ofpbuf *out)
2908
9.18k
{
2909
9.18k
    struct ofpbuf b = ofpbuf_const_initializer(eah, ntohs(eah->len));
2910
9.18k
    ofpbuf_pull(&b, OBJECT_OFFSETOF(eah, pad));
2911
2912
9.18k
    union mf_value value, mask;
2913
9.18k
    const struct mf_field *field;
2914
9.18k
    enum ofperr error;
2915
9.18k
    error = mf_vl_mff_nx_pull_entry(&b, vl_mff_map, &field, &value, &mask,
2916
9.18k
                                    tlv_bitmap);
2917
9.18k
    if (error) {
2918
1.07k
        return error;
2919
1.07k
    }
2920
2921
8.11k
    if (!is_all_zeros(b.data, b.size)) {
2922
34
        return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2923
34
    }
2924
2925
8.07k
    if (!field->writable) {
2926
288
        VLOG_WARN_RL(&rl, "destination field %s is not writable", field->name);
2927
288
        return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2928
288
    }
2929
2930
    /* Put value and mask. */
2931
7.79k
    ofpact_put_reg_load2(out, field, &value, &mask);
2932
7.79k
    return 0;
2933
8.07k
}
2934
2935
static void
2936
put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2937
              enum mf_field_id field, uint64_t value_)
2938
4.91k
{
2939
4.91k
    struct ofp12_action_set_field *oasf OVS_UNUSED;
2940
4.91k
    int n_bytes = mf_from_id(field)->n_bytes;
2941
4.91k
    size_t start_ofs = openflow->size;
2942
4.91k
    union mf_value value;
2943
2944
4.91k
    value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2945
2946
4.91k
    oasf = put_OFPAT12_SET_FIELD(openflow);
2947
4.91k
    openflow->size = openflow->size - sizeof oasf->pad;
2948
4.91k
    nx_put_entry(openflow, mf_from_id(field), ofp_version, &value, NULL);
2949
4.91k
    pad_ofpat(openflow, start_ofs);
2950
4.91k
}
2951
2952
static void
2953
put_reg_load(struct ofpbuf *openflow,
2954
             const struct mf_subfield *dst, uint64_t value)
2955
72.2k
{
2956
72.2k
    ovs_assert(dst->n_bits <= 64);
2957
2958
72.2k
    struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2959
72.2k
    narl->ofs_nbits = nxm_encode_ofs_nbits(dst->ofs, dst->n_bits);
2960
72.2k
    narl->dst = htonl(nxm_header_from_mff(dst->field));
2961
72.2k
    narl->value = htonll(value);
2962
72.2k
}
2963
2964
static bool
2965
next_load_segment(const struct ofpact_set_field *sf,
2966
                  struct mf_subfield *dst, uint64_t *value)
2967
88.9k
{
2968
88.9k
    int n_bits = sf->field->n_bits;
2969
88.9k
    int n_bytes = sf->field->n_bytes;
2970
88.9k
    int start = dst->ofs + dst->n_bits;
2971
2972
88.9k
    if (start < n_bits) {
2973
84.4k
        dst->field = sf->field;
2974
84.4k
        dst->ofs = bitwise_scan(ofpact_set_field_mask(sf), n_bytes, 1, start,
2975
84.4k
                                n_bits);
2976
84.4k
        if (dst->ofs < n_bits) {
2977
73.2k
            dst->n_bits = bitwise_scan(ofpact_set_field_mask(sf), n_bytes, 0,
2978
73.2k
                                       dst->ofs + 1,
2979
73.2k
                                       MIN(dst->ofs + 64, n_bits)) - dst->ofs;
2980
73.2k
            *value = bitwise_get(sf->value, n_bytes, dst->ofs, dst->n_bits);
2981
73.2k
            return true;
2982
73.2k
        }
2983
84.4k
    }
2984
15.6k
    return false;
2985
88.9k
}
2986
2987
/* Convert 'sf' to a series of REG_LOADs. */
2988
static void
2989
set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2990
20.9k
{
2991
    /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2992
     * experimenter OXM or is variable length (or if it came in as
2993
     * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2.  Otherwise use
2994
     * NXAST_REG_LOAD, which is backward compatible. */
2995
20.9k
    if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
2996
20.9k
        || !mf_nxm_header(sf->field->id) || sf->field->variable_len) {
2997
8.11k
        struct ext_action_header *eah OVS_UNUSED;
2998
8.11k
        size_t start_ofs = openflow->size;
2999
3000
8.11k
        eah = put_NXAST_REG_LOAD2(openflow);
3001
8.11k
        openflow->size = openflow->size - sizeof eah->pad;
3002
8.11k
        nx_put_entry(openflow, sf->field, 0, sf->value,
3003
8.11k
                     ofpact_set_field_mask(sf));
3004
8.11k
        pad_ofpat(openflow, start_ofs);
3005
12.8k
    } else {
3006
12.8k
        struct mf_subfield dst;
3007
12.8k
        uint64_t value;
3008
3009
12.8k
        dst.ofs = dst.n_bits = 0;
3010
83.1k
        while (next_load_segment(sf, &dst, &value)) {
3011
70.3k
            put_reg_load(openflow, &dst, value);
3012
70.3k
        }
3013
12.8k
    }
3014
20.9k
}
3015
3016
/* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
3017
 * actions, if we can, falling back to Nicira extensions if we must.
3018
 *
3019
 * We check only meta-flow types that can appear within set field actions and
3020
 * that have a mapping to compatible action types.  These struct mf_field
3021
 * definitions have a defined OXM or NXM header value and specify the field as
3022
 * writable. */
3023
static void
3024
set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
3025
                             enum ofp_version ofp_version,
3026
                             struct ofpbuf *out)
3027
21.5k
{
3028
21.5k
    switch ((int) sf->field->id) {
3029
1.45k
    case MFF_VLAN_TCI: {
3030
1.45k
        ovs_be16 tci = sf->value->be16;
3031
1.45k
        bool cfi = (tci & htons(VLAN_CFI)) != 0;
3032
1.45k
        uint16_t vid = vlan_tci_to_vid(tci);
3033
1.45k
        uint8_t pcp = vlan_tci_to_pcp(tci);
3034
3035
1.45k
        if (ofp_version < OFP11_VERSION) {
3036
            /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
3037
             *
3038
             * If CFI=1, Add or modify VLAN VID & PCP.
3039
             * If CFI=0, strip VLAN header, if any.
3040
             */
3041
493
            if (cfi) {
3042
295
                put_OFPAT10_SET_VLAN_VID(out, vid);
3043
295
                put_OFPAT10_SET_VLAN_PCP(out, pcp);
3044
295
            } else {
3045
198
                put_OFPAT10_STRIP_VLAN(out);
3046
198
            }
3047
957
        } else {
3048
            /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
3049
             *
3050
             * If CFI=1, Add or modify VLAN VID & PCP.
3051
             *    OpenFlow 1.1 set actions only apply if the packet
3052
             *    already has VLAN tags.  To be sure that is the case
3053
             *    we have to push a VLAN header.  As we do not support
3054
             *    multiple layers of VLANs, this is a no-op, if a VLAN
3055
             *    header already exists.  This may backfire, however,
3056
             *    when we start supporting multiple layers of VLANs.
3057
             * If CFI=0, strip VLAN header, if any.
3058
             */
3059
957
            if (cfi) {
3060
                /* Push a VLAN tag, if one was not seen at action validation
3061
                 * time. */
3062
446
                if (!sf->flow_has_vlan) {
3063
320
                    put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
3064
320
                }
3065
446
                put_OFPAT11_SET_VLAN_VID(out, vid);
3066
446
                put_OFPAT11_SET_VLAN_PCP(out, pcp);
3067
511
            } else {
3068
                /* If the flow did not match on vlan, we have no way of
3069
                 * knowing if the vlan tag exists, so we must POP just to be
3070
                 * sure. */
3071
511
                put_OFPAT11_POP_VLAN(out);
3072
511
            }
3073
957
        }
3074
1.45k
        break;
3075
0
    }
3076
3077
388
    case MFF_VLAN_VID: {
3078
388
        uint16_t vid = ntohs(sf->value->be16) & VLAN_VID_MASK;
3079
388
        if (ofp_version == OFP10_VERSION) {
3080
194
            put_OFPAT10_SET_VLAN_VID(out, vid);
3081
194
        } else {
3082
194
            put_OFPAT11_SET_VLAN_VID(out, vid);
3083
194
        }
3084
388
        break;
3085
0
    }
3086
3087
393
    case MFF_VLAN_PCP:
3088
393
        if (ofp_version == OFP10_VERSION) {
3089
195
            put_OFPAT10_SET_VLAN_PCP(out, sf->value->u8);
3090
198
        } else {
3091
198
            put_OFPAT11_SET_VLAN_PCP(out, sf->value->u8);
3092
198
        }
3093
393
        break;
3094
3095
194
    case MFF_ETH_SRC:
3096
194
        put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr = sf->value->mac;
3097
194
        break;
3098
3099
195
    case MFF_ETH_DST:
3100
195
        put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr = sf->value->mac;
3101
195
        break;
3102
3103
194
    case MFF_IPV4_SRC:
3104
194
        put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value->be32);
3105
194
        break;
3106
3107
66
    case MFF_IPV4_DST:
3108
66
        put_OFPAT_SET_NW_DST(out, ofp_version, sf->value->be32);
3109
66
        break;
3110
3111
246
    case MFF_IP_DSCP:
3112
246
        put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value->u8);
3113
246
        break;
3114
3115
215
    case MFF_IP_DSCP_SHIFTED:
3116
215
        put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value->u8 << 2);
3117
215
        break;
3118
3119
199
    case MFF_IP_ECN: {
3120
199
        struct ofpact_ecn ip_ecn = { .ecn = sf->value->u8 };
3121
199
        encode_SET_IP_ECN(&ip_ecn, ofp_version, out);
3122
199
        break;
3123
0
    }
3124
3125
200
    case MFF_TCP_SRC:
3126
422
    case MFF_UDP_SRC:
3127
422
        put_OFPAT_SET_TP_SRC(out, sf->value->be16);
3128
422
        break;
3129
3130
308
    case MFF_TCP_DST:
3131
685
    case MFF_UDP_DST:
3132
685
        put_OFPAT_SET_TP_DST(out, sf->value->be16);
3133
685
        break;
3134
3135
16.8k
    default:
3136
16.8k
        set_field_to_nxast(sf, out);
3137
16.8k
        break;
3138
21.5k
    }
3139
21.5k
}
3140
3141
static void
3142
set_field_to_set_field(const struct ofpact_set_field *sf,
3143
                       enum ofp_version ofp_version, struct ofpbuf *out)
3144
1.27k
{
3145
1.27k
    struct ofp12_action_set_field *oasf OVS_UNUSED;
3146
1.27k
    size_t start_ofs = out->size;
3147
3148
1.27k
    oasf = put_OFPAT12_SET_FIELD(out);
3149
1.27k
    out->size = out->size - sizeof oasf->pad;
3150
1.27k
    nx_put_entry(out, sf->field, ofp_version, sf->value,
3151
1.27k
                 ofpact_set_field_mask(sf));
3152
1.27k
    pad_ofpat(out, start_ofs);
3153
1.27k
}
3154
3155
static void
3156
encode_SET_FIELD(const struct ofpact_set_field *sf,
3157
                 enum ofp_version ofp_version, struct ofpbuf *out)
3158
26.8k
{
3159
26.8k
    if (ofp_version >= OFP15_VERSION) {
3160
        /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
3161
         * entirely). */
3162
0
        set_field_to_set_field(sf, ofp_version, out);
3163
26.8k
    } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
3164
26.0k
               sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
3165
        /* It came in as reg_load, send it out the same way. */
3166
805
        set_field_to_nxast(sf, out);
3167
26.0k
    } else if (ofp_version < OFP12_VERSION) {
3168
        /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
3169
21.5k
        set_field_to_legacy_openflow(sf, ofp_version, out);
3170
21.5k
    } else if (is_all_ones(ofpact_set_field_mask(sf), sf->field->n_bytes)) {
3171
        /* We're encoding to OpenFlow 1.2, 1.3, or 1.4.  The action sets an
3172
         * entire field, so encode it as OFPAT_SET_FIELD. */
3173
1.27k
        set_field_to_set_field(sf, ofp_version, out);
3174
3.26k
    } else {
3175
        /* We're encoding to OpenFlow 1.2, 1.3, or 1.4.  The action cannot be
3176
         * encoded as OFPAT_SET_FIELD because it does not set an entire field,
3177
         * so encode it as reg_load. */
3178
3.26k
        set_field_to_nxast(sf, out);
3179
3.26k
    }
3180
26.8k
}
3181
3182
/* Parses the input argument 'arg' into the key, value, and delimiter
3183
 * components that are common across the reg_load and set_field action format.
3184
 *
3185
 * With an argument like "1->metadata", sets the following pointers to
3186
 * point within 'arg':
3187
 * key: "metadata"
3188
 * value: "1"
3189
 * delim: "->"
3190
 *
3191
 * Returns NULL if successful, otherwise a malloc()'d string describing the
3192
 * error.  The caller is responsible for freeing the returned string. */
3193
static char * OVS_WARN_UNUSED_RESULT
3194
set_field_split_str(char *arg, char **key, char **value, char **delim)
3195
47.0k
{
3196
47.0k
    char *value_end;
3197
3198
47.0k
    *key = NULL;
3199
47.0k
    *value = arg;
3200
47.0k
    if (delim) {
3201
46.1k
        *delim = NULL;
3202
46.1k
    }
3203
3204
47.0k
    value_end = strstr(arg, "->");
3205
47.0k
    if (!value_end) {
3206
18
        return xasprintf("%s: missing `->'", arg);
3207
18
    }
3208
3209
47.0k
    *key = value_end + strlen("->");
3210
47.0k
    if (delim) {
3211
46.1k
        *delim = value_end;
3212
46.1k
    }
3213
47.0k
    if (strlen(value_end) <= strlen("->")) {
3214
3
        return xasprintf("%s: missing field name following `->'", arg);
3215
3
    }
3216
3217
47.0k
    return NULL;
3218
47.0k
}
3219
3220
/* Parses a "set_field" action with argument 'arg', appending the parsed
3221
 * action to 'pp->ofpacts'.
3222
 *
3223
 * Returns NULL if successful, otherwise a malloc()'d string describing the
3224
 * error.  The caller is responsible for freeing the returned string. */
3225
static char * OVS_WARN_UNUSED_RESULT
3226
set_field_parse__(char *arg, const struct ofpact_parse_params *pp)
3227
45.1k
{
3228
45.1k
    char *value;
3229
45.1k
    char *delim;
3230
45.1k
    char *key;
3231
45.1k
    const struct mf_field *mf;
3232
45.1k
    union mf_value sf_value, sf_mask;
3233
45.1k
    char *error;
3234
3235
45.1k
    error = set_field_split_str(arg, &key, &value, &delim);
3236
45.1k
    if (error) {
3237
15
        return error;
3238
15
    }
3239
3240
45.0k
    mf = mf_from_name(key);
3241
45.0k
    if (!mf) {
3242
55
        return xasprintf("%s is not a valid OXM field name", key);
3243
55
    }
3244
45.0k
    if (!mf->writable) {
3245
1
        return xasprintf("%s is read-only", key);
3246
1
    }
3247
3248
45.0k
    delim[0] = '\0';
3249
45.0k
    error = mf_parse(mf, value, pp->port_map, &sf_value, &sf_mask);
3250
45.0k
    if (error) {
3251
5
        return error;
3252
5
    }
3253
3254
45.0k
    if (!mf_is_value_valid(mf, &sf_value)) {
3255
5
        return xasprintf("%s is not a valid value for field %s", value, key);
3256
5
    }
3257
3258
45.0k
    *pp->usable_protocols &= mf->usable_protocols_exact;
3259
3260
45.0k
    ofpact_put_set_field(pp->ofpacts, mf, &sf_value, &sf_mask);
3261
45.0k
    return NULL;
3262
45.0k
}
3263
3264
/* Parses 'arg' as the argument to a "set_field" action, and appends such an
3265
 * action to 'pp->ofpacts'.
3266
 *
3267
 * Returns NULL if successful, otherwise a malloc()'d string describing the
3268
 * error.  The caller is responsible for freeing the returned string. */
3269
static char * OVS_WARN_UNUSED_RESULT
3270
parse_SET_FIELD(const char *arg, const struct ofpact_parse_params *pp)
3271
45.1k
{
3272
45.1k
    char *copy = xstrdup(arg);
3273
45.1k
    char *error = set_field_parse__(copy, pp);
3274
45.1k
    free(copy);
3275
45.1k
    return error;
3276
45.1k
}
3277
3278
static char * OVS_WARN_UNUSED_RESULT
3279
parse_reg_load(char *arg, const struct ofpact_parse_params *pp)
3280
899
{
3281
899
    struct mf_subfield dst;
3282
899
    char *key, *value_str;
3283
899
    union mf_value value;
3284
899
    char *error;
3285
3286
899
    error = set_field_split_str(arg, &key, &value_str, NULL);
3287
899
    if (error) {
3288
3
        return error;
3289
3
    }
3290
3291
896
    error = mf_parse_subfield(&dst, key);
3292
896
    if (error) {
3293
4
        return error;
3294
4
    }
3295
3296
892
    if (parse_int_string(value_str, (uint8_t *)&value, dst.field->n_bytes,
3297
892
                         &key)) {
3298
1
        return xasprintf("%s: cannot parse integer value", arg);
3299
1
    }
3300
3301
891
    if (!bitwise_is_all_zeros(&value, dst.field->n_bytes, dst.n_bits,
3302
891
                              dst.field->n_bytes * 8 - dst.n_bits)) {
3303
1
        struct ds ds;
3304
3305
1
        ds_init(&ds);
3306
1
        mf_format(dst.field, &value, NULL, NULL, &ds);
3307
1
        error = xasprintf("%s: value %s does not fit into %d bits",
3308
1
                          arg, ds_cstr(&ds), dst.n_bits);
3309
1
        ds_destroy(&ds);
3310
1
        return error;
3311
1
    }
3312
3313
890
    struct ofpact_set_field *sf = ofpact_put_reg_load(pp->ofpacts, dst.field,
3314
890
                                                      NULL, NULL);
3315
3316
890
    bitwise_copy(&value, dst.field->n_bytes, 0, sf->value,
3317
890
                 dst.field->n_bytes, dst.ofs, dst.n_bits);
3318
890
    bitwise_one(ofpact_set_field_mask(sf), dst.field->n_bytes, dst.ofs,
3319
890
                dst.n_bits);
3320
890
    return NULL;
3321
891
}
3322
3323
static void
3324
format_SET_FIELD(const struct ofpact_set_field *a,
3325
                 const struct ofpact_format_params *fp)
3326
10.4k
{
3327
10.4k
    if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
3328
2.87k
        struct mf_subfield dst;
3329
2.87k
        uint64_t value;
3330
3331
2.87k
        dst.ofs = dst.n_bits = 0;
3332
5.75k
        while (next_load_segment(a, &dst, &value)) {
3333
2.87k
            ds_put_format(fp->s, "%sload:%s%#"PRIx64"%s->%s",
3334
2.87k
                          colors.special, colors.end, value,
3335
2.87k
                          colors.special, colors.end);
3336
2.87k
            mf_format_subfield(&dst, fp->s);
3337
2.87k
            ds_put_char(fp->s, ',');
3338
2.87k
        }
3339
2.87k
        ds_chomp(fp->s, ',');
3340
7.57k
    } else {
3341
7.57k
        ds_put_format(fp->s, "%sset_field:%s", colors.special, colors.end);
3342
7.57k
        mf_format(a->field, a->value, ofpact_set_field_mask(a),
3343
7.57k
                  fp->port_map, fp->s);
3344
7.57k
        ds_put_format(fp->s, "%s->%s%s",
3345
7.57k
                      colors.special, colors.end, a->field->name);
3346
7.57k
    }
3347
10.4k
}
3348
3349
static enum ofperr
3350
check_SET_FIELD(struct ofpact_set_field *a,
3351
                const struct ofpact_check_params *cp)
3352
34.9k
{
3353
34.9k
    const struct mf_field *mf = a->field;
3354
34.9k
    struct flow *flow = &cp->match->flow;
3355
34.9k
    ovs_be16 *tci = &flow->vlans[0].tci;
3356
3357
    /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
3358
34.9k
    if (!mf_are_prereqs_ok(mf, flow, NULL)
3359
33.8k
        || (mf->id == MFF_VLAN_VID && !(*tci & htons(VLAN_CFI)))) {
3360
1.07k
        VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisites",
3361
1.07k
                     mf->name);
3362
1.07k
        return OFPERR_OFPBAC_MATCH_INCONSISTENT;
3363
1.07k
    }
3364
3365
    /* Remember if we saw a VLAN tag in the flow, to aid translating to
3366
     * OpenFlow 1.1 if need be. */
3367
33.8k
    a->flow_has_vlan = (*tci & htons(VLAN_CFI)) != 0;
3368
33.8k
    if (mf->id == MFF_VLAN_TCI) {
3369
        /* The set field may add or remove the VLAN tag,
3370
         * Mark the status temporarily. */
3371
1.63k
        *tci = a->value->be16;
3372
1.63k
    }
3373
3374
33.8k
    return 0;
3375
34.9k
}
3376
3377
/* Appends an OFPACT_SET_FIELD ofpact with enough space for the value and a
3378
 * properly aligned mask for the 'field' to 'ofpacts' and returns it.  Fills
3379
 * in the value from 'value', if non-NULL, and mask from 'mask' if non-NULL.
3380
 * If 'value' is non-NULL and 'mask' is NULL, an all-ones mask will be
3381
 * filled in. */
3382
struct ofpact_set_field *
3383
ofpact_put_set_field(struct ofpbuf *ofpacts, const struct mf_field *field,
3384
                     const void *value, const void *mask)
3385
64.2k
{
3386
    /* Ensure there's enough space for:
3387
     * - value (8-byte aligned)
3388
     * - mask  (8-byte aligned)
3389
     * - padding (to make the whole ofpact_set_field 8-byte aligned)
3390
     */
3391
64.2k
    size_t total_size = 2 * ROUND_UP(field->n_bytes, OFPACT_ALIGNTO);
3392
64.2k
    struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
3393
64.2k
    sf->field = field;
3394
3395
    /* Fill with all zeroes to make sure the padding is initialized. */
3396
64.2k
    ofpbuf_put_zeros(ofpacts, total_size);
3397
64.2k
    sf = ofpacts->header;
3398
3399
    /* Fill in the value and mask if given, otherwise keep the zeroes
3400
     * so that the caller may fill in the value and mask itself. */
3401
64.2k
    if (value) {
3402
59.3k
        memcpy(sf->value, value, field->n_bytes);
3403
59.3k
        if (mask) {
3404
59.3k
            memcpy(ofpact_set_field_mask(sf), mask, field->n_bytes);
3405
59.3k
        } else {
3406
0
            memset(ofpact_set_field_mask(sf), 0xff, field->n_bytes);
3407
0
        }
3408
59.3k
    }
3409
3410
    /* Update length. */
3411
64.2k
    ofpact_finish_SET_FIELD(ofpacts, &sf);
3412
3413
64.2k
    return sf;
3414
64.2k
}
3415
3416
/* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it.  The ofpact
3417
 * is marked such that, if possible, it will be translated to OpenFlow as
3418
 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
3419
 * that was the way that the action was expressed when it came into OVS or for
3420
 * backward compatibility. */
3421
struct ofpact_set_field *
3422
ofpact_put_reg_load(struct ofpbuf *ofpacts, const struct mf_field *field,
3423
                    const void *value, const void *mask)
3424
4.91k
{
3425
4.91k
    struct ofpact_set_field *sf = ofpact_put_set_field(ofpacts, field, value,
3426
4.91k
                                                       mask);
3427
4.91k
    sf->ofpact.raw = NXAST_RAW_REG_LOAD;
3428
3429
4.91k
    return sf;
3430
4.91k
}
3431
3432
struct ofpact_set_field *
3433
ofpact_put_reg_load2(struct ofpbuf *ofpacts, const struct mf_field *field,
3434
                     const void *value, const void *mask)
3435
7.79k
{
3436
7.79k
    struct ofpact_set_field *sf = ofpact_put_set_field(ofpacts, field, value,
3437
7.79k
                                                       mask);
3438
7.79k
    sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
3439
3440
7.79k
    return sf;
3441
7.79k
}
3442
3443

3444
/* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
3445
 *
3446
 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
3447
 * top of the stack.
3448
 */
3449
struct nx_action_stack {
3450
    ovs_be16 type;                  /* OFPAT_VENDOR. */
3451
    ovs_be16 len;                   /* Length is 16. */
3452
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
3453
    ovs_be16 subtype;               /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
3454
    ovs_be16 offset;                /* Bit offset into the field. */
3455
    /* Followed by:
3456
     * - OXM/NXM header for field to push or pop (4 or 8 bytes).
3457
     * - ovs_be16 'n_bits', the number of bits to extract from the field.
3458
     * - Enough 0-bytes to pad out the action to 24 bytes. */
3459
    uint8_t pad[12];                /* See above. */
3460
};
3461
OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
3462
3463
static enum ofperr
3464
decode_stack_action(const struct nx_action_stack *nasp,
3465
                    const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap,
3466
                    struct ofpact_stack *stack_action)
3467
4.75k
{
3468
4.75k
    enum ofperr error;
3469
4.75k
    stack_action->subfield.ofs = ntohs(nasp->offset);
3470
3471
4.75k
    struct ofpbuf b = ofpbuf_const_initializer(nasp, sizeof *nasp);
3472
4.75k
    ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
3473
4.75k
    error  = mf_vl_mff_nx_pull_header(&b, vl_mff_map,
3474
4.75k
                                      &stack_action->subfield.field, NULL,
3475
4.75k
                                      tlv_bitmap);
3476
4.75k
    if (error) {
3477
1.14k
        return error;
3478
1.14k
    }
3479
3480
3.61k
    stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
3481
3.61k
    ofpbuf_pull(&b, 2);
3482
3.61k
    if (!is_all_zeros(b.data, b.size)) {
3483
691
        return OFPERR_NXBRC_MUST_BE_ZERO;
3484
691
    }
3485
3486
2.92k
    return 0;
3487
3.61k
}
3488
3489
static enum ofperr
3490
decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
3491
                            enum ofp_version ofp_version OVS_UNUSED,
3492
                            const struct vl_mff_map *vl_mff_map,
3493
                            uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
3494
1.81k
{
3495
1.81k
    struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
3496
1.81k
    enum ofperr error = decode_stack_action(nasp, vl_mff_map, tlv_bitmap,
3497
1.81k
                                            push);
3498
1.81k
    return error ? error : nxm_stack_push_check(push, NULL);
3499
1.81k
}
3500
3501
static enum ofperr
3502
decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
3503
                           enum ofp_version ofp_version OVS_UNUSED,
3504
                           const struct vl_mff_map *vl_mff_map,
3505
                           uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
3506
2.94k
{
3507
2.94k
    struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
3508
2.94k
    enum ofperr error = decode_stack_action(nasp, vl_mff_map, tlv_bitmap,
3509
2.94k
                                            pop);
3510
2.94k
    return error ? error : nxm_stack_pop_check(pop, NULL);
3511
2.94k
}
3512
3513
static void
3514
encode_STACK_op(const struct ofpact_stack *stack_action,
3515
                struct nx_action_stack *nasp)
3516
2.24k
{
3517
2.24k
    struct ofpbuf b;
3518
2.24k
    ovs_be16 n_bits;
3519
3520
2.24k
    nasp->offset = htons(stack_action->subfield.ofs);
3521
3522
2.24k
    ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
3523
2.24k
    ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
3524
2.24k
    nx_put_mff_header(&b, stack_action->subfield.field, 0, false);
3525
2.24k
    n_bits = htons(stack_action->subfield.n_bits);
3526
2.24k
    ofpbuf_put(&b, &n_bits, sizeof n_bits);
3527
2.24k
}
3528
3529
static void
3530
encode_STACK_PUSH(const struct ofpact_stack *stack,
3531
                  enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3532
340
{
3533
340
    encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
3534
340
}
3535
3536
static void
3537
encode_STACK_POP(const struct ofpact_stack *stack,
3538
                 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3539
1.90k
{
3540
1.90k
    encode_STACK_op(stack, put_NXAST_STACK_POP(out));
3541
1.90k
}
3542
3543
static char * OVS_WARN_UNUSED_RESULT
3544
parse_STACK_PUSH(char *arg, const struct ofpact_parse_params *pp)
3545
746
{
3546
746
    return nxm_parse_stack_action(ofpact_put_STACK_PUSH(pp->ofpacts), arg);
3547
746
}
3548
3549
static char * OVS_WARN_UNUSED_RESULT
3550
parse_STACK_POP(char *arg, const struct ofpact_parse_params *pp)
3551
2.14k
{
3552
2.14k
    return nxm_parse_stack_action(ofpact_put_STACK_POP(pp->ofpacts), arg);
3553
2.14k
}
3554
3555
static void
3556
format_STACK_PUSH(const struct ofpact_stack *a,
3557
                  const struct ofpact_format_params *fp)
3558
19
{
3559
19
    nxm_format_stack_push(a, fp->s);
3560
19
}
3561
3562
static void
3563
format_STACK_POP(const struct ofpact_stack *a,
3564
                 const struct ofpact_format_params *fp)
3565
681
{
3566
681
    nxm_format_stack_pop(a, fp->s);
3567
681
}
3568
3569
static enum ofperr
3570
check_STACK_PUSH(const struct ofpact_stack *a,
3571
                 const struct ofpact_check_params *cp)
3572
365
{
3573
365
    return nxm_stack_push_check(a, cp->match);
3574
365
}
3575
3576
static enum ofperr
3577
check_STACK_POP(const struct ofpact_stack *a,
3578
                const struct ofpact_check_params *cp)
3579
2.95k
{
3580
2.95k
    return nxm_stack_pop_check(a, cp->match);
3581
2.95k
}
3582

3583
/* Action structure for NXAST_DEC_TTL_CNT_IDS.
3584
 *
3585
 * If the packet is not IPv4 or IPv6, does nothing.  For IPv4 or IPv6, if the
3586
 * TTL or hop limit is at least 2, decrements it by 1.  Otherwise, if TTL or
3587
 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
3588
 * 'n_controllers' controller IDs specified in 'cnt_ids'.
3589
 *
3590
 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
3591
 * sent only to controllers with id 0.)
3592
 */
3593
struct nx_action_cnt_ids {
3594
    ovs_be16 type;              /* OFPAT_VENDOR. */
3595
    ovs_be16 len;               /* Length including cnt_ids. */
3596
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
3597
    ovs_be16 subtype;           /* NXAST_DEC_TTL_CNT_IDS. */
3598
3599
    ovs_be16 n_controllers;     /* Number of controllers. */
3600
    uint8_t zeros[4];           /* Must be zero. */
3601
3602
    /* Followed by 1 or more controller ids:
3603
     *
3604
     * uint16_t cnt_ids[];      -- Controller ids.
3605
     * uint8_t pad[];           -- Must be 0 to 8-byte align cnt_ids[].
3606
     */
3607
};
3608
OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
3609
3610
static enum ofperr
3611
decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
3612
9.03k
{
3613
9.03k
    uint16_t id = 0;
3614
9.03k
    struct ofpact_cnt_ids *ids;
3615
9.03k
    enum ofperr error = 0;
3616
3617
9.03k
    ids = ofpact_put_DEC_TTL(out);
3618
9.03k
    ids->n_controllers = 1;
3619
9.03k
    ofpbuf_put(out, &id, sizeof id);
3620
9.03k
    ids = out->header;
3621
9.03k
    ofpact_finish_DEC_TTL(out, &ids);
3622
9.03k
    return error;
3623
9.03k
}
3624
3625
static enum ofperr
3626
decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
3627
                                 enum ofp_version ofp_version OVS_UNUSED,
3628
                                 struct ofpbuf *out)
3629
11.3k
{
3630
11.3k
    struct ofpact_cnt_ids *ids;
3631
11.3k
    size_t ids_size;
3632
11.3k
    int i;
3633
3634
11.3k
    ids = ofpact_put_DEC_TTL(out);
3635
11.3k
    ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
3636
11.3k
    ids->n_controllers = ntohs(nac_ids->n_controllers);
3637
11.3k
    ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
3638
3639
11.3k
    if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
3640
1.26k
        return OFPERR_NXBRC_MUST_BE_ZERO;
3641
1.26k
    }
3642
3643
10.0k
    if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
3644
862
        VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
3645
862
                     "bytes allocated for controller ids.  %"PRIuSIZE" bytes "
3646
862
                     "are required for %u controllers.",
3647
862
                     ids_size, ids->n_controllers * sizeof(ovs_be16),
3648
862
                     ids->n_controllers);
3649
862
        return OFPERR_OFPBAC_BAD_LEN;
3650
862
    }
3651
3652
31.2k
    for (i = 0; i < ids->n_controllers; i++) {
3653
22.0k
        uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
3654
22.0k
        ofpbuf_put(out, &id, sizeof id);
3655
22.0k
        ids = out->header;
3656
22.0k
    }
3657
3658
9.19k
    ofpact_finish_DEC_TTL(out, &ids);
3659
3660
9.19k
    return 0;
3661
10.0k
}
3662
3663
static void
3664
encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
3665
               enum ofp_version ofp_version, struct ofpbuf *out)
3666
2.48k
{
3667
2.48k
    if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
3668
1.20k
        || dec_ttl->n_controllers != 1
3669
1.27k
        || dec_ttl->cnt_ids[0] != 0) {
3670
1.27k
        struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
3671
1.27k
        int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
3672
1.27k
        ovs_be16 *ids;
3673
1.27k
        size_t i;
3674
3675
1.27k
        nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
3676
1.27k
        nac_ids->n_controllers = htons(dec_ttl->n_controllers);
3677
3678
1.27k
        ids = ofpbuf_put_zeros(out, ids_len);
3679
384k
        for (i = 0; i < dec_ttl->n_controllers; i++) {
3680
383k
            ids[i] = htons(dec_ttl->cnt_ids[i]);
3681
383k
        }
3682
1.27k
    } else {
3683
1.20k
        put_OFPAT_DEC_NW_TTL(out, ofp_version);
3684
1.20k
    }
3685
2.48k
}
3686
3687
static void
3688
parse_noargs_dec_ttl(const struct ofpact_parse_params *pp)
3689
1.38k
{
3690
1.38k
    struct ofpact_cnt_ids *ids;
3691
1.38k
    uint16_t id = 0;
3692
3693
1.38k
    ofpact_put_DEC_TTL(pp->ofpacts);
3694
1.38k
    ofpbuf_put(pp->ofpacts, &id, sizeof id);
3695
1.38k
    ids = pp->ofpacts->header;
3696
1.38k
    ids->n_controllers++;
3697
1.38k
    ofpact_finish_DEC_TTL(pp->ofpacts, &ids);
3698
1.38k
}
3699
3700
static char * OVS_WARN_UNUSED_RESULT
3701
parse_DEC_TTL(char *arg, const struct ofpact_parse_params *pp)
3702
2.68k
{
3703
2.68k
    if (*arg == '\0') {
3704
1.38k
        parse_noargs_dec_ttl(pp);
3705
1.38k
    } else {
3706
1.29k
        struct ofpact_cnt_ids *ids;
3707
1.29k
        char *cntr;
3708
3709
1.29k
        ids = ofpact_put_DEC_TTL(pp->ofpacts);
3710
1.29k
        ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
3711
949k
        for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
3712
948k
             cntr = strtok_r(NULL, ", ", &arg)) {
3713
948k
            uint16_t id = atoi(cntr);
3714
3715
948k
            ofpbuf_put(pp->ofpacts, &id, sizeof id);
3716
948k
            ids = pp->ofpacts->header;
3717
948k
            ids->n_controllers++;
3718
948k
        }
3719
1.29k
        if (!ids->n_controllers) {
3720
1
            return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
3721
1
                           "id.");
3722
1
        }
3723
3724
1.29k
        if (ofpbuf_oversized(pp->ofpacts)) {
3725
6
            return xasprintf("input too big");
3726
6
        }
3727
3728
1.29k
        ofpact_finish_DEC_TTL(pp->ofpacts, &ids);
3729
1.29k
    }
3730
2.67k
    return NULL;
3731
2.68k
}
3732
3733
static void
3734
format_DEC_TTL(const struct ofpact_cnt_ids *a,
3735
               const struct ofpact_format_params *fp)
3736
9.62k
{
3737
9.62k
    size_t i;
3738
3739
9.62k
    ds_put_format(fp->s, "%sdec_ttl%s", colors.paren, colors.end);
3740
9.62k
    if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
3741
3.72k
        ds_put_format(fp->s, "%s(%s", colors.paren, colors.end);
3742
14.8k
        for (i = 0; i < a->n_controllers; i++) {
3743
11.0k
            if (i) {
3744
7.41k
                ds_put_cstr(fp->s, ",");
3745
7.41k
            }
3746
11.0k
            ds_put_format(fp->s, "%"PRIu16, a->cnt_ids[i]);
3747
11.0k
        }
3748
3.72k
        ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
3749
3.72k
    }
3750
9.62k
}
3751
3752
static enum ofperr
3753
check_DEC_TTL(const struct ofpact_cnt_ids *a OVS_UNUSED,
3754
              struct ofpact_check_params *cp)
3755
8.36k
{
3756
8.36k
    return check_set_ip(cp);
3757
8.36k
}
3758

3759
/* Set MPLS label actions. */
3760
3761
static enum ofperr
3762
decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label,
3763
                                enum ofp_version ofp_version OVS_UNUSED,
3764
                                struct ofpbuf *out)
3765
2.32k
{
3766
2.32k
    ofpact_put_SET_MPLS_LABEL(out)->label = label;
3767
2.32k
    return 0;
3768
2.32k
}
3769
3770
static void
3771
encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
3772
                      enum ofp_version ofp_version,
3773
                                  struct ofpbuf *out)
3774
1.87k
{
3775
1.87k
    if (ofp_version < OFP12_VERSION) {
3776
641
        put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
3777
1.23k
    } else {
3778
1.23k
        put_set_field(out, ofp_version, MFF_MPLS_LABEL, ntohl(label->label));
3779
1.23k
    }
3780
1.87k
}
3781
3782
static char * OVS_WARN_UNUSED_RESULT
3783
parse_SET_MPLS_LABEL(char *arg, const struct ofpact_parse_params *pp)
3784
2.03k
{
3785
2.03k
    struct ofpact_mpls_label *mpls_label
3786
2.03k
        = ofpact_put_SET_MPLS_LABEL(pp->ofpacts);
3787
2.03k
    uint32_t label;
3788
2.03k
    char *error;
3789
3790
2.03k
    if (*arg == '\0') {
3791
3
        return xstrdup("set_mpls_label: expected label.");
3792
3
    }
3793
3794
2.03k
    error = str_to_u32(arg, &label);
3795
2.03k
    if (error) {
3796
1
        return error;
3797
1
    }
3798
3799
2.03k
    if (label & ~0xfffff) {
3800
6
        return xasprintf("%s: not a valid MPLS label", arg);
3801
6
    }
3802
2.02k
    mpls_label->label = htonl(label);
3803
2.02k
    return NULL;
3804
2.03k
}
3805
3806
static void
3807
format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a,
3808
                      const struct ofpact_format_params *fp)
3809
264
{
3810
264
    ds_put_format(fp->s, "%sset_mpls_label(%s%"PRIu32"%s)%s",
3811
264
                  colors.paren, colors.end, ntohl(a->label),
3812
264
                  colors.paren, colors.end);
3813
264
}
3814
3815
static enum ofperr
3816
check_set_mpls(struct ofpact_check_params *cp)
3817
12.1k
{
3818
12.1k
    ovs_be16 dl_type = get_dl_type(&cp->match->flow);
3819
12.1k
    if (!eth_type_mpls(dl_type)) {
3820
7.42k
        inconsistent_match(&cp->usable_protocols);
3821
7.42k
    }
3822
12.1k
    return 0;
3823
12.1k
}
3824
3825
static enum ofperr
3826
check_SET_MPLS_LABEL(const struct ofpact_mpls_label *a OVS_UNUSED,
3827
                     struct ofpact_check_params *cp)
3828
4.22k
{
3829
4.22k
    return check_set_mpls(cp);
3830
4.22k
}
3831

3832
/* Set MPLS TC actions. */
3833
3834
static enum ofperr
3835
decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc,
3836
                             enum ofp_version ofp_version OVS_UNUSED,
3837
                             struct ofpbuf *out)
3838
639
{
3839
639
    ofpact_put_SET_MPLS_TC(out)->tc = tc;
3840
639
    return 0;
3841
639
}
3842
3843
static void
3844
encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
3845
                   enum ofp_version ofp_version, struct ofpbuf *out)
3846
872
{
3847
872
    if (ofp_version < OFP12_VERSION) {
3848
435
        put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
3849
437
    } else {
3850
437
        put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
3851
437
    }
3852
872
}
3853
3854
static char * OVS_WARN_UNUSED_RESULT
3855
parse_SET_MPLS_TC(char *arg, const struct ofpact_parse_params *pp)
3856
1.14k
{
3857
1.14k
    struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(pp->ofpacts);
3858
1.14k
    uint8_t tc;
3859
1.14k
    char *error;
3860
3861
1.14k
    if (*arg == '\0') {
3862
1
        return xstrdup("set_mpls_tc: expected tc.");
3863
1
    }
3864
3865
1.13k
    error = str_to_u8(arg, "MPLS TC", &tc);
3866
1.13k
    if (error) {
3867
8
        return error;
3868
8
    }
3869
3870
1.13k
    if (tc & ~7) {
3871
4
        return xasprintf("%s: not a valid MPLS TC", arg);
3872
4
    }
3873
1.12k
    mpls_tc->tc = tc;
3874
1.12k
    return NULL;
3875
1.13k
}
3876
3877
static void
3878
format_SET_MPLS_TC(const struct ofpact_mpls_tc *a,
3879
                   const struct ofpact_format_params *fp)
3880
629
{
3881
629
    ds_put_format(fp->s, "%sset_mpls_tc(%s%"PRIu8"%s)%s",
3882
629
                  colors.paren, colors.end, a->tc,
3883
629
                  colors.paren, colors.end);
3884
629
}
3885
3886
static enum ofperr
3887
check_SET_MPLS_TC(const struct ofpact_mpls_tc *a OVS_UNUSED,
3888
                  struct ofpact_check_params *cp)
3889
1.66k
{
3890
1.66k
    return check_set_mpls(cp);
3891
1.66k
}
3892

3893
/* Set MPLS TTL actions. */
3894
3895
static enum ofperr
3896
decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl,
3897
                              enum ofp_version ofp_version OVS_UNUSED,
3898
                              struct ofpbuf *out)
3899
2.20k
{
3900
2.20k
    ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
3901
2.20k
    return 0;
3902
2.20k
}
3903
3904
static void
3905
encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
3906
                    enum ofp_version ofp_version, struct ofpbuf *out)
3907
1.08k
{
3908
1.08k
    put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
3909
1.08k
}
3910
3911
/* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3912
 * action to 'pp->ofpacts'.
3913
 *
3914
 * Returns NULL if successful, otherwise a malloc()'d string describing the
3915
 * error.  The caller is responsible for freeing the returned string. */
3916
static char * OVS_WARN_UNUSED_RESULT
3917
parse_SET_MPLS_TTL(char *arg, const struct ofpact_parse_params *pp)
3918
1.20k
{
3919
1.20k
    struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(pp->ofpacts);
3920
1.20k
    uint8_t ttl;
3921
1.20k
    char *error;
3922
3923
1.20k
    if (*arg == '\0') {
3924
4
        return xstrdup("set_mpls_ttl: expected ttl.");
3925
4
    }
3926
3927
1.20k
    error = str_to_u8(arg, "MPLS TTL", &ttl);
3928
1.20k
    if (error) {
3929
1
        return error;
3930
1
    }
3931
1.20k
    mpls_ttl->ttl = ttl;
3932
1.20k
    return NULL;
3933
1.20k
}
3934
3935
static void
3936
format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a,
3937
                    const struct ofpact_format_params *fp)
3938
1.21k
{
3939
1.21k
    ds_put_format(fp->s, "%sset_mpls_ttl(%s%"PRIu8"%s)%s",
3940
1.21k
                  colors.paren, colors.end, a->ttl,
3941
1.21k
                  colors.paren, colors.end);
3942
1.21k
}
3943
3944
static enum ofperr
3945
check_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a OVS_UNUSED,
3946
                   struct ofpact_check_params *cp)
3947
1.99k
{
3948
1.99k
    return check_set_mpls(cp);
3949
1.99k
}
3950

3951
/* Decrement MPLS TTL actions. */
3952
3953
static enum ofperr
3954
decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
3955
3.11k
{
3956
3.11k
    ofpact_put_DEC_MPLS_TTL(out);
3957
3.11k
    return 0;
3958
3.11k
}
3959
3960
static void
3961
encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
3962
                    enum ofp_version ofp_version, struct ofpbuf *out)
3963
1.19k
{
3964
1.19k
    put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
3965
1.19k
}
3966
3967
static char * OVS_WARN_UNUSED_RESULT
3968
parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
3969
1.26k
{
3970
1.26k
    ofpact_put_DEC_MPLS_TTL(pp->ofpacts);
3971
1.26k
    return NULL;
3972
1.26k
}
3973
3974
static void
3975
format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED,
3976
                    const struct ofpact_format_params *fp)
3977
3.03k
{
3978
3.03k
    ds_put_format(fp->s, "%sdec_mpls_ttl%s", colors.value, colors.end);
3979
3.03k
}
3980
3981
static enum ofperr
3982
check_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED,
3983
                   struct ofpact_check_params *cp)
3984
4.22k
{
3985
4.22k
    return check_set_mpls(cp);
3986
4.22k
}
3987

3988
/* Push MPLS label action. */
3989
3990
static enum ofperr
3991
decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype,
3992
                           enum ofp_version ofp_version OVS_UNUSED,
3993
                           struct ofpbuf *out)
3994
2.39k
{
3995
2.39k
    struct ofpact_push_mpls *oam;
3996
3997
2.39k
    if (!eth_type_mpls(ethertype)) {
3998
210
        return OFPERR_OFPBAC_BAD_ARGUMENT;
3999
210
    }
4000
2.18k
    oam = ofpact_put_PUSH_MPLS(out);
4001
2.18k
    oam->ethertype = ethertype;
4002
4003
2.18k
    return 0;
4004
2.39k
}
4005
4006
static void
4007
encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
4008
                 enum ofp_version ofp_version, struct ofpbuf *out)
4009
316
{
4010
316
    put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
4011
316
}
4012
4013
static char * OVS_WARN_UNUSED_RESULT
4014
parse_PUSH_MPLS(char *arg, const struct ofpact_parse_params *pp)
4015
533
{
4016
533
    uint16_t ethertype;
4017
533
    char *error;
4018
4019
533
    error = str_to_u16(arg, "push_mpls", &ethertype);
4020
533
    if (!error) {
4021
529
        ofpact_put_PUSH_MPLS(pp->ofpacts)->ethertype = htons(ethertype);
4022
529
    }
4023
533
    return error;
4024
533
}
4025
4026
static void
4027
format_PUSH_MPLS(const struct ofpact_push_mpls *a,
4028
                 const struct ofpact_format_params *fp)
4029
1.84k
{
4030
1.84k
    ds_put_format(fp->s, "%spush_mpls:%s0x%04"PRIx16,
4031
1.84k
                  colors.param, colors.end, ntohs(a->ethertype));
4032
1.84k
}
4033
4034
static enum ofperr
4035
check_PUSH_MPLS(const struct ofpact_push_mpls *a,
4036
                struct ofpact_check_params *cp)
4037
2.35k
{
4038
2.35k
    struct flow *flow = &cp->match->flow;
4039
4040
2.35k
    if (flow->packet_type != htonl(PT_ETH)) {
4041
334
        inconsistent_match(&cp->usable_protocols);
4042
334
    }
4043
2.35k
    flow->dl_type = a->ethertype;
4044
4045
    /* The packet is now MPLS and the MPLS payload is opaque.
4046
     * Thus nothing can be assumed about the network protocol.
4047
     * Temporarily mark that we have no nw_proto. */
4048
2.35k
    flow->nw_proto = 0;
4049
4050
2.35k
    return 0;
4051
2.35k
}
4052

4053
/* Pop MPLS label action. */
4054
4055
static enum ofperr
4056
decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype,
4057
                          enum ofp_version ofp_version OVS_UNUSED,
4058
                          struct ofpbuf *out)
4059
9.69k
{
4060
9.69k
    ofpact_put_POP_MPLS(out)->ethertype = ethertype;
4061
9.69k
    return 0;
4062
9.69k
}
4063
4064
static void
4065
encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
4066
                enum ofp_version ofp_version, struct ofpbuf *out)
4067
1.12k
{
4068
1.12k
    put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
4069
1.12k
}
4070
4071
static char * OVS_WARN_UNUSED_RESULT
4072
parse_POP_MPLS(char *arg, const struct ofpact_parse_params *pp)
4073
1.15k
{
4074
1.15k
    uint16_t ethertype;
4075
1.15k
    char *error;
4076
4077
1.15k
    error = str_to_u16(arg, "pop_mpls", &ethertype);
4078
1.15k
    if (!error) {
4079
1.14k
        ofpact_put_POP_MPLS(pp->ofpacts)->ethertype = htons(ethertype);
4080
1.14k
    }
4081
1.15k
    return error;
4082
1.15k
}
4083
4084
static void
4085
format_POP_MPLS(const struct ofpact_pop_mpls *a,
4086
                const struct ofpact_format_params *fp)
4087
7.71k
{
4088
7.71k
    ds_put_format(fp->s, "%spop_mpls:%s0x%04"PRIx16,
4089
7.71k
                  colors.param, colors.end, ntohs(a->ethertype));
4090
7.71k
}
4091
4092
static enum ofperr
4093
check_POP_MPLS(const struct ofpact_pop_mpls *a, struct ofpact_check_params *cp)
4094
8.73k
{
4095
8.73k
    struct flow *flow = &cp->match->flow;
4096
8.73k
    ovs_be16 dl_type = get_dl_type(flow);
4097
4098
8.73k
    if (flow->packet_type != htonl(PT_ETH) || !eth_type_mpls(dl_type)) {
4099
7.30k
        inconsistent_match(&cp->usable_protocols);
4100
7.30k
    }
4101
8.73k
    flow->dl_type = a->ethertype;
4102
8.73k
    return 0;
4103
8.73k
}
4104

4105
/* Set tunnel ID actions. */
4106
4107
static enum ofperr
4108
decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id,
4109
                            enum ofp_version ofp_version OVS_UNUSED,
4110
                            struct ofpbuf *out)
4111
936
{
4112
936
    struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
4113
936
    tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
4114
936
    tunnel->tun_id = tun_id;
4115
936
    return 0;
4116
936
}
4117
4118
static enum ofperr
4119
decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id,
4120
                              enum ofp_version ofp_version OVS_UNUSED,
4121
                              struct ofpbuf *out)
4122
267
{
4123
267
    struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
4124
267
    tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
4125
267
    tunnel->tun_id = tun_id;
4126
267
    return 0;
4127
267
}
4128
4129
static void
4130
encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
4131
                  enum ofp_version ofp_version, struct ofpbuf *out)
4132
1.42k
{
4133
1.42k
    uint64_t tun_id = tunnel->tun_id;
4134
4135
1.42k
    if (ofp_version < OFP12_VERSION) {
4136
1.13k
        if (tun_id <= UINT32_MAX
4137
714
            && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
4138
399
            put_NXAST_SET_TUNNEL(out, tun_id);
4139
739
        } else {
4140
739
            put_NXAST_SET_TUNNEL64(out, tun_id);
4141
739
        }
4142
1.13k
    } else {
4143
287
        put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
4144
287
    }
4145
1.42k
}
4146
4147
static char * OVS_WARN_UNUSED_RESULT
4148
parse_set_tunnel(char *arg, enum ofp_raw_action_type raw,
4149
                 const struct ofpact_parse_params *pp)
4150
1.48k
{
4151
1.48k
    struct ofpact_tunnel *tunnel;
4152
4153
1.48k
    tunnel = ofpact_put_SET_TUNNEL(pp->ofpacts);
4154
1.48k
    tunnel->ofpact.raw = raw;
4155
1.48k
    return str_to_u64(arg, &tunnel->tun_id);
4156
1.48k
}
4157
4158
static char * OVS_WARN_UNUSED_RESULT
4159
parse_SET_TUNNEL(char *arg, const struct ofpact_parse_params *pp)
4160
918
{
4161
918
    return parse_set_tunnel(arg, NXAST_RAW_SET_TUNNEL, pp);
4162
918
}
4163
4164
static void
4165
format_SET_TUNNEL(const struct ofpact_tunnel *a,
4166
                  const struct ofpact_format_params *fp)
4167
396
{
4168
396
    ds_put_format(fp->s, "%sset_tunnel%s:%s%#"PRIx64, colors.param,
4169
396
                  (a->tun_id > UINT32_MAX
4170
262
                   || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
4171
396
                  colors.end, a->tun_id);
4172
396
}
4173
4174
static enum ofperr
4175
check_SET_TUNNEL(const struct ofpact_tunnel *a OVS_UNUSED,
4176
                 const struct ofpact_check_params *cp OVS_UNUSED)
4177
1.49k
{
4178
1.49k
    return 0;
4179
1.49k
}
4180

4181
/* Delete field action. */
4182
4183
/* Action structure for DELETE_FIELD */
4184
struct nx_action_delete_field {
4185
    ovs_be16 type;          /* OFPAT_VENDOR */
4186
    ovs_be16 len;           /* Length is 24. */
4187
    ovs_be32 vendor;        /* NX_VENDOR_ID. */
4188
    ovs_be16 subtype;       /* NXAST_DELETE_FIELD. */
4189
    /* Followed by:
4190
     * - OXM/NXM header for field to delete (4 or 8 bytes).
4191
     * - Enough 0-bytes to pad out the action to 24 bytes. */
4192
    uint8_t pad[14];
4193
};
4194
OFP_ASSERT(sizeof(struct nx_action_delete_field ) == 24);
4195
4196
static enum ofperr
4197
decode_NXAST_RAW_DELETE_FIELD(const struct nx_action_delete_field *nadf,
4198
                              enum ofp_version ofp_version OVS_UNUSED,
4199
                              const struct vl_mff_map *vl_mff_map,
4200
                              uint64_t *tlv_bitmap, struct ofpbuf *out)
4201
108
{
4202
108
    struct ofpact_delete_field *delete_field;
4203
108
    enum ofperr err;
4204
4205
108
    delete_field = ofpact_put_DELETE_FIELD(out);
4206
108
    delete_field->ofpact.raw = NXAST_RAW_DELETE_FIELD;
4207
4208
108
    struct ofpbuf b = ofpbuf_const_initializer(nadf, ntohs(nadf->len));
4209
108
    ofpbuf_pull(&b, OBJECT_OFFSETOF(nadf, pad));
4210
4211
108
    err = mf_vl_mff_nx_pull_header(&b, vl_mff_map, &delete_field->field,
4212
108
                                   NULL, tlv_bitmap);
4213
108
    if (err) {
4214
36
        return err;
4215
36
    }
4216
4217
72
    return 0;
4218
108
}
4219
4220
static void
4221
encode_DELETE_FIELD(const struct ofpact_delete_field *delete_field,
4222
                    enum ofp_version ofp_version OVS_UNUSED,
4223
                    struct ofpbuf *out)
4224
448
{
4225
448
    size_t size;
4226
4227
448
    put_NXAST_DELETE_FIELD(out);
4228
448
    size = out->size;
4229
4230
448
    out->size = size - MEMBER_SIZEOF(struct nx_action_delete_field, pad);
4231
448
    nx_put_mff_header(out, delete_field->field, 0, false);
4232
448
    out->size = size;
4233
448
}
4234
4235
static char * OVS_WARN_UNUSED_RESULT
4236
parse_DELETE_FIELD(char *arg, const struct ofpact_parse_params *pp)
4237
681
{
4238
681
    struct ofpact_delete_field *delete_field;
4239
4240
681
    delete_field = ofpact_put_DELETE_FIELD(pp->ofpacts);
4241
681
    return mf_parse_field(&delete_field->field, arg);
4242
681
}
4243
4244
static void
4245
format_DELETE_FIELD(const struct ofpact_delete_field *odf,
4246
                          const struct ofpact_format_params *fp)
4247
36
{
4248
36
    ds_put_format(fp->s, "%sdelete_field:%s", colors.param,
4249
36
                  colors.end);
4250
36
    ds_put_format(fp->s, "%s", odf->field->name);
4251
36
}
4252
4253
static enum ofperr
4254
check_DELETE_FIELD(const struct ofpact_delete_field *odf,
4255
                         struct ofpact_check_params *cp OVS_UNUSED)
4256
512
{
4257
512
    if (odf->field->id < MFF_TUN_METADATA0 ||
4258
493
        odf->field->id > MFF_TUN_METADATA63) {
4259
46
        return OFPERR_OFPBAC_BAD_ARGUMENT;
4260
46
    }
4261
466
    return 0;
4262
512
}
4263

4264
/* Set queue action. */
4265
4266
static enum ofperr
4267
decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id,
4268
                           enum ofp_version ofp_version OVS_UNUSED,
4269
                           struct ofpbuf *out)
4270
1.01k
{
4271
1.01k
    ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
4272
1.01k
    return 0;
4273
1.01k
}
4274
4275
static void
4276
encode_SET_QUEUE(const struct ofpact_queue *queue,
4277
                 enum ofp_version ofp_version, struct ofpbuf *out)
4278
199
{
4279
199
    put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
4280
199
}
4281
4282
static char * OVS_WARN_UNUSED_RESULT
4283
parse_SET_QUEUE(char *arg, const struct ofpact_parse_params *pp)
4284
359
{
4285
359
    return str_to_u32(arg, &ofpact_put_SET_QUEUE(pp->ofpacts)->queue_id);
4286
359
}
4287
4288
static void
4289
format_SET_QUEUE(const struct ofpact_queue *a,
4290
                 const struct ofpact_format_params *fp)
4291
947
{
4292
947
    ds_put_format(fp->s, "%sset_queue:%s%"PRIu32,
4293
947
                  colors.param, colors.end, a->queue_id);
4294
947
}
4295
4296
static enum ofperr
4297
check_SET_QUEUE(const struct ofpact_queue *a OVS_UNUSED,
4298
                const struct ofpact_check_params *cp OVS_UNUSED)
4299
223
{
4300
223
    return 0;
4301
223
}
4302

4303
/* Pop queue action. */
4304
4305
static enum ofperr
4306
decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
4307
1.00k
{
4308
1.00k
    ofpact_put_POP_QUEUE(out);
4309
1.00k
    return 0;
4310
1.00k
}
4311
4312
static void
4313
encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
4314
                 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4315
815
{
4316
815
    put_NXAST_POP_QUEUE(out);
4317
815
}
4318
4319
static char * OVS_WARN_UNUSED_RESULT
4320
parse_POP_QUEUE(const char *arg OVS_UNUSED,
4321
                const struct ofpact_parse_params *pp)
4322
1.07k
{
4323
1.07k
    ofpact_put_POP_QUEUE(pp->ofpacts);
4324
1.07k
    return NULL;
4325
1.07k
}
4326
4327
static void
4328
format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED,
4329
                 const struct ofpact_format_params *fp)
4330
279
{
4331
279
    ds_put_format(fp->s, "%spop_queue%s", colors.value, colors.end);
4332
279
}
4333
4334
static enum ofperr
4335
check_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED,
4336
                const struct ofpact_check_params *cp OVS_UNUSED)
4337
850
{
4338
850
    return 0;
4339
850
}
4340

4341
/* Action structure for NXAST_FIN_TIMEOUT.
4342
 *
4343
 * This action changes the idle timeout or hard timeout, or both, of this
4344
 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
4345
 * When such a packet is observed, the action reduces the rule's idle timeout
4346
 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'.  This
4347
 * action has no effect on an existing timeout that is already shorter than the
4348
 * one that the action specifies.  A 'fin_idle_timeout' or 'fin_hard_timeout'
4349
 * of zero has no effect on the respective timeout.
4350
 *
4351
 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
4352
 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
4353
 * receipt of the FIN or RST.
4354
 *
4355
 * This is useful for quickly discarding learned TCP flows that otherwise will
4356
 * take a long time to expire.
4357
 *
4358
 * This action is intended for use with an OpenFlow rule that matches only a
4359
 * single TCP flow.  If the rule matches multiple TCP flows (e.g. it wildcards
4360
 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
4361
 * RST in any of those flows will cause the entire OpenFlow rule to expire
4362
 * early, which is not normally desirable.
4363
 */
4364
struct nx_action_fin_timeout {
4365
    ovs_be16 type;              /* OFPAT_VENDOR. */
4366
    ovs_be16 len;               /* 16. */
4367
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
4368
    ovs_be16 subtype;           /* NXAST_FIN_TIMEOUT. */
4369
    ovs_be16 fin_idle_timeout;  /* New idle timeout, if nonzero. */
4370
    ovs_be16 fin_hard_timeout;  /* New hard timeout, if nonzero. */
4371
    ovs_be16 pad;               /* Must be zero. */
4372
};
4373
OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
4374
4375
static enum ofperr
4376
decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
4377
                             enum ofp_version ofp_version OVS_UNUSED,
4378
                             struct ofpbuf *out)
4379
2.59k
{
4380
2.59k
    struct ofpact_fin_timeout *oft;
4381
4382
2.59k
    oft = ofpact_put_FIN_TIMEOUT(out);
4383
2.59k
    oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
4384
2.59k
    oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
4385
2.59k
    return 0;
4386
2.59k
}
4387
4388
static void
4389
encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
4390
                   enum ofp_version ofp_version OVS_UNUSED,
4391
                   struct ofpbuf *out)
4392
714
{
4393
714
    struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
4394
714
    naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
4395
714
    naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
4396
714
}
4397
4398
static char * OVS_WARN_UNUSED_RESULT
4399
parse_FIN_TIMEOUT(char *arg, const struct ofpact_parse_params *pp)
4400
1.12k
{
4401
1.12k
    struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(pp->ofpacts);
4402
1.12k
    char *key, *value;
4403
4404
1.97k
    while (ofputil_parse_key_value(&arg, &key, &value)) {
4405
1.00k
        char *error;
4406
4407
1.00k
        if (!strcmp(key, "idle_timeout")) {
4408
361
            error =  str_to_u16(value, key, &oft->fin_idle_timeout);
4409
640
        } else if (!strcmp(key, "hard_timeout")) {
4410
498
            error = str_to_u16(value, key, &oft->fin_hard_timeout);
4411
498
        } else {
4412
142
            error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
4413
142
                              key);
4414
142
        }
4415
4416
1.00k
        if (error) {
4417
150
            return error;
4418
150
        }
4419
1.00k
    }
4420
970
    return NULL;
4421
1.12k
}
4422
4423
static void
4424
format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a,
4425
                   const struct ofpact_format_params *fp)
4426
2.12k
{
4427
2.12k
    ds_put_format(fp->s, "%sfin_timeout(%s", colors.paren, colors.end);
4428
2.12k
    if (a->fin_idle_timeout) {
4429
1.77k
        ds_put_format(fp->s, "%sidle_timeout=%s%"PRIu16",",
4430
1.77k
                      colors.param, colors.end, a->fin_idle_timeout);
4431
1.77k
    }
4432
2.12k
    if (a->fin_hard_timeout) {
4433
1.81k
        ds_put_format(fp->s, "%shard_timeout=%s%"PRIu16",",
4434
1.81k
                      colors.param, colors.end, a->fin_hard_timeout);
4435
1.81k
    }
4436
2.12k
    ds_chomp(fp->s, ',');
4437
2.12k
    ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
4438
2.12k
}
4439
4440
4441
static enum ofperr
4442
check_FIN_TIMEOUT(const struct ofpact_fin_timeout *a OVS_UNUSED,
4443
                  struct ofpact_check_params *cp)
4444
2.01k
{
4445
2.01k
    if (cp->match->flow.nw_proto != IPPROTO_TCP) {
4446
1.43k
        inconsistent_match(&cp->usable_protocols);
4447
1.43k
    }
4448
2.01k
    return 0;
4449
2.01k
}
4450

4451
/* Action structure for NXAST_ENCAP */
4452
struct nx_action_encap {
4453
    ovs_be16 type;         /* OFPAT_VENDOR. */
4454
    ovs_be16 len;          /* Total size including any property TLVs. */
4455
    ovs_be32 vendor;       /* NX_VENDOR_ID. */
4456
    ovs_be16 subtype;      /* NXAST_ENCAP. */
4457
    ovs_be16 hdr_size;     /* Header size in bytes, 0 = 'not specified'.*/
4458
    ovs_be32 new_pkt_type; /* Header type to add and PACKET_TYPE of result. */
4459
    struct ofp_ed_prop_header props[];  /* Encap TLV properties. */
4460
};
4461
OFP_ASSERT(sizeof(struct nx_action_encap) == 16);
4462
4463
static enum ofperr
4464
decode_NXAST_RAW_ENCAP(const struct nx_action_encap *nae,
4465
                       enum ofp_version ofp_version OVS_UNUSED,
4466
                       struct ofpbuf *out)
4467
7.96k
{
4468
7.96k
    struct ofpact_encap *encap;
4469
7.96k
    const struct ofp_ed_prop_header *ofp_prop;
4470
7.96k
    const size_t encap_ofs = out->size;
4471
7.96k
    size_t props_len;
4472
7.96k
    uint16_t n_props = 0;
4473
7.96k
    int err;
4474
4475
7.96k
    encap = ofpact_put_ENCAP(out);
4476
7.96k
    encap->ofpact.raw = NXAST_RAW_ENCAP;
4477
7.96k
    switch (ntohl(nae->new_pkt_type)) {
4478
5.26k
    case PT_ETH:
4479
6.25k
    case PT_NSH:
4480
7.19k
    case PT_MPLS:
4481
7.38k
    case PT_MPLS_MC:
4482
        /* Add supported encap header types here. */
4483
7.38k
        break;
4484
584
    default:
4485
584
        return OFPERR_NXBAC_BAD_HEADER_TYPE;
4486
7.96k
    }
4487
7.38k
    encap->new_pkt_type = nae->new_pkt_type;
4488
7.38k
    encap->hdr_size = ntohs(nae->hdr_size);
4489
4490
7.38k
    ofp_prop = nae->props;
4491
7.38k
    props_len = ntohs(nae->len) - offsetof(struct nx_action_encap, props);
4492
7.38k
    n_props = 0;
4493
16.3k
    while (props_len > 0) {
4494
13.5k
        err = decode_ed_prop(&ofp_prop, out, &props_len);
4495
13.5k
        if (err) {
4496
4.61k
            return err;
4497
4.61k
        }
4498
8.95k
        n_props++;
4499
8.95k
    }
4500
2.76k
    encap = ofpbuf_at_assert(out, encap_ofs, sizeof *encap);
4501
2.76k
    encap->n_props = n_props;
4502
2.76k
    out->header = &encap->ofpact;
4503
2.76k
    ofpact_finish_ENCAP(out, &encap);
4504
4505
2.76k
    return 0;
4506
7.38k
}
4507
4508
static void
4509
encode_ENCAP(const struct ofpact_encap *encap,
4510
             enum ofp_version ofp_version OVS_UNUSED,
4511
             struct ofpbuf *out)
4512
1.30k
{
4513
1.30k
    size_t start_ofs = out->size;
4514
1.30k
    struct nx_action_encap *nae = put_NXAST_ENCAP(out);
4515
1.30k
    int i;
4516
4517
1.30k
    nae->new_pkt_type = encap->new_pkt_type;
4518
1.30k
    nae->hdr_size = htons(encap->hdr_size);
4519
1.30k
    const struct ofpact_ed_prop *prop = encap->props;
4520
1.77k
    for (i = 0; i < encap->n_props; i++) {
4521
473
        encode_ed_prop(&prop, out);
4522
473
    }
4523
1.30k
    pad_ofpat(out, start_ofs);
4524
1.30k
}
4525
4526
static bool
4527
parse_encap_header(const char *hdr, ovs_be32 *packet_type)
4528
1.89k
{
4529
1.89k
    if (strcmp(hdr, "ethernet") == 0) {
4530
493
        *packet_type = htonl(PT_ETH);
4531
1.40k
    } else if (strcmp(hdr, "nsh") == 0) {
4532
994
        *packet_type = htonl(PT_NSH);
4533
994
    } else if (strcmp(hdr, "mpls") == 0) {
4534
223
        *packet_type = htonl(PT_MPLS);
4535
223
    } else if (strcmp(hdr, "mpls_mc") == 0) {
4536
70
        *packet_type = htonl(PT_MPLS_MC);
4537
119
    } else {
4538
119
        return false;
4539
119
    }
4540
1.78k
    return true;
4541
1.89k
}
4542
4543
static char *
4544
parse_ed_props(const uint16_t prop_class, char **arg, int *n_props, struct ofpbuf *out)
4545
1.78k
{
4546
1.78k
    char *key, *value, *err;
4547
1.78k
    uint8_t prop_type;
4548
4549
2.51k
    while (ofputil_parse_key_value(arg, &key, &value)) {
4550
842
        if (!parse_ed_prop_type(prop_class, key, &prop_type)) {
4551
87
            return xasprintf("Invalid property: %s", key);
4552
87
        }
4553
755
        if (value == NULL) {
4554
0
            return xasprintf("Missing the value for property: %s", key);
4555
0
        }
4556
755
        err = parse_ed_prop_value(prop_class, prop_type, value, out);
4557
755
        if (err != NULL) {
4558
21
            return err;
4559
21
        }
4560
734
        (*n_props)++;
4561
734
    }
4562
1.67k
    return NULL;
4563
1.78k
}
4564
4565
/* The string representation of the encap action is
4566
 * encap(header_type(prop=<value>,tlv(<class>,<type>,<value>),...))
4567
 */
4568
4569
static char * OVS_WARN_UNUSED_RESULT
4570
parse_ENCAP(char *arg, const struct ofpact_parse_params *pp)
4571
1.90k
{
4572
1.90k
    *pp->usable_protocols &= OFPUTIL_P_OF13_UP;
4573
4574
1.90k
    struct ofpact_encap *encap;
4575
1.90k
    char *key, *value, *str;
4576
1.90k
    char *error = NULL;
4577
1.90k
    uint16_t prop_class;
4578
1.90k
    int n_props = 0;
4579
4580
1.90k
    encap = ofpact_put_ENCAP(pp->ofpacts);
4581
1.90k
    encap->hdr_size = 0;
4582
    /* Parse encap header type. */
4583
1.90k
    str = arg;
4584
1.90k
    if (!ofputil_parse_key_value(&arg, &key, &value)) {
4585
4
        return xasprintf("Missing encap hdr: %s", str);
4586
4
    }
4587
1.89k
    if (!parse_encap_header(key, &encap->new_pkt_type)) {
4588
119
        return xasprintf("Encap hdr not supported: %s", value);
4589
119
    }
4590
1.78k
    if (!parse_ed_prop_class(key, &prop_class)) {
4591
0
        return xasprintf("Invalid encap prop class: %s", key);
4592
0
    }
4593
    /* Parse encap properties. */
4594
1.78k
    error = parse_ed_props(prop_class, &value, &n_props, pp->ofpacts);
4595
1.78k
    if (error != NULL) {
4596
108
        return error;
4597
108
    }
4598
    /* ofpbuf may have been re-allocated. */
4599
1.67k
    encap = pp->ofpacts->header;
4600
1.67k
    encap->n_props = n_props;
4601
4602
1.67k
    if (ofpbuf_oversized(pp->ofpacts)) {
4603
0
        return xasprintf("input too big");
4604
0
    }
4605
4606
1.67k
    ofpact_finish_ENCAP(pp->ofpacts, &encap);
4607
1.67k
    return NULL;
4608
1.67k
}
4609
4610
static char *
4611
format_encap_pkt_type(const ovs_be32 pkt_type)
4612
2.45k
{
4613
2.45k
    switch (ntohl(pkt_type)) {
4614
586
    case PT_ETH:
4615
586
        return "ethernet";
4616
980
    case PT_NSH:
4617
980
        return "nsh";
4618
751
    case PT_MPLS:
4619
751
        return "mpls";
4620
133
    case PT_MPLS_MC:
4621
133
        return "mpls_mc";
4622
0
    default:
4623
0
        return "UNKNOWN";
4624
2.45k
    }
4625
2.45k
}
4626
4627
static void
4628
format_ed_props(struct ds *s, uint16_t n_props,
4629
                const struct ofpact_ed_prop *prop)
4630
326
{
4631
326
    const uint8_t *p = (uint8_t *) prop;
4632
326
    int i;
4633
4634
326
    if (n_props == 0) {
4635
0
        return;
4636
0
    }
4637
1.14k
    for (i = 0; i < n_props; i++) {
4638
823
        format_ed_prop(s, prop);
4639
823
        ds_put_char(s, ',');
4640
823
        p += ROUND_UP(prop->len, 8);
4641
823
        prop = ALIGNED_CAST(const struct ofpact_ed_prop *, p);
4642
823
    }
4643
326
    if (n_props > 0) {
4644
326
        ds_chomp(s, ',');
4645
326
    }
4646
326
}
4647
4648
static void
4649
format_ENCAP(const struct ofpact_encap *a,
4650
             const struct ofpact_format_params *fp)
4651
2.45k
{
4652
2.45k
    ds_put_format(fp->s, "%sencap(%s", colors.paren, colors.end);
4653
2.45k
    ds_put_format(fp->s, "%s", format_encap_pkt_type(a->new_pkt_type));
4654
2.45k
    if (a->n_props > 0) {
4655
326
        ds_put_format(fp->s, "%s(%s", colors.paren, colors.end);
4656
326
        format_ed_props(fp->s, a->n_props, a->props);
4657
326
        ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
4658
326
    }
4659
2.45k
    ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
4660
2.45k
}
4661
4662
static enum ofperr
4663
check_ENCAP(const struct ofpact_encap *a, struct ofpact_check_params *cp)
4664
3.47k
{
4665
3.47k
    struct flow *flow = &cp->match->flow;
4666
3.47k
    flow->packet_type = a->new_pkt_type;
4667
3.47k
    if (pt_ns(flow->packet_type) == OFPHTN_ETHERTYPE) {
4668
2.73k
        flow->dl_type = htons(pt_ns_type(flow->packet_type));
4669
2.73k
    }
4670
3.47k
    if (!is_ip_any(flow)) {
4671
3.06k
        flow->nw_proto = 0;
4672
3.06k
    }
4673
3.47k
    return 0;
4674
3.47k
}
4675

4676
/* Action structure for NXAST_DECAP */
4677
struct nx_action_decap {
4678
    ovs_be16 type;         /* OFPAT_VENDOR. */
4679
    ovs_be16 len;          /* Total size including any property TLVs. */
4680
    ovs_be32 vendor;       /* NX_VENDOR_ID. */
4681
    ovs_be16 subtype;      /* NXAST_DECAP. */
4682
    uint8_t pad[2];        /* 2 bytes padding */
4683
4684
    /* Packet type or result.
4685
     *
4686
     * The special value (0,0xFFFE) "Use next proto"
4687
     * is used to request OVS to automatically set the new packet type based
4688
     * on the decap'ed header's next protocol.
4689
     */
4690
    ovs_be32 new_pkt_type;
4691
};
4692
OFP_ASSERT(sizeof(struct nx_action_decap) == 16);
4693
4694
static enum ofperr
4695
decode_NXAST_RAW_DECAP(const struct nx_action_decap *nad,
4696
                       enum ofp_version ofp_version OVS_UNUSED,
4697
                       struct ofpbuf *ofpacts)
4698
21.0k
{
4699
21.0k
    struct ofpact_decap * decap;
4700
4701
21.0k
    if (ntohs(nad->len) > sizeof *nad) {
4702
        /* No properties supported yet. */
4703
10
        return OFPERR_NXBAC_UNKNOWN_ED_PROP;
4704
10
    }
4705
4706
21.0k
    decap = ofpact_put_DECAP(ofpacts);
4707
21.0k
    decap->ofpact.raw = NXAST_RAW_DECAP;
4708
21.0k
    decap->new_pkt_type = nad->new_pkt_type;
4709
21.0k
    return 0;
4710
21.0k
}
4711
4712
static void
4713
encode_DECAP(const struct ofpact_decap *decap,
4714
                enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4715
1.23k
{
4716
1.23k
    struct nx_action_decap *nad = put_NXAST_DECAP(out);
4717
4718
1.23k
    nad->len = htons(sizeof(struct nx_action_decap));
4719
1.23k
    nad->new_pkt_type = decap->new_pkt_type;
4720
1.23k
}
4721
4722
static char * OVS_WARN_UNUSED_RESULT
4723
parse_DECAP(char *arg, const struct ofpact_parse_params *pp)
4724
1.64k
{
4725
1.64k
    struct ofpact_decap *decap;
4726
1.64k
    char *key, *value, *pos;
4727
1.64k
    char *error = NULL;
4728
1.64k
    uint16_t ns, type;
4729
4730
1.64k
    decap = ofpact_put_DECAP(pp->ofpacts);
4731
    /* Default next packet_type is PT_USE_NEXT_PROTO. */
4732
1.64k
    decap->new_pkt_type = htonl(PT_USE_NEXT_PROTO);
4733
4734
    /* Parse decap packet_type if given. */
4735
1.64k
    if (ofputil_parse_key_value(&arg, &key, &value)) {
4736
101
        if (strcmp(key, "packet_type") == 0) {
4737
55
            pos = value;
4738
55
            if (!ofputil_parse_key_value(&pos, &key, &value)
4739
54
                || strcmp(key, "ns") != 0) {
4740
11
                return xstrdup("Missing packet_type attribute ns");
4741
11
            }
4742
44
            error = str_to_u16(value, "ns", &ns);
4743
44
            if (error) {
4744
1
                return error;
4745
1
            }
4746
43
            if (ns >= OFPHTN_N_TYPES) {
4747
3
                return xasprintf("Unsupported ns value: %"PRIu16, ns);
4748
3
            }
4749
40
            if (!ofputil_parse_key_value(&pos, &key, &value)
4750
38
                || strcmp(key, "type") != 0) {
4751
38
                return xstrdup("Missing packet_type attribute type");
4752
38
            }
4753
2
            error = str_to_u16(value, "type", &type);
4754
2
            if (error) {
4755
1
                return error;
4756
1
            }
4757
1
            decap->new_pkt_type = htonl(PACKET_TYPE(ns, type));
4758
46
        } else {
4759
46
            return xasprintf("Invalid decap argument: %s", key);
4760
46
        }
4761
101
    }
4762
1.54k
    return NULL;
4763
1.64k
}
4764
4765
static void
4766
format_DECAP(const struct ofpact_decap *a,
4767
             const struct ofpact_format_params *fp)
4768
12.3k
{
4769
12.3k
    ds_put_format(fp->s, "%sdecap(%s", colors.paren, colors.end);
4770
12.3k
    if (a->new_pkt_type != htonl(PT_USE_NEXT_PROTO)) {
4771
12.3k
        ds_put_format(fp->s, "packet_type(ns=%"PRIu16",type=%#"PRIx16")",
4772
12.3k
                      pt_ns(a->new_pkt_type),
4773
12.3k
                      pt_ns_type(a->new_pkt_type));
4774
12.3k
    }
4775
12.3k
    ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
4776
12.3k
}
4777
4778
static enum ofperr
4779
check_DECAP(const struct ofpact_decap *a OVS_UNUSED,
4780
            struct ofpact_check_params *cp)
4781
16.6k
{
4782
16.6k
    struct flow *flow = &cp->match->flow;
4783
16.6k
    if (flow->packet_type == htonl(PT_ETH)) {
4784
        /* Adjust the packet_type to allow subsequent actions. */
4785
10.8k
        flow->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE,
4786
10.8k
                                           ntohs(flow->dl_type));
4787
10.8k
    } else {
4788
        /* The actual packet_type is only known after decapsulation.
4789
         * Do not allow subsequent actions that depend on packet headers. */
4790
5.83k
        flow->packet_type = htonl(PT_UNKNOWN);
4791
5.83k
        flow->dl_type = OVS_BE16_MAX;
4792
5.83k
    }
4793
16.6k
    return 0;
4794
16.6k
}
4795
4796
/* Action dec_nsh_ttl */
4797
4798
static enum ofperr
4799
decode_NXAST_RAW_DEC_NSH_TTL(struct ofpbuf *out)
4800
309
{
4801
309
    ofpact_put_DEC_NSH_TTL(out);
4802
309
    return 0;
4803
309
}
4804
4805
static void
4806
encode_DEC_NSH_TTL(const struct ofpact_null *null OVS_UNUSED,
4807
            enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4808
480
{
4809
480
    put_NXAST_DEC_NSH_TTL(out);
4810
480
}
4811
4812
static char * OVS_WARN_UNUSED_RESULT
4813
parse_DEC_NSH_TTL(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
4814
924
{
4815
924
    ofpact_put_DEC_NSH_TTL(pp->ofpacts);
4816
924
    return NULL;
4817
924
}
4818
4819
static void
4820
format_DEC_NSH_TTL(const struct ofpact_null *a OVS_UNUSED,
4821
                   const struct ofpact_format_params *fp)
4822
196
{
4823
196
    ds_put_format(fp->s, "%sdec_nsh_ttl%s", colors.special, colors.end);
4824
196
}
4825
4826
static enum ofperr
4827
check_DEC_NSH_TTL(const struct ofpact_null *a OVS_UNUSED,
4828
                  struct ofpact_check_params *cp)
4829
870
{
4830
870
    struct flow *flow = &cp->match->flow;
4831
870
    if (flow->packet_type != htonl(PT_NSH) &&
4832
551
        flow->dl_type != htons(ETH_TYPE_NSH)) {
4833
346
        inconsistent_match(&cp->usable_protocols);
4834
346
    }
4835
870
    return 0;
4836
870
}
4837

4838
/* Action structures for NXAST_RESUBMIT, NXAST_RESUBMIT_TABLE, and
4839
 * NXAST_RESUBMIT_TABLE_CT.
4840
 *
4841
 * These actions search one of the switch's flow tables:
4842
 *
4843
 *    - For NXAST_RESUBMIT_TABLE and NXAST_RESUBMIT_TABLE_CT, if the
4844
 *      'table' member is not 255, then it specifies the table to search.
4845
 *
4846
 *    - Otherwise (for NXAST_RESUBMIT_TABLE or NXAST_RESUBMIT_TABLE_CT with a
4847
 *      'table' of 255, or for NXAST_RESUBMIT regardless of 'table'), it
4848
 *      searches the current flow table, that is, the OpenFlow flow table that
4849
 *      contains the flow from which this action was obtained.  If this action
4850
 *      did not come from a flow table (e.g. it came from an OFPT_PACKET_OUT
4851
 *      message), then table 0 is the current table.
4852
 *
4853
 * The flow table lookup uses a flow that may be slightly modified from the
4854
 * original lookup:
4855
 *
4856
 *    - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
4857
 *      is used as the flow's in_port.
4858
 *
4859
 *    - For NXAST_RESUBMIT_TABLE and NXAST_RESUBMIT_TABLE_CT, if the 'in_port'
4860
 *      member is not OFPP_IN_PORT, then its value is used as the flow's
4861
 *      in_port.  Otherwise, the original in_port is used.
4862
 *
4863
 *    - For NXAST_RESUBMIT_TABLE_CT the Conntrack 5-tuple fields are used as
4864
 *      the packets IP header fields during the lookup.
4865
 *
4866
 *    - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
4867
 *      resubmit action, then the flow is updated with the new values.
4868
 *
4869
 * Following the lookup, the original in_port is restored.
4870
 *
4871
 * If the modified flow matched in the flow table, then the corresponding
4872
 * actions are executed.  Afterward, actions following the resubmit in the
4873
 * original set of actions, if any, are executed; any changes made to the
4874
 * packet (e.g. changes to VLAN) by secondary actions persist when those
4875
 * actions are executed, although the original in_port is restored.
4876
 *
4877
 * Resubmit actions may be used any number of times within a set of actions.
4878
 *
4879
 * Resubmit actions may nest.  To prevent infinite loops and excessive resource
4880
 * use, the implementation may limit nesting depth and the total number of
4881
 * resubmits:
4882
 *
4883
 *    - Open vSwitch 1.0.1 and earlier did not support recursion.
4884
 *
4885
 *    - Open vSwitch 1.0.2 and 1.0.3 limited recursion to 8 levels.
4886
 *
4887
 *    - Open vSwitch 1.1 and 1.2 limited recursion to 16 levels.
4888
 *
4889
 *    - Open vSwitch 1.2 through 1.8 limited recursion to 32 levels.
4890
 *
4891
 *    - Open vSwitch 1.9 through 2.0 limited recursion to 64 levels.
4892
 *
4893
 *    - Open vSwitch 2.1 through 2.5 limited recursion to 64 levels and impose
4894
 *      a total limit of 4,096 resubmits per flow translation (earlier versions
4895
 *      did not impose any total limit).
4896
 *
4897
 * NXAST_RESUBMIT ignores 'table' and 'pad'.  NXAST_RESUBMIT_TABLE and
4898
 * NXAST_RESUBMIT_TABLE_CT require 'pad' to be all-bits-zero.
4899
 *
4900
 * Open vSwitch 1.0.1 and earlier did not support recursion.  Open vSwitch
4901
 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.  Open vSwitch before
4902
 * 2.8.0 did not support NXAST_RESUBMIT_TABLE_CT.
4903
 */
4904
struct nx_action_resubmit {
4905
    ovs_be16 type;                  /* OFPAT_VENDOR. */
4906
    ovs_be16 len;                   /* Length is 16. */
4907
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
4908
    ovs_be16 subtype;               /* NXAST_RESUBMIT. */
4909
    ovs_be16 in_port;               /* New in_port for checking flow table. */
4910
    uint8_t table;                  /* NXAST_RESUBMIT_TABLE: table to use. */
4911
    uint8_t pad[3];
4912
};
4913
OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
4914
4915
static enum ofperr
4916
decode_NXAST_RAW_RESUBMIT(uint16_t port,
4917
                          enum ofp_version ofp_version OVS_UNUSED,
4918
                          struct ofpbuf *out)
4919
826
{
4920
826
    struct ofpact_resubmit *resubmit;
4921
4922
826
    resubmit = ofpact_put_RESUBMIT(out);
4923
826
    resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
4924
826
    resubmit->in_port = u16_to_ofp(port);
4925
826
    resubmit->table_id = 0xff;
4926
826
    return 0;
4927
826
}
4928
4929
static enum ofperr
4930
decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
4931
                                enum ofp_version ofp_version OVS_UNUSED,
4932
                                struct ofpbuf *out)
4933
2.55k
{
4934
2.55k
    struct ofpact_resubmit *resubmit;
4935
4936
2.55k
    if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
4937
1.06k
        return OFPERR_OFPBAC_BAD_ARGUMENT;
4938
1.06k
    }
4939
4940
1.48k
    resubmit = ofpact_put_RESUBMIT(out);
4941
1.48k
    resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
4942
1.48k
    resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
4943
1.48k
    resubmit->table_id = nar->table;
4944
1.48k
    return 0;
4945
2.55k
}
4946
4947
static enum ofperr
4948
decode_NXAST_RAW_RESUBMIT_TABLE_CT(const struct nx_action_resubmit *nar,
4949
                                   enum ofp_version ofp_version OVS_UNUSED,
4950
                                   struct ofpbuf *out)
4951
1.59k
{
4952
1.59k
    enum ofperr error = decode_NXAST_RAW_RESUBMIT_TABLE(nar, ofp_version, out);
4953
1.59k
    if (error) {
4954
739
        return error;
4955
739
    }
4956
853
    struct ofpact_resubmit *resubmit = out->header;
4957
853
    resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE_CT;
4958
853
    resubmit->with_ct_orig = true;
4959
853
    return 0;
4960
1.59k
}
4961
4962
static void
4963
encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
4964
                enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4965
1.15k
{
4966
1.15k
    uint16_t in_port = ofp_to_u16(resubmit->in_port);
4967
4968
1.15k
    if (resubmit->table_id == 0xff
4969
692
        && resubmit->ofpact.raw == NXAST_RAW_RESUBMIT) {
4970
0
        put_NXAST_RESUBMIT(out, in_port);
4971
1.15k
    } else {
4972
1.15k
        struct nx_action_resubmit *nar;
4973
1.15k
        nar = resubmit->with_ct_orig
4974
1.15k
            ? put_NXAST_RESUBMIT_TABLE_CT(out) : put_NXAST_RESUBMIT_TABLE(out);
4975
1.15k
        nar->table = resubmit->table_id;
4976
1.15k
        nar->in_port = htons(in_port);
4977
1.15k
    }
4978
1.15k
}
4979
4980
static char * OVS_WARN_UNUSED_RESULT
4981
parse_RESUBMIT(char *arg, const struct ofpact_parse_params *pp)
4982
2.29k
{
4983
2.29k
    struct ofpact_resubmit *resubmit;
4984
2.29k
    char *in_port_s, *table_s, *ct_s;
4985
4986
2.29k
    resubmit = ofpact_put_RESUBMIT(pp->ofpacts);
4987
4988
2.29k
    in_port_s = strsep(&arg, ",");
4989
2.29k
    if (in_port_s && in_port_s[0]) {
4990
1.26k
        if (!ofputil_port_from_string(in_port_s, pp->port_map,
4991
1.26k
                                      &resubmit->in_port)) {
4992
5
            return xasprintf("%s: resubmit to unknown port", in_port_s);
4993
5
        }
4994
1.26k
    } else {
4995
1.02k
        resubmit->in_port = OFPP_IN_PORT;
4996
1.02k
    }
4997
4998
2.28k
    table_s = strsep(&arg, ",");
4999
2.28k
    if (table_s && table_s[0]) {
5000
1.03k
        if (!ofputil_table_from_string(table_s, pp->table_map,
5001
1.03k
                                       &resubmit->table_id)) {
5002
1
            return xasprintf("%s: resubmit to unknown table", table_s);
5003
1
        }
5004
1.24k
    } else {
5005
1.24k
        resubmit->table_id = 255;
5006
1.24k
    }
5007
5008
2.28k
    ct_s = strsep(&arg, ",");
5009
2.28k
    if (ct_s && ct_s[0]) {
5010
471
        if (strcmp(ct_s, "ct")) {
5011
25
            return xasprintf("%s: unknown parameter", ct_s);
5012
25
        }
5013
446
        resubmit->with_ct_orig = true;
5014
1.81k
    } else {
5015
1.81k
        resubmit->with_ct_orig = false;
5016
1.81k
    }
5017
5018
2.25k
    if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
5019
18
        return xstrdup("at least one \"in_port\" or \"table\" must be "
5020
18
                       "specified on resubmit");
5021
18
    }
5022
2.24k
    return NULL;
5023
2.25k
}
5024
5025
static void
5026
format_RESUBMIT(const struct ofpact_resubmit *a,
5027
                const struct ofpact_format_params *fp)
5028
1.54k
{
5029
1.54k
    if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
5030
71
        ds_put_format(fp->s, "%sresubmit:%s", colors.special, colors.end);
5031
71
        ofputil_format_port(a->in_port, fp->port_map, fp->s);
5032
1.47k
    } else {
5033
1.47k
        ds_put_format(fp->s, "%sresubmit(%s", colors.paren, colors.end);
5034
1.47k
        if (a->in_port != OFPP_IN_PORT) {
5035
1.45k
            ofputil_format_port(a->in_port, fp->port_map, fp->s);
5036
1.45k
        }
5037
1.47k
        ds_put_char(fp->s, ',');
5038
1.47k
        if (a->table_id != 255) {
5039
1.45k
            ofputil_format_table(a->table_id, fp->table_map, fp->s);
5040
1.45k
        }
5041
1.47k
        if (a->with_ct_orig) {
5042
822
            ds_put_cstr(fp->s, ",ct");
5043
822
        }
5044
1.47k
        ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
5045
1.47k
    }
5046
1.54k
}
5047
5048
static enum ofperr
5049
check_RESUBMIT(const struct ofpact_resubmit *a,
5050
               const struct ofpact_check_params *cp)
5051
1.21k
{
5052
1.21k
    if (a->with_ct_orig && !is_ct_valid(&cp->match->flow, &cp->match->wc,
5053
405
                                        NULL)) {
5054
16
        return OFPERR_OFPBAC_MATCH_INCONSISTENT;
5055
16
    }
5056
1.19k
    return 0;
5057
1.21k
}
5058

5059
/* Action structure for NXAST_LEARN and NXAST_LEARN2.
5060
 *
5061
 * This action adds or modifies a flow in an OpenFlow table, similar to
5062
 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'.  The new flow has the
5063
 * specified idle timeout, hard timeout, priority, cookie, and flags.  The new
5064
 * flow's match criteria and actions are built by applying each of the series
5065
 * of flow_mod_spec elements included as part of the action.
5066
 *
5067
 * A flow_mod_spec starts with a 16-bit header.  A header that is all-bits-0 is
5068
 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
5069
 * length.  Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
5070
 * bits from a source to a destination.  In this case, the header contains
5071
 * multiple fields:
5072
 *
5073
 *  15  14  13 12  11 10                              0
5074
 * +------+---+------+---------------------------------+
5075
 * |   0  |src|  dst |             n_bits              |
5076
 * +------+---+------+---------------------------------+
5077
 *
5078
 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'.  The
5079
 * following table summarizes the meaning of each possible combination.
5080
 * Details follow the table:
5081
 *
5082
 *   src dst  meaning
5083
 *   --- ---  ----------------------------------------------------------
5084
 *    0   0   Add match criteria based on value in a field.
5085
 *    1   0   Add match criteria based on an immediate value.
5086
 *    0   1   Add NXAST_REG_LOAD action to copy field into a different field.
5087
 *    1   1   Add NXAST_REG_LOAD action to load immediate value into a field.
5088
 *    0   2   Add OFPAT_OUTPUT action to output to port from specified field.
5089
 *   All other combinations are undefined and not allowed.
5090
 *
5091
 * The flow_mod_spec header is followed by a source specification and a
5092
 * destination specification.  The format and meaning of the source
5093
 * specification depends on 'src':
5094
 *
5095
 *   - If 'src' is 0, the source bits are taken from a field in the flow to
5096
 *     which this action is attached.  (This should be a wildcarded field.  If
5097
 *     its value is fully specified then the source bits being copied have
5098
 *     constant values.)
5099
 *
5100
 *     The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
5101
 *     'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
5102
 *     offset within that field.  The source bits are field[ofs:ofs+n_bits-1].
5103
 *     'field' and 'ofs' are subject to the same restrictions as the source
5104
 *     field in NXAST_REG_MOVE.
5105
 *
5106
 *   - If 'src' is 1, the source bits are a constant value.  The source
5107
 *     specification is (n_bits+15)/16*2 bytes long.  Taking those bytes as a
5108
 *     number in network order, the source bits are the 'n_bits'
5109
 *     least-significant bits.  The switch will report an error if other bits
5110
 *     in the constant are nonzero.
5111
 *
5112
 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
5113
 * ovs_be32 'field' and an ovs_be16 'ofs'.  'field' is an nxm_header with
5114
 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field.  The
5115
 * meaning of the flow_mod_spec depends on 'dst':
5116
 *
5117
 *   - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
5118
 *     flow.  The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
5119
 *     packet equal the source bits.  'field' may be any nxm_header with
5120
 *     nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
5121
 *
5122
 *     Order is significant.  Earlier flow_mod_specs must satisfy any
5123
 *     prerequisites for matching fields specified later, by copying constant
5124
 *     values into prerequisite fields.
5125
 *
5126
 *     The switch will reject flow_mod_specs that do not satisfy NXM masking
5127
 *     restrictions.
5128
 *
5129
 *   - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
5130
 *     the new flow.  The new flow copies the source bits into
5131
 *     field[ofs:ofs+n_bits-1].  Actions are executed in the same order as the
5132
 *     flow_mod_specs.
5133
 *
5134
 *     A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
5135
 *     greater than 64 yields multiple NXAST_REG_LOAD actions.
5136
 *
5137
 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
5138
 * empty.  It has the following meaning:
5139
 *
5140
 *   - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
5141
 *     The new flow outputs to the OpenFlow port specified by the source field.
5142
 *     Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
5143
 *     OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported.  Other special ports
5144
 *     may not be used.
5145
 *
5146
 * Resource Management
5147
 * -------------------
5148
 *
5149
 * A switch has a finite amount of flow table space available for learning.
5150
 * When this space is exhausted, no new learning table entries will be learned
5151
 * until some existing flow table entries expire.  The controller should be
5152
 * prepared to handle this by flooding (which can be implemented as a
5153
 * low-priority flow).
5154
 *
5155
 * If a learned flow matches a single TCP stream with a relatively long
5156
 * timeout, one may make the best of resource constraints by setting
5157
 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
5158
 * both, to shorter timeouts.  When either of these is specified as a nonzero
5159
 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
5160
 * the learned flow.
5161
 *
5162
 * Examples
5163
 * --------
5164
 *
5165
 * The following examples give a prose description of the flow_mod_specs along
5166
 * with informal notation for how those would be represented and a hex dump of
5167
 * the bytes that would be required.
5168
 *
5169
 * These examples could work with various nx_action_learn parameters.  Typical
5170
 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
5171
 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
5172
 *
5173
 * 1. Learn input port based on the source MAC, with lookup into
5174
 *    NXM_NX_REG1[16:31] by resubmit to in_port=99:
5175
 *
5176
 *    Match on in_port=99:
5177
 *       ovs_be16(src=1, dst=0, n_bits=16),               20 10
5178
 *       ovs_be16(99),                                    00 63
5179
 *       ovs_be32(NXM_OF_IN_PORT), ovs_be16(0)            00 00 00 02 00 00
5180
 *
5181
 *    Match Ethernet destination on Ethernet source from packet:
5182
 *       ovs_be16(src=0, dst=0, n_bits=48),               00 30
5183
 *       ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0)            00 00 04 06 00 00
5184
 *       ovs_be32(NXM_OF_ETH_DST), ovs_be16(0)            00 00 02 06 00 00
5185
 *
5186
 *    Set NXM_NX_REG1[16:31] to the packet's input port:
5187
 *       ovs_be16(src=0, dst=1, n_bits=16),               08 10
5188
 *       ovs_be32(NXM_OF_IN_PORT), ovs_be16(0)            00 00 00 02 00 00
5189
 *       ovs_be32(NXM_NX_REG1), ovs_be16(16)              00 01 02 04 00 10
5190
 *
5191
 *    Given a packet that arrived on port A with Ethernet source address B,
5192
 *    this would set up the flow "in_port=99, dl_dst=B,
5193
 *    actions=load:A->NXM_NX_REG1[16..31]".
5194
 *
5195
 *    In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
5196
 *    NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
5197
 *    load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
5198
 *
5199
 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
5200
 *    into NXM_NX_REG1[16:31]:
5201
 *
5202
 *    Match on same VLAN ID as packet:
5203
 *       ovs_be16(src=0, dst=0, n_bits=12),               00 0c
5204
 *       ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0)           00 00 08 02 00 00
5205
 *       ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0)           00 00 08 02 00 00
5206
 *
5207
 *    Match Ethernet destination on Ethernet source from packet:
5208
 *       ovs_be16(src=0, dst=0, n_bits=48),               00 30
5209
 *       ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0)            00 00 04 06 00 00
5210
 *       ovs_be32(NXM_OF_ETH_DST), ovs_be16(0)            00 00 02 06 00 00
5211
 *
5212
 *    Output to the packet's input port:
5213
 *       ovs_be16(src=0, dst=2, n_bits=16),               10 10
5214
 *       ovs_be32(NXM_OF_IN_PORT), ovs_be16(0)            00 00 00 02 00 00
5215
 *
5216
 *    Given a packet that arrived on port A with Ethernet source address B in
5217
 *    VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
5218
 *    actions=output:A".
5219
 *
5220
 *    In syntax accepted by ovs-ofctl, this action is:
5221
 *    learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
5222
 *    output:NXM_OF_IN_PORT[])
5223
 *
5224
 * 3. Here's a recipe for a very simple-minded MAC learning switch.  It uses a
5225
 *    10-second MAC expiration time to make it easier to see what's going on
5226
 *
5227
 *      ovs-vsctl del-controller br0
5228
 *      ovs-ofctl del-flows br0
5229
 *      ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
5230
          hard_timeout=10, NXM_OF_VLAN_TCI[0..11],             \
5231
          NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],                   \
5232
          output:NXM_OF_IN_PORT[]), resubmit(,1)"
5233
 *      ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
5234
 *
5235
 *    You can then dump the MAC learning table with:
5236
 *
5237
 *      ovs-ofctl dump-flows br0 table=1
5238
 *
5239
 * Usage Advice
5240
 * ------------
5241
 *
5242
 * For best performance, segregate learned flows into a table that is not used
5243
 * for any other flows except possibly for a lowest-priority "catch-all" flow
5244
 * (a flow with no match criteria).  If different learning actions specify
5245
 * different match criteria, use different tables for the learned flows.
5246
 *
5247
 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
5248
 * These timeouts apply to the flow that is added, which means that a flow with
5249
 * an idle timeout will expire when no traffic has been sent *to* the learned
5250
 * address.  This is not usually the intent in MAC learning; instead, we want
5251
 * the MAC learn entry to expire when no traffic has been sent *from* the
5252
 * learned address.  Use a hard timeout for that.
5253
 *
5254
 *
5255
 * Visibility of Changes
5256
 * ---------------------
5257
 *
5258
 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
5259
 * flow translation are visible to flow table lookups made later in the flow
5260
 * translation.  This means that, in the example above, a MAC learned by the
5261
 * learn action in table 0 would be found in table 1 (if the packet being
5262
 * processed had the same source and destination MAC address).
5263
 *
5264
 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
5265
 * modify a flow) by a "learn" action are visible only for later flow
5266
 * translations, not for later lookups within the same flow translation.  In
5267
 * the MAC learning example, a MAC learned by the learn action in table 0 would
5268
 * not be found in table 1 if the flow translation would resubmit to table 1
5269
 * after the processing of the learn action, meaning that if this MAC had not
5270
 * been learned before then the packet would be flooded. */
5271
struct nx_action_learn {
5272
    ovs_be16 type;              /* OFPAT_VENDOR. */
5273
    ovs_be16 len;               /* At least 24. */
5274
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
5275
    ovs_be16 subtype;           /* NXAST_LEARN. */
5276
    ovs_be16 idle_timeout;      /* Idle time before discarding (seconds). */
5277
    ovs_be16 hard_timeout;      /* Max time before discarding (seconds). */
5278
    ovs_be16 priority;          /* Priority level of flow entry. */
5279
    ovs_be64 cookie;            /* Cookie for new flow. */
5280
    ovs_be16 flags;             /* NX_LEARN_F_*. */
5281
    uint8_t table_id;           /* Table to insert flow entry. */
5282
    uint8_t pad;                /* Must be zero. */
5283
    ovs_be16 fin_idle_timeout;  /* Idle timeout after FIN, if nonzero. */
5284
    ovs_be16 fin_hard_timeout;  /* Hard timeout after FIN, if nonzero. */
5285
    /* Followed by a sequence of flow_mod_spec elements, as described above,
5286
     * until the end of the action is reached. */
5287
};
5288
OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
5289
5290
struct nx_action_learn2 {
5291
    struct nx_action_learn up;  /* The wire format includes nx_action_learn. */
5292
    ovs_be32 limit;             /* Maximum number of learned flows.
5293
                                 * 0 indicates unlimited. */
5294
5295
    /* Where to store the result. */
5296
    ovs_be16 result_dst_ofs;    /* Starting bit offset in destination. */
5297
5298
    ovs_be16 pad2;              /* Must be zero. */
5299
    /* Followed by:
5300
     * - OXM/NXM header for destination field (4 or 8 bytes),
5301
     *   if NX_LEARN_F_WRITE_RESULT is set in 'flags'
5302
     * - a sequence of flow_mod_spec elements, as described above,
5303
     *   until the end of the action is reached. */
5304
};
5305
OFP_ASSERT(sizeof(struct nx_action_learn2) == 40);
5306
5307
static ovs_be16
5308
get_be16(const void **pp)
5309
119k
{
5310
119k
    const ovs_be16 *p = *pp;
5311
119k
    ovs_be16 value = *p;
5312
119k
    *pp = p + 1;
5313
119k
    return value;
5314
119k
}
5315
5316
static ovs_be32
5317
get_be32(const void **pp)
5318
53.0k
{
5319
53.0k
    const ovs_be32 *p = *pp;
5320
53.0k
    ovs_be32 value = get_unaligned_be32(p);
5321
53.0k
    *pp = p + 1;
5322
53.0k
    return value;
5323
53.0k
}
5324
5325
static enum ofperr
5326
get_subfield(int n_bits, const void **p, struct mf_subfield *sf,
5327
             const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap)
5328
53.0k
{
5329
53.0k
    enum ofperr error;
5330
5331
53.0k
    error = mf_vl_mff_mf_from_nxm_header(ntohl(get_be32(p)), vl_mff_map,
5332
53.0k
                                         &sf->field, tlv_bitmap);
5333
53.0k
    sf->ofs = ntohs(get_be16(p));
5334
53.0k
    sf->n_bits = n_bits;
5335
53.0k
    return error;
5336
53.0k
}
5337
5338
static unsigned int
5339
learn_min_len(uint16_t header)
5340
53.7k
{
5341
53.7k
    int n_bits = header & NX_LEARN_N_BITS_MASK;
5342
53.7k
    int src_type = header & NX_LEARN_SRC_MASK;
5343
53.7k
    int dst_type = header & NX_LEARN_DST_MASK;
5344
53.7k
    unsigned int min_len;
5345
5346
53.7k
    min_len = 0;
5347
53.7k
    if (src_type == NX_LEARN_SRC_FIELD) {
5348
5.00k
        min_len += sizeof(ovs_be32); /* src_field */
5349
5.00k
        min_len += sizeof(ovs_be16); /* src_ofs */
5350
48.7k
    } else {
5351
48.7k
        min_len += 2 * DIV_ROUND_UP(n_bits, 16);
5352
48.7k
    }
5353
53.7k
    if (dst_type == NX_LEARN_DST_MATCH ||
5354
50.1k
        dst_type == NX_LEARN_DST_LOAD) {
5355
50.1k
        min_len += sizeof(ovs_be32); /* dst_field */
5356
50.1k
        min_len += sizeof(ovs_be16); /* dst_ofs */
5357
50.1k
    }
5358
53.7k
    return min_len;
5359
53.7k
}
5360
5361
static enum ofperr
5362
decode_LEARN_common(const struct nx_action_learn *nal,
5363
                    enum ofp_raw_action_type raw,
5364
                    struct ofpact_learn *learn)
5365
30.3k
{
5366
30.3k
    if (nal->pad) {
5367
1.82k
        return OFPERR_OFPBAC_BAD_ARGUMENT;
5368
1.82k
    }
5369
5370
28.5k
    learn->ofpact.raw = raw;
5371
28.5k
    learn->idle_timeout = ntohs(nal->idle_timeout);
5372
28.5k
    learn->hard_timeout = ntohs(nal->hard_timeout);
5373
28.5k
    learn->priority = ntohs(nal->priority);
5374
28.5k
    learn->cookie = nal->cookie;
5375
28.5k
    learn->table_id = nal->table_id;
5376
28.5k
    learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
5377
28.5k
    learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
5378
28.5k
    learn->flags = ntohs(nal->flags);
5379
5380
28.5k
    if (learn->table_id == 0xff) {
5381
199
        return OFPERR_OFPBAC_BAD_ARGUMENT;
5382
199
    }
5383
5384
28.3k
    return 0;
5385
28.5k
}
5386
5387
static enum ofperr
5388
decode_LEARN_specs(const void *p, const void *end,
5389
                   const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap,
5390
                   struct ofpbuf *ofpacts)
5391
27.8k
{
5392
27.8k
    struct ofpact_learn *learn = ofpacts->header;
5393
5394
78.0k
    while (p != end) {
5395
66.2k
        struct ofpact_learn_spec *spec;
5396
66.2k
        uint16_t header = ntohs(get_be16(&p));
5397
5398
66.2k
        if (!header) {
5399
12.0k
            break;
5400
12.0k
        }
5401
5402
54.2k
        spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
5403
54.2k
        learn = ofpacts->header;
5404
5405
54.2k
        spec->src_type = header & NX_LEARN_SRC_MASK;
5406
54.2k
        spec->dst_type = header & NX_LEARN_DST_MASK;
5407
54.2k
        spec->n_bits = header & NX_LEARN_N_BITS_MASK;
5408
5409
        /* Check for valid src and dst type combination. */
5410
54.2k
        if (spec->dst_type == NX_LEARN_DST_MATCH ||
5411
4.70k
            spec->dst_type == NX_LEARN_DST_LOAD ||
5412
4.05k
            (spec->dst_type == NX_LEARN_DST_OUTPUT &&
5413
53.7k
             spec->src_type == NX_LEARN_SRC_FIELD)) {
5414
            /* OK. */
5415
53.7k
        } else {
5416
470
            return OFPERR_OFPBAC_BAD_ARGUMENT;
5417
470
        }
5418
5419
        /* Check that the arguments don't overrun the end of the action. */
5420
53.7k
        if ((char *) end - (char *) p < learn_min_len(header)) {
5421
798
            return OFPERR_OFPBAC_BAD_LEN;
5422
798
        }
5423
5424
        /* Get the source. */
5425
52.9k
        const uint8_t *imm = NULL;
5426
52.9k
        unsigned int imm_bytes = 0;
5427
52.9k
        enum ofperr error;
5428
52.9k
        if (spec->src_type == NX_LEARN_SRC_FIELD) {
5429
4.41k
            error = get_subfield(spec->n_bits, &p, &spec->src, vl_mff_map,
5430
4.41k
                                 tlv_bitmap);
5431
4.41k
            if (error) {
5432
870
                return error;
5433
870
            }
5434
48.5k
        } else {
5435
48.5k
            int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
5436
48.5k
            p = (const uint8_t *) p + p_bytes;
5437
5438
48.5k
            imm_bytes = DIV_ROUND_UP(spec->n_bits, 8);
5439
48.5k
            imm = (const uint8_t *) p - imm_bytes;
5440
48.5k
        }
5441
5442
        /* Get the destination. */
5443
52.0k
        if (spec->dst_type == NX_LEARN_DST_MATCH ||
5444
48.6k
            spec->dst_type == NX_LEARN_DST_LOAD) {
5445
48.6k
            error = get_subfield(spec->n_bits, &p, &spec->dst, vl_mff_map,
5446
48.6k
                                 tlv_bitmap);
5447
48.6k
            if (error) {
5448
1.91k
                return error;
5449
1.91k
            }
5450
48.6k
        }
5451
5452
50.1k
        if (imm) {
5453
46.6k
            uint8_t *src_imm = ofpbuf_put_zeros(ofpacts,
5454
46.6k
                                                OFPACT_ALIGN(imm_bytes));
5455
46.6k
            memcpy(src_imm, imm, imm_bytes);
5456
5457
46.6k
            learn = ofpacts->header;
5458
46.6k
        }
5459
50.1k
    }
5460
23.7k
    ofpact_finish_LEARN(ofpacts, &learn);
5461
5462
23.7k
    if (!is_all_zeros(p, (char *) end - (char *) p)) {
5463
678
        return OFPERR_OFPBAC_BAD_ARGUMENT;
5464
678
    }
5465
5466
23.1k
    return 0;
5467
23.7k
}
5468
5469
/* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
5470
 * 'ofpacts'.  Returns 0 if successful, otherwise an OFPERR_*. */
5471
static enum ofperr
5472
decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
5473
                       enum ofp_version ofp_version OVS_UNUSED,
5474
                       const struct vl_mff_map *vl_mff_map,
5475
                       uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
5476
29.5k
{
5477
29.5k
    struct ofpact_learn *learn;
5478
29.5k
    enum ofperr error;
5479
5480
29.5k
    learn = ofpact_put_LEARN(ofpacts);
5481
5482
29.5k
    error = decode_LEARN_common(nal, NXAST_RAW_LEARN, learn);
5483
29.5k
    if (error) {
5484
1.41k
        return error;
5485
1.41k
    }
5486
5487
28.1k
    if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
5488
28.1k
                         NX_LEARN_F_DELETE_LEARNED)) {
5489
382
        return OFPERR_OFPBAC_BAD_ARGUMENT;
5490
382
    }
5491
5492
27.7k
    return decode_LEARN_specs(nal + 1, (char *) nal + ntohs(nal->len),
5493
27.7k
                              vl_mff_map, tlv_bitmap, ofpacts);
5494
28.1k
}
5495
5496
/* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
5497
 * 'ofpacts'.  Returns 0 if successful, otherwise an OFPERR_*. */
5498
static enum ofperr
5499
decode_NXAST_RAW_LEARN2(const struct nx_action_learn2 *nal,
5500
                        enum ofp_version ofp_version OVS_UNUSED,
5501
                        const struct vl_mff_map *vl_mff_map,
5502
                        uint64_t *tlv_bitmap, struct ofpbuf *ofpacts)
5503
1.30k
{
5504
1.30k
    struct ofpbuf b = ofpbuf_const_initializer(nal, ntohs(nal->up.len));
5505
1.30k
    struct ofpact_learn *learn;
5506
1.30k
    enum ofperr error;
5507
5508
1.30k
    if (nal->pad2) {
5509
519
        return OFPERR_NXBAC_MUST_BE_ZERO;
5510
519
    }
5511
5512
784
    learn = ofpact_put_LEARN(ofpacts);
5513
784
    error = decode_LEARN_common(&nal->up, NXAST_RAW_LEARN2, learn);
5514
784
    if (error) {
5515
615
        return error;
5516
615
    }
5517
5518
169
    learn->limit = ntohl(nal->limit);
5519
5520
169
    if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
5521
169
                         NX_LEARN_F_DELETE_LEARNED |
5522
169
                         NX_LEARN_F_WRITE_RESULT)) {
5523
18
        return OFPERR_OFPBAC_BAD_ARGUMENT;
5524
18
    }
5525
5526
151
    ofpbuf_pull(&b, sizeof *nal);
5527
5528
151
    if (learn->flags & NX_LEARN_F_WRITE_RESULT) {
5529
65
        error = nx_pull_header(&b, vl_mff_map, &learn->result_dst.field, NULL);
5530
65
        if (error) {
5531
29
            return error;
5532
29
        }
5533
36
        if (!learn->result_dst.field->writable) {
5534
18
            return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
5535
18
        }
5536
18
        learn->result_dst.ofs = ntohs(nal->result_dst_ofs);
5537
18
        learn->result_dst.n_bits = 1;
5538
86
    } else if (nal->result_dst_ofs) {
5539
29
        return OFPERR_OFPBAC_BAD_ARGUMENT;
5540
29
    }
5541
5542
75
    return decode_LEARN_specs(b.data, (char *) nal + ntohs(nal->up.len),
5543
75
                              vl_mff_map, tlv_bitmap, ofpacts);
5544
151
}
5545
5546
static void
5547
put_be16(struct ofpbuf *b, ovs_be16 x)
5548
102k
{
5549
102k
    ofpbuf_put(b, &x, sizeof x);
5550
102k
}
5551
5552
static void
5553
put_be32(struct ofpbuf *b, ovs_be32 x)
5554
52.8k
{
5555
52.8k
    ofpbuf_put(b, &x, sizeof x);
5556
52.8k
}
5557
5558
static void
5559
put_u16(struct ofpbuf *b, uint16_t x)
5560
102k
{
5561
102k
    put_be16(b, htons(x));
5562
102k
}
5563
5564
static void
5565
put_u32(struct ofpbuf *b, uint32_t x)
5566
52.8k
{
5567
52.8k
    put_be32(b, htonl(x));
5568
52.8k
}
5569
5570
static void
5571
encode_LEARN(const struct ofpact_learn *learn,
5572
             enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
5573
33.1k
{
5574
33.1k
    const struct ofpact_learn_spec *spec;
5575
33.1k
    struct nx_action_learn *nal;
5576
33.1k
    size_t start_ofs;
5577
5578
33.1k
    start_ofs = out->size;
5579
5580
33.1k
    if (learn->ofpact.raw == NXAST_RAW_LEARN2
5581
33.1k
        || learn->limit != 0
5582
32.7k
        || learn->flags & NX_LEARN_F_WRITE_RESULT) {
5583
388
        struct nx_action_learn2 *nal2;
5584
5585
388
        nal2 = put_NXAST_LEARN2(out);
5586
388
        nal2->limit = htonl(learn->limit);
5587
388
        nal2->result_dst_ofs = htons(learn->result_dst.ofs);
5588
388
        nal = &nal2->up;
5589
32.7k
    } else {
5590
32.7k
        nal = put_NXAST_LEARN(out);
5591
32.7k
    }
5592
33.1k
    nal->idle_timeout = htons(learn->idle_timeout);
5593
33.1k
    nal->hard_timeout = htons(learn->hard_timeout);
5594
33.1k
    nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
5595
33.1k
    nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
5596
33.1k
    nal->priority = htons(learn->priority);
5597
33.1k
    nal->cookie = learn->cookie;
5598
33.1k
    nal->flags = htons(learn->flags);
5599
33.1k
    nal->table_id = learn->table_id;
5600
5601
33.1k
    if (learn->flags & NX_LEARN_F_WRITE_RESULT) {
5602
36
        nx_put_header(out, learn->result_dst.field->id, 0, false);
5603
36
    }
5604
5605
49.9k
    OFPACT_LEARN_SPEC_FOR_EACH (spec, learn) {
5606
49.9k
        put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
5607
5608
49.9k
        if (spec->src_type == NX_LEARN_SRC_FIELD) {
5609
3.11k
            put_u32(out, nxm_header_from_mff(spec->src.field));
5610
3.11k
            put_u16(out, spec->src.ofs);
5611
46.8k
        } else {
5612
46.8k
            size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
5613
46.8k
            uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
5614
46.8k
            unsigned int n_bytes = DIV_ROUND_UP(spec->n_bits, 8);
5615
5616
46.8k
            memcpy(bits + n_dst_bytes - n_bytes, ofpact_learn_spec_imm(spec),
5617
46.8k
                   n_bytes);
5618
46.8k
        }
5619
5620
49.9k
        if (spec->dst_type == NX_LEARN_DST_MATCH ||
5621
49.7k
            spec->dst_type == NX_LEARN_DST_LOAD) {
5622
49.7k
            put_u32(out, nxm_header_from_mff(spec->dst.field));
5623
49.7k
            put_u16(out, spec->dst.ofs);
5624
49.7k
        }
5625
49.9k
    }
5626
5627
33.1k
    pad_ofpat(out, start_ofs);
5628
33.1k
}
5629
5630
static char * OVS_WARN_UNUSED_RESULT
5631
parse_LEARN(char *arg, const struct ofpact_parse_params *pp)
5632
35.1k
{
5633
35.1k
    return learn_parse(arg, pp->port_map, pp->table_map, pp->ofpacts);
5634
35.1k
}
5635
5636
static void
5637
format_LEARN(const struct ofpact_learn *a,
5638
             const struct ofpact_format_params *fp)
5639
21.8k
{
5640
21.8k
    learn_format(a, fp->port_map, fp->table_map, fp->s);
5641
21.8k
}
5642
5643
static enum ofperr
5644
check_LEARN(const struct ofpact_learn *a,
5645
            const struct ofpact_check_params *cp)
5646
55.5k
{
5647
55.5k
    return learn_check(a, cp->match);
5648
55.5k
}
5649

5650
/* Action structure for NXAST_CONJUNCTION. */
5651
struct nx_action_conjunction {
5652
    ovs_be16 type;                  /* OFPAT_VENDOR. */
5653
    ovs_be16 len;                   /* At least 16. */
5654
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
5655
    ovs_be16 subtype;               /* See enum ofp_raw_action_type. */
5656
    uint8_t clause;
5657
    uint8_t n_clauses;
5658
    ovs_be32 id;
5659
};
5660
OFP_ASSERT(sizeof(struct nx_action_conjunction) == 16);
5661
5662
static void
5663
add_conjunction(struct ofpbuf *out,
5664
                uint32_t id, uint8_t clause, uint8_t n_clauses)
5665
4.98k
{
5666
4.98k
    struct ofpact_conjunction *oc;
5667
5668
4.98k
    oc = ofpact_put_CONJUNCTION(out);
5669
4.98k
    oc->id = id;
5670
4.98k
    oc->clause = clause;
5671
4.98k
    oc->n_clauses = n_clauses;
5672
4.98k
}
5673
5674
static enum ofperr
5675
decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction *nac,
5676
                             enum ofp_version ofp_version OVS_UNUSED,
5677
                             struct ofpbuf *out)
5678
5.03k
{
5679
5.03k
    if (nac->n_clauses < 2 || nac->n_clauses > 64
5680
4.30k
        || nac->clause >= nac->n_clauses) {
5681
951
        return OFPERR_NXBAC_BAD_CONJUNCTION;
5682
4.08k
    } else {
5683
4.08k
        add_conjunction(out, ntohl(nac->id), nac->clause, nac->n_clauses);
5684
4.08k
        return 0;
5685
4.08k
    }
5686
5.03k
}
5687
5688
static void
5689
encode_CONJUNCTION(const struct ofpact_conjunction *oc,
5690
                   enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
5691
401
{
5692
401
    struct nx_action_conjunction *nac = put_NXAST_CONJUNCTION(out);
5693
401
    nac->clause = oc->clause;
5694
401
    nac->n_clauses = oc->n_clauses;
5695
401
    nac->id = htonl(oc->id);
5696
401
}
5697
5698
static void
5699
format_CONJUNCTION(const struct ofpact_conjunction *oc,
5700
                   const struct ofpact_format_params *fp)
5701
1.42k
{
5702
1.42k
    ds_put_format(fp->s, "%sconjunction(%s%"PRIu32",%d/%"PRIu8"%s)%s",
5703
1.42k
                  colors.paren, colors.end,
5704
1.42k
                  oc->id, oc->clause + 1, oc->n_clauses,
5705
1.42k
                  colors.paren, colors.end);
5706
1.42k
}
5707
5708
static char * OVS_WARN_UNUSED_RESULT
5709
parse_CONJUNCTION(const char *arg, const struct ofpact_parse_params *pp)
5710
923
{
5711
923
    uint8_t n_clauses;
5712
923
    uint8_t clause;
5713
923
    uint32_t id;
5714
923
    int n;
5715
5716
923
    if (!ovs_scan(arg, "%"SCNi32" , %"SCNu8" / %"SCNu8" %n",
5717
923
                  &id, &clause, &n_clauses, &n) || n != strlen(arg)) {
5718
20
        return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
5719
20
    }
5720
5721
903
    if (n_clauses < 2) {
5722
1
        return xstrdup("conjunction must have at least 2 clauses");
5723
902
    } else if (n_clauses > 64) {
5724
1
        return xstrdup("conjunction must have at most 64 clauses");
5725
901
    } else if (clause < 1) {
5726
1
        return xstrdup("clause index must be positive");
5727
900
    } else if (clause > n_clauses) {
5728
2
        return xstrdup("clause index must be less than or equal to "
5729
2
                       "number of clauses");
5730
2
    }
5731
5732
898
    add_conjunction(pp->ofpacts, id, clause - 1, n_clauses);
5733
898
    return NULL;
5734
903
}
5735
5736
static enum ofperr
5737
check_CONJUNCTION(const struct ofpact_conjunction *a OVS_UNUSED,
5738
                  const struct ofpact_check_params *cp OVS_UNUSED)
5739
987
{
5740
987
    return 0;
5741
987
}
5742

5743
/* Action structure for NXAST_MULTIPATH.
5744
 *
5745
 * This action performs the following steps in sequence:
5746
 *
5747
 *    1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
5748
 *       Refer to the definition of "enum nx_mp_fields" for details.
5749
 *
5750
 *       The 'basis' value is used as a universal hash parameter, that is,
5751
 *       different values of 'basis' yield different hash functions.  The
5752
 *       particular universal hash function used is implementation-defined.
5753
 *
5754
 *       The hashed fields' values are drawn from the current state of the
5755
 *       flow, including all modifications that have been made by actions up to
5756
 *       this point.
5757
 *
5758
 *    2. Applies the multipath link choice algorithm specified by 'algorithm',
5759
 *       one of NX_MP_ALG_*.  Refer to the definition of "enum nx_mp_algorithm"
5760
 *       for details.
5761
 *
5762
 *       The output of the algorithm is 'link', an unsigned integer less than
5763
 *       or equal to 'max_link'.
5764
 *
5765
 *       Some algorithms use 'arg' as an additional argument.
5766
 *
5767
 *    3. Stores 'link' in dst[ofs:ofs+n_bits].  The format and semantics of
5768
 *       'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
5769
 *       action.
5770
 *
5771
 * The switch will reject actions that have an unknown 'fields', or an unknown
5772
 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
5773
 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
5774
 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
5775
 */
5776
struct nx_action_multipath {
5777
    ovs_be16 type;              /* OFPAT_VENDOR. */
5778
    ovs_be16 len;               /* Length is 32. */
5779
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
5780
    ovs_be16 subtype;           /* NXAST_MULTIPATH. */
5781
5782
    /* What fields to hash and how. */
5783
    ovs_be16 fields;            /* One of NX_HASH_FIELDS_*. */
5784
    ovs_be16 basis;             /* Universal hash parameter. */
5785
    ovs_be16 pad0;
5786
5787
    /* Multipath link choice algorithm to apply to hash value. */
5788
    ovs_be16 algorithm;         /* One of NX_MP_ALG_*. */
5789
    ovs_be16 max_link;          /* Number of output links, minus 1. */
5790
    ovs_be32 arg;               /* Algorithm-specific argument. */
5791
    ovs_be16 pad1;
5792
5793
    /* Where to store the result. */
5794
    ovs_be16 ofs_nbits;         /* (ofs << 6) | (n_bits - 1). */
5795
    ovs_be32 dst;               /* Destination. */
5796
};
5797
OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
5798
5799
static enum ofperr
5800
decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
5801
                           enum ofp_version ofp_version OVS_UNUSED,
5802
                           const struct vl_mff_map *vl_mff_map,
5803
                           uint64_t *tlv_bitmap, struct ofpbuf *out)
5804
9.89k
{
5805
9.89k
    uint32_t n_links = ntohs(nam->max_link) + 1;
5806
9.89k
    size_t min_n_bits = log_2_ceil(n_links);
5807
9.89k
    struct ofpact_multipath *mp;
5808
9.89k
    enum ofperr error;
5809
5810
9.89k
    mp = ofpact_put_MULTIPATH(out);
5811
9.89k
    mp->fields = ntohs(nam->fields);
5812
9.89k
    mp->basis = ntohs(nam->basis);
5813
9.89k
    mp->algorithm = ntohs(nam->algorithm);
5814
9.89k
    mp->max_link = ntohs(nam->max_link);
5815
9.89k
    mp->arg = ntohl(nam->arg);
5816
9.89k
    mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
5817
9.89k
    mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
5818
9.89k
    error = mf_vl_mff_mf_from_nxm_header(ntohl(nam->dst), vl_mff_map,
5819
9.89k
                                         &mp->dst.field, tlv_bitmap);
5820
9.89k
    if (error) {
5821
763
        return error;
5822
763
    }
5823
5824
9.12k
    if (!flow_hash_fields_valid(mp->fields)) {
5825
827
        VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
5826
827
        return OFPERR_OFPBAC_BAD_ARGUMENT;
5827
8.30k
    } else if (mp->algorithm != NX_MP_ALG_MODULO_N
5828
7.93k
               && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
5829
7.91k
               && mp->algorithm != NX_MP_ALG_HRW
5830
6.82k
               && mp->algorithm != NX_MP_ALG_ITER_HASH) {
5831
2.79k
        VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
5832
2.79k
        return OFPERR_OFPBAC_BAD_ARGUMENT;
5833
5.50k
    } else if (mp->dst.n_bits < min_n_bits) {
5834
15
        VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
5835
15
                     "%"PRIu32" links", min_n_bits, n_links);
5836
15
        return OFPERR_OFPBAC_BAD_ARGUMENT;
5837
15
    }
5838
5839
5.49k
    return multipath_check(mp, NULL);
5840
9.12k
}
5841
5842
static void
5843
encode_MULTIPATH(const struct ofpact_multipath *mp,
5844
                 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
5845
455
{
5846
455
    struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
5847
5848
455
    nam->fields = htons(mp->fields);
5849
455
    nam->basis = htons(mp->basis);
5850
455
    nam->algorithm = htons(mp->algorithm);
5851
455
    nam->max_link = htons(mp->max_link);
5852
455
    nam->arg = htonl(mp->arg);
5853
455
    nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
5854
455
    nam->dst = htonl(nxm_header_from_mff(mp->dst.field));
5855
455
}
5856
5857
static char * OVS_WARN_UNUSED_RESULT
5858
parse_MULTIPATH(const char *arg, const struct ofpact_parse_params *pp)
5859
2.99k
{
5860
2.99k
    return multipath_parse(ofpact_put_MULTIPATH(pp->ofpacts), arg);
5861
2.99k
}
5862
5863
static void
5864
format_MULTIPATH(const struct ofpact_multipath *a,
5865
                 const struct ofpact_format_params *fp)
5866
3.76k
{
5867
3.76k
    multipath_format(a, fp->s);
5868
3.76k
}
5869
5870
static enum ofperr
5871
check_MULTIPATH(const struct ofpact_multipath *a,
5872
                const struct ofpact_check_params *cp)
5873
538
{
5874
538
    return multipath_check(a, cp->match);
5875
538
}
5876

5877
/* Action structure for NXAST_NOTE.
5878
 *
5879
 * This action has no effect.  It is variable length.  The switch does not
5880
 * attempt to interpret the user-defined 'note' data in any way.  A controller
5881
 * can use this action to attach arbitrary metadata to a flow.
5882
 *
5883
 * This action might go away in the future.
5884
 */
5885
struct nx_action_note {
5886
    ovs_be16 type;                  /* OFPAT_VENDOR. */
5887
    ovs_be16 len;                   /* A multiple of 8, but at least 16. */
5888
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
5889
    ovs_be16 subtype;               /* NXAST_NOTE. */
5890
    uint8_t note[6];                /* Start of user-defined data. */
5891
    /* Possibly followed by additional user-defined data. */
5892
};
5893
OFP_ASSERT(sizeof(struct nx_action_note) == 16);
5894
5895
static enum ofperr
5896
decode_NXAST_RAW_NOTE(const struct nx_action_note *nan,
5897
                      enum ofp_version ofp_version OVS_UNUSED,
5898
                      struct ofpbuf *out)
5899
7.80k
{
5900
7.80k
    struct ofpact_note *note;
5901
7.80k
    unsigned int length;
5902
5903
7.80k
    length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
5904
7.80k
    note = ofpact_put_NOTE(out);
5905
7.80k
    note->length = length;
5906
7.80k
    ofpbuf_put(out, nan->note, length);
5907
7.80k
    note = out->header;
5908
7.80k
    ofpact_finish_NOTE(out, &note);
5909
5910
7.80k
    return 0;
5911
7.80k
}
5912
5913
static void
5914
encode_NOTE(const struct ofpact_note *note,
5915
            enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
5916
1.65k
{
5917
1.65k
    size_t start_ofs = out->size;
5918
1.65k
    struct nx_action_note *nan;
5919
5920
1.65k
    put_NXAST_NOTE(out);
5921
1.65k
    out->size = out->size - sizeof nan->note;
5922
5923
1.65k
    ofpbuf_put(out, note->data, note->length);
5924
1.65k
    pad_ofpat(out, start_ofs);
5925
1.65k
}
5926
5927
static char * OVS_WARN_UNUSED_RESULT
5928
parse_NOTE(const char *arg, const struct ofpact_parse_params *pp)
5929
1.94k
{
5930
1.94k
    size_t start_ofs = pp->ofpacts->size;
5931
1.94k
    ofpact_put_NOTE(pp->ofpacts);
5932
1.94k
    arg = ofpbuf_put_hex(pp->ofpacts, arg, NULL);
5933
1.94k
    if (arg[0]) {
5934
6
        return xstrdup("bad hex digit in `note' argument");
5935
6
    }
5936
1.93k
    struct ofpact_note *note = ofpbuf_at_assert(pp->ofpacts, start_ofs,
5937
1.93k
                                                sizeof *note);
5938
1.93k
    note->length = pp->ofpacts->size - (start_ofs + sizeof *note);
5939
5940
1.93k
    if (ofpbuf_oversized(pp->ofpacts)) {
5941
2
        return xasprintf("input too big");
5942
2
    }
5943
5944
1.93k
    ofpact_finish_NOTE(pp->ofpacts, &note);
5945
1.93k
    return NULL;
5946
1.93k
}
5947
5948
static void
5949
format_NOTE(const struct ofpact_note *a,
5950
            const struct ofpact_format_params *fp)
5951
2.16k
{
5952
2.16k
    ds_put_format(fp->s, "%snote:%s", colors.param, colors.end);
5953
2.16k
    ds_put_hex_with_delimiter(fp->s, a->data, a->length, ".");
5954
2.16k
}
5955
5956
static enum ofperr
5957
check_NOTE(const struct ofpact_note *a OVS_UNUSED,
5958
           const struct ofpact_check_params *cp OVS_UNUSED)
5959
2.49k
{
5960
2.49k
    return 0;
5961
2.49k
}
5962

5963
/* Exit action. */
5964
5965
static enum ofperr
5966
decode_NXAST_RAW_EXIT(struct ofpbuf *out)
5967
2.07k
{
5968
2.07k
    ofpact_put_EXIT(out);
5969
2.07k
    return 0;
5970
2.07k
}
5971
5972
static void
5973
encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
5974
            enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
5975
3.03k
{
5976
3.03k
    put_NXAST_EXIT(out);
5977
3.03k
}
5978
5979
static char * OVS_WARN_UNUSED_RESULT
5980
parse_EXIT(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
5981
3.07k
{
5982
3.07k
    ofpact_put_EXIT(pp->ofpacts);
5983
3.07k
    return NULL;
5984
3.07k
}
5985
5986
static void
5987
format_EXIT(const struct ofpact_null *a OVS_UNUSED,
5988
            const struct ofpact_format_params *fp)
5989
399
{
5990
399
    ds_put_format(fp->s, "%sexit%s", colors.special, colors.end);
5991
399
}
5992
5993
static enum ofperr
5994
check_EXIT(const struct ofpact_null *a OVS_UNUSED,
5995
           const struct ofpact_check_params *cp OVS_UNUSED)
5996
3.06k
{
5997
3.06k
    return 0;
5998
3.06k
}
5999

6000
/* Unroll xlate action. */
6001
6002
static void
6003
encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
6004
                    enum ofp_version ofp_version OVS_UNUSED,
6005
                    struct ofpbuf *out OVS_UNUSED)
6006
0
{
6007
0
    OVS_NOT_REACHED();
6008
0
}
6009
6010
static char * OVS_WARN_UNUSED_RESULT
6011
parse_UNROLL_XLATE(char *arg OVS_UNUSED,
6012
                   const struct ofpact_parse_params *pp OVS_UNUSED)
6013
1
{
6014
1
    return xasprintf("UNROLL is an internal action "
6015
1
                     "that shouldn't be used via OpenFlow");
6016
1
}
6017
6018
static void
6019
format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a,
6020
                    const struct ofpact_format_params *fp)
6021
2.43k
{
6022
2.43k
    ds_put_format(fp->s, "%sunroll_xlate(%s%stable=%s",
6023
2.43k
                  colors.paren,   colors.end,
6024
2.43k
                  colors.special, colors.end);
6025
2.43k
    ofputil_format_table(a->rule_table_id, fp->table_map, fp->s);
6026
2.43k
    ds_put_format(fp->s, ", %scookie=%s%"PRIu64"%s)%s",
6027
2.43k
                  colors.param,   colors.end, ntohll(a->rule_cookie),
6028
2.43k
                  colors.paren,   colors.end);
6029
2.43k
}
6030
6031
static enum ofperr
6032
check_UNROLL_XLATE(const struct ofpact_unroll_xlate *a OVS_UNUSED,
6033
                   const struct ofpact_check_params *cp OVS_UNUSED)
6034
0
{
6035
    /* UNROLL is an internal action that should never be seen via OpenFlow. */
6036
0
    return OFPERR_OFPBAC_BAD_TYPE;
6037
0
}
6038

6039
/* The NXAST_CLONE action is "struct ext_action_header", followed by zero or
6040
 * more embedded OpenFlow actions. */
6041
6042
static enum ofperr
6043
decode_NXAST_RAW_CLONE(const struct ext_action_header *eah,
6044
                       enum ofp_version ofp_version,
6045
                       const struct vl_mff_map *vl_mff_map,
6046
                       uint64_t *tlv_bitmap, struct ofpbuf *out)
6047
1.54k
{
6048
1.54k
    int error;
6049
1.54k
    struct ofpbuf openflow;
6050
1.54k
    const size_t clone_offset = ofpacts_pull(out);
6051
1.54k
    struct ofpact_nest *clone = ofpact_put_CLONE(out);
6052
6053
    /* decode action list */
6054
1.54k
    ofpbuf_pull(out, sizeof(*clone));
6055
1.54k
    openflow = ofpbuf_const_initializer(
6056
1.54k
                    eah + 1, ntohs(eah->len) - sizeof *eah);
6057
1.54k
    error = ofpacts_pull_openflow_actions__(&openflow, openflow.size,
6058
1.54k
                                            ofp_version,
6059
1.54k
                                            1u << OVSINST_OFPIT11_APPLY_ACTIONS,
6060
1.54k
                                            out, 0, vl_mff_map, tlv_bitmap);
6061
1.54k
    if (error) {
6062
557
        return error;
6063
557
    }
6064
990
    clone = ofpbuf_push_uninit(out, sizeof *clone);
6065
990
    out->header = &clone->ofpact;
6066
990
    ofpact_finish_CLONE(out, &clone);
6067
990
    ofpbuf_push_uninit(out, clone_offset);
6068
990
    return error;
6069
1.54k
}
6070
6071
static void
6072
encode_CLONE(const struct ofpact_nest *clone,
6073
              enum ofp_version ofp_version, struct ofpbuf *out)
6074
5.74k
{
6075
5.74k
    size_t len;
6076
5.74k
    const size_t ofs = out->size;
6077
5.74k
    struct ext_action_header *eah;
6078
6079
5.74k
    put_NXAST_CLONE(out);
6080
5.74k
    len = ofpacts_put_openflow_actions(clone->actions,
6081
5.74k
                                       ofpact_nest_get_action_len(clone),
6082
5.74k
                                       out, ofp_version);
6083
5.74k
    len += sizeof *eah;
6084
5.74k
    eah = ofpbuf_at(out, ofs, sizeof *eah);
6085
5.74k
    eah->len = htons(len);
6086
5.74k
}
6087
6088
static char * OVS_WARN_UNUSED_RESULT
6089
parse_CLONE(char *arg, const struct ofpact_parse_params *pp)
6090
8.70k
{
6091
8.70k
    const size_t clone_offset = ofpacts_pull(pp->ofpacts);
6092
8.70k
    struct ofpact_nest *clone = ofpact_put_CLONE(pp->ofpacts);
6093
8.70k
    char *error;
6094
6095
8.70k
    ofpbuf_pull(pp->ofpacts, sizeof *clone);
6096
8.70k
    error = ofpacts_parse_copy(arg, pp, false, OFPACT_CLONE);
6097
    /* header points to the action list */
6098
8.70k
    pp->ofpacts->header = ofpbuf_push_uninit(pp->ofpacts, sizeof *clone);
6099
8.70k
    clone = pp->ofpacts->header;
6100
6101
8.70k
    if (ofpbuf_oversized(pp->ofpacts)) {
6102
1
        free(error);
6103
1
        return xasprintf("input too big");
6104
1
    }
6105
6106
8.70k
    ofpact_finish_CLONE(pp->ofpacts, &clone);
6107
8.70k
    ofpbuf_push_uninit(pp->ofpacts, clone_offset);
6108
8.70k
    return error;
6109
8.70k
}
6110
6111
static void
6112
format_CLONE(const struct ofpact_nest *a,
6113
             const struct ofpact_format_params *fp)
6114
86
{
6115
86
    ds_put_format(fp->s, "%sclone(%s", colors.paren, colors.end);
6116
86
    ofpacts_format(a->actions, ofpact_nest_get_action_len(a), fp);
6117
86
    ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
6118
86
}
6119
6120
static enum ofperr
6121
check_subactions(struct ofpact *ofpacts, size_t ofpacts_len,
6122
                 struct ofpact_check_params *cp)
6123
18.4k
{
6124
18.4k
    struct ofpact_check_params sub = *cp;
6125
18.4k
    enum ofperr error = ofpacts_check(ofpacts, ofpacts_len, &sub);
6126
18.4k
    cp->usable_protocols &= sub.usable_protocols;
6127
18.4k
    return error;
6128
18.4k
}
6129
6130
static enum ofperr
6131
check_CLONE(struct ofpact_nest *a, struct ofpact_check_params *cp)
6132
6.24k
{
6133
6.24k
    return check_subactions(a->actions, ofpact_nest_get_action_len(a), cp);
6134
6.24k
}
6135

6136
/* Action structure for NXAST_SAMPLE.
6137
 *
6138
 * Samples matching packets with the given probability and sends them
6139
 * each to the set of collectors identified with the given ID.  The
6140
 * probability is expressed as a number of packets to be sampled out
6141
 * of USHRT_MAX packets, and must be >0.
6142
 *
6143
 * When sending packet samples to IPFIX collectors, the IPFIX flow
6144
 * record sent for each sampled packet is associated with the given
6145
 * observation domain ID and observation point ID.  Each IPFIX flow
6146
 * record contain the sampled packet's headers when executing this
6147
 * rule.  If a sampled packet's headers are modified by previous
6148
 * actions in the flow, those modified headers are sent. */
6149
struct nx_action_sample {
6150
    ovs_be16 type;                  /* OFPAT_VENDOR. */
6151
    ovs_be16 len;                   /* Length is 24. */
6152
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
6153
    ovs_be16 subtype;               /* NXAST_SAMPLE. */
6154
    ovs_be16 probability;           /* Fraction of packets to sample. */
6155
    ovs_be32 collector_set_id;      /* ID of collector set in OVSDB. */
6156
    ovs_be32 obs_domain_id;         /* ID of sampling observation domain. */
6157
    ovs_be32 obs_point_id;          /* ID of sampling observation point. */
6158
};
6159
OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
6160
6161
/* Action structure for NXAST_SAMPLE2 and NXAST_SAMPLE3.
6162
 *
6163
 * NXAST_SAMPLE2 was added in Open vSwitch 2.5.90.  Compared to NXAST_SAMPLE,
6164
 * it adds support for exporting egress tunnel information.
6165
 *
6166
 * NXAST_SAMPLE3 was added in Open vSwitch 2.6.90.  Compared to NXAST_SAMPLE2,
6167
 * it adds support for the 'direction' field. */
6168
struct nx_action_sample2 {
6169
    ovs_be16 type;                  /* OFPAT_VENDOR. */
6170
    ovs_be16 len;                   /* Length is 32. */
6171
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
6172
    ovs_be16 subtype;               /* NXAST_SAMPLE. */
6173
    ovs_be16 probability;           /* Fraction of packets to sample. */
6174
    ovs_be32 collector_set_id;      /* ID of collector set in OVSDB. */
6175
    ovs_be32 obs_domain_id;         /* ID of sampling observation domain. */
6176
    ovs_be32 obs_point_id;          /* ID of sampling observation point. */
6177
    ovs_be16 sampling_port;         /* Sampling port. */
6178
    uint8_t  direction;             /* NXAST_SAMPLE3 only. */
6179
    uint8_t  zeros[5];              /* Pad to a multiple of 8 bytes */
6180
 };
6181
 OFP_ASSERT(sizeof(struct nx_action_sample2) == 32);
6182
6183
/* Action structure for NXAST_SAMPLE4
6184
 *
6185
 * NXAST_SAMPLE4 was added in Open vSwitch 3.4.0.  Compared to NXAST_SAMPLE3,
6186
 * it adds support for using field specifiers for observation_domain_id and
6187
 * observation_point_id. */
6188
struct nx_action_sample4 {
6189
    ovs_be16 type;                      /* OFPAT_VENDOR. */
6190
    ovs_be16 len;                       /* Length is 40. */
6191
    ovs_be32 vendor;                    /* NX_VENDOR_ID. */
6192
    ovs_be16 subtype;                   /* NXAST_SAMPLE4. */
6193
    ovs_be16 probability;               /* Fraction of packets to sample. */
6194
    ovs_be32 collector_set_id;          /* ID of collector set in OVSDB. */
6195
    ovs_be32 obs_domain_src;            /* The observation_domain_id source. */
6196
    union {
6197
        ovs_be16 obs_domain_ofs_nbits;  /* Range to use from source field. */
6198
        ovs_be32 obs_domain_imm;        /* Immediate value for domain id. */
6199
    };
6200
    ovs_be32 obs_point_src;             /* The observation_point_id source. */
6201
    union {
6202
        ovs_be16 obs_point_ofs_nbits;   /* Range to use from source field. */
6203
        ovs_be32 obs_point_imm;         /* Immediate value for point id. */
6204
    };
6205
    ovs_be16 sampling_port;             /* Sampling port. */
6206
    uint8_t direction;                  /* Sampling direction. */
6207
    uint8_t zeros[5];                   /* Pad to a multiple of 8 bytes */
6208
 };
6209
 OFP_ASSERT(sizeof(struct nx_action_sample4) == 40);
6210
6211
static enum ofperr
6212
decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas,
6213
                        enum ofp_version ofp_version OVS_UNUSED,
6214
                        struct ofpbuf *out)
6215
71
{
6216
71
    struct ofpact_sample *sample;
6217
6218
71
    sample = ofpact_put_SAMPLE(out);
6219
71
    sample->ofpact.raw = NXAST_RAW_SAMPLE;
6220
71
    sample->probability = ntohs(nas->probability);
6221
71
    sample->collector_set_id = ntohl(nas->collector_set_id);
6222
71
    sample->obs_domain_imm = ntohl(nas->obs_domain_id);
6223
71
    sample->obs_domain_src.field = NULL;
6224
71
    sample->obs_point_imm = ntohl(nas->obs_point_id);
6225
71
    sample->obs_point_src.field = NULL;
6226
71
    sample->sampling_port = OFPP_NONE;
6227
71
    sample->direction = NX_ACTION_SAMPLE_DEFAULT;
6228
71
    sample->obs_domain_src.field = NULL;
6229
71
    sample->obs_point_src.field = NULL;
6230
71
    if (sample->probability == 0) {
6231
18
        return OFPERR_OFPBAC_BAD_ARGUMENT;
6232
18
    }
6233
6234
53
    return 0;
6235
71
}
6236
6237
static enum ofperr
6238
decode_SAMPLE2(const struct nx_action_sample2 *nas,
6239
               enum ofp_raw_action_type raw,
6240
               enum nx_action_sample_direction direction,
6241
               struct ofpact_sample *sample)
6242
1.72k
{
6243
1.72k
    sample->ofpact.raw = raw;
6244
1.72k
    sample->probability = ntohs(nas->probability);
6245
1.72k
    sample->collector_set_id = ntohl(nas->collector_set_id);
6246
1.72k
    sample->obs_domain_imm = ntohl(nas->obs_domain_id);
6247
1.72k
    sample->obs_domain_src.field = NULL;
6248
1.72k
    sample->obs_point_imm = ntohl(nas->obs_point_id);
6249
1.72k
    sample->obs_point_src.field = NULL;
6250
1.72k
    sample->sampling_port = u16_to_ofp(ntohs(nas->sampling_port));
6251
1.72k
    sample->direction = direction;
6252
6253
1.72k
    if (sample->probability == 0) {
6254
515
        return OFPERR_OFPBAC_BAD_ARGUMENT;
6255
515
    }
6256
6257
1.21k
    return 0;
6258
1.72k
}
6259
6260
static enum ofperr
6261
decode_NXAST_RAW_SAMPLE2(const struct nx_action_sample2 *nas,
6262
                         enum ofp_version ofp_version OVS_UNUSED,
6263
                         struct ofpbuf *out)
6264
28
{
6265
28
    return decode_SAMPLE2(nas, NXAST_RAW_SAMPLE2, NX_ACTION_SAMPLE_DEFAULT,
6266
28
                          ofpact_put_SAMPLE(out));
6267
28
}
6268
6269
static int
6270
check_sample_direction(enum nx_action_sample_direction direction)
6271
4.14k
{
6272
4.14k
    if (direction != NX_ACTION_SAMPLE_DEFAULT &&
6273
1.91k
        direction != NX_ACTION_SAMPLE_INGRESS &&
6274
1.08k
        direction != NX_ACTION_SAMPLE_EGRESS) {
6275
377
        VLOG_WARN_RL(&rl, "invalid sample direction %"PRIu8, direction);
6276
377
        return OFPERR_OFPBAC_BAD_ARGUMENT;
6277
377
    }
6278
3.77k
    return 0;
6279
4.14k
}
6280
6281
static enum ofperr
6282
decode_NXAST_RAW_SAMPLE3(const struct nx_action_sample2 *nas,
6283
                         enum ofp_version ofp_version OVS_UNUSED,
6284
                         struct ofpbuf *out)
6285
2.16k
{
6286
2.16k
    struct ofpact_sample *sample = ofpact_put_SAMPLE(out);
6287
2.16k
    int err;
6288
6289
2.16k
    if (!is_all_zeros(nas->zeros, sizeof nas->zeros)) {
6290
452
        return OFPERR_NXBRC_MUST_BE_ZERO;
6291
452
    }
6292
1.71k
    err = check_sample_direction(nas->direction);
6293
1.71k
    if (err) {
6294
19
        return err;
6295
19
    }
6296
1.69k
    return decode_SAMPLE2(nas, NXAST_RAW_SAMPLE3, nas->direction, sample);
6297
1.71k
}
6298
6299
static int
6300
decode_sample_obs_id(ovs_be32 src, ovs_be16 ofs_nbits, ovs_be32 imm,
6301
                     const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap,
6302
                     struct mf_subfield *src_out, uint32_t *imm_out)
6303
3.69k
{
6304
3.69k
    if (src) {
6305
2.55k
        enum ofperr error;
6306
6307
2.55k
        src_out->ofs = nxm_decode_ofs(ofs_nbits);
6308
2.55k
        src_out->n_bits = nxm_decode_n_bits(ofs_nbits);
6309
2.55k
        error = mf_vl_mff_mf_from_nxm_header(ntohl(src),
6310
2.55k
                                             vl_mff_map, &src_out->field,
6311
2.55k
                                             tlv_bitmap);
6312
2.55k
        if (error) {
6313
1.39k
            return error;
6314
1.39k
        }
6315
6316
1.16k
        error = mf_check_src(src_out, NULL);
6317
1.16k
        if (error) {
6318
164
            return error;
6319
164
        }
6320
6321
996
        if (src_out->n_bits > 32) {
6322
235
            VLOG_WARN_RL(&rl, "size of field used in observation_id (%d) "
6323
235
                               "exceeds maximum (32)", src_out->n_bits);
6324
235
            return OFPERR_OFPBAC_BAD_ARGUMENT;
6325
235
        }
6326
1.14k
    } else {
6327
1.14k
        src_out->field = NULL;
6328
1.14k
        *imm_out = ntohl(imm);
6329
1.14k
    }
6330
6331
1.90k
    return 0;
6332
3.69k
}
6333
6334
static enum ofperr
6335
decode_NXAST_RAW_SAMPLE4(const struct nx_action_sample4 *nas,
6336
                         enum ofp_version ofp_version OVS_UNUSED,
6337
                         const struct vl_mff_map *vl_mff_map,
6338
                         uint64_t *tlv_bitmap,
6339
                         struct ofpbuf *out)
6340
2.64k
{
6341
2.64k
    struct ofpact_sample *sample = ofpact_put_SAMPLE(out);
6342
2.64k
    int err;
6343
6344
2.64k
    if (!is_all_zeros(nas->zeros, sizeof nas->zeros)) {
6345
211
        return OFPERR_NXBRC_MUST_BE_ZERO;
6346
211
    }
6347
6348
2.43k
    err = check_sample_direction(nas->direction);
6349
2.43k
    if (err) {
6350
358
        return err;
6351
358
    }
6352
6353
2.07k
    sample->ofpact.raw = NXAST_RAW_SAMPLE4;
6354
2.07k
    sample->probability = ntohs(nas->probability);
6355
2.07k
    sample->collector_set_id = ntohl(nas->collector_set_id);
6356
2.07k
    sample->sampling_port = u16_to_ofp(ntohs(nas->sampling_port));
6357
2.07k
    sample->direction = nas->direction;
6358
6359
2.07k
    if (sample->probability == 0) {
6360
24
        return OFPERR_OFPBAC_BAD_ARGUMENT;
6361
24
    }
6362
6363
2.05k
    err = decode_sample_obs_id(nas->obs_domain_src,
6364
2.05k
                               nas->obs_domain_ofs_nbits,
6365
2.05k
                               nas->obs_domain_imm,
6366
2.05k
                               vl_mff_map, tlv_bitmap,
6367
2.05k
                               &sample->obs_domain_src,
6368
2.05k
                               &sample->obs_domain_imm);
6369
2.05k
    if (err) {
6370
407
        return err;
6371
407
    }
6372
6373
1.64k
    return decode_sample_obs_id(nas->obs_point_src,
6374
1.64k
                                nas->obs_point_ofs_nbits,
6375
1.64k
                                nas->obs_point_imm,
6376
1.64k
                                vl_mff_map, tlv_bitmap,
6377
1.64k
                                &sample->obs_point_src,
6378
1.64k
                                &sample->obs_point_imm);
6379
2.05k
}
6380
6381
static void
6382
encode_SAMPLE2(const struct ofpact_sample *sample,
6383
               struct nx_action_sample2 *nas)
6384
377
{
6385
377
    nas->probability = htons(sample->probability);
6386
377
    nas->collector_set_id = htonl(sample->collector_set_id);
6387
377
    nas->obs_domain_id = htonl(sample->obs_domain_imm);
6388
377
    nas->obs_point_id = htonl(sample->obs_point_imm);
6389
377
    nas->sampling_port = htons(ofp_to_u16(sample->sampling_port));
6390
377
    nas->direction = sample->direction;
6391
377
}
6392
6393
static void
6394
encode_SAMPLE4(const struct ofpact_sample *sample,
6395
               struct nx_action_sample4 *nas)
6396
675
{
6397
675
    nas->probability = htons(sample->probability);
6398
675
    nas->collector_set_id = htonl(sample->collector_set_id);
6399
675
    nas->sampling_port = htons(ofp_to_u16(sample->sampling_port));
6400
675
    nas->direction = sample->direction;
6401
6402
675
    if (sample->obs_domain_src.field) {
6403
609
        nas->obs_domain_src =
6404
609
            htonl(nxm_header_from_mff(sample->obs_domain_src.field));
6405
609
        nas->obs_domain_ofs_nbits =
6406
609
            nxm_encode_ofs_nbits(sample->obs_domain_src.ofs,
6407
609
                                 sample->obs_domain_src.n_bits);
6408
609
    } else {
6409
66
        nas->obs_domain_src = htonl(0);
6410
66
        nas->obs_domain_imm = htonl(sample->obs_domain_imm);
6411
66
    }
6412
675
    if (sample->obs_point_src.field) {
6413
66
        nas->obs_point_src =
6414
66
            htonl(nxm_header_from_mff(sample->obs_point_src.field));
6415
66
        nas->obs_point_ofs_nbits =
6416
66
            nxm_encode_ofs_nbits(sample->obs_point_src.ofs,
6417
66
                                 sample->obs_point_src.n_bits);
6418
609
    } else {
6419
609
        nas->obs_point_src = htonl(0);
6420
609
        nas->obs_point_imm = htonl(sample->obs_point_imm);
6421
609
    }
6422
675
}
6423
6424
static void
6425
encode_SAMPLE(const struct ofpact_sample *sample,
6426
              enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
6427
1.76k
{
6428
1.76k
    if (sample->ofpact.raw == NXAST_RAW_SAMPLE4 ||
6429
1.76k
        sample->obs_domain_src.field ||
6430
1.15k
        sample->obs_point_src.field) {
6431
675
        encode_SAMPLE4(sample, put_NXAST_SAMPLE4(out));
6432
1.09k
    } else if (sample->ofpact.raw == NXAST_RAW_SAMPLE3
6433
1.09k
        || sample->direction != NX_ACTION_SAMPLE_DEFAULT) {
6434
293
        encode_SAMPLE2(sample, put_NXAST_SAMPLE3(out));
6435
797
    } else if (sample->ofpact.raw == NXAST_RAW_SAMPLE2
6436
797
               || sample->sampling_port != OFPP_NONE) {
6437
84
        encode_SAMPLE2(sample, put_NXAST_SAMPLE2(out));
6438
713
    } else {
6439
713
        struct nx_action_sample *nas = put_NXAST_SAMPLE(out);
6440
713
        nas->probability = htons(sample->probability);
6441
713
        nas->collector_set_id = htonl(sample->collector_set_id);
6442
713
        nas->obs_domain_id = htonl(sample->obs_domain_imm);
6443
713
        nas->obs_point_id = htonl(sample->obs_point_imm);
6444
713
    }
6445
1.76k
}
6446
6447
/* Parses 'arg' as the argument to a "sample" action, and appends such an
6448
 * action to 'pp->ofpacts'.
6449
 *
6450
 * Returns NULL if successful, otherwise a malloc()'d string describing the
6451
 * error.  The caller is responsible for freeing the returned string. */
6452
static char * OVS_WARN_UNUSED_RESULT
6453
parse_SAMPLE(char *arg, const struct ofpact_parse_params *pp)
6454
2.03k
{
6455
2.03k
    struct ofpact_sample *os = ofpact_put_SAMPLE(pp->ofpacts);
6456
2.03k
    os->sampling_port = OFPP_NONE;
6457
2.03k
    os->direction = NX_ACTION_SAMPLE_DEFAULT;
6458
6459
2.03k
    char *key, *value;
6460
7.11k
    while (ofputil_parse_key_value(&arg, &key, &value)) {
6461
5.23k
        char *error = NULL;
6462
6463
5.23k
        if (!strcmp(key, "probability")) {
6464
2.05k
            error = str_to_u16(value, "probability", &os->probability);
6465
2.05k
            if (!error && os->probability == 0) {
6466
1
                error = xasprintf("invalid probability value \"%s\"", value);
6467
1
            }
6468
3.17k
        } else if (!strcmp(key, "collector_set_id")) {
6469
197
            error = str_to_u32(value, &os->collector_set_id);
6470
2.98k
        } else if (!strcmp(key, "obs_domain_id")) {
6471
1.26k
            error = str_to_u32(value, &os->obs_domain_imm);
6472
6473
1.26k
            if (error) {
6474
617
                free(error);
6475
617
                error = mf_parse_subfield(&os->obs_domain_src, value);
6476
617
                if (error) {
6477
1
                    return error;
6478
1
                }
6479
616
                if (os->obs_domain_src.n_bits > 32) {
6480
1
                    return xasprintf("size of obs_domain_id field (%d) "
6481
1
                                     "exceeds maximum (32)",
6482
1
                                     os->obs_domain_src.n_bits);
6483
1
                }
6484
616
            }
6485
1.71k
        } else if (!strcmp(key, "obs_point_id")) {
6486
336
            error = str_to_u32(value, &os->obs_point_imm);
6487
6488
336
            if (error) {
6489
98
                free(error);
6490
98
                error = mf_parse_subfield(&os->obs_point_src, value);
6491
98
                if (error) {
6492
3
                    return error;
6493
3
                }
6494
95
                if (os->obs_point_src.n_bits > 32) {
6495
1
                    return xasprintf("size of obs_point_id field (%d) "
6496
1
                                     "exceeds maximum (32)",
6497
1
                                     os->obs_point_src.n_bits);
6498
1
                }
6499
95
            }
6500
1.38k
        } else if (!strcmp(key, "sampling_port")) {
6501
127
            if (!ofputil_port_from_string(value, pp->port_map,
6502
127
                                          &os->sampling_port)) {
6503
1
                error = xasprintf("%s: unknown port", value);
6504
1
            }
6505
1.25k
        } else if (!strcmp(key, "ingress")) {
6506
1.01k
            os->direction = NX_ACTION_SAMPLE_INGRESS;
6507
1.01k
        } else if (!strcmp(key, "egress")) {
6508
102
            os->direction = NX_ACTION_SAMPLE_EGRESS;
6509
133
        } else {
6510
133
            error = xasprintf("invalid key \"%s\" in \"sample\" argument",
6511
133
                              key);
6512
133
        }
6513
5.22k
        if (error) {
6514
141
            return error;
6515
141
        }
6516
5.22k
    }
6517
1.88k
    if (os->probability == 0) {
6518
25
        return xstrdup("non-zero \"probability\" must be specified on sample");
6519
25
    }
6520
6521
1.86k
    return NULL;
6522
1.88k
}
6523
6524
static void
6525
format_SAMPLE(const struct ofpact_sample *a,
6526
              const struct ofpact_format_params *fp)
6527
1.51k
{
6528
1.51k
    ds_put_format(fp->s, "%ssample(%s%sprobability=%s%"PRIu16
6529
1.51k
                  ",%scollector_set_id=%s%"PRIu32,
6530
1.51k
                  colors.paren, colors.end,
6531
1.51k
                  colors.param, colors.end, a->probability,
6532
1.51k
                  colors.param, colors.end, a->collector_set_id);
6533
6534
1.51k
    ds_put_format(fp->s, ",%sobs_domain_id=%s", colors.param, colors.end);
6535
1.51k
    if (a->obs_domain_src.field) {
6536
15
        mf_format_subfield(&a->obs_domain_src, fp->s);
6537
1.49k
    } else {
6538
1.49k
        ds_put_format(fp->s, "%"PRIu32, a->obs_domain_imm);
6539
1.49k
    }
6540
1.51k
    ds_put_format(fp->s, ",%sobs_point_id=%s", colors.param, colors.end);
6541
1.51k
    if (a->obs_point_src.field) {
6542
244
        mf_format_subfield(&a->obs_point_src, fp->s);
6543
1.26k
    } else {
6544
1.26k
        ds_put_format(fp->s, "%"PRIu32, a->obs_point_imm);
6545
1.26k
    }
6546
1.51k
    if (a->sampling_port != OFPP_NONE) {
6547
1.46k
        ds_put_format(fp->s, ",%ssampling_port=%s", colors.param, colors.end);
6548
1.46k
        ofputil_format_port(a->sampling_port, fp->port_map, fp->s);
6549
1.46k
    }
6550
1.51k
    if (a->direction == NX_ACTION_SAMPLE_INGRESS) {
6551
241
        ds_put_format(fp->s, ",%singress%s", colors.param, colors.end);
6552
1.27k
    } else if (a->direction == NX_ACTION_SAMPLE_EGRESS) {
6553
606
        ds_put_format(fp->s, ",%segress%s", colors.param, colors.end);
6554
606
    }
6555
1.51k
    ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
6556
1.51k
}
6557
6558
static enum ofperr
6559
check_SAMPLE(const struct ofpact_sample *a OVS_UNUSED,
6560
             const struct ofpact_check_params *cp OVS_UNUSED)
6561
1.80k
{
6562
1.80k
    return 0;
6563
1.80k
}
6564

6565
/* debug instructions. */
6566
6567
static bool enable_debug;
6568
6569
void
6570
ofpact_dummy_enable(void)
6571
0
{
6572
0
    enable_debug = true;
6573
0
}
6574
6575
static enum ofperr
6576
decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
6577
67
{
6578
67
    if (!enable_debug) {
6579
67
        return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
6580
67
    }
6581
6582
0
    ofpact_put_DEBUG_RECIRC(out);
6583
0
    return 0;
6584
67
}
6585
6586
static void
6587
encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
6588
                    enum ofp_version ofp_version OVS_UNUSED,
6589
                    struct ofpbuf *out)
6590
224
{
6591
224
    put_NXAST_DEBUG_RECIRC(out);
6592
224
}
6593
6594
static char * OVS_WARN_UNUSED_RESULT
6595
parse_DEBUG_RECIRC(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
6596
531
{
6597
531
    ofpact_put_DEBUG_RECIRC(pp->ofpacts);
6598
531
    return NULL;
6599
531
}
6600
6601
static void
6602
format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED,
6603
                    const struct ofpact_format_params *fp)
6604
0
{
6605
0
    ds_put_format(fp->s, "%sdebug_recirc%s", colors.value, colors.end);
6606
0
}
6607
6608
static enum ofperr
6609
check_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED,
6610
                   const struct ofpact_check_params *cp OVS_UNUSED)
6611
227
{
6612
227
    return 0;
6613
227
}
6614
6615
static enum ofperr
6616
decode_NXAST_RAW_DEBUG_SLOW(struct ofpbuf *out)
6617
375
{
6618
375
    if (!enable_debug) {
6619
375
        return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
6620
375
    }
6621
6622
0
    ofpact_put_DEBUG_SLOW(out);
6623
0
    return 0;
6624
375
}
6625
6626
static void
6627
encode_DEBUG_SLOW(const struct ofpact_null *n OVS_UNUSED,
6628
                  enum ofp_version ofp_version OVS_UNUSED,
6629
                  struct ofpbuf *out)
6630
726
{
6631
726
    put_NXAST_DEBUG_SLOW(out);
6632
726
}
6633
6634
static char * OVS_WARN_UNUSED_RESULT
6635
parse_DEBUG_SLOW(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
6636
737
{
6637
737
    ofpact_put_DEBUG_SLOW(pp->ofpacts);
6638
737
    return NULL;
6639
737
}
6640
6641
static void
6642
format_DEBUG_SLOW(const struct ofpact_null *a OVS_UNUSED,
6643
                  const struct ofpact_format_params *fp)
6644
0
{
6645
0
    ds_put_format(fp->s, "%sdebug_slow%s", colors.value, colors.end);
6646
0
}
6647
6648
static enum ofperr
6649
check_DEBUG_SLOW(const struct ofpact_null *a OVS_UNUSED,
6650
                 const struct ofpact_check_params *cp OVS_UNUSED)
6651
727
{
6652
727
    return 0;
6653
727
}
6654
6655
/* Action structure for NXAST_CT.
6656
 *
6657
 * Pass traffic to the connection tracker.
6658
 *
6659
 * There are two important concepts to understanding the connection tracking
6660
 * interface: Packet state and Connection state. Packets may be "Untracked" or
6661
 * "Tracked". Connections may be "Uncommitted" or "Committed".
6662
 *
6663
 *   - Packet State:
6664
 *
6665
 *      Untracked packets have an unknown connection state.  In most
6666
 *      cases, packets entering the OpenFlow pipeline will initially be
6667
 *      in the untracked state. Untracked packets may become tracked by
6668
 *      executing NXAST_CT with a "recirc_table" specified. This makes
6669
 *      various aspects about the connection available, in particular
6670
 *      the connection state.
6671
 *
6672
 *      An NXAST_CT action always puts the packet into an untracked
6673
 *      state for the current processing path.  If "recirc_table" is
6674
 *      set, execution is forked and the packet passes through the
6675
 *      connection tracker.  The specified table's processing path is
6676
 *      able to match on Connection state until the end of the OpenFlow
6677
 *      pipeline or NXAST_CT is called again.
6678
 *
6679
 *   - Connection State:
6680
 *
6681
 *      Multiple packets may be associated with a single connection. Initially,
6682
 *      all connections are uncommitted. The connection state corresponding to
6683
 *      a packet is available in the NXM_NX_CT_STATE field for tracked packets.
6684
 *
6685
 *      Uncommitted connections have no state stored about them. Uncommitted
6686
 *      connections may transition into the committed state by executing
6687
 *      NXAST_CT with the NX_CT_F_COMMIT flag.
6688
 *
6689
 *      Once a connection becomes committed, information may be gathered about
6690
 *      the connection by passing subsequent packets through the connection
6691
 *      tracker, and the state of the connection will be stored beyond the
6692
 *      lifetime of packet processing.
6693
 *
6694
 *      A committed connection always has the directionality of the packet that
6695
 *      caused the connection to be committed in the first place.  This is the
6696
 *      "original direction" of the connection, and the opposite direction is
6697
 *      the "reply direction".  If a connection is already committed, but it is
6698
 *      then decided that the original direction should be the opposite of the
6699
 *      existing connection, NX_CT_F_FORCE flag may be used in addition to
6700
 *      NX_CT_F_COMMIT flag to in effect terminate the existing connection and
6701
 *      start a new one in the current direction.
6702
 *
6703
 *      Connections may transition back into the uncommitted state due to
6704
 *      external timers, or due to the contents of packets that are sent to the
6705
 *      connection tracker. This behaviour is outside of the scope of the
6706
 *      OpenFlow interface.
6707
 *
6708
 * The "zone" specifies a context within which the tracking is done:
6709
 *
6710
 *      The connection tracking zone is a 16-bit number. Each zone is an
6711
 *      independent connection tracking context. The connection state for each
6712
 *      connection is completely separate for each zone, so if a connection
6713
 *      is committed to zone A, then it will remain uncommitted in zone B.
6714
 *      If NXAST_CT is executed with the same zone multiple times, later
6715
 *      executions have no effect.
6716
 *
6717
 *      If 'zone_src' is nonzero, this specifies that the zone should be
6718
 *      sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
6719
 *      of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
6720
 *      NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
6721
 *      are the same as the acceptable nxm_header values for the 'src' field of
6722
 *      NXAST_REG_MOVE.
6723
 *
6724
 *      If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
6725
 *      connection tracking zone.
6726
 *
6727
 * The "recirc_table" allows NXM_NX_CT_* fields to become available:
6728
 *
6729
 *      If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
6730
 *      packet will be logically cloned prior to executing this action. One
6731
 *      copy will be sent to the connection tracker, then will be re-injected
6732
 *      into the OpenFlow pipeline beginning at the OpenFlow table specified in
6733
 *      this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
6734
 *      fields will be populated. The original instance of the packet will
6735
 *      continue the current actions list. This can be thought of as similar to
6736
 *      the effect of the "output" action: One copy is sent out (in this case,
6737
 *      to the connection tracker), but the current copy continues processing.
6738
 *
6739
 *      It is strongly recommended that this table is later than the current
6740
 *      table, to prevent loops.
6741
 *
6742
 * The "alg" attaches protocol-specific behaviour to this action:
6743
 *
6744
 *      The ALG is a 16-bit number which specifies that additional
6745
 *      processing should be applied to this traffic.
6746
 *
6747
 *      Protocol | Value | Meaning
6748
 *      --------------------------------------------------------------------
6749
 *      None     |     0 | No protocol-specific behaviour.
6750
 *      FTP      |    21 | Parse FTP control connections and observe the
6751
 *               |       | negotiation of related data connections.
6752
 *      Other    | Other | Unsupported protocols.
6753
 *
6754
 *      By way of example, if FTP control connections have this action applied
6755
 *      with the ALG set to FTP (21), then the connection tracker will observe
6756
 *      the negotiation of data connections. This allows the connection
6757
 *      tracker to identify subsequent data connections as "related" to this
6758
 *      existing connection. The "related" flag will be populated in the
6759
 *      NXM_NX_CT_STATE field for such connections if the 'recirc_table' is
6760
 *      specified.
6761
 *
6762
 * Zero or more actions may immediately follow this action. These actions will
6763
 * be executed within the context of the connection tracker, and they require
6764
 * NX_CT_F_COMMIT flag be set.
6765
 */
6766
struct nx_action_conntrack {
6767
    ovs_be16 type;              /* OFPAT_VENDOR. */
6768
    ovs_be16 len;               /* At least 24. */
6769
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
6770
    ovs_be16 subtype;           /* NXAST_CT. */
6771
    ovs_be16 flags;             /* Zero or more NX_CT_F_* flags.
6772
                                 * Unspecified flag bits must be zero. */
6773
    ovs_be32 zone_src;          /* Connection tracking context. */
6774
    union {
6775
        ovs_be16 zone_ofs_nbits;/* Range to use from source field. */
6776
        ovs_be16 zone_imm;      /* Immediate value for zone. */
6777
    };
6778
    uint8_t recirc_table;       /* Recirculate to a specific table, or
6779
                                   NX_CT_RECIRC_NONE for no recirculation. */
6780
    uint8_t pad[3];             /* Zeroes */
6781
    ovs_be16 alg;               /* Well-known port number for the protocol.
6782
                                 * 0 indicates no ALG is required. */
6783
    /* Followed by a sequence of zero or more OpenFlow actions. The length of
6784
     * these is included in 'len'. */
6785
};
6786
OFP_ASSERT(sizeof(struct nx_action_conntrack) == 24);
6787
6788
static enum ofperr
6789
decode_ct_zone(const struct nx_action_conntrack *nac,
6790
               struct ofpact_conntrack *out,
6791
               const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap)
6792
13.4k
{
6793
13.4k
    if (nac->zone_src) {
6794
5.09k
        enum ofperr error;
6795
6796
5.09k
        out->zone_src.ofs = nxm_decode_ofs(nac->zone_ofs_nbits);
6797
5.09k
        out->zone_src.n_bits = nxm_decode_n_bits(nac->zone_ofs_nbits);
6798
5.09k
        error = mf_vl_mff_mf_from_nxm_header(ntohl(nac->zone_src),
6799
5.09k
                                             vl_mff_map, &out->zone_src.field,
6800
5.09k
                                             tlv_bitmap);
6801
5.09k
        if (error) {
6802
2.14k
            return error;
6803
2.14k
        }
6804
6805
2.94k
        error = mf_check_src(&out->zone_src, NULL);
6806
2.94k
        if (error) {
6807
445
            return error;
6808
445
        }
6809
6810
2.50k
        if (out->zone_src.n_bits != 16) {
6811
69
            VLOG_WARN_RL(&rl, "zone n_bits %d not within valid range [16..16]",
6812
69
                         out->zone_src.n_bits);
6813
69
            return OFPERR_OFPBAC_BAD_SET_LEN;
6814
69
        }
6815
8.35k
    } else {
6816
8.35k
        out->zone_src.field = NULL;
6817
8.35k
        out->zone_imm = ntohs(nac->zone_imm);
6818
8.35k
    }
6819
6820
10.7k
    return 0;
6821
13.4k
}
6822
6823
static enum ofperr
6824
decode_NXAST_RAW_CT(const struct nx_action_conntrack *nac,
6825
                    enum ofp_version ofp_version,
6826
                    const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap,
6827
                    struct ofpbuf *out)
6828
13.5k
{
6829
13.5k
    const size_t ct_offset = ofpacts_pull(out);
6830
13.5k
    struct ofpact_conntrack *conntrack = ofpact_put_CT(out);
6831
13.5k
    int error;
6832
6833
13.5k
    conntrack->flags = ntohs(nac->flags);
6834
13.5k
    if (conntrack->flags & NX_CT_F_FORCE &&
6835
1.69k
        !(conntrack->flags & NX_CT_F_COMMIT)) {
6836
95
        error = OFPERR_OFPBAC_BAD_ARGUMENT;
6837
95
        goto out;
6838
95
    }
6839
6840
13.4k
    error = decode_ct_zone(nac, conntrack, vl_mff_map, tlv_bitmap);
6841
13.4k
    if (error) {
6842
2.65k
        goto out;
6843
2.65k
    }
6844
10.7k
    conntrack->recirc_table = nac->recirc_table;
6845
10.7k
    conntrack->alg = ntohs(nac->alg);
6846
6847
10.7k
    ofpbuf_pull(out, sizeof(*conntrack));
6848
6849
10.7k
    struct ofpbuf openflow = ofpbuf_const_initializer(
6850
10.7k
        nac + 1, ntohs(nac->len) - sizeof(*nac));
6851
10.7k
    error = ofpacts_pull_openflow_actions__(&openflow, openflow.size,
6852
10.7k
                                            ofp_version,
6853
10.7k
                                            1u << OVSINST_OFPIT11_APPLY_ACTIONS,
6854
10.7k
                                            out, OFPACT_CT, vl_mff_map,
6855
10.7k
                                            tlv_bitmap);
6856
10.7k
    if (error) {
6857
2.52k
        return error;
6858
2.52k
    }
6859
6860
8.26k
    conntrack = ofpbuf_push_uninit(out, sizeof(*conntrack));
6861
8.26k
    out->header = &conntrack->ofpact;
6862
8.26k
    ofpact_finish_CT(out, &conntrack);
6863
6864
8.26k
    if (conntrack->ofpact.len > sizeof(*conntrack)
6865
3.08k
        && !(conntrack->flags & NX_CT_F_COMMIT)) {
6866
693
        const struct ofpact *a;
6867
693
        size_t ofpacts_len = conntrack->ofpact.len - sizeof(*conntrack);
6868
6869
693
        OFPACT_FOR_EACH (a, conntrack->actions, ofpacts_len) {
6870
693
            if (a->type != OFPACT_NAT || ofpact_get_NAT(a)->flags
6871
623
                || ofpact_get_NAT(a)->range_af != AF_UNSPEC) {
6872
623
                VLOG_WARN_RL(&rl, "CT action requires commit flag if actions "
6873
623
                             "other than NAT without arguments are specified.");
6874
623
                error = OFPERR_OFPBAC_BAD_ARGUMENT;
6875
623
                goto out;
6876
623
            }
6877
693
        }
6878
693
    }
6879
6880
11.0k
out:
6881
11.0k
    ofpbuf_push_uninit(out, ct_offset);
6882
11.0k
    return error;
6883
8.26k
}
6884
6885
static void
6886
encode_CT(const struct ofpact_conntrack *conntrack,
6887
          enum ofp_version ofp_version, struct ofpbuf *out)
6888
12.3k
{
6889
12.3k
    struct nx_action_conntrack *nac;
6890
12.3k
    const size_t ofs = out->size;
6891
12.3k
    size_t len;
6892
6893
12.3k
    nac = put_NXAST_CT(out);
6894
12.3k
    nac->flags = htons(conntrack->flags);
6895
12.3k
    if (conntrack->zone_src.field) {
6896
204
        nac->zone_src = htonl(nxm_header_from_mff(conntrack->zone_src.field));
6897
204
        nac->zone_ofs_nbits = nxm_encode_ofs_nbits(conntrack->zone_src.ofs,
6898
204
                                                   conntrack->zone_src.n_bits);
6899
12.1k
    } else {
6900
12.1k
        nac->zone_src = htonl(0);
6901
12.1k
        nac->zone_imm = htons(conntrack->zone_imm);
6902
12.1k
    }
6903
12.3k
    nac->recirc_table = conntrack->recirc_table;
6904
12.3k
    nac->alg = htons(conntrack->alg);
6905
6906
12.3k
    len = ofpacts_put_openflow_actions(conntrack->actions,
6907
12.3k
                                       ofpact_ct_get_action_len(conntrack),
6908
12.3k
                                       out, ofp_version);
6909
12.3k
    len += sizeof(*nac);
6910
12.3k
    nac = ofpbuf_at(out, ofs, sizeof(*nac));
6911
12.3k
    nac->len = htons(len);
6912
12.3k
}
6913
6914
static char *OVS_WARN_UNUSED_RESULT
6915
parse_NAT(char *arg, const struct ofpact_parse_params *pp);
6916
6917
/* Parses 'arg' as the argument to a "ct" action, and appends such an
6918
 * action to 'pp->ofpacts'.
6919
 *
6920
 * Returns NULL if successful, otherwise a malloc()'d string describing the
6921
 * error.  The caller is responsible for freeing the returned string. */
6922
static char * OVS_WARN_UNUSED_RESULT
6923
parse_CT(char *arg, const struct ofpact_parse_params *pp)
6924
20.1k
{
6925
20.1k
    const size_t ct_offset = ofpacts_pull(pp->ofpacts);
6926
20.1k
    struct ofpact_conntrack *oc;
6927
20.1k
    char *error = NULL;
6928
20.1k
    char *key, *value;
6929
6930
20.1k
    oc = ofpact_put_CT(pp->ofpacts);
6931
20.1k
    oc->flags = 0;
6932
20.1k
    oc->recirc_table = NX_CT_RECIRC_NONE;
6933
119k
    while (ofputil_parse_key_value(&arg, &key, &value)) {
6934
100k
        if (!strcmp(key, "commit")) {
6935
261
            oc->flags |= NX_CT_F_COMMIT;
6936
100k
        } else if (!strcmp(key, "force")) {
6937
260
            oc->flags |= NX_CT_F_FORCE;
6938
99.8k
        } else if (!strcmp(key, "table")) {
6939
261
            if (!ofputil_table_from_string(value, pp->table_map,
6940
261
                                           &oc->recirc_table)) {
6941
1
                error = xasprintf("unknown table %s", value);
6942
260
            } else if (oc->recirc_table == NX_CT_RECIRC_NONE) {
6943
1
                error = xasprintf("invalid table %#"PRIx8, oc->recirc_table);
6944
1
            }
6945
99.6k
        } else if (!strcmp(key, "zone")) {
6946
304
            error = str_to_u16(value, "zone", &oc->zone_imm);
6947
6948
304
            if (error) {
6949
238
                free(error);
6950
238
                error = mf_parse_subfield(&oc->zone_src, value);
6951
238
                if (error) {
6952
7
                    return error;
6953
7
                }
6954
238
            }
6955
99.3k
        } else if (!strcmp(key, "alg")) {
6956
552
            error = str_to_connhelper(value, &oc->alg);
6957
98.7k
        } else if (!strcmp(key, "nat")) {
6958
96.8k
            const size_t nat_offset = ofpacts_pull(pp->ofpacts);
6959
6960
96.8k
            error = parse_NAT(value, pp);
6961
            /* Update CT action pointer and length. */
6962
96.8k
            pp->ofpacts->header = ofpbuf_push_uninit(pp->ofpacts, nat_offset);
6963
96.8k
            oc = pp->ofpacts->header;
6964
96.8k
        } else if (!strcmp(key, "exec")) {
6965
            /* Hide existing actions from ofpacts_parse_copy(), so the
6966
             * nesting can be handled transparently. */
6967
1.58k
            enum ofputil_protocol usable_protocols2;
6968
1.58k
            const size_t exec_offset = ofpacts_pull(pp->ofpacts);
6969
6970
            /* Initializes 'usable_protocol2', fold it back to
6971
             * '*usable_protocols' afterwards, so that we do not lose
6972
             * restrictions already in there. */
6973
1.58k
            struct ofpact_parse_params pp2 = *pp;
6974
1.58k
            pp2.usable_protocols = &usable_protocols2;
6975
1.58k
            error = ofpacts_parse_copy(value, &pp2, false, OFPACT_CT);
6976
1.58k
            *pp->usable_protocols &= usable_protocols2;
6977
1.58k
            pp->ofpacts->header = ofpbuf_push_uninit(pp->ofpacts, exec_offset);
6978
1.58k
            oc = pp->ofpacts->header;
6979
1.58k
        } else {
6980
291
            error = xasprintf("invalid argument to \"ct\" action: `%s'", key);
6981
291
        }
6982
100k
        if (error) {
6983
1.06k
            break;
6984
1.06k
        }
6985
100k
    }
6986
20.1k
    if (!error && oc->flags & NX_CT_F_FORCE && !(oc->flags & NX_CT_F_COMMIT)) {
6987
7
        error = xasprintf("\"force\" flag requires \"commit\" flag.");
6988
7
    }
6989
6990
20.1k
    if (ofpbuf_oversized(pp->ofpacts)) {
6991
6
        free(error);
6992
6
        return xasprintf("input too big");
6993
6
    }
6994
6995
20.1k
    ofpact_finish_CT(pp->ofpacts, &oc);
6996
20.1k
    ofpbuf_push_uninit(pp->ofpacts, ct_offset);
6997
20.1k
    return error;
6998
20.1k
}
6999
7000
static void
7001
format_alg(int port, struct ds *s)
7002
5.85k
{
7003
5.85k
    switch(port) {
7004
668
    case IPPORT_FTP:
7005
668
        ds_put_format(s, "%salg=%sftp,", colors.param, colors.end);
7006
668
        break;
7007
966
    case IPPORT_TFTP:
7008
966
        ds_put_format(s, "%salg=%stftp,", colors.param, colors.end);
7009
966
        break;
7010
1.64k
    case 0:
7011
        /* Don't print. */
7012
1.64k
        break;
7013
2.57k
    default:
7014
2.57k
        ds_put_format(s, "%salg=%s%d,", colors.param, colors.end, port);
7015
2.57k
        break;
7016
5.85k
    }
7017
5.85k
}
7018
7019
static void format_NAT(const struct ofpact_nat *,
7020
                       const struct ofpact_format_params *fp);
7021
7022
static void
7023
format_CT(const struct ofpact_conntrack *a,
7024
          const struct ofpact_format_params *fp)
7025
5.85k
{
7026
5.85k
    ds_put_format(fp->s, "%sct(%s", colors.paren, colors.end);
7027
5.85k
    if (a->flags & NX_CT_F_COMMIT) {
7028
3.03k
        ds_put_format(fp->s, "%scommit%s,", colors.value, colors.end);
7029
3.03k
    }
7030
5.85k
    if (a->flags & NX_CT_F_FORCE) {
7031
565
        ds_put_format(fp->s, "%sforce%s,", colors.value, colors.end);
7032
565
    }
7033
5.85k
    if (a->recirc_table != NX_CT_RECIRC_NONE) {
7034
4.94k
        ds_put_format(fp->s, "%stable=%s", colors.special, colors.end);
7035
4.94k
        ofputil_format_table(a->recirc_table, fp->table_map, fp->s);
7036
4.94k
        ds_put_char(fp->s, ',');
7037
4.94k
    }
7038
5.85k
    if (a->zone_src.field) {
7039
2.38k
        ds_put_format(fp->s, "%szone=%s", colors.param, colors.end);
7040
2.38k
        mf_format_subfield(&a->zone_src, fp->s);
7041
2.38k
        ds_put_char(fp->s, ',');
7042
3.47k
    } else if (a->zone_imm) {
7043
1.74k
        ds_put_format(fp->s, "%szone=%s%"PRIu16",",
7044
1.74k
                      colors.param, colors.end, a->zone_imm);
7045
1.74k
    }
7046
    /* If the first action is a NAT action, format it outside of the 'exec'
7047
     * envelope. */
7048
5.85k
    const struct ofpact *action = a->actions;
7049
5.85k
    size_t actions_len = ofpact_ct_get_action_len(a);
7050
5.85k
    if (actions_len && action->type == OFPACT_NAT) {
7051
1.85k
        format_NAT(ofpact_get_NAT(action), fp);
7052
1.85k
        ds_put_char(fp->s, ',');
7053
1.85k
        actions_len -= OFPACT_ALIGN(action->len);
7054
1.85k
        action = ofpact_next(action);
7055
1.85k
    }
7056
5.85k
    if (actions_len) {
7057
606
        ds_put_format(fp->s, "%sexec(%s", colors.paren, colors.end);
7058
606
        ofpacts_format(action, actions_len, fp);
7059
606
        ds_put_format(fp->s, "%s),%s", colors.paren, colors.end);
7060
606
    }
7061
5.85k
    format_alg(a->alg, fp->s);
7062
5.85k
    ds_chomp(fp->s, ',');
7063
5.85k
    ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
7064
5.85k
}
7065
7066
static enum ofperr
7067
check_CT(struct ofpact_conntrack *a, struct ofpact_check_params *cp)
7068
15.2k
{
7069
15.2k
    struct flow *flow = &cp->match->flow;
7070
7071
15.2k
    if (!dl_type_is_ip_any(get_dl_type(flow))
7072
13.8k
        || (flow->ct_state & CS_INVALID && a->flags & NX_CT_F_COMMIT)
7073
13.8k
        || (a->alg == IPPORT_FTP && flow->nw_proto != IPPROTO_TCP)
7074
13.8k
        || (a->alg == IPPORT_TFTP && flow->nw_proto != IPPROTO_UDP)) {
7075
        /* We can't downgrade to OF1.0 and expect inconsistent CT actions
7076
         * be silently discarded.  Instead, datapath flow install fails, so
7077
         * it is better to flag inconsistent CT actions as hard errors. */
7078
1.41k
        return OFPERR_OFPBAC_MATCH_INCONSISTENT;
7079
1.41k
    }
7080
7081
13.8k
    if (a->zone_src.field) {
7082
1.61k
        return mf_check_src(&a->zone_src, cp->match);
7083
1.61k
    }
7084
7085
12.2k
    return check_subactions(a->actions, ofpact_ct_get_action_len(a), cp);
7086
13.8k
}
7087

7088
/* ct_clear action. */
7089
7090
static enum ofperr
7091
decode_NXAST_RAW_CT_CLEAR(struct ofpbuf *out)
7092
694
{
7093
694
    ofpact_put_CT_CLEAR(out);
7094
694
    return 0;
7095
694
}
7096
7097
static void
7098
encode_CT_CLEAR(const struct ofpact_null *null OVS_UNUSED,
7099
                enum ofp_version ofp_version OVS_UNUSED,
7100
                struct ofpbuf *out)
7101
504
{
7102
504
    put_NXAST_CT_CLEAR(out);
7103
504
}
7104
7105
static char * OVS_WARN_UNUSED_RESULT
7106
parse_CT_CLEAR(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
7107
637
{
7108
637
    ofpact_put_CT_CLEAR(pp->ofpacts);
7109
637
    return NULL;
7110
637
}
7111
7112
static void
7113
format_CT_CLEAR(const struct ofpact_null *a OVS_UNUSED,
7114
                const struct ofpact_format_params *fp)
7115
152
{
7116
152
    ds_put_format(fp->s, "%sct_clear%s", colors.value, colors.end);
7117
152
}
7118
7119
static enum ofperr
7120
check_CT_CLEAR(const struct ofpact_null *a OVS_UNUSED,
7121
               const struct ofpact_check_params *cp OVS_UNUSED)
7122
1.17k
{
7123
1.17k
    return 0;
7124
1.17k
}
7125

7126
/* NAT action. */
7127
7128
/* Which optional fields are present? */
7129
enum nx_nat_range {
7130
    NX_NAT_RANGE_IPV4_MIN  = 1 << 0, /* ovs_be32 */
7131
    NX_NAT_RANGE_IPV4_MAX  = 1 << 1, /* ovs_be32 */
7132
    NX_NAT_RANGE_IPV6_MIN  = 1 << 2, /* struct in6_addr */
7133
    NX_NAT_RANGE_IPV6_MAX  = 1 << 3, /* struct in6_addr */
7134
    NX_NAT_RANGE_PROTO_MIN = 1 << 4, /* ovs_be16 */
7135
    NX_NAT_RANGE_PROTO_MAX = 1 << 5, /* ovs_be16 */
7136
};
7137
7138
/* Action structure for NXAST_NAT. */
7139
struct nx_action_nat {
7140
    ovs_be16 type;              /* OFPAT_VENDOR. */
7141
    ovs_be16 len;               /* At least 16. */
7142
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
7143
    ovs_be16 subtype;           /* NXAST_NAT. */
7144
    uint8_t  pad[2];            /* Must be zero. */
7145
    ovs_be16 flags;             /* Zero or more NX_NAT_F_* flags.
7146
                                 * Unspecified flag bits must be zero. */
7147
    ovs_be16 range_present;     /* NX_NAT_RANGE_* */
7148
    /* Followed by optional parameters as specified by 'range_present' */
7149
};
7150
OFP_ASSERT(sizeof(struct nx_action_nat) == 16);
7151
7152
static void
7153
encode_NAT(const struct ofpact_nat *nat,
7154
           enum ofp_version ofp_version OVS_UNUSED,
7155
           struct ofpbuf *out)
7156
6.71k
{
7157
6.71k
    struct nx_action_nat *nan;
7158
6.71k
    const size_t ofs = out->size;
7159
6.71k
    uint16_t range_present = 0;
7160
7161
6.71k
    nan = put_NXAST_NAT(out);
7162
6.71k
    nan->flags = htons(nat->flags);
7163
6.71k
    if (nat->range_af == AF_INET) {
7164
1.23k
        if (nat->range.addr.ipv4.min) {
7165
211
            ovs_be32 *min = ofpbuf_put_uninit(out, sizeof *min);
7166
211
            *min = nat->range.addr.ipv4.min;
7167
211
            range_present |= NX_NAT_RANGE_IPV4_MIN;
7168
211
        }
7169
1.23k
        if (nat->range.addr.ipv4.max) {
7170
272
            ovs_be32 *max = ofpbuf_put_uninit(out, sizeof *max);
7171
272
            *max = nat->range.addr.ipv4.max;
7172
272
            range_present |= NX_NAT_RANGE_IPV4_MAX;
7173
272
        }
7174
5.47k
    } else if (nat->range_af == AF_INET6) {
7175
2.23k
        if (!ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
7176
1.32k
            struct in6_addr *min = ofpbuf_put_uninit(out, sizeof *min);
7177
1.32k
            *min = nat->range.addr.ipv6.min;
7178
1.32k
            range_present |= NX_NAT_RANGE_IPV6_MIN;
7179
1.32k
        }
7180
2.23k
        if (!ipv6_mask_is_any(&nat->range.addr.ipv6.max)) {
7181
873
            struct in6_addr *max = ofpbuf_put_uninit(out, sizeof *max);
7182
873
            *max = nat->range.addr.ipv6.max;
7183
873
            range_present |= NX_NAT_RANGE_IPV6_MAX;
7184
873
        }
7185
2.23k
    }
7186
6.71k
    if (nat->range_af != AF_UNSPEC) {
7187
3.47k
        if (nat->range.proto.min) {
7188
303
            ovs_be16 *min = ofpbuf_put_uninit(out, sizeof *min);
7189
303
            *min = htons(nat->range.proto.min);
7190
303
            range_present |= NX_NAT_RANGE_PROTO_MIN;
7191
303
        }
7192
3.47k
        if (nat->range.proto.max) {
7193
273
            ovs_be16 *max = ofpbuf_put_uninit(out, sizeof *max);
7194
273
            *max = htons(nat->range.proto.max);
7195
273
            range_present |= NX_NAT_RANGE_PROTO_MAX;
7196
273
        }
7197
3.47k
    }
7198
6.71k
    pad_ofpat(out, ofs);
7199
6.71k
    nan = ofpbuf_at(out, ofs, sizeof *nan);
7200
6.71k
    nan->range_present = htons(range_present);
7201
6.71k
}
7202
7203
static enum ofperr
7204
decode_NXAST_RAW_NAT(const struct nx_action_nat *nan,
7205
                     enum ofp_version ofp_version OVS_UNUSED,
7206
                     struct ofpbuf *out)
7207
20.4k
{
7208
20.4k
    struct ofpact_nat *nat;
7209
20.4k
    uint16_t range_present = ntohs(nan->range_present);
7210
20.4k
    const char *opts = (char *)(nan + 1);
7211
20.4k
    uint16_t len = ntohs(nan->len) - sizeof *nan;
7212
7213
20.4k
    nat = ofpact_put_NAT(out);
7214
20.4k
    nat->flags = ntohs(nan->flags);
7215
7216
    /* Check for unknown or mutually exclusive flags. */
7217
20.4k
    if ((nat->flags & ~NX_NAT_F_MASK)
7218
17.7k
        || (nat->flags & NX_NAT_F_SRC && nat->flags & NX_NAT_F_DST)
7219
17.6k
        || (nat->flags & NX_NAT_F_PROTO_HASH
7220
2.84k
            && nat->flags & NX_NAT_F_PROTO_RANDOM)) {
7221
2.84k
        return OFPERR_OFPBAC_BAD_ARGUMENT;
7222
2.84k
    }
7223
7224
17.6k
#define NX_NAT_GET_OPT(DST, SRC, LEN, TYPE)                     \
7225
42.4k
    (LEN >= sizeof(TYPE)                                        \
7226
42.4k
     ? (memcpy(DST, SRC, sizeof(TYPE)), LEN -= sizeof(TYPE),    \
7227
22.4k
        SRC += sizeof(TYPE))                                    \
7228
42.4k
     : NULL)
7229
7230
17.6k
    nat->range_af = AF_UNSPEC;
7231
17.6k
    if (range_present & NX_NAT_RANGE_IPV4_MIN) {
7232
10.7k
        if (range_present & (NX_NAT_RANGE_IPV6_MIN | NX_NAT_RANGE_IPV6_MAX)) {
7233
1.75k
            return OFPERR_OFPBAC_BAD_ARGUMENT;
7234
1.75k
        }
7235
7236
8.97k
        if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.min, opts, len, ovs_be32)
7237
8.96k
            || !nat->range.addr.ipv4.min) {
7238
115
            return OFPERR_OFPBAC_BAD_ARGUMENT;
7239
115
        }
7240
7241
8.86k
        nat->range_af = AF_INET;
7242
7243
8.86k
        if (range_present & NX_NAT_RANGE_IPV4_MAX) {
7244
1.56k
            if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.max, opts, len,
7245
1.56k
                                ovs_be32)) {
7246
0
                return OFPERR_OFPBAC_BAD_ARGUMENT;
7247
0
            }
7248
1.56k
            if (ntohl(nat->range.addr.ipv4.max)
7249
1.56k
                < ntohl(nat->range.addr.ipv4.min)) {
7250
773
                return OFPERR_OFPBAC_BAD_ARGUMENT;
7251
773
            }
7252
1.56k
        }
7253
8.86k
    } else if (range_present & NX_NAT_RANGE_IPV4_MAX) {
7254
504
        return OFPERR_OFPBAC_BAD_ARGUMENT;
7255
6.37k
    } else if (range_present & NX_NAT_RANGE_IPV6_MIN) {
7256
5.03k
        if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.min, opts, len,
7257
5.03k
                            struct in6_addr)
7258
3.33k
            || ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
7259
1.71k
            return OFPERR_OFPBAC_BAD_ARGUMENT;
7260
1.71k
        }
7261
7262
3.32k
        nat->range_af = AF_INET6;
7263
7264
3.32k
        if (range_present & NX_NAT_RANGE_IPV6_MAX) {
7265
3.25k
            if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.max, opts, len,
7266
3.25k
                                struct in6_addr)) {
7267
37
                return OFPERR_OFPBAC_BAD_ARGUMENT;
7268
37
            }
7269
3.21k
            if (memcmp(&nat->range.addr.ipv6.max, &nat->range.addr.ipv6.min,
7270
3.21k
                       sizeof(struct in6_addr)) < 0) {
7271
398
                return OFPERR_OFPBAC_BAD_ARGUMENT;
7272
398
            }
7273
3.21k
        }
7274
3.32k
    } else if (range_present & NX_NAT_RANGE_IPV6_MAX) {
7275
373
        return OFPERR_OFPBAC_BAD_ARGUMENT;
7276
373
    }
7277
7278
11.9k
    if (range_present & NX_NAT_RANGE_PROTO_MIN) {
7279
4.30k
        ovs_be16 proto;
7280
7281
4.30k
        if (nat->range_af == AF_UNSPEC) {
7282
706
            return OFPERR_OFPBAC_BAD_ARGUMENT;
7283
706
        }
7284
3.60k
        if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16) || proto == 0) {
7285
746
            return OFPERR_OFPBAC_BAD_ARGUMENT;
7286
746
        }
7287
2.85k
        nat->range.proto.min = ntohs(proto);
7288
2.85k
        if (range_present & NX_NAT_RANGE_PROTO_MAX) {
7289
2.43k
            if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16)) {
7290
0
                return OFPERR_OFPBAC_BAD_ARGUMENT;
7291
0
            }
7292
2.43k
            nat->range.proto.max = ntohs(proto);
7293
2.43k
            if (nat->range.proto.max < nat->range.proto.min) {
7294
801
                return OFPERR_OFPBAC_BAD_ARGUMENT;
7295
801
            }
7296
2.43k
        }
7297
7.63k
    } else if (range_present & NX_NAT_RANGE_PROTO_MAX) {
7298
765
        return OFPERR_OFPBAC_BAD_ARGUMENT;
7299
765
    }
7300
7301
8.92k
    return 0;
7302
11.9k
}
7303
7304
static void
7305
format_NAT(const struct ofpact_nat *a, const struct ofpact_format_params *fp)
7306
1.85k
{
7307
1.85k
    ds_put_format(fp->s, "%snat%s", colors.paren, colors.end);
7308
7309
1.85k
    if (a->flags & (NX_NAT_F_SRC | NX_NAT_F_DST)) {
7310
1.69k
        ds_put_format(fp->s, "%s(%s", colors.paren, colors.end);
7311
1.69k
        ds_put_format(fp->s, a->flags & NX_NAT_F_SRC ? "%ssrc%s" : "%sdst%s",
7312
1.69k
                      colors.param, colors.end);
7313
7314
1.69k
        if (a->range_af != AF_UNSPEC) {
7315
1.67k
            ds_put_format(fp->s, "%s=%s", colors.param, colors.end);
7316
7317
1.67k
            if (a->range_af == AF_INET) {
7318
1.67k
                ds_put_format(fp->s, IP_FMT, IP_ARGS(a->range.addr.ipv4.min));
7319
7320
1.67k
                if (a->range.addr.ipv4.max
7321
70
                    && a->range.addr.ipv4.max != a->range.addr.ipv4.min) {
7322
36
                    ds_put_format(fp->s, "-"IP_FMT,
7323
36
                                  IP_ARGS(a->range.addr.ipv4.max));
7324
36
                }
7325
1.67k
            } else if (a->range_af == AF_INET6) {
7326
0
                ipv6_format_addr_bracket(&a->range.addr.ipv6.min, fp->s,
7327
0
                                        a->range.proto.min);
7328
7329
0
                if (!ipv6_mask_is_any(&a->range.addr.ipv6.max)
7330
0
                    && memcmp(&a->range.addr.ipv6.max, &a->range.addr.ipv6.min,
7331
0
                              sizeof(struct in6_addr)) != 0) {
7332
0
                    ds_put_char(fp->s, '-');
7333
0
                    ipv6_format_addr_bracket(&a->range.addr.ipv6.max, fp->s,
7334
0
                                            a->range.proto.min);
7335
0
                }
7336
0
            }
7337
1.67k
            if (a->range.proto.min) {
7338
1.49k
                ds_put_char(fp->s, ':');
7339
1.49k
                ds_put_format(fp->s, "%"PRIu16, a->range.proto.min);
7340
7341
1.49k
                if (a->range.proto.max
7342
1.43k
                    && a->range.proto.max != a->range.proto.min) {
7343
774
                    ds_put_format(fp->s, "-%"PRIu16, a->range.proto.max);
7344
774
                }
7345
1.49k
            }
7346
1.67k
            ds_put_char(fp->s, ',');
7347
7348
1.67k
            if (a->flags & NX_NAT_F_PERSISTENT) {
7349
195
                ds_put_format(fp->s, "%spersistent%s,",
7350
195
                              colors.value, colors.end);
7351
195
            }
7352
1.67k
            if (a->flags & NX_NAT_F_PROTO_HASH) {
7353
566
                ds_put_format(fp->s, "%shash%s,", colors.value, colors.end);
7354
566
            }
7355
1.67k
            if (a->flags & NX_NAT_F_PROTO_RANDOM) {
7356
43
                ds_put_format(fp->s, "%srandom%s,", colors.value, colors.end);
7357
43
            }
7358
1.67k
        }
7359
1.69k
        ds_chomp(fp->s, ',');
7360
1.69k
        ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
7361
1.69k
    }
7362
1.85k
}
7363
7364
static char * OVS_WARN_UNUSED_RESULT
7365
str_to_nat_range(const char *s, struct ofpact_nat *on)
7366
83.6k
{
7367
83.6k
    char ipv6_s[IPV6_SCAN_LEN + 1];
7368
83.6k
    int n = 0;
7369
7370
83.6k
    on->range_af = AF_UNSPEC;
7371
83.6k
    if (ovs_scan_len(s, &n, IP_SCAN_FMT,
7372
83.6k
                     IP_SCAN_ARGS(&on->range.addr.ipv4.min))) {
7373
1.44k
        on->range_af = AF_INET;
7374
7375
1.44k
        if (s[n] == '-') {
7376
428
            n++;
7377
428
            if (!ovs_scan_len(s, &n, IP_SCAN_FMT,
7378
428
                              IP_SCAN_ARGS(&on->range.addr.ipv4.max))
7379
425
                || (ntohl(on->range.addr.ipv4.max)
7380
425
                    < ntohl(on->range.addr.ipv4.min))) {
7381
5
                goto error;
7382
5
            }
7383
428
        }
7384
82.2k
    } else if ((ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
7385
3.75k
                || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
7386
80.0k
               && inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.min) == 1) {
7387
43.5k
        on->range_af = AF_INET6;
7388
7389
43.5k
        if (s[n] == '-') {
7390
1.79k
            n++;
7391
1.79k
            if (!(ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
7392
520
                  || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
7393
1.77k
                || inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.max) != 1
7394
1.76k
                || memcmp(&on->range.addr.ipv6.max, &on->range.addr.ipv6.min,
7395
1.76k
                          sizeof on->range.addr.ipv6.max) < 0) {
7396
36
                goto error;
7397
36
            }
7398
1.79k
        }
7399
43.5k
    }
7400
83.6k
    if (on->range_af != AF_UNSPEC && s[n] == ':') {
7401
1.01k
        n++;
7402
1.01k
        if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.min)) {
7403
3
            goto error;
7404
3
        }
7405
1.00k
        if (s[n] == '-') {
7406
592
            n++;
7407
592
            if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.max)
7408
591
                || on->range.proto.max < on->range.proto.min) {
7409
3
                goto error;
7410
3
            }
7411
592
        }
7412
1.00k
    }
7413
83.6k
    if (strlen(s) != n) {
7414
94
        return xasprintf("garbage (%s) after nat range \"%s\" (pos: %d)",
7415
94
                         &s[n], s, n);
7416
94
    }
7417
83.5k
    return NULL;
7418
47
error:
7419
47
    return xasprintf("invalid nat range \"%s\"", s);
7420
83.6k
}
7421
7422
7423
/* Parses 'arg' as the argument to a "nat" action, and appends such an
7424
 * action to 'pp->ofpacts'.
7425
 *
7426
 * Returns NULL if successful, otherwise a malloc()'d string describing the
7427
 * error.  The caller is responsible for freeing the returned string. */
7428
static char * OVS_WARN_UNUSED_RESULT
7429
parse_NAT(char *arg, const struct ofpact_parse_params *pp)
7430
98.8k
{
7431
98.8k
    struct ofpact_nat *on = ofpact_put_NAT(pp->ofpacts);
7432
98.8k
    char *key, *value;
7433
7434
98.8k
    on->flags = 0;
7435
98.8k
    on->range_af = AF_UNSPEC;
7436
7437
182k
    while (ofputil_parse_key_value(&arg, &key, &value)) {
7438
84.1k
        char *error = NULL;
7439
7440
84.1k
        if (!strcmp(key, "src")) {
7441
2.78k
            on->flags |= NX_NAT_F_SRC;
7442
2.78k
            error = str_to_nat_range(value, on);
7443
81.4k
        } else if (!strcmp(key, "dst")) {
7444
80.8k
            on->flags |= NX_NAT_F_DST;
7445
80.8k
            error = str_to_nat_range(value, on);
7446
80.8k
        } else if (!strcmp(key, "persistent")) {
7447
255
            on->flags |= NX_NAT_F_PERSISTENT;
7448
278
        } else if (!strcmp(key, "hash")) {
7449
150
            on->flags |= NX_NAT_F_PROTO_HASH;
7450
150
        } else if (!strcmp(key, "random")) {
7451
67
            on->flags |= NX_NAT_F_PROTO_RANDOM;
7452
67
        } else {
7453
61
            error = xasprintf("invalid key \"%s\" in \"nat\" argument",
7454
61
                              key);
7455
61
        }
7456
84.1k
        if (error) {
7457
202
            return error;
7458
202
        }
7459
84.1k
    }
7460
98.6k
    if (on->flags & NX_NAT_F_SRC && on->flags & NX_NAT_F_DST) {
7461
4
        return xasprintf("May only specify one of \"src\" or \"dst\".");
7462
4
    }
7463
98.6k
    if (!(on->flags & NX_NAT_F_SRC || on->flags & NX_NAT_F_DST)) {
7464
18.4k
        if (on->flags) {
7465
16
            return xasprintf("Flags allowed only with \"src\" or \"dst\".");
7466
16
        }
7467
18.4k
        if (on->range_af != AF_UNSPEC) {
7468
0
            return xasprintf("Range allowed only with \"src\" or \"dst\".");
7469
0
        }
7470
18.4k
    }
7471
98.6k
    if (on->flags & NX_NAT_F_PROTO_HASH && on->flags & NX_NAT_F_PROTO_RANDOM) {
7472
1
        return xasprintf("Both \"hash\" and \"random\" are not allowed.");
7473
1
    }
7474
7475
98.6k
    return NULL;
7476
98.6k
}
7477
7478
static enum ofperr
7479
check_NAT(const struct ofpact_nat *a, const struct ofpact_check_params *cp)
7480
6.75k
{
7481
6.75k
    ovs_be16 dl_type = get_dl_type(&cp->match->flow);
7482
6.75k
    if (!dl_type_is_ip_any(dl_type) ||
7483
6.75k
        (a->range_af == AF_INET && dl_type != htons(ETH_TYPE_IP)) ||
7484
6.75k
        (a->range_af == AF_INET6 && dl_type != htons(ETH_TYPE_IPV6))) {
7485
3
        return OFPERR_OFPBAC_MATCH_INCONSISTENT;
7486
3
    }
7487
6.74k
    return 0;
7488
6.75k
}
7489

7490
/* Truncate output action. */
7491
struct nx_action_output_trunc {
7492
    ovs_be16 type;              /* OFPAT_VENDOR. */
7493
    ovs_be16 len;               /* At least 16. */
7494
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
7495
    ovs_be16 subtype;           /* NXAST_OUTPUT_TRUNC. */
7496
    ovs_be16 port;              /* Output port */
7497
    ovs_be32 max_len;           /* Truncate packet to size bytes */
7498
};
7499
OFP_ASSERT(sizeof(struct nx_action_output_trunc) == 16);
7500
7501
static enum ofperr
7502
decode_NXAST_RAW_OUTPUT_TRUNC(const struct nx_action_output_trunc *natrc,
7503
                            enum ofp_version ofp_version OVS_UNUSED,
7504
                            struct ofpbuf *out)
7505
1.05k
{
7506
1.05k
    struct ofpact_output_trunc *output_trunc;
7507
7508
1.05k
    output_trunc = ofpact_put_OUTPUT_TRUNC(out);
7509
1.05k
    output_trunc->max_len = ntohl(natrc->max_len);
7510
1.05k
    output_trunc->port = u16_to_ofp(ntohs(natrc->port));
7511
7512
1.05k
    if (output_trunc->max_len < ETH_HEADER_LEN) {
7513
36
        return OFPERR_OFPBAC_BAD_ARGUMENT;
7514
36
    }
7515
1.02k
    return 0;
7516
1.05k
}
7517
7518
static void
7519
encode_OUTPUT_TRUNC(const struct ofpact_output_trunc *output_trunc,
7520
                  enum ofp_version ofp_version OVS_UNUSED,
7521
                  struct ofpbuf *out)
7522
776
{
7523
776
    struct nx_action_output_trunc *natrc = put_NXAST_OUTPUT_TRUNC(out);
7524
7525
776
    natrc->max_len = htonl(output_trunc->max_len);
7526
776
    natrc->port = htons(ofp_to_u16(output_trunc->port));
7527
776
}
7528
7529
static char * OVS_WARN_UNUSED_RESULT
7530
parse_OUTPUT_TRUNC(const char *arg,
7531
                   const struct ofpact_parse_params *pp OVS_UNUSED)
7532
1
{
7533
    /* Disable output_trunc parsing.  Expose as output(port=N,max_len=M) and
7534
     * reuse parse_OUTPUT to parse output_trunc action. */
7535
1
    return xasprintf("unknown action %s", arg);
7536
1
}
7537
7538
static void
7539
format_OUTPUT_TRUNC(const struct ofpact_output_trunc *a,
7540
                    const struct ofpact_format_params *fp)
7541
109
{
7542
109
    ds_put_format(fp->s, "%soutput%s(port=", colors.special, colors.end);
7543
109
    ofputil_format_port(a->port, fp->port_map, fp->s);
7544
109
    ds_put_format(fp->s, ",max_len=%"PRIu32")", a->max_len);
7545
109
}
7546
7547
static enum ofperr
7548
check_OUTPUT_TRUNC(const struct ofpact_output_trunc *a,
7549
                   const struct ofpact_check_params *cp)
7550
875
{
7551
875
    return ofpact_check_output_port(a->port, cp->max_ports);
7552
875
}
7553

7554
/* Meter.
7555
 *
7556
 * In OpenFlow 1.3 and 1.4, "meter" is an instruction.
7557
 * In OpenFlow 1.5 and later, "meter" is an action.
7558
 *
7559
 * OpenFlow 1.5 */
7560
7561
static enum ofperr
7562
decode_OFPAT_RAW15_METER(uint32_t meter_id,
7563
                         enum ofp_version ofp_version OVS_UNUSED,
7564
                         struct ofpbuf *out)
7565
2.87k
{
7566
2.87k
    struct ofpact_meter *om = ofpact_put_METER(out);
7567
2.87k
    om->meter_id = meter_id;
7568
2.87k
    om->provider_meter_id = UINT32_MAX; /* No provider meter ID. */
7569
2.87k
    return 0;
7570
2.87k
}
7571
7572
static void
7573
encode_METER(const struct ofpact_meter *meter,
7574
             enum ofp_version ofp_version, struct ofpbuf *out)
7575
141
{
7576
141
    if (ofp_version == OFP13_VERSION || ofp_version == OFP14_VERSION) {
7577
138
        instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
7578
138
    } else if (ofp_version >= OFP15_VERSION) {
7579
0
        put_OFPAT15_METER(out, meter->meter_id);
7580
0
    }
7581
141
}
7582
7583
static char * OVS_WARN_UNUSED_RESULT
7584
parse_METER(char *arg, const struct ofpact_parse_params *pp)
7585
1.31k
{
7586
1.31k
    *pp->usable_protocols &= OFPUTIL_P_OF13_UP;
7587
1.31k
    return str_to_u32(arg, &ofpact_put_METER(pp->ofpacts)->meter_id);
7588
1.31k
}
7589
7590
static void
7591
format_METER(const struct ofpact_meter *a,
7592
             const struct ofpact_format_params *fp)
7593
1.33k
{
7594
1.33k
    ds_put_format(fp->s, "%smeter:%s%"PRIu32,
7595
1.33k
                  colors.param, colors.end, a->meter_id);
7596
1.33k
}
7597
7598
static enum ofperr
7599
check_METER(const struct ofpact_meter *a,
7600
            const struct ofpact_check_params *cp OVS_UNUSED)
7601
271
{
7602
271
    uint32_t mid = a->meter_id;
7603
271
    return mid == 0 || mid > OFPM13_MAX ? OFPERR_OFPMMFC_INVALID_METER : 0;
7604
271
}
7605

7606
/* Clear-Actions instruction. */
7607
7608
static void
7609
encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
7610
                     enum ofp_version ofp_version OVS_UNUSED,
7611
                     struct ofpbuf *out OVS_UNUSED)
7612
3
{
7613
3
    if (ofp_version > OFP10_VERSION) {
7614
2
        instruction_put_OFPIT11_CLEAR_ACTIONS(out);
7615
2
    }
7616
3
}
7617
7618
static char * OVS_WARN_UNUSED_RESULT
7619
parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, const struct ofpact_parse_params *pp)
7620
69
{
7621
69
    ofpact_put_CLEAR_ACTIONS(pp->ofpacts);
7622
69
    return NULL;
7623
69
}
7624
7625
static void
7626
format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED,
7627
                     const struct ofpact_format_params *fp)
7628
75
{
7629
75
    ds_put_format(fp->s, "%sclear_actions%s", colors.value, colors.end);
7630
75
}
7631
7632
static enum ofperr
7633
check_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED,
7634
                    const struct ofpact_check_params *cp OVS_UNUSED)
7635
78
{
7636
78
    return 0;
7637
78
}
7638

7639
/* Write-Actions instruction. */
7640
7641
static void
7642
encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
7643
                     enum ofp_version ofp_version, struct ofpbuf *out)
7644
50
{
7645
50
    if (ofp_version > OFP10_VERSION) {
7646
38
        const size_t ofs = out->size;
7647
7648
38
        instruction_put_OFPIT11_WRITE_ACTIONS(out);
7649
38
        ofpacts_put_openflow_actions(actions->actions,
7650
38
                                     ofpact_nest_get_action_len(actions),
7651
38
                                     out, ofp_version);
7652
38
        ofpacts_update_instruction_actions(out, ofs);
7653
38
    }
7654
50
}
7655
7656
static char * OVS_WARN_UNUSED_RESULT
7657
parse_WRITE_ACTIONS(char *arg, const struct ofpact_parse_params *pp)
7658
723
{
7659
723
    size_t ofs = ofpacts_pull(pp->ofpacts);
7660
723
    struct ofpact_nest *on;
7661
723
    char *error;
7662
7663
    /* Add a Write-Actions instruction and then pull it off. */
7664
723
    ofpact_put(pp->ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
7665
723
    ofpbuf_pull(pp->ofpacts, sizeof *on);
7666
7667
    /* Parse nested actions.
7668
     *
7669
     * We pulled off "write-actions" and the previous actions because the
7670
     * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
7671
     * that it doesn't actually include the nested actions.  That means that
7672
     * ofpacts_parse() would reject them as being part of an Apply-Actions that
7673
     * follows a Write-Actions, which is an invalid order.  */
7674
723
    error = ofpacts_parse(arg, pp, false, OFPACT_WRITE_ACTIONS);
7675
7676
    /* Put the Write-Actions back on and update its length. */
7677
723
    on = ofpbuf_push_uninit(pp->ofpacts, sizeof *on);
7678
723
    on->ofpact.len = pp->ofpacts->size;
7679
7680
    /* Put any previous actions or instructions back on. */
7681
723
    ofpbuf_push_uninit(pp->ofpacts, ofs);
7682
7683
723
    return error;
7684
723
}
7685
7686
static void
7687
format_WRITE_ACTIONS(const struct ofpact_nest *a,
7688
                     const struct ofpact_format_params *fp)
7689
11.7k
{
7690
11.7k
    ds_put_format(fp->s, "%swrite_actions(%s", colors.paren, colors.end);
7691
11.7k
    ofpacts_format(a->actions, ofpact_nest_get_action_len(a), fp);
7692
11.7k
    ds_put_format(fp->s, "%s)%s", colors.paren, colors.end);
7693
11.7k
}
7694
7695
static enum ofperr
7696
check_WRITE_ACTIONS(struct ofpact_nest *a,
7697
                    const struct ofpact_check_params *cp)
7698
13.8k
{
7699
    /* Use a temporary copy of 'cp' to avoid updating 'cp->usable_protocols',
7700
     * since we can't check consistency of an action set. */
7701
13.8k
    struct ofpact_check_params tmp = *cp;
7702
13.8k
    return ofpacts_check(a->actions, ofpact_nest_get_action_len(a), &tmp);
7703
13.8k
}
7704

7705
/* Action structure for NXAST_WRITE_METADATA.
7706
 *
7707
 * Modifies the 'mask' bits of the metadata value. */
7708
struct nx_action_write_metadata {
7709
    ovs_be16 type;                  /* OFPAT_VENDOR. */
7710
    ovs_be16 len;                   /* Length is 32. */
7711
    ovs_be32 vendor;                /* NX_VENDOR_ID. */
7712
    ovs_be16 subtype;               /* NXAST_WRITE_METADATA. */
7713
    uint8_t zeros[6];               /* Must be zero. */
7714
    ovs_be64 metadata;              /* Metadata register. */
7715
    ovs_be64 mask;                  /* Metadata mask. */
7716
};
7717
OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
7718
7719
static enum ofperr
7720
decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
7721
                                enum ofp_version ofp_version OVS_UNUSED,
7722
                                struct ofpbuf *out)
7723
2.90k
{
7724
2.90k
    struct ofpact_metadata *om;
7725
7726
2.90k
    if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
7727
87
        return OFPERR_NXBRC_MUST_BE_ZERO;
7728
87
    }
7729
7730
2.82k
    om = ofpact_put_WRITE_METADATA(out);
7731
2.82k
    om->metadata = nawm->metadata;
7732
2.82k
    om->mask = nawm->mask;
7733
7734
2.82k
    return 0;
7735
2.90k
}
7736
7737
static void
7738
encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
7739
                      enum ofp_version ofp_version, struct ofpbuf *out)
7740
3
{
7741
3
    if (ofp_version == OFP10_VERSION) {
7742
1
        struct nx_action_write_metadata *nawm;
7743
7744
1
        nawm = put_NXAST_WRITE_METADATA(out);
7745
1
        nawm->metadata = metadata->metadata;
7746
1
        nawm->mask = metadata->mask;
7747
2
    } else {
7748
2
        struct ofp11_instruction_write_metadata *oiwm;
7749
7750
2
        oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
7751
2
        oiwm->metadata = metadata->metadata;
7752
2
        oiwm->metadata_mask = metadata->mask;
7753
2
    }
7754
3
}
7755
7756
static char * OVS_WARN_UNUSED_RESULT
7757
parse_WRITE_METADATA(char *arg, const struct ofpact_parse_params *pp)
7758
593
{
7759
593
    struct ofpact_metadata *om;
7760
593
    char *mask = strchr(arg, '/');
7761
7762
593
    *pp->usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
7763
7764
593
    om = ofpact_put_WRITE_METADATA(pp->ofpacts);
7765
593
    if (mask) {
7766
275
        char *error;
7767
7768
275
        *mask = '\0';
7769
275
        error = str_to_be64(mask + 1, &om->mask);
7770
275
        if (error) {
7771
2
            return error;
7772
2
        }
7773
318
    } else {
7774
318
        om->mask = OVS_BE64_MAX;
7775
318
    }
7776
7777
591
    return str_to_be64(arg, &om->metadata);
7778
593
}
7779
7780
static void
7781
format_WRITE_METADATA(const struct ofpact_metadata *a,
7782
                      const struct ofpact_format_params *fp)
7783
163
{
7784
163
    ds_put_format(fp->s, "%swrite_metadata:%s%#"PRIx64,
7785
163
                  colors.param, colors.end, ntohll(a->metadata));
7786
163
    if (a->mask != OVS_BE64_MAX) {
7787
145
        ds_put_format(fp->s, "/%#"PRIx64, ntohll(a->mask));
7788
145
    }
7789
163
}
7790
7791
static enum ofperr
7792
check_WRITE_METADATA(const struct ofpact_metadata *a OVS_UNUSED,
7793
                     const struct ofpact_check_params *cp OVS_UNUSED)
7794
66
{
7795
66
    return 0;
7796
66
}
7797

7798
/* Check packet length action. */
7799
7800
struct nx_action_check_pkt_larger {
7801
    ovs_be16 type;              /* OFPAT_VENDOR. */
7802
    ovs_be16 len;               /* 24. */
7803
    ovs_be32 vendor;            /* NX_VENDOR_ID. */
7804
    ovs_be16 subtype;           /* NXAST_OUTPUT_REG. */
7805
    ovs_be16 pkt_len;           /* Length of the packet to check. */
7806
    ovs_be16 offset;            /* Result bit offset in destination. */
7807
    /* Followed by:
7808
     * - 'dst', as an OXM/NXM header (either 4 or 8 bytes).
7809
     * - Enough 0-bytes to pad the action out to 24 bytes. */
7810
    uint8_t pad[10];
7811
};
7812
7813
OFP_ASSERT(sizeof(struct nx_action_check_pkt_larger) == 24);
7814
7815
static enum ofperr
7816
decode_NXAST_RAW_CHECK_PKT_LARGER(
7817
    const struct nx_action_check_pkt_larger *ncpl,
7818
    enum ofp_version ofp_version OVS_UNUSED,
7819
    const struct vl_mff_map *vl_mff_map, uint64_t *tlv_bitmap,
7820
    struct ofpbuf *out)
7821
806
{
7822
806
    struct ofpact_check_pkt_larger *check_pkt_larger;
7823
806
    enum ofperr error;
7824
7825
806
    check_pkt_larger = ofpact_put_CHECK_PKT_LARGER(out);
7826
806
    check_pkt_larger->pkt_len = ntohs(ncpl->pkt_len);
7827
806
    check_pkt_larger->dst.ofs = ntohs(ncpl->offset);
7828
806
    check_pkt_larger->dst.n_bits = 1;
7829
7830
806
    struct ofpbuf b = ofpbuf_const_initializer(ncpl, ntohs(ncpl->len));
7831
806
    ofpbuf_pull(&b, OBJECT_OFFSETOF(ncpl, pad));
7832
7833
806
    error = mf_vl_mff_nx_pull_header(&b, vl_mff_map,
7834
806
                                     &check_pkt_larger->dst.field,
7835
806
                                     NULL, tlv_bitmap);
7836
806
    if (error) {
7837
760
        return error;
7838
760
    }
7839
7840
46
    if (!is_all_zeros(b.data, b.size)) {
7841
10
        return OFPERR_NXBRC_MUST_BE_ZERO;
7842
10
    }
7843
7844
36
    return mf_check_dst(&check_pkt_larger->dst, NULL);
7845
46
}
7846
7847
static void
7848
encode_CHECK_PKT_LARGER(const struct ofpact_check_pkt_larger *check_pkt_larger,
7849
                 enum ofp_version ofp_version OVS_UNUSED,
7850
                 struct ofpbuf *out)
7851
250
{
7852
250
    struct nx_action_check_pkt_larger *ncpl = put_NXAST_CHECK_PKT_LARGER(out);
7853
250
    ncpl->pkt_len = htons(check_pkt_larger->pkt_len);
7854
250
    ncpl->offset = htons(check_pkt_larger->dst.ofs);
7855
7856
250
    if (check_pkt_larger->dst.field) {
7857
250
        size_t size = out->size;
7858
250
        out->size = size - sizeof ncpl->pad;
7859
250
        nx_put_mff_header(out, check_pkt_larger->dst.field, 0, false);
7860
250
        out->size = size;
7861
250
    }
7862
250
}
7863
7864
static char * OVS_WARN_UNUSED_RESULT
7865
parse_CHECK_PKT_LARGER(char *arg, const struct ofpact_parse_params *pp)
7866
1.01k
{
7867
1.01k
    char *value;
7868
1.01k
    char *delim;
7869
1.01k
    char *key;
7870
1.01k
    char *error = set_field_split_str(arg, &key, &value, &delim);
7871
1.01k
    if (error) {
7872
3
        return error;
7873
3
    }
7874
7875
1.01k
    delim[0] = '\0';
7876
1.01k
    if (value[strlen(value) - 1] == ')') {
7877
281
        value[strlen(value) - 1] = '\0';
7878
281
    }
7879
1.01k
    struct mf_subfield dst;
7880
1.01k
    error = mf_parse_subfield(&dst, key);
7881
1.01k
    if (error) {
7882
22
        return error;
7883
22
    }
7884
7885
994
    if (dst.n_bits != 1) {
7886
1
        return xstrdup("Only 1-bit destination field is allowed");
7887
1
    }
7888
7889
993
    struct ofpact_check_pkt_larger *check_pkt_larger =
7890
993
        ofpact_put_CHECK_PKT_LARGER(pp->ofpacts);
7891
993
    error = str_to_u16(value, NULL, &check_pkt_larger->pkt_len);
7892
993
    if (error) {
7893
3
        return error;
7894
3
    }
7895
990
    check_pkt_larger->dst = dst;
7896
990
    return NULL;
7897
993
}
7898
7899
static void
7900
format_CHECK_PKT_LARGER(const struct ofpact_check_pkt_larger *a,
7901
                        const struct ofpact_format_params *fp)
7902
36
{
7903
36
    ds_put_format(fp->s, "%scheck_pkt_larger(%s%"PRIu32")->",
7904
36
                  colors.param, colors.end, a->pkt_len);
7905
36
    mf_format_subfield(&a->dst, fp->s);
7906
36
}
7907
7908
static enum ofperr
7909
check_CHECK_PKT_LARGER(const struct ofpact_check_pkt_larger *a OVS_UNUSED,
7910
                       const struct ofpact_check_params *cp OVS_UNUSED)
7911
268
{
7912
268
    return 0;
7913
268
}
7914

7915
7916
/* Goto-Table instruction. */
7917
7918
static void
7919
encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
7920
                  enum ofp_version ofp_version, struct ofpbuf *out)
7921
4
{
7922
4
    if (ofp_version == OFP10_VERSION) {
7923
3
        struct nx_action_resubmit *nar;
7924
7925
3
        nar = put_NXAST_RESUBMIT_TABLE(out);
7926
3
        nar->table = goto_table->table_id;
7927
3
        nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
7928
3
    } else {
7929
1
        struct ofp11_instruction_goto_table *oigt;
7930
7931
1
        oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
7932
1
        oigt->table_id = goto_table->table_id;
7933
1
        memset(oigt->pad, 0, sizeof oigt->pad);
7934
1
    }
7935
4
}
7936
7937
static char * OVS_WARN_UNUSED_RESULT
7938
parse_GOTO_TABLE(char *arg, const struct ofpact_parse_params *pp)
7939
210
{
7940
210
    struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(pp->ofpacts);
7941
210
    if (!ofputil_table_from_string(arg, pp->table_map, &ogt->table_id)) {
7942
4
        return xasprintf("unknown table \"%s\"", arg);
7943
4
    }
7944
206
    return NULL;
7945
210
}
7946
7947
static void
7948
format_GOTO_TABLE(const struct ofpact_goto_table *a,
7949
                  const struct ofpact_format_params *fp)
7950
48
{
7951
48
    ds_put_format(fp->s, "%sgoto_table:%s", colors.param, colors.end);
7952
48
    ofputil_format_table(a->table_id, fp->table_map, fp->s);
7953
48
}
7954
7955
static enum ofperr
7956
check_GOTO_TABLE(const struct ofpact_goto_table *a,
7957
                 const struct ofpact_check_params *cp)
7958
92
{
7959
92
    if ((cp->table_id != 255 && a->table_id <= cp->table_id)
7960
52
        || (cp->n_tables != 255 && a->table_id >= cp->n_tables)) {
7961
40
        return OFPERR_OFPBIC_BAD_TABLE_ID;
7962
40
    }
7963
52
    return 0;
7964
92
}
7965

7966
static void
7967
log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
7968
               size_t action_offset, enum ofperr error)
7969
115k
{
7970
115k
    if (!VLOG_DROP_WARN(&rl)) {
7971
0
        struct ds s;
7972
7973
0
        ds_init(&s);
7974
0
        ds_put_hex_dump(&s, actions, actions_len, 0, false);
7975
0
        VLOG_WARN("bad action at offset %"PRIuSIZE" (%s):\n%s", action_offset,
7976
0
                  ofperr_get_name(error), ds_cstr(&s));
7977
0
        ds_destroy(&s);
7978
0
    }
7979
115k
}
7980
7981
static enum ofperr
7982
ofpacts_decode_aligned(struct ofpbuf *openflow, enum ofp_version ofp_version,
7983
                       const struct vl_mff_map *vl_mff_map,
7984
                       uint64_t *ofpacts_tlv_bitmap, struct ofpbuf *ofpacts,
7985
                       size_t *bad_action_offset)
7986
250k
{
7987
250k
    size_t decoded_len = 0;
7988
250k
    enum ofperr error = 0;
7989
7990
250k
    ovs_assert(OFPACT_IS_ALIGNED(openflow->data));
7991
518k
    while (openflow->size) {
7992
        /* Ensure the next action data is properly aligned before decoding it.
7993
         * Some times it's valid to have to decode actions that are not
7994
         * properly aligned (e.g., when processing OF 1.0 statistics reply
7995
         * messages which have a header of 12 bytes - struct ofp10_stats_msg).
7996
         * In other cases the encoder might be buggy.
7997
         */
7998
383k
        if (!OFPACT_IS_ALIGNED(openflow->data)) {
7999
0
            ofpbuf_align(openflow);
8000
0
        }
8001
8002
383k
        const struct ofp_action_header *action = openflow->data;
8003
383k
        enum ofp_raw_action_type raw;
8004
383k
        size_t act_len = 0;
8005
383k
        uint64_t arg;
8006
8007
383k
        error = ofpact_pull_raw(openflow, ofp_version, &raw, &arg, &act_len);
8008
383k
        if (!error) {
8009
331k
            error = ofpact_decode(action, raw, ofp_version, arg, vl_mff_map,
8010
331k
                                  ofpacts_tlv_bitmap, ofpacts);
8011
331k
        }
8012
8013
383k
        if (error) {
8014
115k
            *bad_action_offset = decoded_len;
8015
115k
            goto done;
8016
115k
        }
8017
267k
        decoded_len += act_len;
8018
267k
    }
8019
8020
250k
done:
8021
250k
    return error;
8022
250k
}
8023
8024
static enum ofperr
8025
ofpacts_decode(const void *actions, size_t actions_len,
8026
               enum ofp_version ofp_version,
8027
               const struct vl_mff_map *vl_mff_map,
8028
               uint64_t *ofpacts_tlv_bitmap, struct ofpbuf *ofpacts)
8029
250k
{
8030
250k
    size_t bad_action_offset = 0;
8031
250k
    struct ofpbuf aligned_buf;
8032
8033
250k
    if (!OFPACT_IS_ALIGNED(actions)) {
8034
168k
        ofpbuf_init(&aligned_buf, actions_len);
8035
168k
        ofpbuf_put(&aligned_buf, actions, actions_len);
8036
168k
    } else {
8037
82.4k
        ofpbuf_use_data(&aligned_buf, actions, actions_len);
8038
82.4k
    }
8039
8040
250k
    enum ofperr error
8041
250k
        = ofpacts_decode_aligned(&aligned_buf, ofp_version, vl_mff_map,
8042
250k
                                 ofpacts_tlv_bitmap, ofpacts,
8043
250k
                                 &bad_action_offset);
8044
250k
    if (error) {
8045
115k
        log_bad_action(actions, actions_len, bad_action_offset, error);
8046
115k
    }
8047
250k
    ofpbuf_uninit(&aligned_buf);
8048
250k
    return error;
8049
250k
}
8050
8051
static enum ofperr
8052
ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
8053
                                unsigned int actions_len,
8054
                                enum ofp_version version,
8055
                                uint32_t allowed_ovsinsts,
8056
                                struct ofpbuf *ofpacts,
8057
                                enum ofpact_type outer_action,
8058
                                const struct vl_mff_map *vl_mff_map,
8059
                                uint64_t *ofpacts_tlv_bitmap)
8060
240k
{
8061
240k
    const struct ofp_action_header *actions;
8062
240k
    enum ofperr error;
8063
8064
240k
    if (actions_len % OFP_ACTION_ALIGN != 0) {
8065
15.0k
        VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
8066
15.0k
                     "multiple of %d", actions_len, OFP_ACTION_ALIGN);
8067
15.0k
        return OFPERR_OFPBRC_BAD_LEN;
8068
15.0k
    }
8069
8070
225k
    actions = ofpbuf_try_pull(openflow, actions_len);
8071
225k
    if (actions == NULL) {
8072
3.67k
        VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
8073
3.67k
                     "remaining message length (%"PRIu32")",
8074
3.67k
                     actions_len, openflow->size);
8075
3.67k
        return OFPERR_OFPBRC_BAD_LEN;
8076
3.67k
    }
8077
8078
221k
    error = ofpacts_decode(actions, actions_len, version, vl_mff_map,
8079
221k
                           ofpacts_tlv_bitmap, ofpacts);
8080
221k
    if (!error) {
8081
113k
        error = ofpacts_verify(ofpacts->data, ofpacts->size, version,
8082
113k
                               allowed_ovsinsts, outer_action, NULL);
8083
113k
    }
8084
221k
    if (error) {
8085
119k
        ofpbuf_clear(ofpacts);
8086
119k
    }
8087
221k
    return error;
8088
225k
}
8089
8090
/* Attempts to convert 'actions_len' bytes of OpenFlow actions from the front
8091
 * of 'openflow' into ofpacts.  On success, appends the converted actions to
8092
 * 'ofpacts'; on failure, clears 'ofpacts'.  Returns 0 if successful, otherwise
8093
 * an OpenFlow error.
8094
 *
8095
 * Actions are processed according to their OpenFlow version which
8096
 * is provided in the 'version' parameter.
8097
 *
8098
 * In most places in OpenFlow, actions appear encapsulated in instructions, so
8099
 * you should call ofpacts_pull_openflow_instructions() instead of this
8100
 * function.
8101
 *
8102
 * 'vl_mff_map' and 'ofpacts_tlv_bitmap' are optional. If 'vl_mff_map' is
8103
 * provided, it is used to get variable length mf_fields with configured
8104
 * length in the actions. If an action uses a variable length mf_field,
8105
 * 'ofpacts_tlv_bitmap' is updated accordingly for ref counting. If
8106
 * 'vl_mff_map' is not provided, the default mf_fields with maximum length
8107
 * will be used.
8108
 *
8109
 * The parsed actions are valid generically, but they may not be valid in a
8110
 * specific context.  For example, port numbers up to OFPP_MAX are valid
8111
 * generically, but specific datapaths may only support port numbers in a
8112
 * smaller range.  Use ofpacts_check() to additional check whether actions are
8113
 * valid in a specific context. */
8114
enum ofperr
8115
ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
8116
                              unsigned int actions_len,
8117
                              enum ofp_version version,
8118
                              const struct vl_mff_map *vl_mff_map,
8119
                              uint64_t *ofpacts_tlv_bitmap,
8120
                              struct ofpbuf *ofpacts)
8121
149k
{
8122
149k
    return ofpacts_pull_openflow_actions__(
8123
149k
        openflow, actions_len, version,
8124
149k
        (1u << OVSINST_OFPIT11_APPLY_ACTIONS) | (1u << OVSINST_OFPIT13_METER),
8125
149k
        ofpacts, 0, vl_mff_map, ofpacts_tlv_bitmap);
8126
149k
}
8127

8128
/* OpenFlow 1.1 action sets. */
8129
8130
/* Append ofpact 'a' onto the tail of 'out' */
8131
static void
8132
ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
8133
0
{
8134
0
    ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
8135
0
}
8136
8137
/* The order in which actions in an action set get executed.  This is only for
8138
 * the actions where only the last instance added is used. */
8139
#define ACTION_SET_ORDER                        \
8140
2.42k
    SLOT(OFPACT_STRIP_VLAN)                     \
8141
7.57k
    SLOT(OFPACT_POP_MPLS)                       \
8142
13.4k
    SLOT(OFPACT_DECAP)                          \
8143
13.4k
    SLOT(OFPACT_ENCAP)                          \
8144
2.16k
    SLOT(OFPACT_PUSH_MPLS)                      \
8145
4.48k
    SLOT(OFPACT_PUSH_VLAN)                      \
8146
5.75k
    SLOT(OFPACT_DEC_TTL)                        \
8147
5.75k
    SLOT(OFPACT_DEC_MPLS_TTL)                   \
8148
3.01k
    SLOT(OFPACT_DEC_NSH_TTL)
8149
8150
/* Priority for "final actions" in an action set.  An action set only gets
8151
 * executed at all if at least one of these actions is present.  If more than
8152
 * one is present, then only the one later in this list is executed (and if
8153
 * more than one of a given type, the one later in the action set). */
8154
#define ACTION_SET_FINAL_PRIORITY               \
8155
1.22k
    FINAL(OFPACT_CT)                            \
8156
1.22k
    FINAL(OFPACT_CT_CLEAR)                      \
8157
50
    FINAL(OFPACT_RESUBMIT)                      \
8158
1.81k
    FINAL(OFPACT_OUTPUT)                        \
8159
1.81k
    FINAL(OFPACT_GROUP)
8160
8161
enum action_set_class {
8162
    /* Actions that individually can usefully appear only once in an action
8163
     * set.  If they do appear more than once, then only the last instance is
8164
     * honored. */
8165
#define SLOT(OFPACT) ACTION_SLOT_##OFPACT,
8166
    ACTION_SET_ORDER
8167
#undef SLOT
8168
8169
    /* Final actions. */
8170
#define FINAL(OFPACT) ACTION_SLOT_##OFPACT,
8171
    ACTION_SET_FINAL_PRIORITY
8172
#undef FINAL
8173
8174
    /* Actions that can appear in an action set more than once and are executed
8175
     * in order. */
8176
    ACTION_SLOT_SET_OR_MOVE,
8177
8178
    /* Actions that shouldn't appear in the action set at all. */
8179
    ACTION_SLOT_INVALID
8180
};
8181
8182
/* Count the action set slots. */
8183
#define SLOT(OFPACT) +1
8184
enum { N_ACTION_SLOTS = ACTION_SET_ORDER };
8185
#undef SLOT
8186
8187
static enum action_set_class
8188
action_set_classify(const struct ofpact *a)
8189
55.9k
{
8190
55.9k
    switch (a->type) {
8191
40.8k
#define SLOT(OFPACT) case OFPACT: return ACTION_SLOT_##OFPACT;
8192
0
        ACTION_SET_ORDER
8193
0
#undef SLOT
8194
8195
4.65k
#define FINAL(OFPACT) case OFPACT: return ACTION_SLOT_##OFPACT;
8196
186
        ACTION_SET_FINAL_PRIORITY
8197
0
#undef FINAL
8198
8199
10
    case OFPACT_SET_FIELD:
8200
10
    case OFPACT_REG_MOVE:
8201
104
    case OFPACT_SET_ETH_DST:
8202
115
    case OFPACT_SET_ETH_SRC:
8203
543
    case OFPACT_SET_IP_DSCP:
8204
792
    case OFPACT_SET_IP_ECN:
8205
1.41k
    case OFPACT_SET_IP_TTL:
8206
1.57k
    case OFPACT_SET_IPV4_DST:
8207
2.10k
    case OFPACT_SET_IPV4_SRC:
8208
2.88k
    case OFPACT_SET_L4_DST_PORT:
8209
2.93k
    case OFPACT_SET_L4_SRC_PORT:
8210
3.64k
    case OFPACT_SET_MPLS_LABEL:
8211
4.18k
    case OFPACT_SET_MPLS_TC:
8212
5.09k
    case OFPACT_SET_MPLS_TTL:
8213
5.12k
    case OFPACT_SET_QUEUE:
8214
5.18k
    case OFPACT_SET_TUNNEL:
8215
6.69k
    case OFPACT_SET_VLAN_PCP:
8216
7.42k
    case OFPACT_SET_VLAN_VID:
8217
7.42k
        return ACTION_SLOT_SET_OR_MOVE;
8218
8219
0
    case OFPACT_BUNDLE:
8220
0
    case OFPACT_CLEAR_ACTIONS:
8221
1
    case OFPACT_CLONE:
8222
10
    case OFPACT_NAT:
8223
15
    case OFPACT_CONTROLLER:
8224
15
    case OFPACT_ENQUEUE:
8225
739
    case OFPACT_EXIT:
8226
739
    case OFPACT_UNROLL_XLATE:
8227
1.06k
    case OFPACT_FIN_TIMEOUT:
8228
1.06k
    case OFPACT_GOTO_TABLE:
8229
1.06k
    case OFPACT_LEARN:
8230
1.21k
    case OFPACT_CONJUNCTION:
8231
2.09k
    case OFPACT_METER:
8232
2.09k
    case OFPACT_MULTIPATH:
8233
2.09k
    case OFPACT_NOTE:
8234
2.09k
    case OFPACT_OUTPUT_REG:
8235
2.95k
    case OFPACT_OUTPUT_TRUNC:
8236
2.98k
    case OFPACT_POP_QUEUE:
8237
2.99k
    case OFPACT_SAMPLE:
8238
2.99k
    case OFPACT_STACK_POP:
8239
2.99k
    case OFPACT_STACK_PUSH:
8240
2.99k
    case OFPACT_WRITE_ACTIONS:
8241
2.99k
    case OFPACT_WRITE_METADATA:
8242
2.99k
    case OFPACT_DEBUG_RECIRC:
8243
2.99k
    case OFPACT_DEBUG_SLOW:
8244
2.99k
    case OFPACT_CHECK_PKT_LARGER:
8245
2.99k
    case OFPACT_DELETE_FIELD:
8246
2.99k
        return ACTION_SLOT_INVALID;
8247
8248
0
    default:
8249
0
        OVS_NOT_REACHED();
8250
55.9k
    }
8251
55.9k
}
8252
8253
/* True if an action is allowed in the action set.
8254
 * False otherwise. */
8255
static bool
8256
ofpact_is_allowed_in_actions_set(const struct ofpact *a)
8257
55.9k
{
8258
55.9k
    return action_set_classify(a) != ACTION_SLOT_INVALID;
8259
55.9k
}
8260
8261
/* Reads 'action_set', which contains ofpacts accumulated by
8262
 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
8263
 * executed directly into 'action_list'.  (These names correspond to the
8264
 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
8265
 *
8266
 * In general this involves appending the last instance of each action that is
8267
 * admissible in the action set in the order described in the OpenFlow
8268
 * specification.
8269
 *
8270
 * Exceptions:
8271
 * + output action is only appended if no group action was present in 'in'.
8272
 * + As a simplification all set actions are copied in the order the are
8273
 *   provided in 'in' as many set actions applied to a field has the same
8274
 *   affect as only applying the last action that sets a field and
8275
 *   duplicates are removed by do_xlate_actions().
8276
 *   This has an unwanted side-effect of compsoting multiple
8277
 *   LOAD_REG actions that touch different regions of the same field. */
8278
void
8279
ofpacts_execute_action_set(struct ofpbuf *action_list,
8280
                           const struct ofpbuf *action_set)
8281
0
{
8282
0
    const struct ofpact *slots[N_ACTION_SLOTS] = {NULL, };
8283
8284
0
    struct ofpbuf set_or_move;
8285
0
    ofpbuf_init(&set_or_move, 0);
8286
8287
0
    const struct ofpact *final_action = NULL;
8288
0
    enum action_set_class final_class = 0;
8289
8290
0
    const struct ofpact *cursor;
8291
0
    OFPACT_FOR_EACH (cursor, action_set->data, action_set->size) {
8292
0
        int class = action_set_classify(cursor);
8293
0
        if (class < N_ACTION_SLOTS) {
8294
0
            slots[class] = cursor;
8295
0
        } else if (class < ACTION_SLOT_SET_OR_MOVE) {
8296
0
            if (class >= final_class) {
8297
0
                final_action = cursor;
8298
0
                final_class = class;
8299
0
            }
8300
0
        } else if (class == ACTION_SLOT_SET_OR_MOVE) {
8301
0
            ofpact_copy(&set_or_move, cursor);
8302
0
        } else {
8303
0
            ovs_assert(class == ACTION_SLOT_INVALID);
8304
0
        }
8305
0
    }
8306
8307
0
    if (final_action) {
8308
0
        for (int i = 0; i < N_ACTION_SLOTS; i++) {
8309
0
            if (slots[i]) {
8310
0
                ofpact_copy(action_list, slots[i]);
8311
0
            }
8312
0
        }
8313
0
        ofpbuf_put(action_list, set_or_move.data, set_or_move.size);
8314
0
        ofpact_copy(action_list, final_action);
8315
0
    }
8316
0
    ofpbuf_uninit(&set_or_move);
8317
0
}
8318
8319
8320
static enum ofperr
8321
ofpacts_decode_for_action_set(const struct ofp_action_header *in,
8322
                              size_t n_in, enum ofp_version version,
8323
                              const struct vl_mff_map *vl_mff_map,
8324
                              uint64_t *ofpacts_tlv_bitmap,
8325
                              struct ofpbuf *out)
8326
21.3k
{
8327
21.3k
    enum ofperr error;
8328
21.3k
    struct ofpact *a;
8329
21.3k
    size_t start = out->size;
8330
8331
21.3k
    error = ofpacts_decode(in, n_in, version, vl_mff_map, ofpacts_tlv_bitmap,
8332
21.3k
                           out);
8333
8334
21.3k
    if (error) {
8335
4.56k
        return error;
8336
4.56k
    }
8337
8338
55.9k
    OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
8339
55.9k
        if (!ofpact_is_allowed_in_actions_set(a)) {
8340
2.99k
            VLOG_WARN_RL(&rl, "disallowed action in action set");
8341
2.99k
            return OFPERR_OFPBAC_BAD_TYPE;
8342
2.99k
        }
8343
55.9k
    }
8344
8345
13.8k
    return 0;
8346
16.8k
}
8347

8348
/* OpenFlow 1.1 instructions. */
8349
8350
struct instruction_type_info {
8351
    enum ovs_instruction_type type;
8352
    const char *name;
8353
};
8354
8355
static const struct instruction_type_info inst_info[] = {
8356
#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)    {OVSINST_##ENUM, NAME},
8357
OVS_INSTRUCTIONS
8358
#undef DEFINE_INST
8359
};
8360
8361
const char *
8362
ovs_instruction_name_from_type(enum ovs_instruction_type type)
8363
23.9k
{
8364
23.9k
    return type < ARRAY_SIZE(inst_info) ? inst_info[type].name : NULL;
8365
23.9k
}
8366
8367
int
8368
ovs_instruction_type_from_name(const char *name)
8369
0
{
8370
0
    const struct instruction_type_info *p;
8371
0
    for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
8372
0
        if (!strcasecmp(name, p->name)) {
8373
0
            return p->type;
8374
0
        }
8375
0
    }
8376
0
    return -1;
8377
0
}
8378
8379
enum ovs_instruction_type
8380
ovs_instruction_type_from_ofpact_type(enum ofpact_type type,
8381
                                      enum ofp_version version)
8382
496k
{
8383
496k
    switch (type) {
8384
1.75k
    case OFPACT_METER:
8385
1.75k
        return (version >= OFP15_VERSION
8386
1.75k
                ? OVSINST_OFPIT11_APPLY_ACTIONS
8387
1.75k
                : OVSINST_OFPIT13_METER);
8388
99
    case OFPACT_CLEAR_ACTIONS:
8389
99
        return OVSINST_OFPIT11_CLEAR_ACTIONS;
8390
13.9k
    case OFPACT_WRITE_ACTIONS:
8391
13.9k
        return OVSINST_OFPIT11_WRITE_ACTIONS;
8392
1.67k
    case OFPACT_WRITE_METADATA:
8393
1.67k
        return OVSINST_OFPIT11_WRITE_METADATA;
8394
110
    case OFPACT_GOTO_TABLE:
8395
110
        return OVSINST_OFPIT11_GOTO_TABLE;
8396
186k
    case OFPACT_OUTPUT:
8397
186k
    case OFPACT_GROUP:
8398
194k
    case OFPACT_CLONE:
8399
206k
    case OFPACT_CONTROLLER:
8400
207k
    case OFPACT_ENQUEUE:
8401
209k
    case OFPACT_OUTPUT_REG:
8402
210k
    case OFPACT_OUTPUT_TRUNC:
8403
213k
    case OFPACT_BUNDLE:
8404
221k
    case OFPACT_SET_VLAN_VID:
8405
227k
    case OFPACT_SET_VLAN_PCP:
8406
237k
    case OFPACT_STRIP_VLAN:
8407
239k
    case OFPACT_PUSH_VLAN:
8408
240k
    case OFPACT_SET_ETH_SRC:
8409
240k
    case OFPACT_SET_ETH_DST:
8410
244k
    case OFPACT_SET_IPV4_SRC:
8411
251k
    case OFPACT_SET_IPV4_DST:
8412
258k
    case OFPACT_SET_IP_DSCP:
8413
259k
    case OFPACT_SET_IP_ECN:
8414
261k
    case OFPACT_SET_IP_TTL:
8415
270k
    case OFPACT_SET_L4_SRC_PORT:
8416
283k
    case OFPACT_SET_L4_DST_PORT:
8417
284k
    case OFPACT_REG_MOVE:
8418
331k
    case OFPACT_SET_FIELD:
8419
331k
    case OFPACT_STACK_PUSH:
8420
334k
    case OFPACT_STACK_POP:
8421
341k
    case OFPACT_DEC_TTL:
8422
346k
    case OFPACT_SET_MPLS_LABEL:
8423
347k
    case OFPACT_SET_MPLS_TC:
8424
350k
    case OFPACT_SET_MPLS_TTL:
8425
351k
    case OFPACT_DEC_MPLS_TTL:
8426
352k
    case OFPACT_PUSH_MPLS:
8427
353k
    case OFPACT_POP_MPLS:
8428
356k
    case OFPACT_SET_TUNNEL:
8429
357k
    case OFPACT_SET_QUEUE:
8430
358k
    case OFPACT_POP_QUEUE:
8431
361k
    case OFPACT_FIN_TIMEOUT:
8432
365k
    case OFPACT_RESUBMIT:
8433
423k
    case OFPACT_LEARN:
8434
423k
    case OFPACT_CONJUNCTION:
8435
427k
    case OFPACT_MULTIPATH:
8436
433k
    case OFPACT_NOTE:
8437
439k
    case OFPACT_EXIT:
8438
443k
    case OFPACT_UNROLL_XLATE:
8439
446k
    case OFPACT_SAMPLE:
8440
447k
    case OFPACT_DEBUG_RECIRC:
8441
447k
    case OFPACT_DEBUG_SLOW:
8442
466k
    case OFPACT_CT:
8443
468k
    case OFPACT_CT_CLEAR:
8444
470k
    case OFPACT_NAT:
8445
473k
    case OFPACT_ENCAP:
8446
476k
    case OFPACT_DECAP:
8447
477k
    case OFPACT_DEC_NSH_TTL:
8448
478k
    case OFPACT_CHECK_PKT_LARGER:
8449
478k
    case OFPACT_DELETE_FIELD:
8450
478k
    default:
8451
478k
        return OVSINST_OFPIT11_APPLY_ACTIONS;
8452
496k
    }
8453
496k
}
8454
8455
enum ofperr
8456
ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
8457
                                    const uint16_t inst_type)
8458
2.95k
{
8459
2.95k
    switch (inst_type) {
8460
8461
0
#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
8462
773
    case ENUM:                                      \
8463
773
        *instruction_type = OVSINST_##ENUM;         \
8464
773
        return 0;
8465
0
OVS_INSTRUCTIONS
8466
0
#undef DEFINE_INST
8467
8468
2.17k
    default:
8469
2.17k
        return OFPERR_OFPBIC_UNKNOWN_INST;
8470
2.95k
    }
8471
2.95k
}
8472
8473
/* Two-way translation between OVS's internal "OVSINST_*" representation of
8474
 * instructions and the "OFPIT_*" representation used in OpenFlow. */
8475
struct ovsinst_map {
8476
    enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
8477
    int ofpit;                         /* OFPIT_* number from OpenFlow spec. */
8478
};
8479
8480
static const struct ovsinst_map *
8481
get_ovsinst_map(enum ofp_version version)
8482
22.3k
{
8483
    /* OpenFlow 1.1, 1.2, and 1.5 instructions. */
8484
22.3k
    static const struct ovsinst_map of11[] = {
8485
22.3k
        { OVSINST_OFPIT11_GOTO_TABLE, 1 },
8486
22.3k
        { OVSINST_OFPIT11_WRITE_METADATA, 2 },
8487
22.3k
        { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
8488
22.3k
        { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
8489
22.3k
        { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
8490
22.3k
        { 0, -1 },
8491
22.3k
    };
8492
8493
    /* OpenFlow 1.3 and 1.4 instructions. */
8494
22.3k
    static const struct ovsinst_map of13[] = {
8495
22.3k
        { OVSINST_OFPIT11_GOTO_TABLE, 1 },
8496
22.3k
        { OVSINST_OFPIT11_WRITE_METADATA, 2 },
8497
22.3k
        { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
8498
22.3k
        { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
8499
22.3k
        { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
8500
22.3k
        { OVSINST_OFPIT13_METER, 6 },
8501
22.3k
        { 0, -1 },
8502
22.3k
    };
8503
8504
22.3k
    return version == OFP13_VERSION || version == OFP14_VERSION ? of13 : of11;
8505
22.3k
}
8506
8507
/* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
8508
 * values, into a bitmap of instructions suitable for OpenFlow 'version'
8509
 * (OFP11_VERSION or later), and returns the result. */
8510
ovs_be32
8511
ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
8512
0
{
8513
0
    uint32_t ofpit_bitmap = 0;
8514
0
    const struct ovsinst_map *x;
8515
8516
0
    for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
8517
0
        if (ovsinst_bitmap & (1u << x->ovsinst)) {
8518
0
            ofpit_bitmap |= 1u << x->ofpit;
8519
0
        }
8520
0
    }
8521
0
    return htonl(ofpit_bitmap);
8522
0
}
8523
8524
/* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
8525
 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
8526
 * correspond to OVSINST_* values, and returns the result. */
8527
uint32_t
8528
ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
8529
22.3k
{
8530
22.3k
    uint32_t ovsinst_bitmap = 0;
8531
22.3k
    const struct ovsinst_map *x;
8532
8533
134k
    for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
8534
111k
        if (ofpit_bitmap & htonl(1u << x->ofpit)) {
8535
30.8k
            ovsinst_bitmap |= 1u << x->ovsinst;
8536
30.8k
        }
8537
111k
    }
8538
22.3k
    return ovsinst_bitmap;
8539
22.3k
}
8540
8541
static inline struct ofp11_instruction *
8542
instruction_next(const struct ofp11_instruction *inst)
8543
30.1k
{
8544
30.1k
    return ((struct ofp11_instruction *) (void *)
8545
30.1k
            ((uint8_t *) inst + ntohs(inst->len)));
8546
30.1k
}
8547
8548
static inline bool
8549
instruction_is_valid(const struct ofp11_instruction *inst,
8550
                     size_t n_instructions)
8551
37.1k
{
8552
37.1k
    uint16_t len = ntohs(inst->len);
8553
37.1k
    return (!(len % OFP11_INSTRUCTION_ALIGN)
8554
32.9k
            && len >= sizeof *inst
8555
32.8k
            && len / sizeof *inst <= n_instructions);
8556
37.1k
}
8557
8558
/* This macro is careful to check for instructions with bad lengths. */
8559
#define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS)  \
8560
50.2k
    for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS);            \
8561
80.3k
         (LEFT) > 0 && instruction_is_valid(ITER, LEFT);                \
8562
50.2k
         ((LEFT) -= (ntohs((ITER)->len)                                 \
8563
30.1k
                     / sizeof(struct ofp11_instruction)),               \
8564
30.1k
          (ITER) = instruction_next(ITER)))
8565
8566
static enum ofperr
8567
decode_openflow11_instruction(const struct ofp11_instruction *inst,
8568
                              enum ovs_instruction_type *type)
8569
32.4k
{
8570
32.4k
    uint16_t len = ntohs(inst->len);
8571
8572
32.4k
    switch (inst->type) {
8573
175
    case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
8574
175
        return OFPERR_OFPBIC_BAD_EXPERIMENTER;
8575
8576
0
#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)     \
8577
30.8k
        case CONSTANT_HTONS(ENUM):                      \
8578
30.8k
            if (EXTENSIBLE                              \
8579
30.8k
                ? len >= sizeof(struct STRUCT)          \
8580
30.8k
                : len == sizeof(struct STRUCT)) {       \
8581
30.5k
                *type = OVSINST_##ENUM;                 \
8582
30.5k
                return 0;                               \
8583
30.5k
            } else {                                    \
8584
223
                return OFPERR_OFPBIC_BAD_LEN;           \
8585
223
            }
8586
175
OVS_INSTRUCTIONS
8587
0
#undef DEFINE_INST
8588
8589
1.42k
    default:
8590
1.42k
        return OFPERR_OFPBIC_UNKNOWN_INST;
8591
32.4k
    }
8592
32.4k
}
8593
8594
static enum ofperr
8595
decode_openflow11_instructions(const struct ofp11_instruction insts[],
8596
                               size_t n_insts, enum ofp_version version,
8597
                               const struct ofp11_instruction *out[])
8598
50.2k
{
8599
50.2k
    const struct ofp11_instruction *inst;
8600
50.2k
    size_t left;
8601
8602
50.2k
    memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
8603
50.2k
    INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
8604
32.4k
        enum ovs_instruction_type type;
8605
32.4k
        enum ofperr error;
8606
8607
32.4k
        error = decode_openflow11_instruction(inst, &type);
8608
32.4k
        if (error) {
8609
1.82k
            return error;
8610
1.82k
        }
8611
8612
30.5k
        if (type == OVSINST_OFPIT13_METER && version >= OFP15_VERSION) {
8613
            /* "meter" is an action, not an instruction, in OpenFlow 1.5. */
8614
236
            return OFPERR_OFPBIC_UNKNOWN_INST;
8615
236
        }
8616
8617
30.3k
        if (out[type]) {
8618
192
            return OFPERR_OFPBIC_DUP_INST;
8619
192
        }
8620
30.1k
        out[type] = inst;
8621
30.1k
    }
8622
8623
47.9k
    if (left) {
8624
4.72k
        VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
8625
4.72k
                     (n_insts - left) * sizeof *inst);
8626
4.72k
        return OFPERR_OFPBIC_BAD_LEN;
8627
4.72k
    }
8628
43.2k
    return 0;
8629
47.9k
}
8630
8631
static void
8632
get_actions_from_instruction(const struct ofp11_instruction *inst,
8633
                             const struct ofp_action_header **actions,
8634
                             size_t *actions_len)
8635
28.5k
{
8636
28.5k
    *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
8637
28.5k
    *actions_len = ntohs(inst->len) - sizeof *inst;
8638
28.5k
}
8639
8640
enum ofperr
8641
ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
8642
                                   unsigned int instructions_len,
8643
                                   enum ofp_version version,
8644
                                   const struct vl_mff_map *vl_mff_map,
8645
                                   uint64_t *ofpacts_tlv_bitmap,
8646
                                   struct ofpbuf *ofpacts)
8647
131k
{
8648
131k
    const struct ofp11_instruction *instructions;
8649
131k
    const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
8650
131k
    enum ofperr error;
8651
8652
131k
    ofpbuf_clear(ofpacts);
8653
131k
    if (version == OFP10_VERSION) {
8654
79.1k
        return ofpacts_pull_openflow_actions__(openflow, instructions_len,
8655
79.1k
                                               version,
8656
79.1k
                                               (1u << N_OVS_INSTRUCTIONS) - 1,
8657
79.1k
                                               ofpacts, 0, vl_mff_map,
8658
79.1k
                                               ofpacts_tlv_bitmap);
8659
79.1k
    }
8660
8661
52.0k
    if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
8662
1.00k
        VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
8663
1.00k
                     "multiple of %d",
8664
1.00k
                     instructions_len, OFP11_INSTRUCTION_ALIGN);
8665
1.00k
        error = OFPERR_OFPBIC_BAD_LEN;
8666
1.00k
        goto exit;
8667
1.00k
    }
8668
8669
51.0k
    instructions = ofpbuf_try_pull(openflow, instructions_len);
8670
51.0k
    if (instructions == NULL) {
8671
798
        VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
8672
798
                     "remaining message length (%"PRIu32")",
8673
798
                     instructions_len, openflow->size);
8674
798
        error = OFPERR_OFPBIC_BAD_LEN;
8675
798
        goto exit;
8676
798
    }
8677
8678
50.2k
    error = decode_openflow11_instructions(
8679
50.2k
        instructions, instructions_len / OFP11_INSTRUCTION_ALIGN, version,
8680
50.2k
        insts);
8681
50.2k
    if (error) {
8682
6.97k
        goto exit;
8683
6.97k
    }
8684
8685
43.2k
    if (insts[OVSINST_OFPIT13_METER]) {
8686
119
        const struct ofp13_instruction_meter *oim;
8687
119
        struct ofpact_meter *om;
8688
8689
119
        oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
8690
119
                           insts[OVSINST_OFPIT13_METER]);
8691
8692
119
        om = ofpact_put_METER(ofpacts);
8693
119
        om->meter_id = ntohl(oim->meter_id);
8694
119
        om->provider_meter_id = UINT32_MAX; /* No provider meter ID. */
8695
119
    }
8696
43.2k
    if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
8697
7.20k
        const struct ofp_action_header *actions;
8698
7.20k
        size_t actions_len;
8699
8700
7.20k
        get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
8701
7.20k
                                     &actions, &actions_len);
8702
7.20k
        error = ofpacts_decode(actions, actions_len, version, vl_mff_map,
8703
7.20k
                               ofpacts_tlv_bitmap, ofpacts);
8704
7.20k
        if (error) {
8705
2.57k
            goto exit;
8706
2.57k
        }
8707
7.20k
    }
8708
40.6k
    if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
8709
80
        instruction_get_OFPIT11_CLEAR_ACTIONS(
8710
80
            insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
8711
80
        ofpact_put_CLEAR_ACTIONS(ofpacts);
8712
80
    }
8713
40.6k
    if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
8714
21.3k
        struct ofpact_nest *on;
8715
21.3k
        const struct ofp_action_header *actions;
8716
21.3k
        size_t actions_len;
8717
21.3k
        size_t start = ofpacts->size;
8718
21.3k
        ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
8719
21.3k
                   offsetof(struct ofpact_nest, actions));
8720
21.3k
        get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
8721
21.3k
                                     &actions, &actions_len);
8722
21.3k
        error = ofpacts_decode_for_action_set(actions, actions_len,
8723
21.3k
                                              version, vl_mff_map,
8724
21.3k
                                              ofpacts_tlv_bitmap, ofpacts);
8725
21.3k
        if (error) {
8726
7.55k
            goto exit;
8727
7.55k
        }
8728
13.8k
        on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
8729
13.8k
        on->ofpact.len = ofpacts->size - start;
8730
13.8k
    }
8731
33.0k
    if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
8732
163
        const struct ofp11_instruction_write_metadata *oiwm;
8733
163
        struct ofpact_metadata *om;
8734
8735
163
        oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
8736
163
                            insts[OVSINST_OFPIT11_WRITE_METADATA]);
8737
8738
163
        om = ofpact_put_WRITE_METADATA(ofpacts);
8739
163
        om->metadata = oiwm->metadata;
8740
163
        om->mask = oiwm->metadata_mask;
8741
163
    }
8742
33.0k
    if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
8743
89
        const struct ofp11_instruction_goto_table *oigt;
8744
89
        struct ofpact_goto_table *ogt;
8745
8746
89
        oigt = instruction_get_OFPIT11_GOTO_TABLE(
8747
89
            insts[OVSINST_OFPIT11_GOTO_TABLE]);
8748
89
        ogt = ofpact_put_GOTO_TABLE(ofpacts);
8749
89
        ogt->table_id = oigt->table_id;
8750
89
    }
8751
8752
33.0k
    error = ofpacts_verify(ofpacts->data, ofpacts->size, version,
8753
33.0k
                           (1u << N_OVS_INSTRUCTIONS) - 1, 0, NULL);
8754
52.0k
exit:
8755
52.0k
    if (error) {
8756
19.0k
        ofpbuf_clear(ofpacts);
8757
19.0k
    }
8758
52.0k
    return error;
8759
33.0k
}
8760
8761
/* Update the length of the instruction that begins at offset 'ofs' within
8762
 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
8763
 * If the instruction contains no nested actions, deletes it entirely. */
8764
static void
8765
ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
8766
581
{
8767
581
    struct ofp11_instruction_actions *oia;
8768
8769
581
    oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
8770
581
    if (openflow->size > ofs + sizeof *oia) {
8771
577
        oia->len = htons(openflow->size - ofs);
8772
577
    } else {
8773
4
        openflow->size = ofs;
8774
4
    }
8775
581
}
8776

8777
/* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
8778
 * switch will never have more than 'max_ports' ports.  Returns 0 if 'port' is
8779
 * valid, otherwise an OpenFlow error code. */
8780
enum ofperr
8781
ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
8782
206k
{
8783
206k
    switch (port) {
8784
4.38k
    case OFPP_IN_PORT:
8785
9.75k
    case OFPP_TABLE:
8786
10.6k
    case OFPP_NORMAL:
8787
15.2k
    case OFPP_FLOOD:
8788
18.3k
    case OFPP_ALL:
8789
19.3k
    case OFPP_CONTROLLER:
8790
19.8k
    case OFPP_LOCAL:
8791
19.8k
        return 0;
8792
8793
30
    case OFPP_NONE:
8794
30
        return OFPERR_OFPBAC_BAD_OUT_PORT;
8795
8796
186k
    default:
8797
186k
        if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
8798
185k
            return 0;
8799
185k
        }
8800
386
        return OFPERR_OFPBAC_BAD_OUT_PORT;
8801
206k
    }
8802
206k
}
8803
8804
/* Removes the protocols that require consistency between match and actions
8805
 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
8806
 *
8807
 * (An example of an inconsistency between match and actions is a flow that
8808
 * does not match on an MPLS Ethertype but has an action that pops an MPLS
8809
 * label.) */
8810
static void
8811
inconsistent_match(enum ofputil_protocol *usable_protocols)
8812
44.7k
{
8813
44.7k
    *usable_protocols &= OFPUTIL_P_OF10_ANY;
8814
44.7k
}
8815
8816
/* May modify flow->packet_type, flow->dl_type, flow->nw_proto and
8817
 * flow->vlan_tci, caller must restore them.
8818
 *
8819
 * Modifies some actions, filling in fields that could not be properly set
8820
 * without context. */
8821
static enum ofperr
8822
ofpact_check__(struct ofpact *a, struct ofpact_check_params *cp)
8823
420k
{
8824
420k
    switch (a->type) {
8825
0
#define OFPACT(ENUM, STRUCT, MEMBER, NAME)                  \
8826
420k
        case OFPACT_##ENUM:                                 \
8827
420k
            return check_##ENUM(ofpact_get_##ENUM(a), cp);
8828
0
        OFPACTS
8829
0
#undef OFPACT
8830
0
    default:
8831
0
        OVS_NOT_REACHED();
8832
420k
    }
8833
420k
}
8834
8835
/* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
8836
 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
8837
 * switch with no more than 'max_ports' ports.
8838
 *
8839
 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
8840
 * '*usable_protocols' the protocols that forbid the inconsistency.  (An
8841
 * example of an inconsistency between match and actions is a flow that does
8842
 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
8843
 *
8844
 * May annotate ofpacts with information gathered from the 'match'.
8845
 *
8846
 * May temporarily modify 'match', but restores the changes before
8847
 * returning. */
8848
enum ofperr
8849
ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
8850
              struct ofpact_check_params *cp)
8851
101k
{
8852
    /* Save fields that might temporarily be modified. */
8853
101k
    struct flow *flow = &cp->match->flow;
8854
101k
    ovs_be32 packet_type = flow->packet_type;
8855
101k
    ovs_be16 dl_type = flow->dl_type;
8856
101k
    uint8_t nw_proto = flow->nw_proto;
8857
101k
    union flow_vlan_hdr vlans[FLOW_MAX_VLAN_HEADERS];
8858
101k
    memcpy(vlans, flow->vlans, sizeof vlans);
8859
8860
    /* Check all the actions. */
8861
101k
    cp->usable_protocols = OFPUTIL_P_ANY;
8862
101k
    enum ofperr error = 0;
8863
101k
    struct ofpact *a;
8864
420k
    OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
8865
420k
        error = ofpact_check__(a, cp);
8866
420k
        if (error) {
8867
7.78k
            break;
8868
7.78k
        }
8869
420k
    }
8870
8871
    /* Restore fields that may have been modified. */
8872
101k
    flow->packet_type = packet_type;
8873
101k
    flow->dl_type = dl_type;
8874
101k
    memcpy(flow->vlans, vlans, sizeof vlans);
8875
101k
    flow->nw_proto = nw_proto;
8876
8877
101k
    return error;
8878
101k
}
8879
8880
/* Like ofpacts_check(), but reports inconsistencies as
8881
 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
8882
enum ofperr
8883
ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
8884
                          enum ofputil_protocol needed_protocols,
8885
                          struct ofpact_check_params *cp)
8886
62.6k
{
8887
62.6k
    enum ofperr error = ofpacts_check(ofpacts, ofpacts_len, cp);
8888
62.6k
    if (!error && needed_protocols & ~cp->usable_protocols) {
8889
1.80k
        return OFPERR_OFPBAC_MATCH_INCONSISTENT;
8890
1.80k
    }
8891
60.8k
    return error;
8892
62.6k
}
8893
8894
/* Returns the destination field that 'ofpact' would write to, or NULL
8895
 * if the action would not write to an mf_field. */
8896
const struct mf_field *
8897
ofpact_get_mf_dst(const struct ofpact *ofpact)
8898
456k
{
8899
456k
    if (ofpact->type == OFPACT_SET_FIELD) {
8900
41.4k
        const struct ofpact_set_field *orl;
8901
8902
41.4k
        orl = CONTAINER_OF(ofpact, struct ofpact_set_field, ofpact);
8903
41.4k
        return orl->field;
8904
415k
    } else if (ofpact->type == OFPACT_REG_MOVE) {
8905
1.04k
        const struct ofpact_reg_move *orm;
8906
8907
1.04k
        orm = CONTAINER_OF(ofpact, struct ofpact_reg_move, ofpact);
8908
1.04k
        return orm->dst.field;
8909
1.04k
    }
8910
8911
414k
    return NULL;
8912
456k
}
8913
8914
static void OVS_PRINTF_FORMAT(2, 3)
8915
verify_error(char **errorp, const char *format, ...)
8916
11.3k
{
8917
11.3k
    va_list args;
8918
11.3k
    va_start(args, format);
8919
11.3k
    char *error = xvasprintf(format, args);
8920
11.3k
    va_end(args);
8921
8922
11.3k
    if (errorp) {
8923
233
        *errorp = error;
8924
11.0k
    } else {
8925
11.0k
        VLOG_WARN("%s", error);
8926
11.0k
        free(error);
8927
11.0k
    }
8928
11.3k
}
8929
8930
static enum ofperr
8931
unsupported_nesting(enum ofpact_type action, enum ofpact_type outer_action,
8932
                    char **errorp)
8933
546
{
8934
546
    verify_error(errorp, "%s action doesn't support nested action %s",
8935
546
                 ofpact_name(outer_action), ofpact_name(action));
8936
546
    return OFPERR_OFPBAC_BAD_ARGUMENT;
8937
546
}
8938
8939
static bool
8940
field_requires_ct(enum mf_field_id field)
8941
44.7k
{
8942
44.7k
    return field == MFF_CT_MARK || field == MFF_CT_LABEL;
8943
44.7k
}
8944
8945
/* Apply nesting constraints for actions */
8946
static enum ofperr
8947
ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action,
8948
                      char **errorp)
8949
456k
{
8950
456k
    const struct mf_field *field = ofpact_get_mf_dst(a);
8951
8952
456k
    if (field && field_requires_ct(field->id) && outer_action != OFPACT_CT) {
8953
28
        verify_error(errorp, "cannot set CT fields outside of ct action");
8954
28
        return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
8955
28
    }
8956
456k
    if (a->type == OFPACT_NAT) {
8957
9.38k
        if (outer_action != OFPACT_CT) {
8958
6.95k
            verify_error(errorp,
8959
6.95k
                         "Cannot have NAT action outside of \"ct\" action");
8960
6.95k
            return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
8961
6.95k
        }
8962
2.43k
        return 0;
8963
9.38k
    }
8964
8965
447k
    if (outer_action) {
8966
70.3k
        ovs_assert(outer_action == OFPACT_WRITE_ACTIONS
8967
67.4k
                   || outer_action == OFPACT_CT
8968
64.6k
                   || outer_action == OFPACT_CLONE);
8969
8970
70.3k
        if (outer_action == OFPACT_CT) {
8971
2.76k
            if (!field) {
8972
542
                return unsupported_nesting(a->type, outer_action, errorp);
8973
2.22k
            } else if (!field_requires_ct(field->id)) {
8974
717
                verify_error(errorp,
8975
717
                             "%s action doesn't support nested modification "
8976
717
                             "of %s", ofpact_name(outer_action), field->name);
8977
717
                return OFPERR_OFPBAC_BAD_ARGUMENT;
8978
717
            }
8979
2.76k
        }
8980
8981
69.0k
        if (a->type == OFPACT_METER) {
8982
4
            return unsupported_nesting(a->type, outer_action, errorp);
8983
4
        }
8984
69.0k
    }
8985
8986
446k
    return 0;
8987
447k
}
8988
8989
/* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
8990
 * appropriate order as defined by the OpenFlow spec and as required by Open
8991
 * vSwitch.
8992
 *
8993
 * The 'version' is relevant only for error reporting: Open vSwitch enforces
8994
 * the same rules for every version of OpenFlow, but different versions require
8995
 * different error codes.
8996
 *
8997
 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
8998
 * instructions that are allowed within 'ofpacts[]'.
8999
 *
9000
 * If 'outer_action' is not zero, it specifies that the actions are nested
9001
 * within another action of type 'outer_action'. */
9002
static enum ofperr
9003
ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
9004
               enum ofp_version version, uint32_t allowed_ovsinsts,
9005
               enum ofpact_type outer_action, char **errorp)
9006
162k
{
9007
162k
    const struct ofpact *a;
9008
162k
    enum ovs_instruction_type inst;
9009
9010
162k
    inst = OVSINST_OFPIT13_METER;
9011
460k
    OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
9012
460k
        enum ovs_instruction_type next;
9013
460k
        enum ofperr error;
9014
9015
460k
        if (a->type == OFPACT_CONJUNCTION) {
9016
7.46k
            OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
9017
7.46k
                if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
9018
2.13k
                    verify_error(errorp, "\"conjunction\" actions may be used "
9019
2.13k
                                 "along with \"note\" but not any other kind "
9020
2.13k
                                 "of action (such as the \"%s\" action used "
9021
2.13k
                                 "here)", ofpact_name(a->type));
9022
2.13k
                    return OFPERR_NXBAC_BAD_CONJUNCTION;
9023
2.13k
                }
9024
7.46k
            }
9025
1.79k
            return 0;
9026
3.92k
        }
9027
9028
456k
        error = ofpacts_verify_nested(a, outer_action, errorp);
9029
456k
        if (error) {
9030
8.24k
            return error;
9031
8.24k
        }
9032
9033
448k
        next = ovs_instruction_type_from_ofpact_type(a->type, version);
9034
448k
        if (a > ofpacts
9035
344k
            && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
9036
344k
                ? next < inst
9037
344k
                : next <= inst)) {
9038
931
            const char *name = ovs_instruction_name_from_type(inst);
9039
931
            const char *next_name = ovs_instruction_name_from_type(next);
9040
9041
931
            if (next == inst) {
9042
618
                verify_error(errorp, "duplicate %s instruction not allowed, "
9043
618
                             "for OpenFlow 1.1+ compatibility", name);
9044
618
            } else {
9045
313
                verify_error(errorp, "invalid instruction ordering: "
9046
313
                             "%s must appear before %s, "
9047
313
                             "for OpenFlow 1.1+ compatibility",
9048
313
                             next_name, name);
9049
313
            }
9050
931
            return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
9051
931
        }
9052
447k
        if (!((1u << next) & allowed_ovsinsts)) {
9053
17
            const char *name = ovs_instruction_name_from_type(next);
9054
9055
17
            if (next == OVSINST_OFPIT13_METER && version >= OFP15_VERSION) {
9056
0
                verify_error(errorp, "%s action not allowed here", name);
9057
0
                return OFPERR_OFPBAC_BAD_TYPE;
9058
17
            } else {
9059
17
                verify_error(errorp, "%s instruction not allowed here", name);
9060
17
                return OFPERR_OFPBIC_UNSUP_INST;
9061
17
            }
9062
17
        }
9063
9064
447k
        inst = next;
9065
447k
    }
9066
9067
149k
    return 0;
9068
162k
}
9069

9070
/* Converting ofpacts to OpenFlow. */
9071
9072
static void
9073
encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
9074
              struct ofpbuf *out)
9075
261k
{
9076
261k
    switch (a->type) {
9077
0
#define OFPACT(ENUM, STRUCT, MEMBER, NAME)                              \
9078
261k
        case OFPACT_##ENUM:                                             \
9079
261k
            encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out);      \
9080
261k
            return;
9081
0
        OFPACTS
9082
0
#undef OFPACT
9083
0
    default:
9084
0
        OVS_NOT_REACHED();
9085
261k
    }
9086
261k
}
9087
9088
/* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
9089
 * actions in 'openflow', appending the actions to any existing data in
9090
 * 'openflow'. */
9091
size_t
9092
ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
9093
                             struct ofpbuf *openflow,
9094
                             enum ofp_version ofp_version)
9095
26.9k
{
9096
26.9k
    const struct ofpact *a;
9097
26.9k
    size_t start_size = openflow->size;
9098
9099
214k
    OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
9100
214k
        encode_ofpact(a, ofp_version, openflow);
9101
214k
    }
9102
26.9k
    return openflow->size - start_size;
9103
26.9k
}
9104
9105
static enum ovs_instruction_type
9106
ofpact_is_apply_actions(const struct ofpact *a, enum ofp_version version)
9107
47.5k
{
9108
47.5k
    return (ovs_instruction_type_from_ofpact_type(a->type, version)
9109
47.5k
            == OVSINST_OFPIT11_APPLY_ACTIONS);
9110
47.5k
}
9111
9112
void
9113
ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
9114
                                  size_t ofpacts_len,
9115
                                  struct ofpbuf *openflow,
9116
                                  enum ofp_version ofp_version)
9117
716
{
9118
716
    const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
9119
716
    const struct ofpact *a;
9120
9121
716
    if (ofp_version == OFP10_VERSION) {
9122
0
        ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
9123
0
                                     ofp_version);
9124
0
        return;
9125
0
    }
9126
9127
716
    a = ofpacts;
9128
1.44k
    while (a < end) {
9129
725
        if (ofpact_is_apply_actions(a, ofp_version)) {
9130
543
            size_t ofs = openflow->size;
9131
9132
543
            instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
9133
47.3k
            do {
9134
47.3k
                encode_ofpact(a, ofp_version, openflow);
9135
47.3k
                a = ofpact_next(a);
9136
47.3k
            } while (a < end && ofpact_is_apply_actions(a, ofp_version));
9137
543
            ofpacts_update_instruction_actions(openflow, ofs);
9138
543
        } else {
9139
182
            encode_ofpact(a, ofp_version, openflow);
9140
182
            a = ofpact_next(a);
9141
182
        }
9142
725
    }
9143
716
}
9144

9145
/* Sets of supported actions. */
9146
9147
/* Two-way translation between OVS's internal "OFPACT_*" representation of
9148
 * actions and the "OFPAT_*" representation used in some OpenFlow version.
9149
 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
9150
 * instance is specific to one OpenFlow version.) */
9151
struct ofpact_map {
9152
    enum ofpact_type ofpact;    /* Internal name for action type. */
9153
    int ofpat;                  /* OFPAT_* number from OpenFlow spec. */
9154
};
9155
9156
static const struct ofpact_map *
9157
get_ofpact_map(enum ofp_version version)
9158
50.8k
{
9159
    /* OpenFlow 1.0 actions. */
9160
50.8k
    static const struct ofpact_map of10[] = {
9161
50.8k
        { OFPACT_OUTPUT, 0 },
9162
50.8k
        { OFPACT_SET_VLAN_VID, 1 },
9163
50.8k
        { OFPACT_SET_VLAN_PCP, 2 },
9164
50.8k
        { OFPACT_STRIP_VLAN, 3 },
9165
50.8k
        { OFPACT_SET_ETH_SRC, 4 },
9166
50.8k
        { OFPACT_SET_ETH_DST, 5 },
9167
50.8k
        { OFPACT_SET_IPV4_SRC, 6 },
9168
50.8k
        { OFPACT_SET_IPV4_DST, 7 },
9169
50.8k
        { OFPACT_SET_IP_DSCP, 8 },
9170
50.8k
        { OFPACT_SET_L4_SRC_PORT, 9 },
9171
50.8k
        { OFPACT_SET_L4_DST_PORT, 10 },
9172
50.8k
        { OFPACT_ENQUEUE, 11 },
9173
50.8k
        { 0, -1 },
9174
50.8k
    };
9175
9176
    /* OpenFlow 1.1 actions. */
9177
50.8k
    static const struct ofpact_map of11[] = {
9178
50.8k
        { OFPACT_OUTPUT, 0 },
9179
50.8k
        { OFPACT_SET_VLAN_VID, 1 },
9180
50.8k
        { OFPACT_SET_VLAN_PCP, 2 },
9181
50.8k
        { OFPACT_SET_ETH_SRC, 3 },
9182
50.8k
        { OFPACT_SET_ETH_DST, 4 },
9183
50.8k
        { OFPACT_SET_IPV4_SRC, 5 },
9184
50.8k
        { OFPACT_SET_IPV4_DST, 6 },
9185
50.8k
        { OFPACT_SET_IP_DSCP, 7 },
9186
50.8k
        { OFPACT_SET_IP_ECN, 8 },
9187
50.8k
        { OFPACT_SET_L4_SRC_PORT, 9 },
9188
50.8k
        { OFPACT_SET_L4_DST_PORT, 10 },
9189
        /* OFPAT_COPY_TTL_OUT (11) not supported. */
9190
        /* OFPAT_COPY_TTL_IN (12) not supported. */
9191
50.8k
        { OFPACT_SET_MPLS_LABEL, 13 },
9192
50.8k
        { OFPACT_SET_MPLS_TC, 14 },
9193
50.8k
        { OFPACT_SET_MPLS_TTL, 15 },
9194
50.8k
        { OFPACT_DEC_MPLS_TTL, 16 },
9195
50.8k
        { OFPACT_PUSH_VLAN, 17 },
9196
50.8k
        { OFPACT_STRIP_VLAN, 18 },
9197
50.8k
        { OFPACT_PUSH_MPLS, 19 },
9198
50.8k
        { OFPACT_POP_MPLS, 20 },
9199
50.8k
        { OFPACT_SET_QUEUE, 21 },
9200
50.8k
        { OFPACT_GROUP, 22 },
9201
50.8k
        { OFPACT_SET_IP_TTL, 23 },
9202
50.8k
        { OFPACT_DEC_TTL, 24 },
9203
50.8k
        { 0, -1 },
9204
50.8k
    };
9205
9206
    /* OpenFlow 1.2, 1.3, and 1.4 actions. */
9207
50.8k
    static const struct ofpact_map of12[] = {
9208
50.8k
        { OFPACT_OUTPUT, 0 },
9209
        /* OFPAT_COPY_TTL_OUT (11) not supported. */
9210
        /* OFPAT_COPY_TTL_IN (12) not supported. */
9211
50.8k
        { OFPACT_SET_MPLS_TTL, 15 },
9212
50.8k
        { OFPACT_DEC_MPLS_TTL, 16 },
9213
50.8k
        { OFPACT_PUSH_VLAN, 17 },
9214
50.8k
        { OFPACT_STRIP_VLAN, 18 },
9215
50.8k
        { OFPACT_PUSH_MPLS, 19 },
9216
50.8k
        { OFPACT_POP_MPLS, 20 },
9217
50.8k
        { OFPACT_SET_QUEUE, 21 },
9218
50.8k
        { OFPACT_GROUP, 22 },
9219
50.8k
        { OFPACT_SET_IP_TTL, 23 },
9220
50.8k
        { OFPACT_DEC_TTL, 24 },
9221
50.8k
        { OFPACT_SET_FIELD, 25 },
9222
        /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
9223
        /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
9224
50.8k
        { 0, -1 },
9225
50.8k
    };
9226
9227
50.8k
    switch (version) {
9228
2.22k
    case OFP10_VERSION:
9229
2.22k
        return of10;
9230
9231
12.0k
    case OFP11_VERSION:
9232
12.0k
        return of11;
9233
9234
32.7k
    case OFP12_VERSION:
9235
32.7k
    case OFP13_VERSION:
9236
35.0k
    case OFP14_VERSION:
9237
36.5k
    case OFP15_VERSION:
9238
36.5k
    default:
9239
36.5k
        return of12;
9240
50.8k
    }
9241
50.8k
}
9242
9243
/* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
9244
 * values, into a bitmap of actions suitable for OpenFlow 'version', and
9245
 * returns the result. */
9246
ovs_be32
9247
ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
9248
0
{
9249
0
    uint32_t openflow_bitmap = 0;
9250
0
    const struct ofpact_map *x;
9251
9252
0
    for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
9253
0
        if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
9254
0
            openflow_bitmap |= 1u << x->ofpat;
9255
0
        }
9256
0
    }
9257
0
    return htonl(openflow_bitmap);
9258
0
}
9259
9260
/* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
9261
 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
9262
 * and returns the result. */
9263
uint64_t
9264
ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
9265
50.8k
{
9266
50.8k
    uint64_t ofpact_bitmap = 0;
9267
50.8k
    const struct ofpact_map *x;
9268
9269
794k
    for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
9270
743k
        if (ofpat_bitmap & htonl(1u << x->ofpat)) {
9271
186k
            ofpact_bitmap |= UINT64_C(1) << x->ofpact;
9272
186k
        }
9273
743k
    }
9274
50.8k
    return ofpact_bitmap;
9275
50.8k
}
9276
9277
/* Appends to 's' a string representation of the set of OFPACT_* represented
9278
 * by 'ofpacts_bitmap'. */
9279
void
9280
ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
9281
26.7k
{
9282
26.7k
    if (!ofpacts_bitmap) {
9283
221
        ds_put_cstr(s, "<none>");
9284
26.5k
    } else {
9285
140k
        while (ofpacts_bitmap) {
9286
114k
            ds_put_format(s, "%s ",
9287
114k
                          ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
9288
114k
            ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
9289
114k
        }
9290
26.5k
        ds_chomp(s, ' ');
9291
26.5k
    }
9292
26.7k
}
9293

9294
/* Returns true if 'action' outputs to 'port', false otherwise. */
9295
static bool
9296
ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
9297
0
{
9298
0
    switch (ofpact->type) {
9299
0
    case OFPACT_OUTPUT:
9300
0
        return ofpact_get_OUTPUT(ofpact)->port == port;
9301
0
    case OFPACT_ENQUEUE:
9302
0
        return ofpact_get_ENQUEUE(ofpact)->port == port;
9303
0
    case OFPACT_CONTROLLER:
9304
0
        return port == OFPP_CONTROLLER;
9305
9306
0
    case OFPACT_OUTPUT_REG:
9307
0
    case OFPACT_OUTPUT_TRUNC:
9308
0
    case OFPACT_BUNDLE:
9309
0
    case OFPACT_SET_VLAN_VID:
9310
0
    case OFPACT_SET_VLAN_PCP:
9311
0
    case OFPACT_STRIP_VLAN:
9312
0
    case OFPACT_PUSH_VLAN:
9313
0
    case OFPACT_SET_ETH_SRC:
9314
0
    case OFPACT_SET_ETH_DST:
9315
0
    case OFPACT_SET_IPV4_SRC:
9316
0
    case OFPACT_SET_IPV4_DST:
9317
0
    case OFPACT_SET_IP_DSCP:
9318
0
    case OFPACT_SET_IP_ECN:
9319
0
    case OFPACT_SET_IP_TTL:
9320
0
    case OFPACT_SET_L4_SRC_PORT:
9321
0
    case OFPACT_SET_L4_DST_PORT:
9322
0
    case OFPACT_REG_MOVE:
9323
0
    case OFPACT_SET_FIELD:
9324
0
    case OFPACT_STACK_PUSH:
9325
0
    case OFPACT_STACK_POP:
9326
0
    case OFPACT_DEC_TTL:
9327
0
    case OFPACT_SET_MPLS_LABEL:
9328
0
    case OFPACT_SET_MPLS_TC:
9329
0
    case OFPACT_SET_MPLS_TTL:
9330
0
    case OFPACT_DEC_MPLS_TTL:
9331
0
    case OFPACT_SET_TUNNEL:
9332
0
    case OFPACT_WRITE_METADATA:
9333
0
    case OFPACT_SET_QUEUE:
9334
0
    case OFPACT_POP_QUEUE:
9335
0
    case OFPACT_FIN_TIMEOUT:
9336
0
    case OFPACT_RESUBMIT:
9337
0
    case OFPACT_LEARN:
9338
0
    case OFPACT_CONJUNCTION:
9339
0
    case OFPACT_MULTIPATH:
9340
0
    case OFPACT_NOTE:
9341
0
    case OFPACT_EXIT:
9342
0
    case OFPACT_UNROLL_XLATE:
9343
0
    case OFPACT_PUSH_MPLS:
9344
0
    case OFPACT_POP_MPLS:
9345
0
    case OFPACT_SAMPLE:
9346
0
    case OFPACT_CLEAR_ACTIONS:
9347
0
    case OFPACT_CLONE:
9348
0
    case OFPACT_WRITE_ACTIONS:
9349
0
    case OFPACT_GOTO_TABLE:
9350
0
    case OFPACT_METER:
9351
0
    case OFPACT_GROUP:
9352
0
    case OFPACT_DEBUG_RECIRC:
9353
0
    case OFPACT_DEBUG_SLOW:
9354
0
    case OFPACT_CT:
9355
0
    case OFPACT_CT_CLEAR:
9356
0
    case OFPACT_NAT:
9357
0
    case OFPACT_ENCAP:
9358
0
    case OFPACT_DECAP:
9359
0
    case OFPACT_DEC_NSH_TTL:
9360
0
    case OFPACT_CHECK_PKT_LARGER:
9361
0
    case OFPACT_DELETE_FIELD:
9362
0
    default:
9363
0
        return false;
9364
0
    }
9365
0
}
9366
9367
/* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
9368
 * to 'port', false otherwise. */
9369
bool
9370
ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
9371
                       ofp_port_t port)
9372
0
{
9373
0
    const struct ofpact *a;
9374
9375
0
    OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
9376
0
        if (ofpact_outputs_to_port(a, port)) {
9377
0
            return true;
9378
0
        }
9379
0
    }
9380
9381
0
    return false;
9382
0
}
9383
9384
/* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
9385
 * to 'group', false otherwise. */
9386
bool
9387
ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
9388
                        uint32_t group_id)
9389
0
{
9390
0
    const struct ofpact *a;
9391
9392
0
    OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
9393
0
        if (a->type == OFPACT_GROUP
9394
0
            && ofpact_get_GROUP(a)->group_id == group_id) {
9395
0
            return true;
9396
0
        }
9397
0
    }
9398
9399
0
    return false;
9400
0
}
9401
9402
/* Returns true if the 'a_len' bytes of actions in 'a' and the 'b_len' bytes of
9403
 * actions in 'b' are bytewise identical. */
9404
bool
9405
ofpacts_equal(const struct ofpact *a, size_t a_len,
9406
              const struct ofpact *b, size_t b_len)
9407
0
{
9408
0
    return a_len == b_len && (!a_len || !memcmp(a, b, a_len));
9409
0
}
9410
9411
/* Returns true if the 'a_len' bytes of actions in 'a' and the 'b_len' bytes of
9412
 * actions in 'b' are identical when formatted as strings.  (Converting actions
9413
 * to string form suppresses some rarely meaningful differences, such as the
9414
 * 'compat' member of actions.) */
9415
bool
9416
ofpacts_equal_stringwise(const struct ofpact *a, size_t a_len,
9417
                         const struct ofpact *b, size_t b_len)
9418
0
{
9419
0
    struct ds a_s = DS_EMPTY_INITIALIZER;
9420
0
    struct ofpact_format_params a_fp = { .s = &a_s };
9421
0
    ofpacts_format(a, a_len, &a_fp);
9422
9423
0
    struct ds b_s = DS_EMPTY_INITIALIZER;
9424
0
    struct ofpact_format_params b_fp = { .s = &b_s };
9425
0
    ofpacts_format(b, b_len, &b_fp);
9426
9427
0
    bool equal = !strcmp(ds_cstr(&a_s), ds_cstr(&b_s));
9428
9429
0
    ds_destroy(&a_s);
9430
0
    ds_destroy(&b_s);
9431
9432
0
    return equal;
9433
0
}
9434
9435
/* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
9436
 * 'ofpacts'.  If found, returns its meter ID; if not, returns 0.
9437
 *
9438
 * This function relies on the order of 'ofpacts' being correct (as checked by
9439
 * ofpacts_verify()). */
9440
uint32_t
9441
ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
9442
0
{
9443
0
    const struct ofpact *a;
9444
9445
0
    OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
9446
0
        if (a->type == OFPACT_METER) {
9447
0
            return ofpact_get_METER(a)->meter_id;
9448
0
        }
9449
9450
0
        enum ovs_instruction_type inst
9451
0
            = ovs_instruction_type_from_ofpact_type(a->type, 0);
9452
0
        if (inst > OVSINST_OFPIT13_METER) {
9453
0
            break;
9454
0
        }
9455
0
    }
9456
9457
0
    return 0;
9458
0
}
9459

9460
/* Formatting ofpacts. */
9461
9462
static void
9463
ofpact_format(const struct ofpact *a,
9464
              const struct ofpact_format_params *fp)
9465
173k
{
9466
173k
    switch (a->type) {
9467
0
#define OFPACT(ENUM, STRUCT, MEMBER, NAME)                              \
9468
173k
        case OFPACT_##ENUM:                                             \
9469
173k
            format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), fp);  \
9470
173k
            break;
9471
0
        OFPACTS
9472
0
#undef OFPACT
9473
0
    default:
9474
0
        OVS_NOT_REACHED();
9475
173k
    }
9476
173k
}
9477
9478
/* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
9479
 * 'ofpacts' to 'fp->s'.  If 'port_map' is nonnull, uses it to translate port
9480
 * numbers to names in output. */
9481
void
9482
ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
9483
               const struct ofpact_format_params *fp)
9484
116k
{
9485
116k
    if (!ofpacts_len) {
9486
28.4k
        ds_put_format(fp->s, "%sdrop%s", colors.drop, colors.end);
9487
88.1k
    } else {
9488
88.1k
        const struct ofpact *a;
9489
9490
173k
        OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
9491
173k
            if (a != ofpacts) {
9492
84.8k
                ds_put_char(fp->s, ',');
9493
84.8k
            }
9494
9495
173k
            ofpact_format(a, fp);
9496
173k
        }
9497
88.1k
    }
9498
116k
}
9499

9500
/* Internal use by helpers. */
9501
9502
/* Implementation of ofpact_put_<ENUM>(). */
9503
void *
9504
ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
9505
831k
{
9506
831k
    struct ofpact *ofpact;
9507
9508
831k
    ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
9509
831k
    ofpact = ofpacts->header;
9510
831k
    ofpact_init(ofpact, type, len);
9511
831k
    return ofpact;
9512
831k
}
9513
9514
/* Implementation of ofpact_init_<ENUM>(). */
9515
void
9516
ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
9517
834k
{
9518
834k
    memset(ofpact, 0, len);
9519
834k
    ofpact->type = type;
9520
834k
    ofpact->raw = -1;
9521
834k
    ofpact->len = len;
9522
834k
}
9523
9524
/* Implementation of ofpact_finish_<ENUM>().
9525
 *
9526
 * Finishes composing a variable-length action (begun using
9527
 * ofpact_put_<NAME>()), by padding the action to a multiple of OFPACT_ALIGNTO
9528
 * bytes and updating its embedded length field.  See the large comment near
9529
 * the end of ofp-actions.h for more information.
9530
 *
9531
 * May reallocate 'ofpacts'. Callers should consider updating their 'ofpact'
9532
 * pointer to the return value of this function. */
9533
void *
9534
ofpact_finish(struct ofpbuf *ofpacts, struct ofpact *ofpact)
9535
211k
{
9536
211k
    ptrdiff_t len;
9537
9538
211k
    ovs_assert(ofpact == ofpacts->header);
9539
211k
    len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
9540
211k
    ovs_assert(len > 0 && len <= UINT16_MAX);
9541
211k
    ofpact->len = len;
9542
211k
    ofpbuf_padto(ofpacts, OFPACT_ALIGN(ofpacts->size));
9543
9544
211k
    return ofpacts->header;
9545
211k
}
9546

9547
static char * OVS_WARN_UNUSED_RESULT
9548
ofpact_parse(enum ofpact_type type, char *value,
9549
             const struct ofpact_parse_params *pp)
9550
182k
{
9551
182k
    switch (type) {
9552
0
#define OFPACT(ENUM, STRUCT, MEMBER, NAME)                              \
9553
182k
        case OFPACT_##ENUM:                                             \
9554
182k
            return parse_##ENUM(value, pp);
9555
0
        OFPACTS
9556
0
#undef OFPACT
9557
0
    default:
9558
0
        OVS_NOT_REACHED();
9559
182k
    }
9560
182k
}
9561
9562
static bool
9563
ofpact_type_from_name(const char *name, enum ofpact_type *type)
9564
395k
{
9565
395k
#define OFPACT(ENUM, STRUCT, MEMBER, NAME)                            \
9566
17.6M
    if (!strcasecmp(name, NAME)) {                                    \
9567
182k
        *type = OFPACT_##ENUM;                                          \
9568
182k
        return true;                                                    \
9569
182k
    }
9570
395k
    OFPACTS
9571
213k
#undef OFPACT
9572
9573
213k
    return false;
9574
17.2M
}
9575
9576
/* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
9577
 *
9578
 * Returns NULL if successful, otherwise a malloc()'d string describing the
9579
 * error.  The caller is responsible for freeing the returned string.
9580
 *
9581
 * If 'outer_action' is specified, indicates that the actions being parsed
9582
 * are nested within another action of the type specified in 'outer_action'. */
9583
static char * OVS_WARN_UNUSED_RESULT
9584
ofpacts_parse__(char *str, const struct ofpact_parse_params *pp,
9585
                bool allow_instructions, enum ofpact_type outer_action)
9586
25.6k
{
9587
25.6k
    uint32_t orig_size = pp->ofpacts->size;
9588
25.6k
    char *key, *value;
9589
25.6k
    bool drop = false;
9590
25.6k
    char *pos;
9591
9592
25.6k
    pos = str;
9593
412k
    while (ofputil_parse_key_value(&pos, &key, &value)) {
9594
395k
        enum ofpact_type type;
9595
395k
        char *error = NULL;
9596
395k
        ofp_port_t port;
9597
395k
        if (ofpact_type_from_name(key, &type)) {
9598
182k
            error = ofpact_parse(type, value, pp);
9599
9600
182k
            if (type == OFPACT_METER && !allow_instructions) {
9601
                /* Meter is an action in OF1.5 and it's being used in a
9602
                 * context where instructions aren't allowed.  Therefore,
9603
                 * this must be OF1.5+. */
9604
891
                *pp->usable_protocols &= OFPUTIL_P_OF15_UP;
9605
891
            }
9606
213k
        } else if (!strcasecmp(key, "mod_vlan_vid")) {
9607
1.06k
            error = parse_set_vlan_vid(value, true, pp);
9608
212k
        } else if (!strcasecmp(key, "mod_vlan_pcp")) {
9609
1.30k
            error = parse_set_vlan_pcp(value, true, pp);
9610
211k
        } else if (!strcasecmp(key, "set_nw_ttl")) {
9611
536
            error = parse_SET_IP_TTL(value, pp);
9612
210k
        } else if (!strcasecmp(key, "pop_vlan")) {
9613
2.69k
            error = parse_pop_vlan(pp);
9614
208k
        } else if (!strcasecmp(key, "set_tunnel64")) {
9615
568
            error = parse_set_tunnel(value, NXAST_RAW_SET_TUNNEL64, pp);
9616
207k
        } else if (!strcasecmp(key, "load")) {
9617
899
            error = parse_reg_load(value, pp);
9618
206k
        } else if (!strcasecmp(key, "bundle_load")) {
9619
1
            error = parse_bundle_load(value, pp);
9620
206k
        } else if (!strcasecmp(key, "drop")) {
9621
562
            drop = true;
9622
206k
        } else if (!strcasecmp(key, "apply_actions")) {
9623
1
            return xstrdup("apply_actions is the default instruction");
9624
206k
        } else if (ofputil_port_from_string(key, pp->port_map, &port)) {
9625
202k
            ofpact_put_OUTPUT(pp->ofpacts)->port = port;
9626
202k
        } else {
9627
3.43k
            return xasprintf("unknown action %s", key);
9628
3.43k
        }
9629
392k
        if (error) {
9630
6.07k
            return error;
9631
6.07k
        }
9632
386k
        if (pp->ofpacts->size - orig_size > UINT16_MAX) {
9633
20
            return xasprintf("input too big");
9634
20
        }
9635
386k
    }
9636
9637
16.1k
    if (drop && pp->ofpacts->size) {
9638
8
        return xstrdup("\"drop\" must not be accompanied by any other action "
9639
8
                       "or instruction");
9640
8
    }
9641
9642
16.1k
    char *error = NULL;
9643
16.1k
    ofpacts_verify(pp->ofpacts->data, pp->ofpacts->size, OFP11_VERSION,
9644
16.1k
                   (allow_instructions
9645
16.1k
                    ? (1u << N_OVS_INSTRUCTIONS) - 1
9646
16.1k
                    : ((1u << OVSINST_OFPIT11_APPLY_ACTIONS)
9647
9.03k
                       | (1u << OVSINST_OFPIT13_METER))),
9648
16.1k
                   outer_action, &error);
9649
16.1k
    if (error) {
9650
233
        return error;
9651
233
    }
9652
9653
15.9k
    return NULL;
9654
16.1k
}
9655
9656
static char * OVS_WARN_UNUSED_RESULT
9657
ofpacts_parse(char *str, const struct ofpact_parse_params *pp,
9658
              bool allow_instructions, enum ofpact_type outer_action)
9659
25.6k
{
9660
25.6k
    if (pp->depth >= MAX_OFPACT_PARSE_DEPTH) {
9661
3
        return xstrdup("Action nested too deeply");
9662
3
    }
9663
25.6k
    CONST_CAST(struct ofpact_parse_params *, pp)->depth++;
9664
25.6k
    uint32_t orig_size = pp->ofpacts->size;
9665
25.6k
    char *error = ofpacts_parse__(str, pp, allow_instructions, outer_action);
9666
25.6k
    if (error) {
9667
9.77k
        ofpbuf_truncate(pp->ofpacts, orig_size);
9668
9.77k
    }
9669
25.6k
    CONST_CAST(struct ofpact_parse_params *, pp)->depth--;
9670
25.6k
    return error;
9671
25.6k
}
9672
9673
static char * OVS_WARN_UNUSED_RESULT
9674
ofpacts_parse_copy(const char *s_, const struct ofpact_parse_params *pp,
9675
                   bool allow_instructions, enum ofpact_type outer_action)
9676
24.9k
{
9677
24.9k
    char *error, *s;
9678
9679
24.9k
    *pp->usable_protocols = OFPUTIL_P_ANY;
9680
9681
24.9k
    s = xstrdup(s_);
9682
24.9k
    error = ofpacts_parse(s, pp, allow_instructions, outer_action);
9683
24.9k
    free(s);
9684
9685
24.9k
    return error;
9686
24.9k
}
9687
9688
/* Parses 's' as a set of OpenFlow actions and appends the actions to
9689
 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
9690
 * that are nested within the action of type 'outer_action'.
9691
 *
9692
 * Returns NULL if successful, otherwise a malloc()'d string describing the
9693
 * error.  The caller is responsible for freeing the returned string. */
9694
char * OVS_WARN_UNUSED_RESULT
9695
ofpacts_parse_actions(const char *s, const struct ofpact_parse_params *pp)
9696
0
{
9697
0
    return ofpacts_parse_copy(s, pp, false, 0);
9698
0
}
9699
9700
/* Parses 's' as a set of OpenFlow instructions and appends the instructions to
9701
 * 'ofpacts'.
9702
 *
9703
 * Returns NULL if successful, otherwise a malloc()'d string describing the
9704
 * error.  The caller is responsible for freeing the returned string. */
9705
char * OVS_WARN_UNUSED_RESULT
9706
ofpacts_parse_instructions(const char *s, const struct ofpact_parse_params *pp)
9707
14.6k
{
9708
14.6k
    return ofpacts_parse_copy(s, pp, true, 0);
9709
14.6k
}
9710
9711
const char *
9712
ofpact_name(enum ofpact_type type)
9713
118k
{
9714
118k
    switch (type) {
9715
118k
#define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
9716
118k
        OFPACTS
9717
118k
#undef OFPACT
9718
118k
    }
9719
0
    return "<unknown>";
9720
118k
}
9721

9722
/* Low-level action decoding and encoding functions. */
9723
9724
/* Everything needed to identify a particular OpenFlow action. */
9725
struct ofpact_hdrs {
9726
    uint32_t vendor;              /* 0 if standard, otherwise a vendor code. */
9727
    uint16_t type;                /* Type if standard, otherwise subtype. */
9728
    uint8_t ofp_version;          /* From ofp_header. */
9729
};
9730
9731
/* Information about a particular OpenFlow action. */
9732
struct ofpact_raw_instance {
9733
    /* The action's identity. */
9734
    struct ofpact_hdrs hdrs;
9735
    enum ofp_raw_action_type raw;
9736
9737
    /* Looking up the action. */
9738
    struct hmap_node decode_node; /* Based on 'hdrs'. */
9739
    struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
9740
9741
    /* The action's encoded size.
9742
     *
9743
     * If this action is fixed-length, 'min_length' == 'max_length'.
9744
     * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
9745
     * OFP_ACTION_ALIGN) == 65528. */
9746
    unsigned short int min_length;
9747
    unsigned short int max_length;
9748
9749
    /* For actions with a simple integer numeric argument, 'arg_ofs' is the
9750
     * offset of that argument from the beginning of the action and 'arg_len'
9751
     * its length, both in bytes.
9752
     *
9753
     * For actions that take other forms, these are both zero. */
9754
    unsigned short int arg_ofs;
9755
    unsigned short int arg_len;
9756
9757
    /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
9758
    const char *name;
9759
9760
    /* If this action is deprecated, a human-readable string with a brief
9761
     * explanation. */
9762
    const char *deprecation;
9763
};
9764
9765
/* Action header. */
9766
struct ofp_action_header {
9767
    /* The meaning of other values of 'type' generally depends on the OpenFlow
9768
     * version (see enum ofp_raw_action_type).
9769
     *
9770
     * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
9771
     * designates an OpenFlow vendor ID and that the remainder of the action
9772
     * structure has a vendor-defined meaning.
9773
     */
9774
#define OFPAT_VENDOR 0xffff
9775
    ovs_be16 type;
9776
9777
    /* Always a multiple of 8. */
9778
    ovs_be16 len;
9779
9780
    /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
9781
     * ONF_VENDOR_ID.  Other 'type's use this space for some other purpose. */
9782
    ovs_be32 vendor;
9783
};
9784
OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
9785
9786
static bool
9787
ofpact_hdrs_equal(const struct ofpact_hdrs *a,
9788
                  const struct ofpact_hdrs *b)
9789
345k
{
9790
345k
    return (a->vendor == b->vendor
9791
345k
            && a->type == b->type
9792
345k
            && a->ofp_version == b->ofp_version);
9793
345k
}
9794
9795
static uint32_t
9796
ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
9797
368k
{
9798
368k
    return hash_2words(hdrs->vendor,
9799
368k
                       ((uint32_t) hdrs->type << 16) | hdrs->ofp_version);
9800
368k
}
9801
9802
#include "ofp-actions.inc2"
9803
9804
static struct hmap *
9805
ofpact_decode_hmap(void)
9806
367k
{
9807
367k
    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
9808
367k
    static struct hmap hmap;
9809
9810
367k
    if (ovsthread_once_start(&once)) {
9811
1
        struct ofpact_raw_instance *inst;
9812
9813
1
        hmap_init(&hmap);
9814
1
        for (inst = all_raw_instances;
9815
385
             inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
9816
384
             inst++) {
9817
384
            hmap_insert(&hmap, &inst->decode_node,
9818
384
                        ofpact_hdrs_hash(&inst->hdrs));
9819
384
        }
9820
1
        ovsthread_once_done(&once);
9821
1
    }
9822
367k
    return &hmap;
9823
367k
}
9824
9825
static struct hmap *
9826
ofpact_encode_hmap(void)
9827
321k
{
9828
321k
    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
9829
321k
    static struct hmap hmap;
9830
9831
321k
    if (ovsthread_once_start(&once)) {
9832
1
        struct ofpact_raw_instance *inst;
9833
9834
1
        hmap_init(&hmap);
9835
1
        for (inst = all_raw_instances;
9836
385
             inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
9837
384
             inst++) {
9838
384
            hmap_insert(&hmap, &inst->encode_node,
9839
384
                        hash_2words(inst->raw, inst->hdrs.ofp_version));
9840
384
        }
9841
1
        ovsthread_once_done(&once);
9842
1
    }
9843
321k
    return &hmap;
9844
321k
}
9845
9846
static enum ofperr
9847
ofpact_decode_raw(enum ofp_version ofp_version,
9848
                  const struct ofp_action_header *oah, size_t length,
9849
                  const struct ofpact_raw_instance **instp)
9850
383k
{
9851
383k
    const struct ofpact_raw_instance *inst;
9852
383k
    struct ofpact_hdrs hdrs;
9853
9854
383k
    *instp = NULL;
9855
383k
    if (length < sizeof *oah) {
9856
0
        return OFPERR_OFPBAC_BAD_LEN;
9857
0
    }
9858
9859
    /* Get base action type. */
9860
383k
    if (oah->type == htons(OFPAT_VENDOR)) {
9861
        /* Get vendor. */
9862
204k
        hdrs.vendor = ntohl(oah->vendor);
9863
204k
        if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
9864
            /* Get extension subtype. */
9865
188k
            const struct ext_action_header *nah;
9866
9867
188k
            nah = ALIGNED_CAST(const struct ext_action_header *, oah);
9868
188k
            if (length < sizeof *nah) {
9869
65
                return OFPERR_OFPBAC_BAD_LEN;
9870
65
            }
9871
188k
            hdrs.type = ntohs(nah->subtype);
9872
188k
        } else {
9873
15.8k
            VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
9874
15.8k
                         hdrs.vendor);
9875
15.8k
            return OFPERR_OFPBAC_BAD_VENDOR;
9876
15.8k
        }
9877
204k
    } else {
9878
179k
        hdrs.vendor = 0;
9879
179k
        hdrs.type = ntohs(oah->type);
9880
179k
    }
9881
9882
367k
    hdrs.ofp_version = ofp_version;
9883
367k
    HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
9884
367k
                             ofpact_decode_hmap()) {
9885
345k
        if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
9886
345k
            *instp = inst;
9887
345k
            return 0;
9888
345k
        }
9889
345k
    }
9890
9891
22.0k
    VLOG_WARN_RL(&rl, "unknown %s action for vendor %#"PRIx32" and "
9892
22.0k
                 "type %"PRIu16, ofputil_version_to_string(ofp_version),
9893
22.0k
                 hdrs.vendor, hdrs.type);
9894
22.0k
    return (hdrs.vendor
9895
22.0k
            ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
9896
22.0k
            : OFPERR_OFPBAC_BAD_TYPE);
9897
367k
}
9898
9899
static enum ofperr
9900
ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
9901
                enum ofp_raw_action_type *raw, uint64_t *arg,
9902
                size_t *raw_len)
9903
383k
{
9904
383k
    const struct ofp_action_header *oah = buf->data;
9905
383k
    const struct ofpact_raw_instance *action;
9906
383k
    unsigned int length;
9907
383k
    enum ofperr error;
9908
9909
383k
    *raw = *arg = *raw_len = 0;
9910
383k
    error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
9911
383k
    if (error) {
9912
37.9k
        return error;
9913
37.9k
    }
9914
9915
345k
    if (action->deprecation) {
9916
14.6k
        VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
9917
14.6k
                     action->name, ofputil_version_to_string(ofp_version),
9918
14.6k
                     action->deprecation);
9919
14.6k
    }
9920
9921
345k
    length = ntohs(oah->len);
9922
345k
    if (length > buf->size) {
9923
5.76k
        VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
9924
5.76k
                     "length %"PRIu32, action->name, length, buf->size);
9925
5.76k
        return OFPERR_OFPBAC_BAD_LEN;
9926
5.76k
    }
9927
339k
    if (length < action->min_length || length > action->max_length) {
9928
5.87k
        VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
9929
5.87k
                     "[%hu,%hu]", action->name, length,
9930
5.87k
                     action->min_length, action->max_length);
9931
5.87k
        return OFPERR_OFPBAC_BAD_LEN;
9932
5.87k
    }
9933
334k
    if (length % 8) {
9934
2.71k
        VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
9935
2.71k
                     "of 8", action->name, length);
9936
2.71k
        return OFPERR_OFPBAC_BAD_LEN;
9937
2.71k
    }
9938
9939
331k
    *raw = action->raw;
9940
331k
    *arg = 0;
9941
331k
    if (action->arg_len) {
9942
98.6k
        const uint8_t *p;
9943
98.6k
        int i;
9944
9945
98.6k
        p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
9946
320k
        for (i = 0; i < action->arg_len; i++) {
9947
221k
            *arg = (*arg << 8) | p[i];
9948
221k
        }
9949
98.6k
    }
9950
9951
331k
    ofpbuf_pull(buf, length);
9952
331k
    *raw_len = length;
9953
9954
331k
    return 0;
9955
334k
}
9956
9957
static const struct ofpact_raw_instance *
9958
ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
9959
321k
{
9960
321k
    const struct ofpact_raw_instance *inst;
9961
9962
321k
    HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
9963
321k
                             ofpact_encode_hmap()) {
9964
321k
        if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
9965
321k
            return inst;
9966
321k
        }
9967
321k
    }
9968
321k
    OVS_NOT_REACHED();
9969
321k
}
9970
9971
static void *
9972
ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
9973
               enum ofp_raw_action_type raw, uint64_t arg)
9974
321k
{
9975
321k
    const struct ofpact_raw_instance *inst;
9976
321k
    struct ofp_action_header *oah;
9977
321k
    const struct ofpact_hdrs *hdrs;
9978
9979
321k
    inst = ofpact_raw_lookup(ofp_version, raw);
9980
321k
    hdrs = &inst->hdrs;
9981
9982
321k
    oah = ofpbuf_put_zeros(buf, inst->min_length);
9983
321k
    oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
9984
321k
    oah->len = htons(inst->min_length);
9985
321k
    oah->vendor = htonl(hdrs->vendor);
9986
9987
321k
    switch (hdrs->vendor) {
9988
147k
    case 0:
9989
147k
        break;
9990
9991
173k
    case NX_VENDOR_ID:
9992
173k
    case ONF_VENDOR_ID: {
9993
173k
        struct ext_action_header *nah = (struct ext_action_header *) oah;
9994
173k
        nah->subtype = htons(hdrs->type);
9995
173k
        break;
9996
173k
    }
9997
9998
0
    default:
9999
0
        OVS_NOT_REACHED();
10000
321k
    }
10001
10002
321k
    if (inst->arg_len) {
10003
20.0k
        uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
10004
20.0k
        int i;
10005
10006
65.7k
        for (i = 0; i < inst->arg_len; i++) {
10007
45.6k
            *--p = arg;
10008
45.6k
            arg >>= 8;
10009
45.6k
        }
10010
301k
    } else {
10011
301k
        ovs_assert(!arg);
10012
301k
    }
10013
10014
321k
    return oah;
10015
321k
}
10016
10017
static void
10018
pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
10019
63.9k
{
10020
63.9k
    struct ofp_action_header *oah;
10021
10022
63.9k
    ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs,
10023
63.9k
                                        OFP_ACTION_ALIGN));
10024
10025
63.9k
    oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
10026
    oah->len = htons(openflow->size - start_ofs);
10027
63.9k
}
10028