Introduce ofpacts, an abstraction of OpenFlow actions.
[cascardo/ovs.git] / lib / nx-match.h
1 /*
2  * Copyright (c) 2010, 2011, 2012 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef NX_MATCH_H
18 #define NX_MATCH_H 1
19
20 #include <stdint.h>
21 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include "flow.h"
24 #include "ofp-errors.h"
25 #include "openvswitch/types.h"
26 #include "ofp-errors.h"
27
28 struct cls_rule;
29 struct ds;
30 struct flow;
31 struct mf_subfield;
32 struct ofpact_reg_move;
33 struct ofpact_reg_load;
34 struct ofpbuf;
35 struct nx_action_reg_load;
36 struct nx_action_reg_move;
37
38 /* Nicira Extended Match (NXM) flexible flow match helper functions.
39  *
40  * See include/openflow/nicira-ext.h for NXM specification.
41  */
42
43 enum ofperr nx_pull_match(struct ofpbuf *, unsigned int match_len,
44                           uint16_t priority, struct cls_rule *,
45                           ovs_be64 *cookie, ovs_be64 *cookie_mask);
46 enum ofperr nx_pull_match_loose(struct ofpbuf *, unsigned int match_len,
47                                 uint16_t priority, struct cls_rule *,
48                                 ovs_be64 *cookie, ovs_be64 *cookie_mask);
49 int nx_put_match(struct ofpbuf *, bool oxm, const struct cls_rule *,
50                  ovs_be64 cookie, ovs_be64 cookie_mask);
51
52 char *nx_match_to_string(const uint8_t *, unsigned int match_len);
53 int nx_match_from_string(const char *, struct ofpbuf *);
54
55 void nxm_parse_reg_move(struct ofpact_reg_move *, const char *);
56 void nxm_parse_reg_load(struct ofpact_reg_load *, const char *);
57
58 void nxm_format_reg_move(const struct ofpact_reg_move *, struct ds *);
59 void nxm_format_reg_load(const struct ofpact_reg_load *, struct ds *);
60
61 enum ofperr nxm_reg_move_from_openflow(const struct nx_action_reg_move *,
62                                        struct ofpbuf *ofpacts);
63 enum ofperr nxm_reg_load_from_openflow(const struct nx_action_reg_load *,
64                                        struct ofpbuf *ofpacts);
65
66 enum ofperr nxm_reg_move_check(const struct ofpact_reg_move *,
67                                const struct flow *);
68 enum ofperr nxm_reg_load_check(const struct ofpact_reg_load *,
69                                const struct flow *);
70
71 void nxm_reg_move_to_nxast(const struct ofpact_reg_move *,
72                            struct ofpbuf *openflow);
73 void nxm_reg_load_to_nxast(const struct ofpact_reg_load *,
74                            struct ofpbuf *openflow);
75
76 void nxm_execute_reg_move(const struct ofpact_reg_move *, struct flow *);
77 void nxm_execute_reg_load(const struct ofpact_reg_load *, struct flow *);
78 void nxm_reg_load(const struct mf_subfield *, uint64_t src_data,
79                   struct flow *);
80
81 int nxm_field_bytes(uint32_t header);
82 int nxm_field_bits(uint32_t header);
83
84 /* Dealing with the 'ofs_nbits' members in several Nicira extensions. */
85
86 static inline ovs_be16
87 nxm_encode_ofs_nbits(int ofs, int n_bits)
88 {
89     return htons((ofs << 6) | (n_bits - 1));
90 }
91
92 static inline int
93 nxm_decode_ofs(ovs_be16 ofs_nbits)
94 {
95     return ntohs(ofs_nbits) >> 6;
96 }
97
98 static inline int
99 nxm_decode_n_bits(ovs_be16 ofs_nbits)
100 {
101     return (ntohs(ofs_nbits) & 0x3f) + 1;
102 }
103 \f
104 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 12);
105 /* Upper bound on the length of an nx_match.  The longest nx_match (an
106  * IPV6 neighbor discovery message using all the registers) would be:
107  *
108  *                   header  value  mask  total
109  *                   ------  -----  ----  -----
110  *  NXM_OF_IN_PORT      4       2    --      6
111  *  NXM_OF_ETH_DST_W    4       6     6     16
112  *  NXM_OF_ETH_SRC_W    4       6     6     16
113  *  NXM_OF_ETH_TYPE     4       2    --      6
114  *  NXM_OF_VLAN_TCI     4       2     2      8
115  *  NXM_OF_IP_TOS       4       1    --      5
116  *  NXM_NX_IP_ECN       4       1    --      5
117  *  NXM_OF_IP_TTL       4       1    --      5
118  *  NXM_NX_IP_FRAG      4       1     1      8
119  *  NXM_OF_IP_PROTO     4       2    --      6
120  *  NXM_OF_IPV6_SRC_W   4      16    16     36
121  *  NXM_OF_IPV6_DST_W   4      16    16     36
122  *  NXM_OF_IPV6_LABEL   4       4    --      8
123  *  NXM_OF_ICMP_TYPE    4       1    --      5
124  *  NXM_OF_ICMP_CODE    4       1    --      5
125  *  NXM_NX_ND_TARGET    4      16    16     36
126  *  NXM_NX_ND_SLL       4       6    --     10
127  *  NXM_NX_REG_W(0)     4       4     4     12
128  *  NXM_NX_REG_W(1)     4       4     4     12
129  *  NXM_NX_REG_W(2)     4       4     4     12
130  *  NXM_NX_REG_W(3)     4       4     4     12
131  *  NXM_NX_REG_W(4)     4       4     4     12
132  *  NXM_NX_REG_W(5)     4       4     4     12
133  *  NXM_NX_REG_W(6)     4       4     4     12
134  *  NXM_NX_REG_W(7)     4       4     4     12
135  *  NXM_NX_TUN_ID_W     4       8     8     20
136  *  OXM_OF_METADATA     4       8     8     20
137  *  -------------------------------------------
138  *  total                                  353
139  *
140  * So this value is conservative.
141  */
142 #define NXM_MAX_LEN 400
143
144 /* This is my guess at the length of a "typical" nx_match, for use in
145  * predicting space requirements. */
146 #define NXM_TYPICAL_LEN 64
147
148 #endif /* nx-match.h */