]>
Commit | Line | Data |
---|---|---|
6a885fd0 | 1 | /* |
9d84066c | 2 | * Copyright (c) 2011, 2012, 2013, 2014 Nicira, Inc. |
6a885fd0 BP |
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 | #include <config.h> | |
18 | ||
19 | #include "meta-flow.h" | |
20 | ||
6a885fd0 BP |
21 | #include <errno.h> |
22 | #include <limits.h> | |
23 | #include <netinet/icmp6.h> | |
24 | #include <netinet/ip6.h> | |
25 | ||
26 | #include "classifier.h" | |
27 | #include "dynamic-string.h" | |
178742f9 | 28 | #include "nx-match.h" |
816fd533 | 29 | #include "ofp-errors.h" |
6a885fd0 | 30 | #include "ofp-util.h" |
4663f9e0 | 31 | #include "ovs-thread.h" |
6a885fd0 BP |
32 | #include "packets.h" |
33 | #include "random.h" | |
34 | #include "shash.h" | |
35 | #include "socket-util.h" | |
36 | #include "unaligned.h" | |
86f35fb5 | 37 | #include "util.h" |
e6211adc | 38 | #include "openvswitch/vlog.h" |
816fd533 BP |
39 | |
40 | VLOG_DEFINE_THIS_MODULE(meta_flow); | |
6a885fd0 | 41 | |
13751fd8 JR |
42 | #define FLOW_U32OFS(FIELD) \ |
43 | offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4 | |
44 | ||
6a885fd0 BP |
45 | #define MF_FIELD_SIZES(MEMBER) \ |
46 | sizeof ((union mf_value *)0)->MEMBER, \ | |
47 | 8 * sizeof ((union mf_value *)0)->MEMBER | |
48 | ||
7f98c44d JR |
49 | extern const struct mf_field mf_fields[MFF_N_IDS]; /* Silence a warning. */ |
50 | ||
51 | const struct mf_field mf_fields[MFF_N_IDS] = { | |
a4ce8b25 | 52 | #include "meta-flow.inc" |
6a885fd0 BP |
53 | }; |
54 | ||
4663f9e0 BP |
55 | /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */ |
56 | static struct shash mf_by_name; | |
28da1f8f | 57 | |
816fd533 BP |
58 | /* Rate limit for parse errors. These always indicate a bug in an OpenFlow |
59 | * controller and so there's not much point in showing a lot of them. */ | |
60 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
61 | ||
4663f9e0 | 62 | static void nxm_init(void); |
b5e5143b | 63 | |
6a885fd0 BP |
64 | /* Returns the field with the given 'name', or a null pointer if no field has |
65 | * that name. */ | |
66 | const struct mf_field * | |
67 | mf_from_name(const char *name) | |
68 | { | |
4663f9e0 | 69 | nxm_init(); |
6a885fd0 BP |
70 | return shash_find_data(&mf_by_name, name); |
71 | } | |
72 | ||
28da1f8f | 73 | static void |
4663f9e0 | 74 | nxm_do_init(void) |
28da1f8f | 75 | { |
216a32dd | 76 | int i; |
28da1f8f | 77 | |
4663f9e0 | 78 | shash_init(&mf_by_name); |
216a32dd JR |
79 | for (i = 0; i < MFF_N_IDS; i++) { |
80 | const struct mf_field *mf = &mf_fields[i]; | |
81 | ||
82 | ovs_assert(mf->id == i); /* Fields must be in the enum order. */ | |
83 | ||
4663f9e0 BP |
84 | shash_add_once(&mf_by_name, mf->name, mf); |
85 | if (mf->extra_name) { | |
86 | shash_add_once(&mf_by_name, mf->extra_name, mf); | |
87 | } | |
28da1f8f | 88 | } |
28da1f8f BP |
89 | } |
90 | ||
4663f9e0 BP |
91 | static void |
92 | nxm_init(void) | |
93 | { | |
94 | static pthread_once_t once = PTHREAD_ONCE_INIT; | |
95 | pthread_once(&once, nxm_do_init); | |
96 | } | |
97 | ||
6a885fd0 BP |
98 | /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc' |
99 | * specifies at least one bit in the field. | |
100 | * | |
101 | * The caller is responsible for ensuring that 'wc' corresponds to a flow that | |
102 | * meets 'mf''s prerequisites. */ | |
103 | bool | |
104 | mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc) | |
105 | { | |
106 | switch (mf->id) { | |
a79f29f2 AZ |
107 | case MFF_DP_HASH: |
108 | return !wc->masks.dp_hash; | |
109 | case MFF_RECIRC_ID: | |
110 | return !wc->masks.recirc_id; | |
18080541 BP |
111 | case MFF_CONJ_ID: |
112 | return !wc->masks.conj_id; | |
4fe3445a | 113 | case MFF_TUN_SRC: |
0ad90c84 | 114 | return !wc->masks.tunnel.ip_src; |
4fe3445a | 115 | case MFF_TUN_DST: |
0ad90c84 JR |
116 | return !wc->masks.tunnel.ip_dst; |
117 | case MFF_TUN_ID: | |
4fe3445a PS |
118 | case MFF_TUN_TOS: |
119 | case MFF_TUN_TTL: | |
120 | case MFF_TUN_FLAGS: | |
296e07ac | 121 | return !wc->masks.tunnel.tun_id; |
ac6073e3 MC |
122 | case MFF_TUN_GBP_ID: |
123 | return !wc->masks.tunnel.gbp_id; | |
124 | case MFF_TUN_GBP_FLAGS: | |
125 | return !wc->masks.tunnel.gbp_flags; | |
969fc56c | 126 | case MFF_METADATA: |
26720e24 | 127 | return !wc->masks.metadata; |
0bdc4bec | 128 | case MFF_IN_PORT: |
72333065 | 129 | case MFF_IN_PORT_OXM: |
4e022ec0 | 130 | return !wc->masks.in_port.ofp_port; |
1b567fb9 AA |
131 | case MFF_SKB_PRIORITY: |
132 | return !wc->masks.skb_priority; | |
1362e248 JG |
133 | case MFF_PKT_MARK: |
134 | return !wc->masks.pkt_mark; | |
0d7e2fe4 | 135 | CASE_MFF_REGS: |
26720e24 | 136 | return !wc->masks.regs[mf->id - MFF_REG0]; |
79fe0f46 BP |
137 | CASE_MFF_XREGS: |
138 | return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0); | |
c61f3870 BP |
139 | case MFF_ACTSET_OUTPUT: |
140 | return !wc->masks.actset_output; | |
6a885fd0 | 141 | |
73c0ce34 | 142 | case MFF_ETH_SRC: |
26720e24 | 143 | return eth_addr_is_zero(wc->masks.dl_src); |
6a885fd0 | 144 | case MFF_ETH_DST: |
26720e24 | 145 | return eth_addr_is_zero(wc->masks.dl_dst); |
e2170cff | 146 | case MFF_ETH_TYPE: |
26720e24 | 147 | return !wc->masks.dl_type; |
6a885fd0 | 148 | |
e878338b SH |
149 | case MFF_ARP_SHA: |
150 | case MFF_ND_SLL: | |
26720e24 | 151 | return eth_addr_is_zero(wc->masks.arp_sha); |
e878338b SH |
152 | |
153 | case MFF_ARP_THA: | |
154 | case MFF_ND_TLL: | |
26720e24 | 155 | return eth_addr_is_zero(wc->masks.arp_tha); |
e878338b | 156 | |
6a885fd0 | 157 | case MFF_VLAN_TCI: |
26720e24 | 158 | return !wc->masks.vlan_tci; |
441c57a9 | 159 | case MFF_DL_VLAN: |
26720e24 | 160 | return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK)); |
cc34bc8c | 161 | case MFF_VLAN_VID: |
26720e24 | 162 | return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI)); |
441c57a9 | 163 | case MFF_DL_VLAN_PCP: |
cc34bc8c | 164 | case MFF_VLAN_PCP: |
26720e24 | 165 | return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK)); |
6a885fd0 | 166 | |
b02475c5 | 167 | case MFF_MPLS_LABEL: |
8bfd0fda | 168 | return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)); |
b02475c5 | 169 | case MFF_MPLS_TC: |
60df616f | 170 | return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK)); |
b02475c5 | 171 | case MFF_MPLS_BOS: |
60df616f | 172 | return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)); |
b02475c5 | 173 | |
6a885fd0 | 174 | case MFF_IPV4_SRC: |
26720e24 | 175 | return !wc->masks.nw_src; |
6a885fd0 | 176 | case MFF_IPV4_DST: |
26720e24 | 177 | return !wc->masks.nw_dst; |
6a885fd0 BP |
178 | |
179 | case MFF_IPV6_SRC: | |
26720e24 | 180 | return ipv6_mask_is_any(&wc->masks.ipv6_src); |
6a885fd0 | 181 | case MFF_IPV6_DST: |
26720e24 | 182 | return ipv6_mask_is_any(&wc->masks.ipv6_dst); |
6a885fd0 | 183 | |
32455024 | 184 | case MFF_IPV6_LABEL: |
26720e24 | 185 | return !wc->masks.ipv6_label; |
32455024 | 186 | |
851d3105 | 187 | case MFF_IP_PROTO: |
26720e24 | 188 | return !wc->masks.nw_proto; |
5d9499c4 | 189 | case MFF_IP_DSCP: |
1638b906 | 190 | case MFF_IP_DSCP_SHIFTED: |
26720e24 | 191 | return !(wc->masks.nw_tos & IP_DSCP_MASK); |
5d9499c4 | 192 | case MFF_IP_ECN: |
26720e24 | 193 | return !(wc->masks.nw_tos & IP_ECN_MASK); |
3840c406 | 194 | case MFF_IP_TTL: |
26720e24 | 195 | return !wc->masks.nw_ttl; |
5d9499c4 | 196 | |
47284b1f | 197 | case MFF_ND_TARGET: |
26720e24 | 198 | return ipv6_mask_is_any(&wc->masks.nd_target); |
47284b1f | 199 | |
7257b535 | 200 | case MFF_IP_FRAG: |
26720e24 | 201 | return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK); |
7257b535 | 202 | |
851d3105 | 203 | case MFF_ARP_OP: |
26720e24 | 204 | return !wc->masks.nw_proto; |
6a885fd0 | 205 | case MFF_ARP_SPA: |
26720e24 | 206 | return !wc->masks.nw_src; |
6a885fd0 | 207 | case MFF_ARP_TPA: |
26720e24 | 208 | return !wc->masks.nw_dst; |
6a885fd0 | 209 | |
73f33563 BP |
210 | case MFF_TCP_SRC: |
211 | case MFF_UDP_SRC: | |
0d56eaf2 | 212 | case MFF_SCTP_SRC: |
73f33563 BP |
213 | case MFF_ICMPV4_TYPE: |
214 | case MFF_ICMPV6_TYPE: | |
26720e24 | 215 | return !wc->masks.tp_src; |
73f33563 BP |
216 | case MFF_TCP_DST: |
217 | case MFF_UDP_DST: | |
0d56eaf2 | 218 | case MFF_SCTP_DST: |
73f33563 BP |
219 | case MFF_ICMPV4_CODE: |
220 | case MFF_ICMPV6_CODE: | |
26720e24 | 221 | return !wc->masks.tp_dst; |
dc235f7f JR |
222 | case MFF_TCP_FLAGS: |
223 | return !wc->masks.tcp_flags; | |
73f33563 | 224 | |
6a885fd0 BP |
225 | case MFF_N_IDS: |
226 | default: | |
428b2edd | 227 | OVS_NOT_REACHED(); |
6a885fd0 BP |
228 | } |
229 | } | |
230 | ||
231 | /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'. | |
232 | * Each bit in 'mask' will be set to 1 if the bit is significant for matching | |
233 | * purposes, or to 0 if it is wildcarded. | |
234 | * | |
235 | * The caller is responsible for ensuring that 'wc' corresponds to a flow that | |
236 | * meets 'mf''s prerequisites. */ | |
237 | void | |
238 | mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc, | |
239 | union mf_value *mask) | |
240 | { | |
fb15feb0 | 241 | mf_get_value(mf, &wc->masks, mask); |
6a885fd0 BP |
242 | } |
243 | ||
244 | /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true | |
245 | * if the mask is valid, false otherwise. */ | |
246 | bool | |
247 | mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask) | |
248 | { | |
249 | switch (mf->maskable) { | |
250 | case MFM_NONE: | |
53cb9c3e JR |
251 | return (is_all_zeros(mask, mf->n_bytes) || |
252 | is_all_ones(mask, mf->n_bytes)); | |
6a885fd0 BP |
253 | |
254 | case MFM_FULLY: | |
255 | return true; | |
6a885fd0 BP |
256 | } |
257 | ||
428b2edd | 258 | OVS_NOT_REACHED(); |
6a885fd0 BP |
259 | } |
260 | ||
6a885fd0 BP |
261 | /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */ |
262 | bool | |
263 | mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow) | |
264 | { | |
265 | switch (mf->prereqs) { | |
266 | case MFP_NONE: | |
267 | return true; | |
268 | ||
269 | case MFP_ARP: | |
8087f5ff MM |
270 | return (flow->dl_type == htons(ETH_TYPE_ARP) || |
271 | flow->dl_type == htons(ETH_TYPE_RARP)); | |
6a885fd0 BP |
272 | case MFP_IPV4: |
273 | return flow->dl_type == htons(ETH_TYPE_IP); | |
274 | case MFP_IPV6: | |
275 | return flow->dl_type == htons(ETH_TYPE_IPV6); | |
8069b0da | 276 | case MFP_VLAN_VID: |
5921e99a | 277 | return (flow->vlan_tci & htons(VLAN_CFI)) != 0; |
b02475c5 SH |
278 | case MFP_MPLS: |
279 | return eth_type_mpls(flow->dl_type); | |
6a885fd0 BP |
280 | case MFP_IP_ANY: |
281 | return is_ip_any(flow); | |
282 | ||
283 | case MFP_TCP: | |
b8778a0d JR |
284 | return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP |
285 | && !(flow->nw_frag & FLOW_NW_FRAG_LATER); | |
6a885fd0 | 286 | case MFP_UDP: |
b8778a0d JR |
287 | return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP |
288 | && !(flow->nw_frag & FLOW_NW_FRAG_LATER); | |
0d56eaf2 | 289 | case MFP_SCTP: |
b8778a0d JR |
290 | return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP |
291 | && !(flow->nw_frag & FLOW_NW_FRAG_LATER); | |
268a95e0 BP |
292 | case MFP_ICMPV4: |
293 | return is_icmpv4(flow); | |
6a885fd0 BP |
294 | case MFP_ICMPV6: |
295 | return is_icmpv6(flow); | |
6a885fd0 BP |
296 | |
297 | case MFP_ND: | |
298 | return (is_icmpv6(flow) | |
3ee8a9f0 BP |
299 | && flow->tp_dst == htons(0) |
300 | && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) || | |
301 | flow->tp_src == htons(ND_NEIGHBOR_ADVERT))); | |
6a885fd0 BP |
302 | case MFP_ND_SOLICIT: |
303 | return (is_icmpv6(flow) | |
3ee8a9f0 BP |
304 | && flow->tp_dst == htons(0) |
305 | && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT))); | |
6a885fd0 BP |
306 | case MFP_ND_ADVERT: |
307 | return (is_icmpv6(flow) | |
3ee8a9f0 BP |
308 | && flow->tp_dst == htons(0) |
309 | && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT))); | |
6a885fd0 BP |
310 | } |
311 | ||
428b2edd | 312 | OVS_NOT_REACHED(); |
6a885fd0 BP |
313 | } |
314 | ||
b283836c JR |
315 | /* Set field and it's prerequisities in the mask. |
316 | * This is only ever called for writeable 'mf's, but we do not make the | |
317 | * distinction here. */ | |
318 | void | |
319 | mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow *mask) | |
320 | { | |
321 | static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER; | |
322 | ||
323 | mf_set_flow_value(mf, &exact_match_mask, mask); | |
324 | ||
325 | switch (mf->prereqs) { | |
326 | case MFP_ND: | |
327 | case MFP_ND_SOLICIT: | |
328 | case MFP_ND_ADVERT: | |
329 | mask->tp_src = OVS_BE16_MAX; | |
330 | mask->tp_dst = OVS_BE16_MAX; | |
331 | /* Fall through. */ | |
332 | case MFP_TCP: | |
333 | case MFP_UDP: | |
334 | case MFP_SCTP: | |
335 | case MFP_ICMPV4: | |
336 | case MFP_ICMPV6: | |
b8778a0d | 337 | /* nw_frag always unwildcarded. */ |
b283836c JR |
338 | mask->nw_proto = 0xff; |
339 | /* Fall through. */ | |
340 | case MFP_ARP: | |
341 | case MFP_IPV4: | |
342 | case MFP_IPV6: | |
343 | case MFP_MPLS: | |
344 | case MFP_IP_ANY: | |
345 | mask->dl_type = OVS_BE16_MAX; | |
346 | break; | |
347 | case MFP_VLAN_VID: | |
348 | mask->vlan_tci |= htons(VLAN_CFI); | |
349 | break; | |
350 | case MFP_NONE: | |
351 | break; | |
352 | } | |
353 | } | |
354 | ||
0c4b9393 SH |
355 | /* Set bits of 'bm' corresponding to the field 'mf' and it's prerequisities. */ |
356 | void | |
357 | mf_bitmap_set_field_and_prereqs(const struct mf_field *mf, struct mf_bitmap *bm) | |
358 | { | |
359 | bitmap_set1(bm->bm, mf->id); | |
360 | ||
361 | switch (mf->prereqs) { | |
362 | case MFP_ND: | |
363 | case MFP_ND_SOLICIT: | |
364 | case MFP_ND_ADVERT: | |
365 | bitmap_set1(bm->bm, MFF_TCP_SRC); | |
366 | bitmap_set1(bm->bm, MFF_TCP_DST); | |
367 | /* Fall through. */ | |
368 | case MFP_TCP: | |
369 | case MFP_UDP: | |
370 | case MFP_SCTP: | |
371 | case MFP_ICMPV4: | |
372 | case MFP_ICMPV6: | |
373 | /* nw_frag always unwildcarded. */ | |
374 | bitmap_set1(bm->bm, MFF_IP_PROTO); | |
375 | /* Fall through. */ | |
376 | case MFP_ARP: | |
377 | case MFP_IPV4: | |
378 | case MFP_IPV6: | |
379 | case MFP_MPLS: | |
380 | case MFP_IP_ANY: | |
381 | bitmap_set1(bm->bm, MFF_ETH_TYPE); | |
382 | break; | |
383 | case MFP_VLAN_VID: | |
384 | bitmap_set1(bm->bm, MFF_VLAN_TCI); | |
385 | break; | |
386 | case MFP_NONE: | |
387 | break; | |
388 | } | |
389 | } | |
b283836c | 390 | |
6a885fd0 BP |
391 | /* Returns true if 'value' may be a valid value *as part of a masked match*, |
392 | * false otherwise. | |
393 | * | |
394 | * A value is not rejected just because it is not valid for the field in | |
395 | * question, but only if it doesn't make sense to test the bits in question at | |
396 | * all. For example, the MFF_VLAN_TCI field will never have a nonzero value | |
397 | * without the VLAN_CFI bit being set, but we can't reject those values because | |
398 | * it is still legitimate to test just for those bits (see the documentation | |
399 | * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a | |
530180fd | 400 | * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */ |
6a885fd0 BP |
401 | bool |
402 | mf_is_value_valid(const struct mf_field *mf, const union mf_value *value) | |
403 | { | |
404 | switch (mf->id) { | |
a79f29f2 AZ |
405 | case MFF_DP_HASH: |
406 | case MFF_RECIRC_ID: | |
18080541 | 407 | case MFF_CONJ_ID: |
6a885fd0 | 408 | case MFF_TUN_ID: |
4fe3445a PS |
409 | case MFF_TUN_SRC: |
410 | case MFF_TUN_DST: | |
411 | case MFF_TUN_TOS: | |
412 | case MFF_TUN_TTL: | |
413 | case MFF_TUN_FLAGS: | |
ac6073e3 MC |
414 | case MFF_TUN_GBP_ID: |
415 | case MFF_TUN_GBP_FLAGS: | |
969fc56c | 416 | case MFF_METADATA: |
6a885fd0 | 417 | case MFF_IN_PORT: |
1b567fb9 | 418 | case MFF_SKB_PRIORITY: |
1362e248 | 419 | case MFF_PKT_MARK: |
0d7e2fe4 | 420 | CASE_MFF_REGS: |
79fe0f46 | 421 | CASE_MFF_XREGS: |
6a885fd0 BP |
422 | case MFF_ETH_SRC: |
423 | case MFF_ETH_DST: | |
424 | case MFF_ETH_TYPE: | |
425 | case MFF_VLAN_TCI: | |
426 | case MFF_IPV4_SRC: | |
427 | case MFF_IPV4_DST: | |
428 | case MFF_IPV6_SRC: | |
429 | case MFF_IPV6_DST: | |
430 | case MFF_IP_PROTO: | |
a61680c6 | 431 | case MFF_IP_TTL: |
6a885fd0 BP |
432 | case MFF_ARP_SPA: |
433 | case MFF_ARP_TPA: | |
434 | case MFF_ARP_SHA: | |
435 | case MFF_ARP_THA: | |
436 | case MFF_TCP_SRC: | |
437 | case MFF_TCP_DST: | |
438 | case MFF_UDP_SRC: | |
439 | case MFF_UDP_DST: | |
0d56eaf2 JS |
440 | case MFF_SCTP_SRC: |
441 | case MFF_SCTP_DST: | |
268a95e0 BP |
442 | case MFF_ICMPV4_TYPE: |
443 | case MFF_ICMPV4_CODE: | |
444 | case MFF_ICMPV6_TYPE: | |
445 | case MFF_ICMPV6_CODE: | |
6a885fd0 BP |
446 | case MFF_ND_TARGET: |
447 | case MFF_ND_SLL: | |
448 | case MFF_ND_TLL: | |
449 | return true; | |
450 | ||
c61f3870 BP |
451 | case MFF_IN_PORT_OXM: |
452 | case MFF_ACTSET_OUTPUT: { | |
4e022ec0 | 453 | ofp_port_t port; |
72333065 JR |
454 | return !ofputil_port_from_ofp11(value->be32, &port); |
455 | } | |
456 | ||
530180fd | 457 | case MFF_IP_DSCP: |
7257b535 | 458 | return !(value->u8 & ~IP_DSCP_MASK); |
1638b906 BP |
459 | case MFF_IP_DSCP_SHIFTED: |
460 | return !(value->u8 & (~IP_DSCP_MASK >> 2)); | |
530180fd JP |
461 | case MFF_IP_ECN: |
462 | return !(value->u8 & ~IP_ECN_MASK); | |
7257b535 | 463 | case MFF_IP_FRAG: |
eadef313 | 464 | return !(value->u8 & ~FLOW_NW_FRAG_MASK); |
dc235f7f JR |
465 | case MFF_TCP_FLAGS: |
466 | return !(value->be16 & ~htons(0x0fff)); | |
6a885fd0 BP |
467 | |
468 | case MFF_ARP_OP: | |
469 | return !(value->be16 & htons(0xff00)); | |
470 | ||
441c57a9 | 471 | case MFF_DL_VLAN: |
6a885fd0 | 472 | return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK)); |
cc34bc8c BP |
473 | case MFF_VLAN_VID: |
474 | return !(value->be16 & htons(VLAN_PCP_MASK)); | |
6a885fd0 | 475 | |
441c57a9 | 476 | case MFF_DL_VLAN_PCP: |
cc34bc8c | 477 | case MFF_VLAN_PCP: |
24016f5a | 478 | return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT)); |
6a885fd0 | 479 | |
fa8223b7 JP |
480 | case MFF_IPV6_LABEL: |
481 | return !(value->be32 & ~htonl(IPV6_LABEL_MASK)); | |
482 | ||
b02475c5 SH |
483 | case MFF_MPLS_LABEL: |
484 | return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT)); | |
485 | ||
486 | case MFF_MPLS_TC: | |
487 | return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT)); | |
488 | ||
489 | case MFF_MPLS_BOS: | |
490 | return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT)); | |
491 | ||
6a885fd0 BP |
492 | case MFF_N_IDS: |
493 | default: | |
428b2edd | 494 | OVS_NOT_REACHED(); |
6a885fd0 BP |
495 | } |
496 | } | |
497 | ||
498 | /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is | |
499 | * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */ | |
500 | void | |
501 | mf_get_value(const struct mf_field *mf, const struct flow *flow, | |
502 | union mf_value *value) | |
503 | { | |
504 | switch (mf->id) { | |
a79f29f2 AZ |
505 | case MFF_DP_HASH: |
506 | value->be32 = htonl(flow->dp_hash); | |
507 | break; | |
508 | case MFF_RECIRC_ID: | |
509 | value->be32 = htonl(flow->recirc_id); | |
510 | break; | |
18080541 BP |
511 | case MFF_CONJ_ID: |
512 | value->be32 = htonl(flow->conj_id); | |
513 | break; | |
6a885fd0 | 514 | case MFF_TUN_ID: |
296e07ac | 515 | value->be64 = flow->tunnel.tun_id; |
6a885fd0 | 516 | break; |
4fe3445a PS |
517 | case MFF_TUN_SRC: |
518 | value->be32 = flow->tunnel.ip_src; | |
519 | break; | |
520 | case MFF_TUN_DST: | |
521 | value->be32 = flow->tunnel.ip_dst; | |
522 | break; | |
523 | case MFF_TUN_FLAGS: | |
524 | value->be16 = htons(flow->tunnel.flags); | |
525 | break; | |
ac6073e3 MC |
526 | case MFF_TUN_GBP_ID: |
527 | value->be16 = flow->tunnel.gbp_id; | |
528 | break; | |
529 | case MFF_TUN_GBP_FLAGS: | |
530 | value->u8 = flow->tunnel.gbp_flags; | |
531 | break; | |
4fe3445a PS |
532 | case MFF_TUN_TTL: |
533 | value->u8 = flow->tunnel.ip_ttl; | |
534 | break; | |
535 | case MFF_TUN_TOS: | |
536 | value->u8 = flow->tunnel.ip_tos; | |
537 | break; | |
538 | ||
969fc56c JS |
539 | case MFF_METADATA: |
540 | value->be64 = flow->metadata; | |
541 | break; | |
6a885fd0 BP |
542 | |
543 | case MFF_IN_PORT: | |
4e022ec0 | 544 | value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port)); |
6a885fd0 | 545 | break; |
72333065 | 546 | case MFF_IN_PORT_OXM: |
4e022ec0 | 547 | value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port); |
72333065 | 548 | break; |
c61f3870 BP |
549 | case MFF_ACTSET_OUTPUT: |
550 | value->be32 = ofputil_port_to_ofp11(flow->actset_output); | |
551 | break; | |
72333065 | 552 | |
1b567fb9 | 553 | case MFF_SKB_PRIORITY: |
a81f0a44 | 554 | value->be32 = htonl(flow->skb_priority); |
1b567fb9 AA |
555 | break; |
556 | ||
1362e248 JG |
557 | case MFF_PKT_MARK: |
558 | value->be32 = htonl(flow->pkt_mark); | |
1b567fb9 AA |
559 | break; |
560 | ||
0d7e2fe4 | 561 | CASE_MFF_REGS: |
2f98b0b7 | 562 | value->be32 = htonl(flow->regs[mf->id - MFF_REG0]); |
6a885fd0 BP |
563 | break; |
564 | ||
79fe0f46 BP |
565 | CASE_MFF_XREGS: |
566 | value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0)); | |
567 | break; | |
568 | ||
6a885fd0 BP |
569 | case MFF_ETH_SRC: |
570 | memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN); | |
571 | break; | |
572 | ||
573 | case MFF_ETH_DST: | |
574 | memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN); | |
575 | break; | |
576 | ||
577 | case MFF_ETH_TYPE: | |
578 | value->be16 = flow->dl_type; | |
579 | break; | |
580 | ||
581 | case MFF_VLAN_TCI: | |
582 | value->be16 = flow->vlan_tci; | |
583 | break; | |
584 | ||
441c57a9 | 585 | case MFF_DL_VLAN: |
6a885fd0 BP |
586 | value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK); |
587 | break; | |
cc34bc8c BP |
588 | case MFF_VLAN_VID: |
589 | value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI); | |
590 | break; | |
6a885fd0 | 591 | |
441c57a9 | 592 | case MFF_DL_VLAN_PCP: |
cc34bc8c | 593 | case MFF_VLAN_PCP: |
6a885fd0 BP |
594 | value->u8 = vlan_tci_to_pcp(flow->vlan_tci); |
595 | break; | |
596 | ||
b02475c5 | 597 | case MFF_MPLS_LABEL: |
8bfd0fda | 598 | value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0])); |
b02475c5 SH |
599 | break; |
600 | ||
601 | case MFF_MPLS_TC: | |
8bfd0fda | 602 | value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]); |
b02475c5 SH |
603 | break; |
604 | ||
605 | case MFF_MPLS_BOS: | |
8bfd0fda BP |
606 | value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]); |
607 | break; | |
b02475c5 | 608 | |
6a885fd0 BP |
609 | case MFF_IPV4_SRC: |
610 | value->be32 = flow->nw_src; | |
611 | break; | |
612 | ||
613 | case MFF_IPV4_DST: | |
614 | value->be32 = flow->nw_dst; | |
615 | break; | |
616 | ||
617 | case MFF_IPV6_SRC: | |
618 | value->ipv6 = flow->ipv6_src; | |
619 | break; | |
620 | ||
621 | case MFF_IPV6_DST: | |
622 | value->ipv6 = flow->ipv6_dst; | |
623 | break; | |
624 | ||
fa8223b7 JP |
625 | case MFF_IPV6_LABEL: |
626 | value->be32 = flow->ipv6_label; | |
627 | break; | |
628 | ||
6a885fd0 BP |
629 | case MFF_IP_PROTO: |
630 | value->u8 = flow->nw_proto; | |
631 | break; | |
632 | ||
530180fd | 633 | case MFF_IP_DSCP: |
eadef313 | 634 | value->u8 = flow->nw_tos & IP_DSCP_MASK; |
7257b535 BP |
635 | break; |
636 | ||
1638b906 BP |
637 | case MFF_IP_DSCP_SHIFTED: |
638 | value->u8 = flow->nw_tos >> 2; | |
639 | break; | |
640 | ||
530180fd | 641 | case MFF_IP_ECN: |
eadef313 | 642 | value->u8 = flow->nw_tos & IP_ECN_MASK; |
530180fd JP |
643 | break; |
644 | ||
a61680c6 JP |
645 | case MFF_IP_TTL: |
646 | value->u8 = flow->nw_ttl; | |
647 | break; | |
648 | ||
7257b535 | 649 | case MFF_IP_FRAG: |
eadef313 | 650 | value->u8 = flow->nw_frag; |
6a885fd0 BP |
651 | break; |
652 | ||
653 | case MFF_ARP_OP: | |
654 | value->be16 = htons(flow->nw_proto); | |
655 | break; | |
656 | ||
657 | case MFF_ARP_SPA: | |
658 | value->be32 = flow->nw_src; | |
659 | break; | |
660 | ||
661 | case MFF_ARP_TPA: | |
662 | value->be32 = flow->nw_dst; | |
663 | break; | |
664 | ||
665 | case MFF_ARP_SHA: | |
666 | case MFF_ND_SLL: | |
667 | memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN); | |
668 | break; | |
669 | ||
670 | case MFF_ARP_THA: | |
671 | case MFF_ND_TLL: | |
672 | memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN); | |
673 | break; | |
674 | ||
675 | case MFF_TCP_SRC: | |
6a885fd0 | 676 | case MFF_UDP_SRC: |
0d56eaf2 | 677 | case MFF_SCTP_SRC: |
6a885fd0 BP |
678 | value->be16 = flow->tp_src; |
679 | break; | |
680 | ||
71baec06 | 681 | case MFF_TCP_DST: |
6a885fd0 | 682 | case MFF_UDP_DST: |
0d56eaf2 | 683 | case MFF_SCTP_DST: |
6a885fd0 BP |
684 | value->be16 = flow->tp_dst; |
685 | break; | |
686 | ||
dc235f7f JR |
687 | case MFF_TCP_FLAGS: |
688 | value->be16 = flow->tcp_flags; | |
689 | break; | |
690 | ||
268a95e0 BP |
691 | case MFF_ICMPV4_TYPE: |
692 | case MFF_ICMPV6_TYPE: | |
6a885fd0 BP |
693 | value->u8 = ntohs(flow->tp_src); |
694 | break; | |
695 | ||
268a95e0 BP |
696 | case MFF_ICMPV4_CODE: |
697 | case MFF_ICMPV6_CODE: | |
6a885fd0 BP |
698 | value->u8 = ntohs(flow->tp_dst); |
699 | break; | |
700 | ||
701 | case MFF_ND_TARGET: | |
702 | value->ipv6 = flow->nd_target; | |
703 | break; | |
704 | ||
705 | case MFF_N_IDS: | |
706 | default: | |
428b2edd | 707 | OVS_NOT_REACHED(); |
6a885fd0 BP |
708 | } |
709 | } | |
710 | ||
81a76618 BP |
711 | /* Makes 'match' match field 'mf' exactly, with the value matched taken from |
712 | * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s | |
6a885fd0 BP |
713 | * prerequisites. */ |
714 | void | |
715 | mf_set_value(const struct mf_field *mf, | |
81a76618 | 716 | const union mf_value *value, struct match *match) |
6a885fd0 BP |
717 | { |
718 | switch (mf->id) { | |
a79f29f2 AZ |
719 | case MFF_DP_HASH: |
720 | match_set_dp_hash(match, ntohl(value->be32)); | |
721 | break; | |
722 | case MFF_RECIRC_ID: | |
723 | match_set_recirc_id(match, ntohl(value->be32)); | |
724 | break; | |
18080541 BP |
725 | case MFF_CONJ_ID: |
726 | match_set_conj_id(match, ntohl(value->be32)); | |
727 | break; | |
6a885fd0 | 728 | case MFF_TUN_ID: |
81a76618 | 729 | match_set_tun_id(match, value->be64); |
6a885fd0 | 730 | break; |
4fe3445a PS |
731 | case MFF_TUN_SRC: |
732 | match_set_tun_src(match, value->be32); | |
733 | break; | |
734 | case MFF_TUN_DST: | |
735 | match_set_tun_dst(match, value->be32); | |
736 | break; | |
737 | case MFF_TUN_FLAGS: | |
738 | match_set_tun_flags(match, ntohs(value->be16)); | |
739 | break; | |
ac6073e3 MC |
740 | case MFF_TUN_GBP_ID: |
741 | match_set_tun_gbp_id(match, value->be16); | |
742 | break; | |
743 | case MFF_TUN_GBP_FLAGS: | |
744 | match_set_tun_gbp_flags(match, value->u8); | |
745 | break; | |
4fe3445a PS |
746 | case MFF_TUN_TOS: |
747 | match_set_tun_tos(match, value->u8); | |
748 | break; | |
749 | case MFF_TUN_TTL: | |
750 | match_set_tun_ttl(match, value->u8); | |
751 | break; | |
752 | ||
969fc56c | 753 | case MFF_METADATA: |
81a76618 | 754 | match_set_metadata(match, value->be64); |
969fc56c | 755 | break; |
6a885fd0 BP |
756 | |
757 | case MFF_IN_PORT: | |
4e022ec0 | 758 | match_set_in_port(match, u16_to_ofp(ntohs(value->be16))); |
6a885fd0 BP |
759 | break; |
760 | ||
72333065 | 761 | case MFF_IN_PORT_OXM: { |
4e022ec0 | 762 | ofp_port_t port; |
bc146369 | 763 | ofputil_port_from_ofp11(value->be32, &port); |
72333065 JR |
764 | match_set_in_port(match, port); |
765 | break; | |
766 | } | |
c61f3870 BP |
767 | case MFF_ACTSET_OUTPUT: { |
768 | ofp_port_t port; | |
769 | ofputil_port_from_ofp11(value->be32, &port); | |
770 | match_set_actset_output(match, port); | |
771 | break; | |
772 | } | |
72333065 | 773 | |
1b567fb9 AA |
774 | case MFF_SKB_PRIORITY: |
775 | match_set_skb_priority(match, ntohl(value->be32)); | |
776 | break; | |
777 | ||
1362e248 JG |
778 | case MFF_PKT_MARK: |
779 | match_set_pkt_mark(match, ntohl(value->be32)); | |
1b567fb9 AA |
780 | break; |
781 | ||
0d7e2fe4 | 782 | CASE_MFF_REGS: |
81a76618 | 783 | match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32)); |
6a885fd0 | 784 | break; |
6a885fd0 | 785 | |
79fe0f46 BP |
786 | CASE_MFF_XREGS: |
787 | match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64)); | |
788 | break; | |
789 | ||
6a885fd0 | 790 | case MFF_ETH_SRC: |
81a76618 | 791 | match_set_dl_src(match, value->mac); |
6a885fd0 BP |
792 | break; |
793 | ||
794 | case MFF_ETH_DST: | |
81a76618 | 795 | match_set_dl_dst(match, value->mac); |
6a885fd0 BP |
796 | break; |
797 | ||
798 | case MFF_ETH_TYPE: | |
81a76618 | 799 | match_set_dl_type(match, value->be16); |
6a885fd0 BP |
800 | break; |
801 | ||
802 | case MFF_VLAN_TCI: | |
81a76618 | 803 | match_set_dl_tci(match, value->be16); |
6a885fd0 BP |
804 | break; |
805 | ||
441c57a9 | 806 | case MFF_DL_VLAN: |
81a76618 | 807 | match_set_dl_vlan(match, value->be16); |
6a885fd0 | 808 | break; |
cc34bc8c | 809 | case MFF_VLAN_VID: |
81a76618 | 810 | match_set_vlan_vid(match, value->be16); |
cc34bc8c | 811 | break; |
6a885fd0 | 812 | |
441c57a9 | 813 | case MFF_DL_VLAN_PCP: |
cc34bc8c | 814 | case MFF_VLAN_PCP: |
81a76618 | 815 | match_set_dl_vlan_pcp(match, value->u8); |
6a885fd0 BP |
816 | break; |
817 | ||
b02475c5 | 818 | case MFF_MPLS_LABEL: |
8bfd0fda | 819 | match_set_mpls_label(match, 0, value->be32); |
b02475c5 SH |
820 | break; |
821 | ||
822 | case MFF_MPLS_TC: | |
8bfd0fda | 823 | match_set_mpls_tc(match, 0, value->u8); |
b02475c5 SH |
824 | break; |
825 | ||
826 | case MFF_MPLS_BOS: | |
8bfd0fda BP |
827 | match_set_mpls_bos(match, 0, value->u8); |
828 | break; | |
b02475c5 | 829 | |
6a885fd0 | 830 | case MFF_IPV4_SRC: |
81a76618 | 831 | match_set_nw_src(match, value->be32); |
6a885fd0 BP |
832 | break; |
833 | ||
834 | case MFF_IPV4_DST: | |
81a76618 | 835 | match_set_nw_dst(match, value->be32); |
6a885fd0 BP |
836 | break; |
837 | ||
838 | case MFF_IPV6_SRC: | |
81a76618 | 839 | match_set_ipv6_src(match, &value->ipv6); |
6a885fd0 BP |
840 | break; |
841 | ||
842 | case MFF_IPV6_DST: | |
81a76618 | 843 | match_set_ipv6_dst(match, &value->ipv6); |
6a885fd0 BP |
844 | break; |
845 | ||
fa8223b7 | 846 | case MFF_IPV6_LABEL: |
81a76618 | 847 | match_set_ipv6_label(match, value->be32); |
fa8223b7 JP |
848 | break; |
849 | ||
6a885fd0 | 850 | case MFF_IP_PROTO: |
81a76618 | 851 | match_set_nw_proto(match, value->u8); |
6a885fd0 BP |
852 | break; |
853 | ||
530180fd | 854 | case MFF_IP_DSCP: |
81a76618 | 855 | match_set_nw_dscp(match, value->u8); |
530180fd JP |
856 | break; |
857 | ||
1638b906 BP |
858 | case MFF_IP_DSCP_SHIFTED: |
859 | match_set_nw_dscp(match, value->u8 << 2); | |
860 | break; | |
861 | ||
530180fd | 862 | case MFF_IP_ECN: |
81a76618 | 863 | match_set_nw_ecn(match, value->u8); |
6a885fd0 BP |
864 | break; |
865 | ||
a61680c6 | 866 | case MFF_IP_TTL: |
81a76618 | 867 | match_set_nw_ttl(match, value->u8); |
a61680c6 JP |
868 | break; |
869 | ||
7257b535 | 870 | case MFF_IP_FRAG: |
81a76618 | 871 | match_set_nw_frag(match, value->u8); |
7257b535 BP |
872 | break; |
873 | ||
6a885fd0 | 874 | case MFF_ARP_OP: |
81a76618 | 875 | match_set_nw_proto(match, ntohs(value->be16)); |
6a885fd0 BP |
876 | break; |
877 | ||
878 | case MFF_ARP_SPA: | |
81a76618 | 879 | match_set_nw_src(match, value->be32); |
6a885fd0 BP |
880 | break; |
881 | ||
882 | case MFF_ARP_TPA: | |
81a76618 | 883 | match_set_nw_dst(match, value->be32); |
6a885fd0 BP |
884 | break; |
885 | ||
886 | case MFF_ARP_SHA: | |
887 | case MFF_ND_SLL: | |
81a76618 | 888 | match_set_arp_sha(match, value->mac); |
6a885fd0 BP |
889 | break; |
890 | ||
891 | case MFF_ARP_THA: | |
892 | case MFF_ND_TLL: | |
81a76618 | 893 | match_set_arp_tha(match, value->mac); |
6a885fd0 BP |
894 | break; |
895 | ||
896 | case MFF_TCP_SRC: | |
6a885fd0 | 897 | case MFF_UDP_SRC: |
0d56eaf2 | 898 | case MFF_SCTP_SRC: |
81a76618 | 899 | match_set_tp_src(match, value->be16); |
6a885fd0 BP |
900 | break; |
901 | ||
71baec06 | 902 | case MFF_TCP_DST: |
6a885fd0 | 903 | case MFF_UDP_DST: |
0d56eaf2 | 904 | case MFF_SCTP_DST: |
81a76618 | 905 | match_set_tp_dst(match, value->be16); |
6a885fd0 BP |
906 | break; |
907 | ||
dc235f7f JR |
908 | case MFF_TCP_FLAGS: |
909 | match_set_tcp_flags(match, value->be16); | |
910 | break; | |
911 | ||
268a95e0 BP |
912 | case MFF_ICMPV4_TYPE: |
913 | case MFF_ICMPV6_TYPE: | |
81a76618 | 914 | match_set_icmp_type(match, value->u8); |
6a885fd0 BP |
915 | break; |
916 | ||
268a95e0 BP |
917 | case MFF_ICMPV4_CODE: |
918 | case MFF_ICMPV6_CODE: | |
81a76618 | 919 | match_set_icmp_code(match, value->u8); |
6a885fd0 BP |
920 | break; |
921 | ||
922 | case MFF_ND_TARGET: | |
81a76618 | 923 | match_set_nd_target(match, &value->ipv6); |
6a885fd0 BP |
924 | break; |
925 | ||
926 | case MFF_N_IDS: | |
927 | default: | |
428b2edd | 928 | OVS_NOT_REACHED(); |
6a885fd0 BP |
929 | } |
930 | } | |
931 | ||
5a0a5702 GS |
932 | /* Unwildcard 'mask' member field described by 'mf'. The caller is |
933 | * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */ | |
934 | void | |
935 | mf_mask_field(const struct mf_field *mf, struct flow *mask) | |
936 | { | |
937 | static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER; | |
938 | ||
939 | /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan() | |
940 | * as that will be considered as OFP10_VLAN_NONE. So consider it as a | |
941 | * special case. For the rest, calling mf_set_flow_value() is good | |
942 | * enough. */ | |
943 | if (mf->id == MFF_DL_VLAN) { | |
944 | flow_set_dl_vlan(mask, htons(VLAN_VID_MASK)); | |
945 | } else { | |
946 | mf_set_flow_value(mf, &exact_match_mask, mask); | |
947 | } | |
948 | } | |
949 | ||
910fb1d8 JP |
950 | /* Sets 'flow' member field described by 'mf' to 'value'. The caller is |
951 | * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/ | |
28da1f8f BP |
952 | void |
953 | mf_set_flow_value(const struct mf_field *mf, | |
954 | const union mf_value *value, struct flow *flow) | |
955 | { | |
956 | switch (mf->id) { | |
a79f29f2 AZ |
957 | case MFF_DP_HASH: |
958 | flow->dp_hash = ntohl(value->be32); | |
959 | break; | |
960 | case MFF_RECIRC_ID: | |
961 | flow->recirc_id = ntohl(value->be32); | |
962 | break; | |
18080541 BP |
963 | case MFF_CONJ_ID: |
964 | flow->conj_id = ntohl(value->be32); | |
965 | break; | |
28da1f8f | 966 | case MFF_TUN_ID: |
296e07ac | 967 | flow->tunnel.tun_id = value->be64; |
28da1f8f | 968 | break; |
4fe3445a PS |
969 | case MFF_TUN_SRC: |
970 | flow->tunnel.ip_src = value->be32; | |
971 | break; | |
972 | case MFF_TUN_DST: | |
973 | flow->tunnel.ip_dst = value->be32; | |
974 | break; | |
975 | case MFF_TUN_FLAGS: | |
976 | flow->tunnel.flags = ntohs(value->be16); | |
977 | break; | |
ac6073e3 MC |
978 | case MFF_TUN_GBP_ID: |
979 | flow->tunnel.gbp_id = value->be16; | |
980 | break; | |
981 | case MFF_TUN_GBP_FLAGS: | |
982 | flow->tunnel.gbp_flags = value->u8; | |
983 | break; | |
4fe3445a PS |
984 | case MFF_TUN_TOS: |
985 | flow->tunnel.ip_tos = value->u8; | |
986 | break; | |
987 | case MFF_TUN_TTL: | |
988 | flow->tunnel.ip_ttl = value->u8; | |
989 | break; | |
990 | ||
969fc56c JS |
991 | case MFF_METADATA: |
992 | flow->metadata = value->be64; | |
993 | break; | |
28da1f8f BP |
994 | |
995 | case MFF_IN_PORT: | |
4e022ec0 | 996 | flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16)); |
28da1f8f BP |
997 | break; |
998 | ||
c61f3870 BP |
999 | case MFF_IN_PORT_OXM: |
1000 | ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port); | |
1001 | break; | |
1002 | case MFF_ACTSET_OUTPUT: | |
1003 | ofputil_port_from_ofp11(value->be32, &flow->actset_output); | |
72333065 | 1004 | break; |
72333065 | 1005 | |
1b567fb9 AA |
1006 | case MFF_SKB_PRIORITY: |
1007 | flow->skb_priority = ntohl(value->be32); | |
1008 | break; | |
1009 | ||
1362e248 JG |
1010 | case MFF_PKT_MARK: |
1011 | flow->pkt_mark = ntohl(value->be32); | |
1b567fb9 AA |
1012 | break; |
1013 | ||
0d7e2fe4 | 1014 | CASE_MFF_REGS: |
28da1f8f BP |
1015 | flow->regs[mf->id - MFF_REG0] = ntohl(value->be32); |
1016 | break; | |
28da1f8f | 1017 | |
79fe0f46 BP |
1018 | CASE_MFF_XREGS: |
1019 | flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64)); | |
1020 | break; | |
1021 | ||
28da1f8f BP |
1022 | case MFF_ETH_SRC: |
1023 | memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN); | |
1024 | break; | |
1025 | ||
1026 | case MFF_ETH_DST: | |
b5a36e38 | 1027 | memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN); |
28da1f8f BP |
1028 | break; |
1029 | ||
1030 | case MFF_ETH_TYPE: | |
1031 | flow->dl_type = value->be16; | |
1032 | break; | |
1033 | ||
1034 | case MFF_VLAN_TCI: | |
1035 | flow->vlan_tci = value->be16; | |
1036 | break; | |
1037 | ||
441c57a9 | 1038 | case MFF_DL_VLAN: |
fb0451d9 | 1039 | flow_set_dl_vlan(flow, value->be16); |
28da1f8f | 1040 | break; |
cc34bc8c BP |
1041 | case MFF_VLAN_VID: |
1042 | flow_set_vlan_vid(flow, value->be16); | |
1043 | break; | |
28da1f8f | 1044 | |
441c57a9 | 1045 | case MFF_DL_VLAN_PCP: |
cc34bc8c | 1046 | case MFF_VLAN_PCP: |
28da1f8f BP |
1047 | flow_set_vlan_pcp(flow, value->u8); |
1048 | break; | |
1049 | ||
b02475c5 | 1050 | case MFF_MPLS_LABEL: |
8bfd0fda | 1051 | flow_set_mpls_label(flow, 0, value->be32); |
b02475c5 SH |
1052 | break; |
1053 | ||
1054 | case MFF_MPLS_TC: | |
8bfd0fda | 1055 | flow_set_mpls_tc(flow, 0, value->u8); |
b02475c5 SH |
1056 | break; |
1057 | ||
1058 | case MFF_MPLS_BOS: | |
8bfd0fda BP |
1059 | flow_set_mpls_bos(flow, 0, value->u8); |
1060 | break; | |
b02475c5 | 1061 | |
28da1f8f BP |
1062 | case MFF_IPV4_SRC: |
1063 | flow->nw_src = value->be32; | |
1064 | break; | |
1065 | ||
1066 | case MFF_IPV4_DST: | |
1067 | flow->nw_dst = value->be32; | |
1068 | break; | |
1069 | ||
1070 | case MFF_IPV6_SRC: | |
1071 | flow->ipv6_src = value->ipv6; | |
1072 | break; | |
1073 | ||
1074 | case MFF_IPV6_DST: | |
1075 | flow->ipv6_dst = value->ipv6; | |
1076 | break; | |
1077 | ||
1078 | case MFF_IPV6_LABEL: | |
88cc95c1 | 1079 | flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK); |
28da1f8f BP |
1080 | break; |
1081 | ||
1082 | case MFF_IP_PROTO: | |
1083 | flow->nw_proto = value->u8; | |
1084 | break; | |
1085 | ||
1086 | case MFF_IP_DSCP: | |
1087 | flow->nw_tos &= ~IP_DSCP_MASK; | |
1088 | flow->nw_tos |= value->u8 & IP_DSCP_MASK; | |
1089 | break; | |
1090 | ||
1638b906 BP |
1091 | case MFF_IP_DSCP_SHIFTED: |
1092 | flow->nw_tos &= ~IP_DSCP_MASK; | |
1093 | flow->nw_tos |= value->u8 << 2; | |
1094 | break; | |
1095 | ||
28da1f8f BP |
1096 | case MFF_IP_ECN: |
1097 | flow->nw_tos &= ~IP_ECN_MASK; | |
1098 | flow->nw_tos |= value->u8 & IP_ECN_MASK; | |
1099 | break; | |
1100 | ||
1101 | case MFF_IP_TTL: | |
1102 | flow->nw_ttl = value->u8; | |
1103 | break; | |
1104 | ||
1105 | case MFF_IP_FRAG: | |
c8abb8af | 1106 | flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK; |
28da1f8f BP |
1107 | break; |
1108 | ||
1109 | case MFF_ARP_OP: | |
1110 | flow->nw_proto = ntohs(value->be16); | |
1111 | break; | |
1112 | ||
1113 | case MFF_ARP_SPA: | |
1114 | flow->nw_src = value->be32; | |
1115 | break; | |
1116 | ||
1117 | case MFF_ARP_TPA: | |
1118 | flow->nw_dst = value->be32; | |
1119 | break; | |
1120 | ||
1121 | case MFF_ARP_SHA: | |
1122 | case MFF_ND_SLL: | |
1123 | memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN); | |
1124 | break; | |
1125 | ||
1126 | case MFF_ARP_THA: | |
1127 | case MFF_ND_TLL: | |
1128 | memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN); | |
1129 | break; | |
1130 | ||
1131 | case MFF_TCP_SRC: | |
1132 | case MFF_UDP_SRC: | |
0d56eaf2 | 1133 | case MFF_SCTP_SRC: |
28da1f8f BP |
1134 | flow->tp_src = value->be16; |
1135 | break; | |
1136 | ||
1137 | case MFF_TCP_DST: | |
1138 | case MFF_UDP_DST: | |
0d56eaf2 | 1139 | case MFF_SCTP_DST: |
28da1f8f BP |
1140 | flow->tp_dst = value->be16; |
1141 | break; | |
1142 | ||
dc235f7f JR |
1143 | case MFF_TCP_FLAGS: |
1144 | flow->tcp_flags = value->be16; | |
1145 | break; | |
1146 | ||
28da1f8f BP |
1147 | case MFF_ICMPV4_TYPE: |
1148 | case MFF_ICMPV6_TYPE: | |
1149 | flow->tp_src = htons(value->u8); | |
1150 | break; | |
1151 | ||
1152 | case MFF_ICMPV4_CODE: | |
1153 | case MFF_ICMPV6_CODE: | |
1154 | flow->tp_dst = htons(value->u8); | |
1155 | break; | |
1156 | ||
1157 | case MFF_ND_TARGET: | |
1158 | flow->nd_target = value->ipv6; | |
1159 | break; | |
1160 | ||
1161 | case MFF_N_IDS: | |
1162 | default: | |
428b2edd | 1163 | OVS_NOT_REACHED(); |
28da1f8f BP |
1164 | } |
1165 | } | |
1166 | ||
7eb4b1f1 BP |
1167 | /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n |
1168 | * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] = | |
1169 | * src[i]. */ | |
1170 | static void | |
1171 | apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n) | |
1172 | { | |
1173 | size_t i; | |
1174 | ||
1175 | for (i = 0; i < n; i++) { | |
1176 | dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]); | |
1177 | } | |
1178 | } | |
1179 | ||
1180 | /* Sets 'flow' member field described by 'field' to 'value', except that bits | |
1181 | * for which 'mask' has a 0-bit keep their existing values. The caller is | |
1182 | * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/ | |
1183 | void | |
1184 | mf_set_flow_value_masked(const struct mf_field *field, | |
1185 | const union mf_value *value, | |
1186 | const union mf_value *mask, | |
1187 | struct flow *flow) | |
1188 | { | |
1189 | union mf_value tmp; | |
1190 | ||
1191 | mf_get_value(field, flow, &tmp); | |
1192 | apply_mask((const uint8_t *) value, (const uint8_t *) mask, | |
1193 | (uint8_t *) &tmp, field->n_bytes); | |
1194 | mf_set_flow_value(field, &tmp, flow); | |
1195 | } | |
1196 | ||
ccbe50f8 BP |
1197 | /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero. |
1198 | * | |
1199 | * The caller is responsible for ensuring that 'flow' meets 'mf''s | |
1200 | * prerequisites. */ | |
1201 | bool | |
1202 | mf_is_zero(const struct mf_field *mf, const struct flow *flow) | |
1203 | { | |
1204 | union mf_value value; | |
1205 | ||
1206 | mf_get_value(mf, flow, &value); | |
53cb9c3e | 1207 | return is_all_zeros(&value, mf->n_bytes); |
ccbe50f8 BP |
1208 | } |
1209 | ||
81a76618 | 1210 | /* Makes 'match' wildcard field 'mf'. |
6a885fd0 | 1211 | * |
81a76618 | 1212 | * The caller is responsible for ensuring that 'match' meets 'mf''s |
6a885fd0 BP |
1213 | * prerequisites. */ |
1214 | void | |
81a76618 | 1215 | mf_set_wild(const struct mf_field *mf, struct match *match) |
6a885fd0 BP |
1216 | { |
1217 | switch (mf->id) { | |
a79f29f2 AZ |
1218 | case MFF_DP_HASH: |
1219 | match->flow.dp_hash = 0; | |
1220 | match->wc.masks.dp_hash = 0; | |
1221 | break; | |
1222 | case MFF_RECIRC_ID: | |
1223 | match->flow.recirc_id = 0; | |
1224 | match->wc.masks.recirc_id = 0; | |
1225 | break; | |
18080541 BP |
1226 | case MFF_CONJ_ID: |
1227 | match->flow.conj_id = 0; | |
1228 | match->wc.masks.conj_id = 0; | |
1229 | break; | |
6a885fd0 | 1230 | case MFF_TUN_ID: |
81a76618 | 1231 | match_set_tun_id_masked(match, htonll(0), htonll(0)); |
6a885fd0 | 1232 | break; |
4fe3445a PS |
1233 | case MFF_TUN_SRC: |
1234 | match_set_tun_src_masked(match, htonl(0), htonl(0)); | |
1235 | break; | |
1236 | case MFF_TUN_DST: | |
1237 | match_set_tun_dst_masked(match, htonl(0), htonl(0)); | |
1238 | break; | |
1239 | case MFF_TUN_FLAGS: | |
1240 | match_set_tun_flags_masked(match, 0, 0); | |
1241 | break; | |
ac6073e3 MC |
1242 | case MFF_TUN_GBP_ID: |
1243 | match_set_tun_gbp_id_masked(match, 0, 0); | |
1244 | break; | |
1245 | case MFF_TUN_GBP_FLAGS: | |
1246 | match_set_tun_gbp_flags_masked(match, 0, 0); | |
1247 | break; | |
4fe3445a PS |
1248 | case MFF_TUN_TOS: |
1249 | match_set_tun_tos_masked(match, 0, 0); | |
1250 | break; | |
1251 | case MFF_TUN_TTL: | |
1252 | match_set_tun_ttl_masked(match, 0, 0); | |
1253 | break; | |
1254 | ||
969fc56c | 1255 | case MFF_METADATA: |
81a76618 | 1256 | match_set_metadata_masked(match, htonll(0), htonll(0)); |
801d2b2a | 1257 | break; |
6a885fd0 BP |
1258 | |
1259 | case MFF_IN_PORT: | |
72333065 | 1260 | case MFF_IN_PORT_OXM: |
4e022ec0 AW |
1261 | match->flow.in_port.ofp_port = 0; |
1262 | match->wc.masks.in_port.ofp_port = 0; | |
6a885fd0 | 1263 | break; |
c61f3870 BP |
1264 | case MFF_ACTSET_OUTPUT: |
1265 | match->flow.actset_output = 0; | |
1266 | match->wc.masks.actset_output = 0; | |
1267 | break; | |
6a885fd0 | 1268 | |
1b567fb9 AA |
1269 | case MFF_SKB_PRIORITY: |
1270 | match->flow.skb_priority = 0; | |
1271 | match->wc.masks.skb_priority = 0; | |
1272 | break; | |
1273 | ||
1362e248 JG |
1274 | case MFF_PKT_MARK: |
1275 | match->flow.pkt_mark = 0; | |
1276 | match->wc.masks.pkt_mark = 0; | |
1b567fb9 AA |
1277 | break; |
1278 | ||
0d7e2fe4 | 1279 | CASE_MFF_REGS: |
81a76618 | 1280 | match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0); |
e9358af6 | 1281 | break; |
6a885fd0 | 1282 | |
79fe0f46 BP |
1283 | CASE_MFF_XREGS: |
1284 | match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0); | |
1285 | break; | |
1286 | ||
6a885fd0 | 1287 | case MFF_ETH_SRC: |
81a76618 BP |
1288 | memset(match->flow.dl_src, 0, ETH_ADDR_LEN); |
1289 | memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN); | |
6a885fd0 BP |
1290 | break; |
1291 | ||
1292 | case MFF_ETH_DST: | |
81a76618 BP |
1293 | memset(match->flow.dl_dst, 0, ETH_ADDR_LEN); |
1294 | memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN); | |
6a885fd0 BP |
1295 | break; |
1296 | ||
1297 | case MFF_ETH_TYPE: | |
81a76618 BP |
1298 | match->flow.dl_type = htons(0); |
1299 | match->wc.masks.dl_type = htons(0); | |
6a885fd0 BP |
1300 | break; |
1301 | ||
1302 | case MFF_VLAN_TCI: | |
81a76618 | 1303 | match_set_dl_tci_masked(match, htons(0), htons(0)); |
6a885fd0 BP |
1304 | break; |
1305 | ||
441c57a9 | 1306 | case MFF_DL_VLAN: |
cc34bc8c | 1307 | case MFF_VLAN_VID: |
81a76618 | 1308 | match_set_any_vid(match); |
6a885fd0 BP |
1309 | break; |
1310 | ||
441c57a9 | 1311 | case MFF_DL_VLAN_PCP: |
cc34bc8c | 1312 | case MFF_VLAN_PCP: |
81a76618 | 1313 | match_set_any_pcp(match); |
6a885fd0 BP |
1314 | break; |
1315 | ||
b02475c5 | 1316 | case MFF_MPLS_LABEL: |
8bfd0fda | 1317 | match_set_any_mpls_label(match, 0); |
b02475c5 SH |
1318 | break; |
1319 | ||
1320 | case MFF_MPLS_TC: | |
8bfd0fda | 1321 | match_set_any_mpls_tc(match, 0); |
b02475c5 SH |
1322 | break; |
1323 | ||
1324 | case MFF_MPLS_BOS: | |
8bfd0fda BP |
1325 | match_set_any_mpls_bos(match, 0); |
1326 | break; | |
b02475c5 | 1327 | |
6a885fd0 BP |
1328 | case MFF_IPV4_SRC: |
1329 | case MFF_ARP_SPA: | |
81a76618 | 1330 | match_set_nw_src_masked(match, htonl(0), htonl(0)); |
6a885fd0 BP |
1331 | break; |
1332 | ||
1333 | case MFF_IPV4_DST: | |
1334 | case MFF_ARP_TPA: | |
81a76618 | 1335 | match_set_nw_dst_masked(match, htonl(0), htonl(0)); |
6a885fd0 BP |
1336 | break; |
1337 | ||
1338 | case MFF_IPV6_SRC: | |
81a76618 BP |
1339 | memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src); |
1340 | memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src); | |
6a885fd0 BP |
1341 | break; |
1342 | ||
1343 | case MFF_IPV6_DST: | |
81a76618 BP |
1344 | memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst); |
1345 | memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst); | |
6a885fd0 BP |
1346 | break; |
1347 | ||
fa8223b7 | 1348 | case MFF_IPV6_LABEL: |
81a76618 BP |
1349 | match->wc.masks.ipv6_label = htonl(0); |
1350 | match->flow.ipv6_label = htonl(0); | |
fa8223b7 JP |
1351 | break; |
1352 | ||
6a885fd0 | 1353 | case MFF_IP_PROTO: |
81a76618 BP |
1354 | match->wc.masks.nw_proto = 0; |
1355 | match->flow.nw_proto = 0; | |
6a885fd0 BP |
1356 | break; |
1357 | ||
530180fd | 1358 | case MFF_IP_DSCP: |
1638b906 | 1359 | case MFF_IP_DSCP_SHIFTED: |
81a76618 BP |
1360 | match->wc.masks.nw_tos &= ~IP_DSCP_MASK; |
1361 | match->flow.nw_tos &= ~IP_DSCP_MASK; | |
7257b535 BP |
1362 | break; |
1363 | ||
530180fd | 1364 | case MFF_IP_ECN: |
81a76618 BP |
1365 | match->wc.masks.nw_tos &= ~IP_ECN_MASK; |
1366 | match->flow.nw_tos &= ~IP_ECN_MASK; | |
530180fd JP |
1367 | break; |
1368 | ||
a61680c6 | 1369 | case MFF_IP_TTL: |
81a76618 BP |
1370 | match->wc.masks.nw_ttl = 0; |
1371 | match->flow.nw_ttl = 0; | |
a61680c6 JP |
1372 | break; |
1373 | ||
7257b535 | 1374 | case MFF_IP_FRAG: |
81a76618 BP |
1375 | match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK; |
1376 | match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK; | |
6a885fd0 BP |
1377 | break; |
1378 | ||
1379 | case MFF_ARP_OP: | |
81a76618 BP |
1380 | match->wc.masks.nw_proto = 0; |
1381 | match->flow.nw_proto = 0; | |
6a885fd0 BP |
1382 | break; |
1383 | ||
1384 | case MFF_ARP_SHA: | |
1385 | case MFF_ND_SLL: | |
81a76618 BP |
1386 | memset(match->flow.arp_sha, 0, ETH_ADDR_LEN); |
1387 | memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN); | |
6a885fd0 BP |
1388 | break; |
1389 | ||
1390 | case MFF_ARP_THA: | |
1391 | case MFF_ND_TLL: | |
81a76618 BP |
1392 | memset(match->flow.arp_tha, 0, ETH_ADDR_LEN); |
1393 | memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN); | |
6a885fd0 BP |
1394 | break; |
1395 | ||
1396 | case MFF_TCP_SRC: | |
1397 | case MFF_UDP_SRC: | |
0d56eaf2 | 1398 | case MFF_SCTP_SRC: |
268a95e0 BP |
1399 | case MFF_ICMPV4_TYPE: |
1400 | case MFF_ICMPV6_TYPE: | |
81a76618 BP |
1401 | match->wc.masks.tp_src = htons(0); |
1402 | match->flow.tp_src = htons(0); | |
6a885fd0 BP |
1403 | break; |
1404 | ||
1405 | case MFF_TCP_DST: | |
1406 | case MFF_UDP_DST: | |
0d56eaf2 | 1407 | case MFF_SCTP_DST: |
268a95e0 BP |
1408 | case MFF_ICMPV4_CODE: |
1409 | case MFF_ICMPV6_CODE: | |
81a76618 BP |
1410 | match->wc.masks.tp_dst = htons(0); |
1411 | match->flow.tp_dst = htons(0); | |
6a885fd0 BP |
1412 | break; |
1413 | ||
dc235f7f JR |
1414 | case MFF_TCP_FLAGS: |
1415 | match->wc.masks.tcp_flags = htons(0); | |
1416 | match->flow.tcp_flags = htons(0); | |
1417 | break; | |
1418 | ||
6a885fd0 | 1419 | case MFF_ND_TARGET: |
81a76618 BP |
1420 | memset(&match->wc.masks.nd_target, 0, |
1421 | sizeof match->wc.masks.nd_target); | |
1422 | memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target); | |
6a885fd0 BP |
1423 | break; |
1424 | ||
1425 | case MFF_N_IDS: | |
1426 | default: | |
428b2edd | 1427 | OVS_NOT_REACHED(); |
6a885fd0 BP |
1428 | } |
1429 | } | |
1430 | ||
81a76618 | 1431 | /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'. |
6a885fd0 BP |
1432 | * 'value' specifies a value to match and 'mask' specifies a wildcard pattern, |
1433 | * with a 1-bit indicating that the corresponding value bit must match and a | |
1434 | * 0-bit indicating a don't-care. | |
1435 | * | |
1436 | * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to | |
81a76618 BP |
1437 | * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this |
1438 | * call is equivalent to mf_set_wild(mf, match). | |
6a885fd0 BP |
1439 | * |
1440 | * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller | |
81a76618 | 1441 | * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */ |
db0b6c29 | 1442 | enum ofputil_protocol |
6a885fd0 BP |
1443 | mf_set(const struct mf_field *mf, |
1444 | const union mf_value *value, const union mf_value *mask, | |
81a76618 | 1445 | struct match *match) |
6a885fd0 | 1446 | { |
53cb9c3e | 1447 | if (!mask || is_all_ones(mask, mf->n_bytes)) { |
81a76618 | 1448 | mf_set_value(mf, value, match); |
a4ce8b25 | 1449 | return mf->usable_protocols_exact; |
53cb9c3e | 1450 | } else if (is_all_zeros(mask, mf->n_bytes)) { |
81a76618 | 1451 | mf_set_wild(mf, match); |
db0b6c29 | 1452 | return OFPUTIL_P_ANY; |
6a885fd0 BP |
1453 | } |
1454 | ||
1455 | switch (mf->id) { | |
a79f29f2 | 1456 | case MFF_RECIRC_ID: |
18080541 | 1457 | case MFF_CONJ_ID: |
6a885fd0 | 1458 | case MFF_IN_PORT: |
72333065 | 1459 | case MFF_IN_PORT_OXM: |
c61f3870 | 1460 | case MFF_ACTSET_OUTPUT: |
1b567fb9 | 1461 | case MFF_SKB_PRIORITY: |
6a885fd0 | 1462 | case MFF_ETH_TYPE: |
441c57a9 BP |
1463 | case MFF_DL_VLAN: |
1464 | case MFF_DL_VLAN_PCP: | |
cc34bc8c | 1465 | case MFF_VLAN_PCP: |
b02475c5 SH |
1466 | case MFF_MPLS_LABEL: |
1467 | case MFF_MPLS_TC: | |
1468 | case MFF_MPLS_BOS: | |
6a885fd0 | 1469 | case MFF_IP_PROTO: |
a61680c6 | 1470 | case MFF_IP_TTL: |
530180fd | 1471 | case MFF_IP_DSCP: |
1638b906 | 1472 | case MFF_IP_DSCP_SHIFTED: |
530180fd | 1473 | case MFF_IP_ECN: |
6a885fd0 | 1474 | case MFF_ARP_OP: |
268a95e0 BP |
1475 | case MFF_ICMPV4_TYPE: |
1476 | case MFF_ICMPV4_CODE: | |
1477 | case MFF_ICMPV6_TYPE: | |
1478 | case MFF_ICMPV6_CODE: | |
af69bf19 | 1479 | return OFPUTIL_P_NONE; |
6a885fd0 | 1480 | |
a79f29f2 AZ |
1481 | case MFF_DP_HASH: |
1482 | match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32)); | |
1483 | break; | |
6a885fd0 | 1484 | case MFF_TUN_ID: |
81a76618 | 1485 | match_set_tun_id_masked(match, value->be64, mask->be64); |
6a885fd0 | 1486 | break; |
4fe3445a PS |
1487 | case MFF_TUN_SRC: |
1488 | match_set_tun_src_masked(match, value->be32, mask->be32); | |
1489 | break; | |
1490 | case MFF_TUN_DST: | |
1491 | match_set_tun_dst_masked(match, value->be32, mask->be32); | |
1492 | break; | |
1493 | case MFF_TUN_FLAGS: | |
1494 | match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16)); | |
1495 | break; | |
ac6073e3 MC |
1496 | case MFF_TUN_GBP_ID: |
1497 | match_set_tun_gbp_id_masked(match, value->be16, mask->be16); | |
1498 | break; | |
1499 | case MFF_TUN_GBP_FLAGS: | |
1500 | match_set_tun_gbp_flags_masked(match, value->u8, mask->u8); | |
1501 | break; | |
4fe3445a PS |
1502 | case MFF_TUN_TTL: |
1503 | match_set_tun_ttl_masked(match, value->u8, mask->u8); | |
1504 | break; | |
1505 | case MFF_TUN_TOS: | |
1506 | match_set_tun_tos_masked(match, value->u8, mask->u8); | |
1507 | break; | |
1508 | ||
969fc56c | 1509 | case MFF_METADATA: |
81a76618 | 1510 | match_set_metadata_masked(match, value->be64, mask->be64); |
969fc56c | 1511 | break; |
6a885fd0 | 1512 | |
0d7e2fe4 | 1513 | CASE_MFF_REGS: |
81a76618 BP |
1514 | match_set_reg_masked(match, mf->id - MFF_REG0, |
1515 | ntohl(value->be32), ntohl(mask->be32)); | |
6a885fd0 | 1516 | break; |
ac923e91 | 1517 | |
79fe0f46 BP |
1518 | CASE_MFF_XREGS: |
1519 | match_set_xreg_masked(match, mf->id - MFF_XREG0, | |
1520 | ntohll(value->be64), ntohll(mask->be64)); | |
1521 | break; | |
1522 | ||
ac923e91 JG |
1523 | case MFF_PKT_MARK: |
1524 | match_set_pkt_mark_masked(match, ntohl(value->be32), | |
1525 | ntohl(mask->be32)); | |
1526 | break; | |
6a885fd0 BP |
1527 | |
1528 | case MFF_ETH_DST: | |
81a76618 | 1529 | match_set_dl_dst_masked(match, value->mac, mask->mac); |
73c0ce34 JS |
1530 | break; |
1531 | ||
1532 | case MFF_ETH_SRC: | |
81a76618 | 1533 | match_set_dl_src_masked(match, value->mac, mask->mac); |
6a885fd0 BP |
1534 | break; |
1535 | ||
e878338b SH |
1536 | case MFF_ARP_SHA: |
1537 | case MFF_ND_SLL: | |
81a76618 | 1538 | match_set_arp_sha_masked(match, value->mac, mask->mac); |
e878338b SH |
1539 | break; |
1540 | ||
1541 | case MFF_ARP_THA: | |
1542 | case MFF_ND_TLL: | |
81a76618 | 1543 | match_set_arp_tha_masked(match, value->mac, mask->mac); |
e878338b SH |
1544 | break; |
1545 | ||
6a885fd0 | 1546 | case MFF_VLAN_TCI: |
81a76618 | 1547 | match_set_dl_tci_masked(match, value->be16, mask->be16); |
6a885fd0 BP |
1548 | break; |
1549 | ||
cc34bc8c | 1550 | case MFF_VLAN_VID: |
81a76618 | 1551 | match_set_vlan_vid_masked(match, value->be16, mask->be16); |
cc34bc8c BP |
1552 | break; |
1553 | ||
6a885fd0 | 1554 | case MFF_IPV4_SRC: |
81a76618 | 1555 | match_set_nw_src_masked(match, value->be32, mask->be32); |
a4ce8b25 | 1556 | break; |
6a885fd0 BP |
1557 | |
1558 | case MFF_IPV4_DST: | |
81a76618 | 1559 | match_set_nw_dst_masked(match, value->be32, mask->be32); |
a4ce8b25 | 1560 | break; |
6a885fd0 BP |
1561 | |
1562 | case MFF_IPV6_SRC: | |
81a76618 | 1563 | match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6); |
6a885fd0 BP |
1564 | break; |
1565 | ||
1566 | case MFF_IPV6_DST: | |
81a76618 | 1567 | match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6); |
6a885fd0 BP |
1568 | break; |
1569 | ||
32455024 SH |
1570 | case MFF_IPV6_LABEL: |
1571 | if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) { | |
81a76618 | 1572 | mf_set_value(mf, value, match); |
32455024 | 1573 | } else { |
81a76618 | 1574 | match_set_ipv6_label_masked(match, value->be32, mask->be32); |
32455024 SH |
1575 | } |
1576 | break; | |
1577 | ||
47284b1f | 1578 | case MFF_ND_TARGET: |
81a76618 | 1579 | match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6); |
47284b1f AA |
1580 | break; |
1581 | ||
7257b535 | 1582 | case MFF_IP_FRAG: |
81a76618 | 1583 | match_set_nw_frag_masked(match, value->u8, mask->u8); |
7257b535 BP |
1584 | break; |
1585 | ||
6a885fd0 | 1586 | case MFF_ARP_SPA: |
81a76618 | 1587 | match_set_nw_src_masked(match, value->be32, mask->be32); |
a4ce8b25 | 1588 | break; |
6a885fd0 BP |
1589 | |
1590 | case MFF_ARP_TPA: | |
81a76618 | 1591 | match_set_nw_dst_masked(match, value->be32, mask->be32); |
a4ce8b25 | 1592 | break; |
6a885fd0 | 1593 | |
73f33563 BP |
1594 | case MFF_TCP_SRC: |
1595 | case MFF_UDP_SRC: | |
0d56eaf2 | 1596 | case MFF_SCTP_SRC: |
81a76618 | 1597 | match_set_tp_src_masked(match, value->be16, mask->be16); |
73f33563 BP |
1598 | break; |
1599 | ||
1600 | case MFF_TCP_DST: | |
1601 | case MFF_UDP_DST: | |
0d56eaf2 | 1602 | case MFF_SCTP_DST: |
81a76618 | 1603 | match_set_tp_dst_masked(match, value->be16, mask->be16); |
73f33563 BP |
1604 | break; |
1605 | ||
dc235f7f JR |
1606 | case MFF_TCP_FLAGS: |
1607 | match_set_tcp_flags_masked(match, value->be16, mask->be16); | |
1608 | break; | |
1609 | ||
6a885fd0 BP |
1610 | case MFF_N_IDS: |
1611 | default: | |
428b2edd | 1612 | OVS_NOT_REACHED(); |
6a885fd0 | 1613 | } |
db0b6c29 | 1614 | |
a4ce8b25 BP |
1615 | return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr |
1616 | || ip_is_cidr(mask->be32)) | |
1617 | ? mf->usable_protocols_cidr | |
1618 | : mf->usable_protocols_bitwise); | |
6a885fd0 BP |
1619 | } |
1620 | ||
816fd533 BP |
1621 | static enum ofperr |
1622 | mf_check__(const struct mf_subfield *sf, const struct flow *flow, | |
1623 | const char *type) | |
6a885fd0 | 1624 | { |
816fd533 BP |
1625 | if (!sf->field) { |
1626 | VLOG_WARN_RL(&rl, "unknown %s field", type); | |
9e404d1e | 1627 | return OFPERR_OFPBAC_BAD_SET_TYPE; |
816fd533 BP |
1628 | } else if (!sf->n_bits) { |
1629 | VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name); | |
9e404d1e | 1630 | return OFPERR_OFPBAC_BAD_SET_LEN; |
816fd533 BP |
1631 | } else if (sf->ofs >= sf->field->n_bits) { |
1632 | VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s", | |
1633 | sf->ofs, sf->field->n_bits, type, sf->field->name); | |
9e404d1e | 1634 | return OFPERR_OFPBAC_BAD_SET_LEN; |
816fd533 BP |
1635 | } else if (sf->ofs + sf->n_bits > sf->field->n_bits) { |
1636 | VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width " | |
1637 | "of %s field %s", sf->ofs, sf->n_bits, | |
1638 | sf->field->n_bits, type, sf->field->name); | |
9e404d1e | 1639 | return OFPERR_OFPBAC_BAD_SET_LEN; |
816fd533 BP |
1640 | } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) { |
1641 | VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites", | |
1642 | type, sf->field->name); | |
9e404d1e | 1643 | return OFPERR_OFPBAC_MATCH_INCONSISTENT; |
6a885fd0 | 1644 | } else { |
816fd533 BP |
1645 | return 0; |
1646 | } | |
816fd533 | 1647 | } |
6a885fd0 | 1648 | |
816fd533 BP |
1649 | /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns |
1650 | * 0 if so, otherwise an OpenFlow error code (e.g. as returned by | |
1651 | * ofp_mkerr()). */ | |
1652 | enum ofperr | |
1653 | mf_check_src(const struct mf_subfield *sf, const struct flow *flow) | |
1654 | { | |
1655 | return mf_check__(sf, flow, "source"); | |
1656 | } | |
6a885fd0 | 1657 | |
816fd533 BP |
1658 | /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0 |
1659 | * if so, otherwise an OpenFlow error code (e.g. as returned by | |
1660 | * ofp_mkerr()). */ | |
1661 | enum ofperr | |
1662 | mf_check_dst(const struct mf_subfield *sf, const struct flow *flow) | |
1663 | { | |
1664 | int error = mf_check__(sf, flow, "destination"); | |
1665 | if (!error && !sf->field->writable) { | |
1666 | VLOG_WARN_RL(&rl, "destination field %s is not writable", | |
1667 | sf->field->name); | |
9e404d1e | 1668 | return OFPERR_OFPBAC_BAD_SET_ARGUMENT; |
6a885fd0 | 1669 | } |
816fd533 | 1670 | return error; |
6a885fd0 BP |
1671 | } |
1672 | ||
81a76618 | 1673 | /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the |
6a885fd0 BP |
1674 | * 'value' and 'mask', respectively. */ |
1675 | void | |
81a76618 | 1676 | mf_get(const struct mf_field *mf, const struct match *match, |
6a885fd0 BP |
1677 | union mf_value *value, union mf_value *mask) |
1678 | { | |
81a76618 BP |
1679 | mf_get_value(mf, &match->flow, value); |
1680 | mf_get_mask(mf, &match->wc, mask); | |
6a885fd0 BP |
1681 | } |
1682 | ||
6a885fd0 BP |
1683 | static char * |
1684 | mf_from_integer_string(const struct mf_field *mf, const char *s, | |
1685 | uint8_t *valuep, uint8_t *maskp) | |
1686 | { | |
1687 | unsigned long long int integer, mask; | |
1688 | char *tail; | |
1689 | int i; | |
1690 | ||
1691 | errno = 0; | |
1692 | integer = strtoull(s, &tail, 0); | |
1693 | if (errno || (*tail != '\0' && *tail != '/')) { | |
1694 | goto syntax_error; | |
1695 | } | |
1696 | ||
1697 | if (*tail == '/') { | |
1698 | mask = strtoull(tail + 1, &tail, 0); | |
1699 | if (errno || *tail != '\0') { | |
1700 | goto syntax_error; | |
1701 | } | |
1702 | } else { | |
1703 | mask = ULLONG_MAX; | |
1704 | } | |
1705 | ||
1706 | for (i = mf->n_bytes - 1; i >= 0; i--) { | |
1707 | valuep[i] = integer; | |
1708 | maskp[i] = mask; | |
1709 | integer >>= 8; | |
1710 | mask >>= 8; | |
1711 | } | |
1712 | if (integer) { | |
1713 | return xasprintf("%s: value too large for %u-byte field %s", | |
1714 | s, mf->n_bytes, mf->name); | |
1715 | } | |
1716 | return NULL; | |
1717 | ||
1718 | syntax_error: | |
1719 | return xasprintf("%s: bad syntax for %s", s, mf->name); | |
1720 | } | |
1721 | ||
1722 | static char * | |
1723 | mf_from_ethernet_string(const struct mf_field *mf, const char *s, | |
1724 | uint8_t mac[ETH_ADDR_LEN], | |
1725 | uint8_t mask[ETH_ADDR_LEN]) | |
1726 | { | |
279fb135 | 1727 | int n; |
6a885fd0 | 1728 | |
279fb135 | 1729 | ovs_assert(mf->n_bytes == ETH_ADDR_LEN); |
6a885fd0 | 1730 | |
279fb135 | 1731 | n = -1; |
c2c28dfd | 1732 | if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n) |
279fb135 | 1733 | && n == strlen(s)) { |
6a885fd0 BP |
1734 | memset(mask, 0xff, ETH_ADDR_LEN); |
1735 | return NULL; | |
279fb135 | 1736 | } |
6a885fd0 | 1737 | |
279fb135 | 1738 | n = -1; |
c2c28dfd BP |
1739 | if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n", |
1740 | ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n) | |
279fb135 BP |
1741 | && n == strlen(s)) { |
1742 | return NULL; | |
6a885fd0 | 1743 | } |
279fb135 BP |
1744 | |
1745 | return xasprintf("%s: invalid Ethernet address", s); | |
6a885fd0 BP |
1746 | } |
1747 | ||
1748 | static char * | |
1749 | mf_from_ipv4_string(const struct mf_field *mf, const char *s, | |
1750 | ovs_be32 *ip, ovs_be32 *mask) | |
1751 | { | |
1752 | int prefix; | |
1753 | ||
cb22974d | 1754 | ovs_assert(mf->n_bytes == sizeof *ip); |
6a885fd0 | 1755 | |
c2c28dfd BP |
1756 | if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT, |
1757 | IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) { | |
6a885fd0 | 1758 | /* OK. */ |
c2c28dfd | 1759 | } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) { |
6a885fd0 | 1760 | if (prefix <= 0 || prefix > 32) { |
86f35fb5 | 1761 | return xasprintf("%s: network prefix bits not between 0 and " |
6a885fd0 | 1762 | "32", s); |
6a885fd0 | 1763 | } |
86f35fb5 | 1764 | *mask = be32_prefix_mask(prefix); |
c2c28dfd | 1765 | } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) { |
b8266395 | 1766 | *mask = OVS_BE32_MAX; |
6a885fd0 BP |
1767 | } else { |
1768 | return xasprintf("%s: invalid IP address", s); | |
1769 | } | |
1770 | return NULL; | |
1771 | } | |
1772 | ||
1773 | static char * | |
1774 | mf_from_ipv6_string(const struct mf_field *mf, const char *s, | |
1775 | struct in6_addr *value, struct in6_addr *mask) | |
1776 | { | |
1777 | char *str = xstrdup(s); | |
1778 | char *save_ptr = NULL; | |
1779 | const char *name, *netmask; | |
1780 | int retval; | |
1781 | ||
cb22974d | 1782 | ovs_assert(mf->n_bytes == sizeof *value); |
6a885fd0 BP |
1783 | |
1784 | name = strtok_r(str, "/", &save_ptr); | |
1785 | retval = name ? lookup_ipv6(name, value) : EINVAL; | |
1786 | if (retval) { | |
1787 | char *err; | |
1788 | ||
1789 | err = xasprintf("%s: could not convert to IPv6 address", str); | |
1790 | free(str); | |
1791 | ||
1792 | return err; | |
1793 | } | |
1794 | ||
1795 | netmask = strtok_r(NULL, "/", &save_ptr); | |
1796 | if (netmask) { | |
ff0b06ee BP |
1797 | if (inet_pton(AF_INET6, netmask, mask) != 1) { |
1798 | int prefix = atoi(netmask); | |
1799 | if (prefix <= 0 || prefix > 128) { | |
1800 | free(str); | |
1801 | return xasprintf("%s: prefix bits not between 1 and 128", s); | |
1802 | } else { | |
1803 | *mask = ipv6_create_mask(prefix); | |
1804 | } | |
6a885fd0 BP |
1805 | } |
1806 | } else { | |
1807 | *mask = in6addr_exact; | |
1808 | } | |
1809 | free(str); | |
1810 | ||
1811 | return NULL; | |
1812 | } | |
1813 | ||
1814 | static char * | |
1815 | mf_from_ofp_port_string(const struct mf_field *mf, const char *s, | |
1816 | ovs_be16 *valuep, ovs_be16 *maskp) | |
1817 | { | |
4e022ec0 | 1818 | ofp_port_t port; |
6a885fd0 | 1819 | |
cb22974d | 1820 | ovs_assert(mf->n_bytes == sizeof(ovs_be16)); |
33ab38d9 JR |
1821 | |
1822 | if (ofputil_port_from_string(s, &port)) { | |
4e022ec0 | 1823 | *valuep = htons(ofp_to_u16(port)); |
b8266395 | 1824 | *maskp = OVS_BE16_MAX; |
6a885fd0 | 1825 | return NULL; |
6a885fd0 | 1826 | } |
33ab38d9 | 1827 | return xasprintf("%s: port value out of range for %s", s, mf->name); |
6a885fd0 BP |
1828 | } |
1829 | ||
72333065 JR |
1830 | static char * |
1831 | mf_from_ofp_port_string32(const struct mf_field *mf, const char *s, | |
1832 | ovs_be32 *valuep, ovs_be32 *maskp) | |
1833 | { | |
4e022ec0 | 1834 | ofp_port_t port; |
72333065 JR |
1835 | |
1836 | ovs_assert(mf->n_bytes == sizeof(ovs_be32)); | |
1837 | if (ofputil_port_from_string(s, &port)) { | |
1838 | *valuep = ofputil_port_to_ofp11(port); | |
b8266395 | 1839 | *maskp = OVS_BE32_MAX; |
72333065 JR |
1840 | return NULL; |
1841 | } | |
1842 | return xasprintf("%s: port value out of range for %s", s, mf->name); | |
1843 | } | |
1844 | ||
7257b535 BP |
1845 | struct frag_handling { |
1846 | const char *name; | |
1847 | uint8_t mask; | |
1848 | uint8_t value; | |
1849 | }; | |
1850 | ||
1851 | static const struct frag_handling all_frags[] = { | |
eadef313 JP |
1852 | #define A FLOW_NW_FRAG_ANY |
1853 | #define L FLOW_NW_FRAG_LATER | |
7257b535 BP |
1854 | /* name mask value */ |
1855 | ||
1856 | { "no", A|L, 0 }, | |
1857 | { "first", A|L, A }, | |
1858 | { "later", A|L, A|L }, | |
1859 | ||
1860 | { "no", A, 0 }, | |
1861 | { "yes", A, A }, | |
1862 | ||
1863 | { "not_later", L, 0 }, | |
1864 | { "later", L, L }, | |
1865 | #undef A | |
1866 | #undef L | |
1867 | }; | |
1868 | ||
1869 | static char * | |
1870 | mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp) | |
1871 | { | |
1872 | const struct frag_handling *h; | |
1873 | ||
1874 | for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) { | |
1875 | if (!strcasecmp(s, h->name)) { | |
1876 | /* We force the upper bits of the mask on to make mf_parse_value() | |
1877 | * happy (otherwise it will never think it's an exact match.) */ | |
eadef313 | 1878 | *maskp = h->mask | ~FLOW_NW_FRAG_MASK; |
7257b535 BP |
1879 | *valuep = h->value; |
1880 | return NULL; | |
1881 | } | |
1882 | } | |
1883 | ||
1884 | return xasprintf("%s: unknown fragment type (valid types are \"no\", " | |
1885 | "\"yes\", \"first\", \"later\", \"not_first\"", s); | |
1886 | } | |
1887 | ||
4fe3445a PS |
1888 | static int |
1889 | parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t), | |
1890 | ovs_be16 *res) | |
1891 | { | |
1892 | uint32_t result = 0; | |
1893 | char *save_ptr = NULL; | |
1894 | char *name; | |
1895 | int rc = 0; | |
1896 | char *s = xstrdup(s_); | |
1897 | ||
1898 | for (name = strtok_r((char *)s, " |", &save_ptr); name; | |
1899 | name = strtok_r(NULL, " |", &save_ptr)) { | |
1900 | int name_len; | |
1901 | unsigned long long int flags; | |
1902 | uint32_t bit; | |
4fe3445a | 1903 | |
c2c28dfd | 1904 | if (ovs_scan(name, "%lli", &flags)) { |
4fe3445a PS |
1905 | result |= flags; |
1906 | continue; | |
1907 | } | |
1908 | name_len = strlen(name); | |
1909 | for (bit = 1; bit; bit <<= 1) { | |
1910 | const char *fname = bit_to_string(bit); | |
1911 | size_t len; | |
1912 | ||
1913 | if (!fname) { | |
1914 | continue; | |
1915 | } | |
1916 | ||
1917 | len = strlen(fname); | |
1918 | if (len != name_len) { | |
1919 | continue; | |
1920 | } | |
1921 | if (!strncmp(name, fname, len)) { | |
1922 | result |= bit; | |
1923 | break; | |
1924 | } | |
1925 | } | |
1926 | ||
1927 | if (!bit) { | |
1928 | rc = -ENOENT; | |
1929 | goto out; | |
1930 | } | |
1931 | } | |
1932 | ||
1933 | *res = htons(result); | |
1934 | out: | |
1935 | free(s); | |
1936 | return rc; | |
1937 | } | |
1938 | ||
1939 | static char * | |
b0aa8146 | 1940 | mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp) |
4fe3445a PS |
1941 | { |
1942 | if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) { | |
b8266395 | 1943 | *maskp = OVS_BE16_MAX; |
4fe3445a PS |
1944 | return NULL; |
1945 | } | |
1946 | ||
1947 | return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", " | |
c49a5e33 | 1948 | "\"csum\", \"key\")", s); |
4fe3445a PS |
1949 | } |
1950 | ||
61bf6666 JR |
1951 | static char * |
1952 | mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp) | |
1953 | { | |
1954 | uint16_t flags = 0; | |
1955 | uint16_t mask = 0; | |
1956 | uint16_t bit; | |
1957 | int n; | |
1958 | ||
1959 | if (ovs_scan(s, "%"SCNi16"/%"SCNi16"%n", &flags, &mask, &n) && !s[n]) { | |
1960 | *flagsp = htons(flags); | |
1961 | *maskp = htons(mask); | |
1962 | return NULL; | |
1963 | } | |
1964 | if (ovs_scan(s, "%"SCNi16"%n", &flags, &n) && !s[n]) { | |
1965 | *flagsp = htons(flags); | |
1966 | *maskp = OVS_BE16_MAX; | |
1967 | return NULL; | |
1968 | } | |
1969 | ||
1970 | while (*s != '\0') { | |
1971 | bool set; | |
1972 | int name_len; | |
1973 | ||
1974 | switch (*s) { | |
1975 | case '+': | |
1976 | set = true; | |
1977 | break; | |
1978 | case '-': | |
1979 | set = false; | |
1980 | break; | |
1981 | default: | |
1982 | return xasprintf("%s: TCP flag must be preceded by '+' (for SET) " | |
1983 | "or '-' (NOT SET)", s); | |
1984 | } | |
1985 | s++; | |
1986 | ||
1987 | name_len = strcspn(s,"+-"); | |
1988 | ||
1989 | for (bit = 1; bit; bit <<= 1) { | |
1990 | const char *fname = packet_tcp_flag_to_string(bit); | |
1991 | size_t len; | |
1992 | ||
1993 | if (!fname) { | |
1994 | continue; | |
1995 | } | |
1996 | ||
1997 | len = strlen(fname); | |
1998 | if (len != name_len) { | |
1999 | continue; | |
2000 | } | |
2001 | if (!strncmp(s, fname, len)) { | |
2002 | if (mask & bit) { | |
2003 | return xasprintf("%s: Each TCP flag can be specified only " | |
2004 | "once", s); | |
2005 | } | |
2006 | if (set) { | |
2007 | flags |= bit; | |
2008 | } | |
2009 | mask |= bit; | |
2010 | break; | |
2011 | } | |
2012 | } | |
2013 | ||
2014 | if (!bit) { | |
2015 | return xasprintf("%s: unknown TCP flag(s)", s); | |
2016 | } | |
2017 | s += name_len; | |
2018 | } | |
2019 | ||
2020 | *flagsp = htons(flags); | |
2021 | *maskp = htons(mask); | |
2022 | return NULL; | |
2023 | } | |
2024 | ||
2025 | ||
6a885fd0 BP |
2026 | /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns |
2027 | * NULL if successful, otherwise a malloc()'d string describing the error. */ | |
2028 | char * | |
2029 | mf_parse(const struct mf_field *mf, const char *s, | |
2030 | union mf_value *value, union mf_value *mask) | |
2031 | { | |
3634dff8 BP |
2032 | char *error; |
2033 | ||
bedde04c | 2034 | if (!strcmp(s, "*")) { |
6a885fd0 BP |
2035 | memset(value, 0, mf->n_bytes); |
2036 | memset(mask, 0, mf->n_bytes); | |
2037 | return NULL; | |
2038 | } | |
2039 | ||
2040 | switch (mf->string) { | |
2041 | case MFS_DECIMAL: | |
2042 | case MFS_HEXADECIMAL: | |
3634dff8 BP |
2043 | error = mf_from_integer_string(mf, s, |
2044 | (uint8_t *) value, (uint8_t *) mask); | |
2045 | break; | |
6a885fd0 BP |
2046 | |
2047 | case MFS_ETHERNET: | |
3634dff8 BP |
2048 | error = mf_from_ethernet_string(mf, s, value->mac, mask->mac); |
2049 | break; | |
6a885fd0 BP |
2050 | |
2051 | case MFS_IPV4: | |
3634dff8 BP |
2052 | error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32); |
2053 | break; | |
6a885fd0 BP |
2054 | |
2055 | case MFS_IPV6: | |
3634dff8 BP |
2056 | error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6); |
2057 | break; | |
6a885fd0 BP |
2058 | |
2059 | case MFS_OFP_PORT: | |
3634dff8 BP |
2060 | error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16); |
2061 | break; | |
7257b535 | 2062 | |
72333065 | 2063 | case MFS_OFP_PORT_OXM: |
3634dff8 BP |
2064 | error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32); |
2065 | break; | |
72333065 | 2066 | |
7257b535 | 2067 | case MFS_FRAG: |
3634dff8 BP |
2068 | error = mf_from_frag_string(s, &value->u8, &mask->u8); |
2069 | break; | |
4fe3445a PS |
2070 | |
2071 | case MFS_TNL_FLAGS: | |
cb22974d | 2072 | ovs_assert(mf->n_bytes == sizeof(ovs_be16)); |
3634dff8 BP |
2073 | error = mf_from_tun_flags_string(s, &value->be16, &mask->be16); |
2074 | break; | |
2075 | ||
61bf6666 JR |
2076 | case MFS_TCP_FLAGS: |
2077 | ovs_assert(mf->n_bytes == sizeof(ovs_be16)); | |
2078 | error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16); | |
2079 | break; | |
2080 | ||
3634dff8 | 2081 | default: |
428b2edd | 2082 | OVS_NOT_REACHED(); |
6a885fd0 | 2083 | } |
3634dff8 BP |
2084 | |
2085 | if (!error && !mf_is_mask_valid(mf, mask)) { | |
2086 | error = xasprintf("%s: invalid mask for field %s", s, mf->name); | |
2087 | } | |
2088 | return error; | |
6a885fd0 BP |
2089 | } |
2090 | ||
2091 | /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if | |
2092 | * successful, otherwise a malloc()'d string describing the error. */ | |
2093 | char * | |
2094 | mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value) | |
2095 | { | |
2096 | union mf_value mask; | |
2097 | char *error; | |
2098 | ||
2099 | error = mf_parse(mf, s, value, &mask); | |
2100 | if (error) { | |
2101 | return error; | |
2102 | } | |
2103 | ||
2104 | if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) { | |
2105 | return xasprintf("%s: wildcards not allowed here", s); | |
2106 | } | |
2107 | return NULL; | |
2108 | } | |
2109 | ||
2110 | static void | |
2111 | mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep, | |
2112 | const uint8_t *maskp, struct ds *s) | |
2113 | { | |
2114 | unsigned long long int integer; | |
2115 | int i; | |
2116 | ||
cb22974d | 2117 | ovs_assert(mf->n_bytes <= 8); |
6a885fd0 BP |
2118 | |
2119 | integer = 0; | |
2120 | for (i = 0; i < mf->n_bytes; i++) { | |
2121 | integer = (integer << 8) | valuep[i]; | |
2122 | } | |
2123 | if (mf->string == MFS_HEXADECIMAL) { | |
2124 | ds_put_format(s, "%#llx", integer); | |
2125 | } else { | |
2126 | ds_put_format(s, "%lld", integer); | |
2127 | } | |
2128 | ||
2129 | if (maskp) { | |
2130 | unsigned long long int mask; | |
2131 | ||
2132 | mask = 0; | |
2133 | for (i = 0; i < mf->n_bytes; i++) { | |
2134 | mask = (mask << 8) | maskp[i]; | |
2135 | } | |
2136 | ||
2137 | /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm | |
2138 | * not sure that that a bit-mask written in decimal is ever easier to | |
2139 | * understand than the same bit-mask written in hexadecimal. */ | |
2140 | ds_put_format(s, "/%#llx", mask); | |
2141 | } | |
2142 | } | |
2143 | ||
7257b535 | 2144 | static void |
8f75dea3 | 2145 | mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s) |
7257b535 BP |
2146 | { |
2147 | const struct frag_handling *h; | |
7257b535 | 2148 | |
eadef313 | 2149 | mask &= FLOW_NW_FRAG_MASK; |
8f75dea3 | 2150 | value &= mask; |
7257b535 BP |
2151 | |
2152 | for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) { | |
2153 | if (value == h->value && mask == h->mask) { | |
2154 | ds_put_cstr(s, h->name); | |
2155 | return; | |
2156 | } | |
2157 | } | |
2158 | ds_put_cstr(s, "<error>"); | |
2159 | } | |
2160 | ||
4fe3445a PS |
2161 | static void |
2162 | mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s) | |
2163 | { | |
2164 | format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|'); | |
2165 | } | |
2166 | ||
61bf6666 JR |
2167 | static void |
2168 | mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s) | |
2169 | { | |
2170 | format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value), | |
2171 | TCP_FLAGS(mask)); | |
2172 | } | |
2173 | ||
6a885fd0 BP |
2174 | /* Appends to 's' a string representation of field 'mf' whose value is in |
2175 | * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */ | |
2176 | void | |
2177 | mf_format(const struct mf_field *mf, | |
2178 | const union mf_value *value, const union mf_value *mask, | |
2179 | struct ds *s) | |
2180 | { | |
2181 | if (mask) { | |
53cb9c3e | 2182 | if (is_all_zeros(mask, mf->n_bytes)) { |
6a885fd0 BP |
2183 | ds_put_cstr(s, "ANY"); |
2184 | return; | |
53cb9c3e | 2185 | } else if (is_all_ones(mask, mf->n_bytes)) { |
6a885fd0 BP |
2186 | mask = NULL; |
2187 | } | |
2188 | } | |
2189 | ||
2190 | switch (mf->string) { | |
72333065 JR |
2191 | case MFS_OFP_PORT_OXM: |
2192 | if (!mask) { | |
4e022ec0 | 2193 | ofp_port_t port; |
bc146369 | 2194 | ofputil_port_from_ofp11(value->be32, &port); |
72333065 JR |
2195 | ofputil_format_port(port, s); |
2196 | break; | |
2197 | } | |
2198 | /* fall through */ | |
6a885fd0 BP |
2199 | case MFS_OFP_PORT: |
2200 | if (!mask) { | |
4e022ec0 | 2201 | ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s); |
6a885fd0 BP |
2202 | break; |
2203 | } | |
2204 | /* fall through */ | |
2205 | case MFS_DECIMAL: | |
2206 | case MFS_HEXADECIMAL: | |
2207 | mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s); | |
2208 | break; | |
2209 | ||
2210 | case MFS_ETHERNET: | |
3b4d8ad3 | 2211 | eth_format_masked(value->mac, mask->mac, s); |
6a885fd0 BP |
2212 | break; |
2213 | ||
2214 | case MFS_IPV4: | |
b8266395 | 2215 | ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s); |
6a885fd0 BP |
2216 | break; |
2217 | ||
2218 | case MFS_IPV6: | |
2219 | print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL); | |
2220 | break; | |
2221 | ||
7257b535 | 2222 | case MFS_FRAG: |
8f75dea3 | 2223 | mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s); |
7257b535 BP |
2224 | break; |
2225 | ||
4fe3445a PS |
2226 | case MFS_TNL_FLAGS: |
2227 | mf_format_tnl_flags_string(&value->be16, s); | |
2228 | break; | |
2229 | ||
61bf6666 JR |
2230 | case MFS_TCP_FLAGS: |
2231 | mf_format_tcp_flags_string(value->be16, | |
2232 | mask ? mask->be16 : OVS_BE16_MAX, s); | |
2233 | break; | |
2234 | ||
6a885fd0 | 2235 | default: |
428b2edd | 2236 | OVS_NOT_REACHED(); |
6a885fd0 BP |
2237 | } |
2238 | } | |
816fd533 | 2239 | \f |
9bab681f IY |
2240 | /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits' |
2241 | * least-significant bits in 'x'. | |
2242 | */ | |
2243 | void | |
2244 | mf_write_subfield_flow(const struct mf_subfield *sf, | |
2245 | const union mf_subvalue *x, struct flow *flow) | |
2246 | { | |
2247 | const struct mf_field *field = sf->field; | |
2248 | union mf_value value; | |
2249 | ||
2250 | mf_get_value(field, flow, &value); | |
158edc8d | 2251 | bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, |
9bab681f IY |
2252 | sf->ofs, sf->n_bits); |
2253 | mf_set_flow_value(field, &value, flow); | |
2254 | } | |
2255 | ||
81a76618 | 2256 | /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits' |
1b35df45 | 2257 | * least-significant bits in 'x'. |
615660a9 | 2258 | */ |
1b35df45 BP |
2259 | void |
2260 | mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x, | |
81a76618 | 2261 | struct match *match) |
1b35df45 BP |
2262 | { |
2263 | const struct mf_field *field = sf->field; | |
2264 | union mf_value value, mask; | |
2265 | ||
81a76618 | 2266 | mf_get(field, match, &value, &mask); |
1b35df45 BP |
2267 | bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits); |
2268 | bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits); | |
81a76618 | 2269 | mf_set(field, &value, &mask, match); |
1b35df45 BP |
2270 | } |
2271 | ||
1b35df45 BP |
2272 | /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for |
2273 | * reading 'flow', e.g. as checked by mf_check_src(). */ | |
2274 | void | |
2275 | mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow, | |
2276 | union mf_subvalue *x) | |
2277 | { | |
2278 | union mf_value value; | |
2279 | ||
2280 | mf_get_value(sf->field, flow, &value); | |
2281 | ||
2282 | memset(x, 0, sizeof *x); | |
2283 | bitwise_copy(&value, sf->field->n_bytes, sf->ofs, | |
2284 | x, sizeof *x, 0, | |
2285 | sf->n_bits); | |
2286 | } | |
2287 | ||
816fd533 BP |
2288 | /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading |
2289 | * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or | |
2290 | * less. */ | |
2291 | uint64_t | |
2292 | mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow) | |
2293 | { | |
2294 | union mf_value value; | |
2295 | ||
2296 | mf_get_value(sf->field, flow, &value); | |
2297 | return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits); | |
2298 | } | |
2299 | ||
9bab681f IY |
2300 | void |
2301 | mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s) | |
2302 | { | |
2303 | int i; | |
2304 | ||
2305 | for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) { | |
2306 | if (subvalue->u8[i]) { | |
2307 | ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]); | |
2308 | for (i++; i < ARRAY_SIZE(subvalue->u8); i++) { | |
2309 | ds_put_format(s, "%02"PRIx8, subvalue->u8[i]); | |
2310 | } | |
2311 | return; | |
2312 | } | |
2313 | } | |
2314 | ds_put_char(s, '0'); | |
2315 | } | |
bc65c25a SH |
2316 | |
2317 | void | |
2318 | field_array_set(enum mf_field_id id, const union mf_value *value, | |
2319 | struct field_array *fa) | |
2320 | { | |
2321 | ovs_assert(id < MFF_N_IDS); | |
2322 | bitmap_set1(fa->used.bm, id); | |
2323 | fa->value[id] = *value; | |
2324 | } |