LCOV - code coverage report
Current view: top level - lib - ofp-actions.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 2504 2818 88.9 %
Date: 2016-09-14 01:02:56 Functions: 332 345 96.2 %
Branches: 1327 1790 74.1 %

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

Generated by: LCOV version 1.12