meta-flow: New "subfield" data structure.
[sliver-openvswitch.git] / lib / meta-flow.h
1 /*
2  * Copyright (c) 2011, 2012 Nicira Networks.
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 #error
54 #endif
55
56     /* L2. */
57     MFF_ETH_SRC,                /* mac */
58     MFF_ETH_DST,                /* mac */
59     MFF_ETH_TYPE,               /* be16 */
60
61     MFF_VLAN_TCI,               /* be16 */
62     MFF_VLAN_VID,               /* be16 */
63     MFF_VLAN_PCP,               /* u8 */
64
65     /* L3. */
66     MFF_IPV4_SRC,               /* be32 */
67     MFF_IPV4_DST,               /* be32 */
68
69     MFF_IPV6_SRC,               /* ipv6 */
70     MFF_IPV6_DST,               /* ipv6 */
71     MFF_IPV6_LABEL,             /* be32 */
72
73     MFF_IP_PROTO,               /* u8 (used for IPv4 or IPv6) */
74     MFF_IP_DSCP,                /* u8 (used for IPv4 or IPv6) */
75     MFF_IP_ECN,                 /* u8 (used for IPv4 or IPv6) */
76     MFF_IP_TTL,                 /* u8 (used for IPv4 or IPv6) */
77     MFF_IP_FRAG,                /* u8 (used for IPv4 or IPv6) */
78
79     MFF_ARP_OP,                 /* be16 */
80     MFF_ARP_SPA,                /* be32 */
81     MFF_ARP_TPA,                /* be32 */
82     MFF_ARP_SHA,                /* mac */
83     MFF_ARP_THA,                /* mac */
84
85     /* L4. */
86     MFF_TCP_SRC,                /* be16 (used for IPv4 or IPv6) */
87     MFF_TCP_DST,                /* be16 (used for IPv4 or IPv6) */
88
89     MFF_UDP_SRC,                /* be16 (used for IPv4 or IPv6) */
90     MFF_UDP_DST,                /* be16 (used for IPv4 or IPv6) */
91
92     MFF_ICMPV4_TYPE,            /* u8 */
93     MFF_ICMPV4_CODE,            /* u8 */
94
95     MFF_ICMPV6_TYPE,            /* u8 */
96     MFF_ICMPV6_CODE,            /* u8 */
97
98     /* ICMPv6 Neighbor Discovery. */
99     MFF_ND_TARGET,              /* ipv6 */
100     MFF_ND_SLL,                 /* mac */
101     MFF_ND_TLL,                 /* mac */
102
103     MFF_N_IDS
104 };
105
106 /* Prerequisites for matching a field.
107  *
108  * A field may only be matched if the correct lower-level protocols are also
109  * matched.  For example, the TCP port may be matched only if the Ethernet type
110  * matches ETH_TYPE_IP and the IP protocol matches IPPROTO_TCP. */
111 enum mf_prereqs {
112     MFP_NONE,
113
114     /* L2 requirements. */
115     MFP_ARP,
116     MFP_IPV4,
117     MFP_IPV6,
118     MFP_IP_ANY,
119
120     /* L2+L3 requirements. */
121     MFP_TCP,                    /* On IPv4 or IPv6. */
122     MFP_UDP,                    /* On IPv4 or IPv6. */
123     MFP_ICMPV4,
124     MFP_ICMPV6,
125
126     /* L2+L3+L4 requirements. */
127     MFP_ND,
128     MFP_ND_SOLICIT,
129     MFP_ND_ADVERT
130 };
131
132 /* Forms of partial-field masking allowed for a field.
133  *
134  * Every field may be masked as a whole. */
135 enum mf_maskable {
136     MFM_NONE,                   /* No sub-field masking. */
137     MFM_FULLY,                  /* Every bit is individually maskable. */
138     MFM_CIDR,                   /* Contiguous low-order bits may be masked. */
139     MFM_MCAST                   /* Byte 0, bit 0 is separately maskable. */
140 };
141
142 /* How to format or parse a field's value. */
143 enum mf_string {
144     /* Integer formats.
145      *
146      * The particular MFS_* constant sets the output format.  On input, either
147      * decimal or hexadecimal (prefixed with 0x) is accepted. */
148     MFS_DECIMAL,
149     MFS_HEXADECIMAL,
150
151     /* Other formats. */
152     MFS_ETHERNET,
153     MFS_IPV4,
154     MFS_IPV6,
155     MFS_OFP_PORT,               /* An OpenFlow port number or name. */
156     MFS_FRAG                    /* no, yes, first, later, not_later */
157 };
158
159 struct mf_field {
160     /* Identification. */
161     enum mf_field_id id;        /* MFF_*. */
162     const char *name;           /* Name of this field, e.g. "eth_type". */
163     const char *extra_name;     /* Alternate name, e.g. "dl_type", or NULL. */
164
165     /* Size.
166      *
167      * Most fields have n_bytes * 8 == n_bits.  There are a few exceptions:
168      *
169      *     - "dl_vlan" is 2 bytes but only 12 bits.
170      *     - "dl_vlan_pcp" is 1 byte but only 3 bits.
171      *     - "is_frag" is 1 byte but only 2 bits.
172      *     - "ipv6_label" is 4 bytes but only 20 bits.
173      */
174     unsigned int n_bytes;       /* Width of the field in bytes. */
175     unsigned int n_bits;        /* Number of significant bits in field. */
176
177     /* Properties. */
178     enum mf_maskable maskable;
179     flow_wildcards_t fww_bit;   /* Either 0 or exactly one FWW_* bit. */
180     enum mf_string string;
181     enum mf_prereqs prereqs;
182     bool writable;              /* May be written by actions? */
183
184     /* NXM properties.
185      *
186      * A few "mf_field"s don't correspond to NXM fields.  Those have 0 and
187      * NULL for the following members, respectively. */
188     uint32_t nxm_header;        /* An NXM_* constant (a few fields have 0). */
189     const char *nxm_name;       /* The "NXM_*" constant's name. */
190 };
191
192 /* The representation of a field's value. */
193 union mf_value {
194     uint8_t u8;
195     ovs_be16 be16;
196     ovs_be32 be32;
197     ovs_be64 be64;
198     uint8_t mac[ETH_ADDR_LEN];
199     struct in6_addr ipv6;
200 };
201
202 /* Part of a field. */
203 struct mf_subfield {
204     const struct mf_field *field;
205     unsigned int ofs;           /* Bit offset. */
206     unsigned int n_bits;        /* Number of bits. */
207 };
208
209 /* Finding mf_fields. */
210 const struct mf_field *mf_from_id(enum mf_field_id);
211 const struct mf_field *mf_from_name(const char *name);
212 const struct mf_field *mf_from_nxm_header(uint32_t nxm_header);
213 const struct mf_field *mf_from_nxm_name(const char *nxm_name);
214
215 /* Inspecting wildcarded bits. */
216 bool mf_is_all_wild(const struct mf_field *, const struct flow_wildcards *);
217
218 bool mf_is_mask_valid(const struct mf_field *, const union mf_value *mask);
219 void mf_get_mask(const struct mf_field *, const struct flow_wildcards *,
220                  union mf_value *mask);
221
222 /* Prerequisites. */
223 bool mf_are_prereqs_ok(const struct mf_field *, const struct flow *);
224 void mf_force_prereqs(const struct mf_field *, struct cls_rule *);
225
226 /* Field values. */
227 bool mf_is_value_valid(const struct mf_field *, const union mf_value *value);
228
229 void mf_get_value(const struct mf_field *, const struct flow *,
230                   union mf_value *value);
231 void mf_set_value(const struct mf_field *, const union mf_value *value,
232                   struct cls_rule *);
233 void mf_set_flow_value(const struct mf_field *, const union mf_value *value,
234                        struct flow *);
235
236 void mf_get(const struct mf_field *, const struct cls_rule *,
237             union mf_value *value, union mf_value *mask);
238 void mf_set(const struct mf_field *,
239             const union mf_value *value, const union mf_value *mask,
240             struct cls_rule *);
241
242 void mf_set_wild(const struct mf_field *, struct cls_rule *);
243
244 void mf_random_value(const struct mf_field *, union mf_value *value);
245
246 /* Subfields. */
247 void mf_set_subfield(const struct mf_subfield *, uint64_t value,
248                      struct cls_rule *);
249 void mf_set_subfield_value(const struct mf_subfield *, uint64_t value,
250                            struct flow *);
251 uint64_t mf_get_subfield(const struct mf_subfield *, const struct flow *);
252
253 void mf_format_subfield(const struct mf_subfield *, struct ds *);
254 char *mf_parse_subfield__(struct mf_subfield *sf, const char **s);
255 const char *mf_parse_subfield(struct mf_subfield *, const char *);
256
257 enum ofperr mf_check_src(const struct mf_subfield *, const struct flow *);
258 enum ofperr mf_check_dst(const struct mf_subfield *, const struct flow *);
259
260 /* Parsing and formatting. */
261 char *mf_parse(const struct mf_field *, const char *,
262                union mf_value *value, union mf_value *mask);
263 char *mf_parse_value(const struct mf_field *, const char *, union mf_value *);
264 void mf_format(const struct mf_field *,
265                const union mf_value *value, const union mf_value *mask,
266                struct ds *);
267
268 #endif /* meta-flow.h */