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