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