Global replace of Nicira Networks.
[sliver-openvswitch.git] / lib / meta-flow.h
1 /*
2  * Copyright (c) 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 META_FLOW_H
18 #define META_FLOW_H 1
19
20 #include <sys/types.h>
21 #include <netinet/in.h>
22 #include <netinet/ip6.h>
23 #include "flow.h"
24 #include "ofp-errors.h"
25 #include "packets.h"
26
27 struct cls_rule;
28 struct ds;
29
30 /* The comment on each of these indicates the member in "union mf_value" used
31  * to represent its value. */
32 enum mf_field_id {
33     /* Metadata. */
34     MFF_TUN_ID,                 /* be64 */
35     MFF_IN_PORT,                /* be16 */
36
37 #if FLOW_N_REGS > 0
38     MFF_REG0,                   /* be32 */
39 #endif
40 #if FLOW_N_REGS > 1
41     MFF_REG1,                   /* be32 */
42 #endif
43 #if FLOW_N_REGS > 2
44     MFF_REG2,                   /* be32 */
45 #endif
46 #if FLOW_N_REGS > 3
47     MFF_REG3,                   /* be32 */
48 #endif
49 #if FLOW_N_REGS > 4
50     MFF_REG4,                   /* be32 */
51 #endif
52 #if FLOW_N_REGS > 5
53     MFF_REG5,                   /* be32 */
54 #endif
55 #if FLOW_N_REGS > 6
56     MFF_REG6,                   /* be32 */
57 #endif
58 #if FLOW_N_REGS > 7
59     MFF_REG7,                   /* be32 */
60 #endif
61 #if FLOW_N_REGS > 8
62 #error
63 #endif
64
65     /* L2. */
66     MFF_ETH_SRC,                /* mac */
67     MFF_ETH_DST,                /* mac */
68     MFF_ETH_TYPE,               /* be16 */
69
70     MFF_VLAN_TCI,               /* be16 */
71     MFF_VLAN_VID,               /* be16 */
72     MFF_VLAN_PCP,               /* u8 */
73
74     /* L3. */
75     MFF_IPV4_SRC,               /* be32 */
76     MFF_IPV4_DST,               /* be32 */
77
78     MFF_IPV6_SRC,               /* ipv6 */
79     MFF_IPV6_DST,               /* ipv6 */
80     MFF_IPV6_LABEL,             /* be32 */
81
82     MFF_IP_PROTO,               /* u8 (used for IPv4 or IPv6) */
83     MFF_IP_DSCP,                /* u8 (used for IPv4 or IPv6) */
84     MFF_IP_ECN,                 /* u8 (used for IPv4 or IPv6) */
85     MFF_IP_TTL,                 /* u8 (used for IPv4 or IPv6) */
86     MFF_IP_FRAG,                /* u8 (used for IPv4 or IPv6) */
87
88     MFF_ARP_OP,                 /* be16 */
89     MFF_ARP_SPA,                /* be32 */
90     MFF_ARP_TPA,                /* be32 */
91     MFF_ARP_SHA,                /* mac */
92     MFF_ARP_THA,                /* mac */
93
94     /* L4. */
95     MFF_TCP_SRC,                /* be16 (used for IPv4 or IPv6) */
96     MFF_TCP_DST,                /* be16 (used for IPv4 or IPv6) */
97
98     MFF_UDP_SRC,                /* be16 (used for IPv4 or IPv6) */
99     MFF_UDP_DST,                /* be16 (used for IPv4 or IPv6) */
100
101     MFF_ICMPV4_TYPE,            /* u8 */
102     MFF_ICMPV4_CODE,            /* u8 */
103
104     MFF_ICMPV6_TYPE,            /* u8 */
105     MFF_ICMPV6_CODE,            /* u8 */
106
107     /* ICMPv6 Neighbor Discovery. */
108     MFF_ND_TARGET,              /* ipv6 */
109     MFF_ND_SLL,                 /* mac */
110     MFF_ND_TLL,                 /* mac */
111
112     MFF_N_IDS
113 };
114
115 /* Prerequisites for matching a field.
116  *
117  * A field may only be matched if the correct lower-level protocols are also
118  * matched.  For example, the TCP port may be matched only if the Ethernet type
119  * matches ETH_TYPE_IP and the IP protocol matches IPPROTO_TCP. */
120 enum mf_prereqs {
121     MFP_NONE,
122
123     /* L2 requirements. */
124     MFP_ARP,
125     MFP_IPV4,
126     MFP_IPV6,
127     MFP_IP_ANY,
128
129     /* L2+L3 requirements. */
130     MFP_TCP,                    /* On IPv4 or IPv6. */
131     MFP_UDP,                    /* On IPv4 or IPv6. */
132     MFP_ICMPV4,
133     MFP_ICMPV6,
134
135     /* L2+L3+L4 requirements. */
136     MFP_ND,
137     MFP_ND_SOLICIT,
138     MFP_ND_ADVERT
139 };
140
141 /* Forms of partial-field masking allowed for a field.
142  *
143  * Every field may be masked as a whole. */
144 enum mf_maskable {
145     MFM_NONE,                   /* No sub-field masking. */
146     MFM_FULLY,                  /* Every bit is individually maskable. */
147     MFM_CIDR,                   /* Contiguous low-order bits may be masked. */
148     MFM_MCAST                   /* Byte 0, bit 0 is separately maskable. */
149 };
150
151 /* How to format or parse a field's value. */
152 enum mf_string {
153     /* Integer formats.
154      *
155      * The particular MFS_* constant sets the output format.  On input, either
156      * decimal or hexadecimal (prefixed with 0x) is accepted. */
157     MFS_DECIMAL,
158     MFS_HEXADECIMAL,
159
160     /* Other formats. */
161     MFS_ETHERNET,
162     MFS_IPV4,
163     MFS_IPV6,
164     MFS_OFP_PORT,               /* An OpenFlow port number or name. */
165     MFS_FRAG                    /* no, yes, first, later, not_later */
166 };
167
168 struct mf_field {
169     /* Identification. */
170     enum mf_field_id id;        /* MFF_*. */
171     const char *name;           /* Name of this field, e.g. "eth_type". */
172     const char *extra_name;     /* Alternate name, e.g. "dl_type", or NULL. */
173
174     /* Size.
175      *
176      * Most fields have n_bytes * 8 == n_bits.  There are a few exceptions:
177      *
178      *     - "dl_vlan" is 2 bytes but only 12 bits.
179      *     - "dl_vlan_pcp" is 1 byte but only 3 bits.
180      *     - "is_frag" is 1 byte but only 2 bits.
181      *     - "ipv6_label" is 4 bytes but only 20 bits.
182      */
183     unsigned int n_bytes;       /* Width of the field in bytes. */
184     unsigned int n_bits;        /* Number of significant bits in field. */
185
186     /* Properties. */
187     enum mf_maskable maskable;
188     flow_wildcards_t fww_bit;   /* Either 0 or exactly one FWW_* bit. */
189     enum mf_string string;
190     enum mf_prereqs prereqs;
191     bool writable;              /* May be written by actions? */
192
193     /* NXM properties.
194      *
195      * A few "mf_field"s don't correspond to NXM fields.  Those have 0 and
196      * NULL for the following members, respectively. */
197     uint32_t nxm_header;        /* An NXM_* constant (a few fields have 0). */
198     const char *nxm_name;       /* The "NXM_*" constant's name. */
199
200     /* OXM properties */
201     uint32_t oxm_header;        /* Field id in the OXM basic class,
202                                  * an OXM_* constant.
203                                  * Ignored if oxm_name is NULL */
204     const char *oxm_name;       /* The OXM_* constant's name,
205                                  * NULL if the field is not present
206                                  * in the OXM basic class */
207
208 };
209
210 /* The representation of a field's value. */
211 union mf_value {
212     uint8_t u8;
213     ovs_be16 be16;
214     ovs_be32 be32;
215     ovs_be64 be64;
216     uint8_t mac[ETH_ADDR_LEN];
217     struct in6_addr ipv6;
218 };
219 BUILD_ASSERT_DECL(sizeof(union mf_value) == 16);
220
221 /* Part of a field. */
222 struct mf_subfield {
223     const struct mf_field *field;
224     unsigned int ofs;           /* Bit offset. */
225     unsigned int n_bits;        /* Number of bits. */
226 };
227
228 /* Data for some part of an mf_field.
229  *
230  * The data is stored "right-justified".  For example, if "union mf_subvalue
231  * value" contains NXM_OF_VLAN_TCI[0..11], then one could access the
232  * corresponding data in value.be16[7] as the bits in the mask htons(0xfff). */
233 union mf_subvalue {
234     uint8_t u8[16];
235     ovs_be16 be16[8];
236     ovs_be32 be32[4];
237     ovs_be64 be64[2];
238 };
239 BUILD_ASSERT_DECL(sizeof(union mf_value) == sizeof (union mf_subvalue));
240
241 /* Finding mf_fields. */
242 const struct mf_field *mf_from_id(enum mf_field_id);
243 const struct mf_field *mf_from_name(const char *name);
244 const struct mf_field *mf_from_nxm_header(uint32_t nxm_header);
245 const struct mf_field *mf_from_nxm_name(const char *nxm_name);
246
247 /* Inspecting wildcarded bits. */
248 bool mf_is_all_wild(const struct mf_field *, const struct flow_wildcards *);
249
250 bool mf_is_mask_valid(const struct mf_field *, const union mf_value *mask);
251 void mf_get_mask(const struct mf_field *, const struct flow_wildcards *,
252                  union mf_value *mask);
253
254 /* Prerequisites. */
255 bool mf_are_prereqs_ok(const struct mf_field *, const struct flow *);
256 void mf_force_prereqs(const struct mf_field *, struct cls_rule *);
257
258 /* Field values. */
259 bool mf_is_value_valid(const struct mf_field *, const union mf_value *value);
260
261 void mf_get_value(const struct mf_field *, const struct flow *,
262                   union mf_value *value);
263 void mf_set_value(const struct mf_field *, const union mf_value *value,
264                   struct cls_rule *);
265 void mf_set_flow_value(const struct mf_field *, const union mf_value *value,
266                        struct flow *);
267
268 void mf_get(const struct mf_field *, const struct cls_rule *,
269             union mf_value *value, union mf_value *mask);
270 void mf_set(const struct mf_field *,
271             const union mf_value *value, const union mf_value *mask,
272             struct cls_rule *);
273
274 void mf_set_wild(const struct mf_field *, struct cls_rule *);
275
276 void mf_random_value(const struct mf_field *, union mf_value *value);
277
278 /* Subfields. */
279 void mf_write_subfield(const struct mf_subfield *, const union mf_subvalue *,
280                        struct cls_rule *);
281 void mf_set_subfield(const struct mf_subfield *, uint64_t value,
282                      struct cls_rule *);
283 void mf_set_subfield_value(const struct mf_subfield *, uint64_t value,
284                            struct flow *);
285
286 void mf_read_subfield(const struct mf_subfield *, const struct flow *,
287                       union mf_subvalue *);
288 uint64_t mf_get_subfield(const struct mf_subfield *, const struct flow *);
289
290
291 void mf_format_subfield(const struct mf_subfield *, struct ds *);
292 char *mf_parse_subfield__(struct mf_subfield *sf, const char **s);
293 const char *mf_parse_subfield(struct mf_subfield *, const char *);
294
295 enum ofperr mf_check_src(const struct mf_subfield *, const struct flow *);
296 enum ofperr mf_check_dst(const struct mf_subfield *, const struct flow *);
297
298 /* Parsing and formatting. */
299 char *mf_parse(const struct mf_field *, const char *,
300                union mf_value *value, union mf_value *mask);
301 char *mf_parse_value(const struct mf_field *, const char *, union mf_value *);
302 void mf_format(const struct mf_field *,
303                const union mf_value *value, const union mf_value *mask,
304                struct ds *);
305
306 #endif /* meta-flow.h */