meta-flow: New library for working with fields by id.
[sliver-openvswitch.git] / lib / meta-flow.h
1 /*
2  * Copyright (c) 2011 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 <netinet/ip6.h>
21 #include "flow.h"
22 #include "packets.h"
23
24 struct cls_rule;
25 struct ds;
26
27 /* The comment on each of these indicates the member in "union mf_value" used
28  * to represent its value. */
29 enum mf_field_id {
30     /* Metadata. */
31     MFF_TUN_ID,                 /* be64 */
32     MFF_IN_PORT,                /* be16 */
33
34 #if FLOW_N_REGS > 0
35     MFF_REG0,                   /* be32 */
36 #endif
37 #if FLOW_N_REGS > 1
38     MFF_REG1,                   /* be32 */
39 #endif
40 #if FLOW_N_REGS > 2
41     MFF_REG2,                   /* be32 */
42 #endif
43 #if FLOW_N_REGS > 3
44     MFF_REG3,                   /* be32 */
45 #endif
46 #if FLOW_N_REGS > 4
47 #error
48 #endif
49
50     /* L2. */
51     MFF_ETH_SRC,                /* mac */
52     MFF_ETH_DST,                /* mac */
53     MFF_ETH_TYPE,               /* be16 */
54
55     MFF_VLAN_TCI,               /* be16 */
56     MFF_VLAN_VID,               /* be16 */
57     MFF_VLAN_PCP,               /* u8 */
58
59     /* L3. */
60     MFF_IPV4_SRC,               /* be32 */
61     MFF_IPV4_DST,               /* be32 */
62
63     MFF_IPV6_SRC,               /* ipv6 */
64     MFF_IPV6_DST,               /* ipv6 */
65
66     MFF_IP_PROTO,               /* u8 (used for IPv4 or IPv6) */
67     MFF_IP_TOS,                 /* u8 (used for IPv4 or IPv6) */
68
69     MFF_ARP_OP,                 /* be16 */
70     MFF_ARP_SPA,                /* be32 */
71     MFF_ARP_TPA,                /* be32 */
72     MFF_ARP_SHA,                /* mac */
73     MFF_ARP_THA,                /* mac */
74
75     /* L4. */
76     MFF_TCP_SRC,                /* be16 (used for IPv4 or IPv6) */
77     MFF_TCP_DST,                /* be16 (used for IPv4 or IPv6) */
78
79     MFF_UDP_SRC,                /* be16 (used for IPv4 or IPv6) */
80     MFF_UDP_DST,                /* be16 (used for IPv4 or IPv6) */
81
82     MFF_ICMP_TYPE,              /* u8 (used for IPv4 or IPv6) */
83     MFF_ICMP_CODE,              /* u8 (used for IPv4 or IPv6) */
84
85     /* ICMPv6 Neighbor Discovery. */
86     MFF_ND_TARGET,              /* ipv6 */
87     MFF_ND_SLL,                 /* mac */
88     MFF_ND_TLL,                 /* mac */
89
90     MFF_N_IDS
91 };
92
93 /* Prerequisites for matching a field.
94  *
95  * A field may only be matched if the correct lower-level protocols are also
96  * matched.  For example, the TCP port may be matched only if the Ethernet type
97  * matches ETH_TYPE_IP and the IP protocol matches IPPROTO_TCP. */
98 enum mf_prereqs {
99     MFP_NONE,
100
101     /* L2 requirements. */
102     MFP_ARP,
103     MFP_IPV4,
104     MFP_IPV6,
105     MFP_IP_ANY,
106
107     /* L2+L3 requirements. */
108     MFP_TCP,                    /* On IPv4 or IPv6. */
109     MFP_UDP,                    /* On IPv4 or IPv6. */
110     MFP_ICMPV6,
111     MFP_ICMP_ANY,
112
113     /* L2+L3+L4 requirements. */
114     MFP_ND,
115     MFP_ND_SOLICIT,
116     MFP_ND_ADVERT
117 };
118
119 /* Forms of partial-field masking allowed for a field.
120  *
121  * Every field may be masked as a whole. */
122 enum mf_maskable {
123     MFM_NONE,                   /* No sub-field masking. */
124     MFM_FULLY,                  /* Every bit is individually maskable. */
125     MFM_CIDR,                   /* Contiguous low-order bits may be masked. */
126     MFM_MCAST                   /* Byte 0, bit 0 is separately maskable. */
127 };
128
129 /* How to format or parse a field's value. */
130 enum mf_string {
131     /* Integer formats.
132      *
133      * The particular MFS_* constant sets the output format.  On input, either
134      * decimal or hexadecimal (prefixed with 0x) is accepted. */
135     MFS_DECIMAL,
136     MFS_HEXADECIMAL,
137
138     /* Other formats. */
139     MFS_ETHERNET,
140     MFS_IPV4,
141     MFS_IPV6,
142     MFS_OFP_PORT                /* An OpenFlow port number or name. */
143 };
144
145 struct mf_field {
146     /* Identification. */
147     enum mf_field_id id;        /* MFF_*. */
148     const char *name;           /* Name of this field, e.g. "eth_type". */
149     const char *extra_name;     /* Alternate name, e.g. "dl_type", or NULL. */
150
151     /* Size.
152      *
153      * Most fields have n_bytes * 8 == n_bits.  There are only two exceptions
154      * currently: "dl_vlan" is 2 bytes but only 12 bits, and "dl_vlan_pcp" is
155      * 1 byte but only 3 bits. */
156     unsigned int n_bytes;       /* Width of the field in bytes. */
157     unsigned int n_bits;        /* Number of significant bits in field. */
158
159     /* Properties. */
160     enum mf_maskable maskable;
161     flow_wildcards_t fww_bit;   /* Either 0 or exactly one FWW_* bit. */
162     enum mf_string string;
163     enum mf_prereqs prereqs;
164     uint32_t nxm_header;        /* An NXM_* constant (a few fields have 0). */
165 };
166
167 /* The representation of a field's value. */
168 union mf_value {
169     uint8_t u8;
170     ovs_be16 be16;
171     ovs_be32 be32;
172     ovs_be64 be64;
173     uint8_t mac[ETH_ADDR_LEN];
174     struct in6_addr ipv6;
175 };
176
177 /* Finding mf_fields. */
178 const struct mf_field *mf_from_id(enum mf_field_id);
179 const struct mf_field *mf_from_name(const char *name);
180
181 /* Inspecting wildcarded bits. */
182 bool mf_is_all_wild(const struct mf_field *, const struct flow_wildcards *);
183
184 bool mf_is_mask_valid(const struct mf_field *, const union mf_value *mask);
185 void mf_get_mask(const struct mf_field *, const struct flow_wildcards *,
186                  union mf_value *mask);
187
188 /* Prerequisites. */
189 bool mf_are_prereqs_ok(const struct mf_field *, const struct flow *);
190 void mf_force_prereqs(const struct mf_field *, struct cls_rule *);
191
192 /* Field values. */
193 bool mf_is_value_valid(const struct mf_field *, const union mf_value *value);
194
195 void mf_get_value(const struct mf_field *, const struct flow *,
196                   union mf_value *value);
197 void mf_set_value(const struct mf_field *, const union mf_value *value,
198                   struct cls_rule *);
199
200 void mf_get(const struct mf_field *, const struct cls_rule *,
201             union mf_value *value, union mf_value *mask);
202 void mf_set(const struct mf_field *,
203             const union mf_value *value, const union mf_value *mask,
204             struct cls_rule *);
205 void mf_set_subfield(const struct mf_field *, uint64_t value, unsigned int ofs,
206                      unsigned int n_bits, struct cls_rule *);
207
208 void mf_set_wild(const struct mf_field *, struct cls_rule *);
209
210 void mf_random_value(const struct mf_field *, union mf_value *value);
211
212 /* Parsing and formatting. */
213 char *mf_parse(const struct mf_field *, const char *,
214                union mf_value *value, union mf_value *mask);
215 char *mf_parse_value(const struct mf_field *, const char *, union mf_value *);
216 void mf_format(const struct mf_field *,
217                const union mf_value *value, const union mf_value *mask,
218                struct ds *);
219
220 #endif /* meta-flow.h */