]>
Commit | Line | Data |
---|---|---|
81a76618 | 1 | /* |
18080541 | 2 | * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc. |
81a76618 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 | #include "match.h" | |
81a76618 BP |
19 | #include <stdlib.h> |
20 | #include "byte-order.h" | |
21 | #include "dynamic-string.h" | |
576ec803 | 22 | #include "ofp-util.h" |
81a76618 | 23 | #include "packets.h" |
9558d2a5 | 24 | #include "tun-metadata.h" |
81a76618 BP |
25 | |
26 | /* Converts the flow in 'flow' into a match in 'match', with the given | |
27 | * 'wildcards'. */ | |
28 | void | |
29 | match_init(struct match *match, | |
30 | const struct flow *flow, const struct flow_wildcards *wc) | |
31 | { | |
32 | match->flow = *flow; | |
33 | match->wc = *wc; | |
34 | match_zero_wildcarded_fields(match); | |
9558d2a5 | 35 | memset(&match->tun_md, 0, sizeof match->tun_md); |
81a76618 BP |
36 | } |
37 | ||
aa6c9932 MM |
38 | /* Converts a flow into a match. It sets the wildcard masks based on |
39 | * the packet contents. It will not set the mask for fields that do not | |
40 | * make sense for the packet type. */ | |
41 | void | |
42 | match_wc_init(struct match *match, const struct flow *flow) | |
43 | { | |
aa6c9932 | 44 | match->flow = *flow; |
aa6c9932 | 45 | |
78c9486d JR |
46 | flow_wildcards_init_for_packet(&match->wc, flow); |
47 | WC_MASK_FIELD(&match->wc, regs); | |
48 | WC_MASK_FIELD(&match->wc, metadata); | |
9558d2a5 JG |
49 | |
50 | memset(&match->tun_md, 0, sizeof match->tun_md); | |
aa6c9932 MM |
51 | } |
52 | ||
81a76618 BP |
53 | /* Initializes 'match' as a "catch-all" match that matches every packet. */ |
54 | void | |
55 | match_init_catchall(struct match *match) | |
56 | { | |
57 | memset(&match->flow, 0, sizeof match->flow); | |
58 | flow_wildcards_init_catchall(&match->wc); | |
9558d2a5 | 59 | memset(&match->tun_md, 0, sizeof match->tun_md); |
81a76618 BP |
60 | } |
61 | ||
62 | /* For each bit or field wildcarded in 'match', sets the corresponding bit or | |
63 | * field in 'flow' to all-0-bits. It is important to maintain this invariant | |
64 | * in a match that might be inserted into a classifier. | |
65 | * | |
66 | * It is never necessary to call this function directly for a match that is | |
67 | * initialized or modified only by match_*() functions. It is useful to | |
68 | * restore the invariant in a match whose 'wc' member is modified by hand. | |
69 | */ | |
70 | void | |
71 | match_zero_wildcarded_fields(struct match *match) | |
72 | { | |
73 | flow_zero_wildcards(&match->flow, &match->wc); | |
74 | } | |
75 | ||
a79f29f2 AZ |
76 | void |
77 | match_set_dp_hash(struct match *match, uint32_t value) | |
78 | { | |
79 | match_set_dp_hash_masked(match, value, UINT32_MAX); | |
80 | } | |
81 | ||
82 | void | |
83 | match_set_dp_hash_masked(struct match *match, uint32_t value, uint32_t mask) | |
84 | { | |
85 | match->wc.masks.dp_hash = mask; | |
86 | match->flow.dp_hash = value & mask; | |
87 | } | |
88 | ||
89 | void | |
90 | match_set_recirc_id(struct match *match, uint32_t value) | |
91 | { | |
92 | match->flow.recirc_id = value; | |
93 | match->wc.masks.recirc_id = UINT32_MAX; | |
94 | } | |
95 | ||
18080541 BP |
96 | void |
97 | match_set_conj_id(struct match *match, uint32_t value) | |
98 | { | |
99 | match->flow.conj_id = value; | |
100 | match->wc.masks.conj_id = UINT32_MAX; | |
101 | } | |
102 | ||
81a76618 BP |
103 | void |
104 | match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value) | |
105 | { | |
106 | match_set_reg_masked(match, reg_idx, value, UINT32_MAX); | |
107 | } | |
108 | ||
109 | void | |
110 | match_set_reg_masked(struct match *match, unsigned int reg_idx, | |
111 | uint32_t value, uint32_t mask) | |
112 | { | |
cb22974d | 113 | ovs_assert(reg_idx < FLOW_N_REGS); |
81a76618 BP |
114 | flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask); |
115 | match->flow.regs[reg_idx] = value & mask; | |
116 | } | |
117 | ||
79fe0f46 BP |
118 | void |
119 | match_set_xreg(struct match *match, unsigned int xreg_idx, uint64_t value) | |
120 | { | |
121 | match_set_xreg_masked(match, xreg_idx, value, UINT64_MAX); | |
122 | } | |
123 | ||
124 | void | |
125 | match_set_xreg_masked(struct match *match, unsigned int xreg_idx, | |
126 | uint64_t value, uint64_t mask) | |
127 | { | |
128 | ovs_assert(xreg_idx < FLOW_N_XREGS); | |
129 | flow_wildcards_set_xreg_mask(&match->wc, xreg_idx, mask); | |
130 | flow_set_xreg(&match->flow, xreg_idx, value & mask); | |
131 | } | |
132 | ||
c61f3870 BP |
133 | void |
134 | match_set_actset_output(struct match *match, ofp_port_t actset_output) | |
135 | { | |
136 | match->wc.masks.actset_output = u16_to_ofp(UINT16_MAX); | |
137 | match->flow.actset_output = actset_output; | |
138 | } | |
139 | ||
81a76618 BP |
140 | void |
141 | match_set_metadata(struct match *match, ovs_be64 metadata) | |
142 | { | |
b8266395 | 143 | match_set_metadata_masked(match, metadata, OVS_BE64_MAX); |
81a76618 BP |
144 | } |
145 | ||
146 | void | |
147 | match_set_metadata_masked(struct match *match, | |
148 | ovs_be64 metadata, ovs_be64 mask) | |
149 | { | |
150 | match->wc.masks.metadata = mask; | |
151 | match->flow.metadata = metadata & mask; | |
152 | } | |
153 | ||
154 | void | |
155 | match_set_tun_id(struct match *match, ovs_be64 tun_id) | |
156 | { | |
b8266395 | 157 | match_set_tun_id_masked(match, tun_id, OVS_BE64_MAX); |
81a76618 BP |
158 | } |
159 | ||
160 | void | |
161 | match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask) | |
162 | { | |
296e07ac JG |
163 | match->wc.masks.tunnel.tun_id = mask; |
164 | match->flow.tunnel.tun_id = tun_id & mask; | |
81a76618 BP |
165 | } |
166 | ||
4fe3445a PS |
167 | void |
168 | match_set_tun_src(struct match *match, ovs_be32 src) | |
169 | { | |
b8266395 | 170 | match_set_tun_src_masked(match, src, OVS_BE32_MAX); |
4fe3445a PS |
171 | } |
172 | ||
173 | void | |
174 | match_set_tun_src_masked(struct match *match, ovs_be32 src, ovs_be32 mask) | |
175 | { | |
176 | match->wc.masks.tunnel.ip_src = mask; | |
177 | match->flow.tunnel.ip_src = src & mask; | |
178 | } | |
179 | ||
180 | void | |
181 | match_set_tun_dst(struct match *match, ovs_be32 dst) | |
182 | { | |
b8266395 | 183 | match_set_tun_dst_masked(match, dst, OVS_BE32_MAX); |
4fe3445a PS |
184 | } |
185 | ||
186 | void | |
187 | match_set_tun_dst_masked(struct match *match, ovs_be32 dst, ovs_be32 mask) | |
188 | { | |
189 | match->wc.masks.tunnel.ip_dst = mask; | |
190 | match->flow.tunnel.ip_dst = dst & mask; | |
191 | } | |
192 | ||
193 | void | |
194 | match_set_tun_ttl(struct match *match, uint8_t ttl) | |
195 | { | |
196 | match_set_tun_ttl_masked(match, ttl, UINT8_MAX); | |
197 | } | |
198 | ||
199 | void | |
200 | match_set_tun_ttl_masked(struct match *match, uint8_t ttl, uint8_t mask) | |
201 | { | |
202 | match->wc.masks.tunnel.ip_ttl = mask; | |
203 | match->flow.tunnel.ip_ttl = ttl & mask; | |
204 | } | |
205 | ||
206 | void | |
207 | match_set_tun_tos(struct match *match, uint8_t tos) | |
208 | { | |
209 | match_set_tun_tos_masked(match, tos, UINT8_MAX); | |
210 | } | |
211 | ||
212 | void | |
213 | match_set_tun_tos_masked(struct match *match, uint8_t tos, uint8_t mask) | |
214 | { | |
215 | match->wc.masks.tunnel.ip_tos = mask; | |
216 | match->flow.tunnel.ip_tos = tos & mask; | |
217 | } | |
218 | ||
219 | void | |
220 | match_set_tun_flags(struct match *match, uint16_t flags) | |
221 | { | |
222 | match_set_tun_flags_masked(match, flags, UINT16_MAX); | |
223 | } | |
224 | ||
225 | void | |
226 | match_set_tun_flags_masked(struct match *match, uint16_t flags, uint16_t mask) | |
227 | { | |
228 | match->wc.masks.tunnel.flags = mask; | |
229 | match->flow.tunnel.flags = flags & mask; | |
230 | } | |
231 | ||
ac6073e3 MC |
232 | void |
233 | match_set_tun_gbp_id_masked(struct match *match, ovs_be16 gbp_id, ovs_be16 mask) | |
234 | { | |
235 | match->wc.masks.tunnel.gbp_id = mask; | |
236 | match->flow.tunnel.gbp_id = gbp_id & mask; | |
237 | } | |
238 | ||
239 | void | |
240 | match_set_tun_gbp_id(struct match *match, ovs_be16 gbp_id) | |
241 | { | |
242 | match_set_tun_gbp_id_masked(match, gbp_id, OVS_BE16_MAX); | |
243 | } | |
244 | ||
245 | void | |
246 | match_set_tun_gbp_flags_masked(struct match *match, uint8_t flags, uint8_t mask) | |
247 | { | |
248 | match->wc.masks.tunnel.gbp_flags = mask; | |
249 | match->flow.tunnel.gbp_flags = flags & mask; | |
250 | } | |
251 | ||
252 | void | |
253 | match_set_tun_gbp_flags(struct match *match, uint8_t flags) | |
254 | { | |
255 | match_set_tun_gbp_flags_masked(match, flags, UINT8_MAX); | |
256 | } | |
257 | ||
81a76618 | 258 | void |
4e022ec0 | 259 | match_set_in_port(struct match *match, ofp_port_t ofp_port) |
81a76618 | 260 | { |
4e022ec0 AW |
261 | match->wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX); |
262 | match->flow.in_port.ofp_port = ofp_port; | |
81a76618 BP |
263 | } |
264 | ||
1b567fb9 AA |
265 | void |
266 | match_set_skb_priority(struct match *match, uint32_t skb_priority) | |
267 | { | |
268 | match->wc.masks.skb_priority = UINT32_MAX; | |
269 | match->flow.skb_priority = skb_priority; | |
270 | } | |
271 | ||
272 | void | |
1362e248 | 273 | match_set_pkt_mark(struct match *match, uint32_t pkt_mark) |
1b567fb9 | 274 | { |
ac923e91 JG |
275 | match_set_pkt_mark_masked(match, pkt_mark, UINT32_MAX); |
276 | } | |
277 | ||
278 | void | |
279 | match_set_pkt_mark_masked(struct match *match, uint32_t pkt_mark, uint32_t mask) | |
280 | { | |
281 | match->flow.pkt_mark = pkt_mark & mask; | |
282 | match->wc.masks.pkt_mark = mask; | |
1b567fb9 AA |
283 | } |
284 | ||
81a76618 BP |
285 | void |
286 | match_set_dl_type(struct match *match, ovs_be16 dl_type) | |
287 | { | |
b8266395 | 288 | match->wc.masks.dl_type = OVS_BE16_MAX; |
81a76618 BP |
289 | match->flow.dl_type = dl_type; |
290 | } | |
291 | ||
292 | /* Modifies 'value_src' so that the Ethernet address must match 'value_dst' | |
293 | * exactly. 'mask_dst' is set to all 1s. */ | |
294 | static void | |
295 | set_eth(const uint8_t value_src[ETH_ADDR_LEN], | |
296 | uint8_t value_dst[ETH_ADDR_LEN], | |
297 | uint8_t mask_dst[ETH_ADDR_LEN]) | |
298 | { | |
299 | memcpy(value_dst, value_src, ETH_ADDR_LEN); | |
300 | memset(mask_dst, 0xff, ETH_ADDR_LEN); | |
301 | } | |
302 | ||
303 | /* Modifies 'value_src' so that the Ethernet address must match 'value_src' | |
304 | * after each byte is ANDed with the appropriate byte in 'mask_src'. | |
305 | * 'mask_dst' is set to 'mask_src' */ | |
306 | static void | |
307 | set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN], | |
308 | const uint8_t mask_src[ETH_ADDR_LEN], | |
309 | uint8_t value_dst[ETH_ADDR_LEN], | |
310 | uint8_t mask_dst[ETH_ADDR_LEN]) | |
311 | { | |
312 | size_t i; | |
313 | ||
314 | for (i = 0; i < ETH_ADDR_LEN; i++) { | |
315 | value_dst[i] = value_src[i] & mask_src[i]; | |
316 | mask_dst[i] = mask_src[i]; | |
317 | } | |
318 | } | |
319 | ||
320 | /* Modifies 'rule' so that the source Ethernet address must match 'dl_src' | |
321 | * exactly. */ | |
322 | void | |
323 | match_set_dl_src(struct match *match, const uint8_t dl_src[ETH_ADDR_LEN]) | |
324 | { | |
325 | set_eth(dl_src, match->flow.dl_src, match->wc.masks.dl_src); | |
326 | } | |
327 | ||
328 | /* Modifies 'rule' so that the source Ethernet address must match 'dl_src' | |
329 | * after each byte is ANDed with the appropriate byte in 'mask'. */ | |
330 | void | |
331 | match_set_dl_src_masked(struct match *match, | |
332 | const uint8_t dl_src[ETH_ADDR_LEN], | |
333 | const uint8_t mask[ETH_ADDR_LEN]) | |
334 | { | |
335 | set_eth_masked(dl_src, mask, match->flow.dl_src, match->wc.masks.dl_src); | |
336 | } | |
337 | ||
338 | /* Modifies 'match' so that the Ethernet address must match 'dl_dst' | |
339 | * exactly. */ | |
340 | void | |
341 | match_set_dl_dst(struct match *match, const uint8_t dl_dst[ETH_ADDR_LEN]) | |
342 | { | |
343 | set_eth(dl_dst, match->flow.dl_dst, match->wc.masks.dl_dst); | |
344 | } | |
345 | ||
346 | /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each | |
347 | * byte is ANDed with the appropriate byte in 'mask'. | |
348 | * | |
349 | * This function will assert-fail if 'mask' is invalid. Only 'mask' values | |
350 | * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */ | |
351 | void | |
352 | match_set_dl_dst_masked(struct match *match, | |
353 | const uint8_t dl_dst[ETH_ADDR_LEN], | |
354 | const uint8_t mask[ETH_ADDR_LEN]) | |
355 | { | |
356 | set_eth_masked(dl_dst, mask, match->flow.dl_dst, match->wc.masks.dl_dst); | |
357 | } | |
358 | ||
359 | void | |
360 | match_set_dl_tci(struct match *match, ovs_be16 tci) | |
361 | { | |
362 | match_set_dl_tci_masked(match, tci, htons(0xffff)); | |
363 | } | |
364 | ||
365 | void | |
366 | match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask) | |
367 | { | |
368 | match->flow.vlan_tci = tci & mask; | |
369 | match->wc.masks.vlan_tci = mask; | |
370 | } | |
371 | ||
372 | /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already | |
373 | * wildcarded, then 'match' will match a packet regardless of whether it has an | |
374 | * 802.1Q header or not. */ | |
375 | void | |
376 | match_set_any_vid(struct match *match) | |
377 | { | |
378 | if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) { | |
379 | match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK); | |
380 | match->flow.vlan_tci &= ~htons(VLAN_VID_MASK); | |
381 | } else { | |
382 | match_set_dl_tci_masked(match, htons(0), htons(0)); | |
383 | } | |
384 | } | |
385 | ||
386 | /* Modifies 'match' depending on 'dl_vlan': | |
387 | * | |
388 | * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets | |
389 | * without an 802.1Q header. | |
390 | * | |
391 | * - Otherwise, makes 'match' match only packets with an 802.1Q header whose | |
392 | * VID equals the low 12 bits of 'dl_vlan'. | |
393 | */ | |
394 | void | |
395 | match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan) | |
396 | { | |
397 | flow_set_dl_vlan(&match->flow, dl_vlan); | |
398 | if (dl_vlan == htons(OFP10_VLAN_NONE)) { | |
b8266395 | 399 | match->wc.masks.vlan_tci = OVS_BE16_MAX; |
81a76618 BP |
400 | } else { |
401 | match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI); | |
402 | } | |
403 | } | |
404 | ||
405 | /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an | |
406 | * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID | |
407 | * plus CFI). */ | |
408 | void | |
409 | match_set_vlan_vid(struct match *match, ovs_be16 vid) | |
410 | { | |
411 | match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI)); | |
412 | } | |
413 | ||
414 | ||
415 | /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an | |
416 | * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID | |
417 | * plus CFI), with the corresponding 'mask'. */ | |
418 | void | |
419 | match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask) | |
420 | { | |
421 | ovs_be16 pcp_mask = htons(VLAN_PCP_MASK); | |
422 | ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI); | |
423 | ||
424 | mask &= vid_mask; | |
425 | flow_set_vlan_vid(&match->flow, vid & mask); | |
426 | match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask); | |
427 | } | |
428 | ||
429 | /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already | |
430 | * wildcarded, then 'match' will match a packet regardless of whether it has an | |
431 | * 802.1Q header or not. */ | |
432 | void | |
433 | match_set_any_pcp(struct match *match) | |
434 | { | |
435 | if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) { | |
436 | match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK); | |
437 | match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK); | |
438 | } else { | |
439 | match_set_dl_tci_masked(match, htons(0), htons(0)); | |
440 | } | |
441 | } | |
442 | ||
443 | /* Modifies 'match' so that it matches only packets with an 802.1Q header whose | |
444 | * PCP equals the low 3 bits of 'dl_vlan_pcp'. */ | |
445 | void | |
446 | match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp) | |
447 | { | |
448 | flow_set_vlan_pcp(&match->flow, dl_vlan_pcp); | |
449 | match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK); | |
450 | } | |
451 | ||
8bfd0fda BP |
452 | /* Modifies 'match' so that the MPLS label 'idx' matches 'lse' exactly. */ |
453 | void | |
454 | match_set_mpls_lse(struct match *match, int idx, ovs_be32 lse) | |
455 | { | |
456 | match->wc.masks.mpls_lse[idx] = OVS_BE32_MAX; | |
457 | match->flow.mpls_lse[idx] = lse; | |
458 | } | |
459 | ||
b02475c5 SH |
460 | /* Modifies 'match' so that the MPLS label is wildcarded. */ |
461 | void | |
8bfd0fda | 462 | match_set_any_mpls_label(struct match *match, int idx) |
b02475c5 | 463 | { |
8bfd0fda BP |
464 | match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_LABEL_MASK); |
465 | flow_set_mpls_label(&match->flow, idx, htonl(0)); | |
b02475c5 SH |
466 | } |
467 | ||
468 | /* Modifies 'match' so that it matches only packets with an MPLS header whose | |
469 | * label equals the low 20 bits of 'mpls_label'. */ | |
470 | void | |
8bfd0fda | 471 | match_set_mpls_label(struct match *match, int idx, ovs_be32 mpls_label) |
b02475c5 | 472 | { |
8bfd0fda BP |
473 | match->wc.masks.mpls_lse[idx] |= htonl(MPLS_LABEL_MASK); |
474 | flow_set_mpls_label(&match->flow, idx, mpls_label); | |
b02475c5 SH |
475 | } |
476 | ||
477 | /* Modifies 'match' so that the MPLS TC is wildcarded. */ | |
478 | void | |
8bfd0fda | 479 | match_set_any_mpls_tc(struct match *match, int idx) |
b02475c5 | 480 | { |
8bfd0fda BP |
481 | match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_TC_MASK); |
482 | flow_set_mpls_tc(&match->flow, idx, 0); | |
b02475c5 SH |
483 | } |
484 | ||
485 | /* Modifies 'match' so that it matches only packets with an MPLS header whose | |
486 | * Traffic Class equals the low 3 bits of 'mpls_tc'. */ | |
487 | void | |
8bfd0fda | 488 | match_set_mpls_tc(struct match *match, int idx, uint8_t mpls_tc) |
b02475c5 | 489 | { |
8bfd0fda BP |
490 | match->wc.masks.mpls_lse[idx] |= htonl(MPLS_TC_MASK); |
491 | flow_set_mpls_tc(&match->flow, idx, mpls_tc); | |
b02475c5 SH |
492 | } |
493 | ||
494 | /* Modifies 'match' so that the MPLS stack flag is wildcarded. */ | |
495 | void | |
8bfd0fda | 496 | match_set_any_mpls_bos(struct match *match, int idx) |
b02475c5 | 497 | { |
8bfd0fda BP |
498 | match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_BOS_MASK); |
499 | flow_set_mpls_bos(&match->flow, idx, 0); | |
b02475c5 SH |
500 | } |
501 | ||
502 | /* Modifies 'match' so that it matches only packets with an MPLS header whose | |
503 | * Stack Flag equals the lower bit of 'mpls_bos' */ | |
504 | void | |
8bfd0fda BP |
505 | match_set_mpls_bos(struct match *match, int idx, uint8_t mpls_bos) |
506 | { | |
507 | match->wc.masks.mpls_lse[idx] |= htonl(MPLS_BOS_MASK); | |
508 | flow_set_mpls_bos(&match->flow, idx, mpls_bos); | |
509 | } | |
510 | ||
511 | /* Modifies 'match' so that the MPLS LSE is wildcarded. */ | |
512 | void | |
513 | match_set_any_mpls_lse(struct match *match, int idx) | |
b02475c5 | 514 | { |
8bfd0fda BP |
515 | match->wc.masks.mpls_lse[idx] = htonl(0); |
516 | flow_set_mpls_lse(&match->flow, idx, htonl(0)); | |
b02475c5 SH |
517 | } |
518 | ||
81a76618 BP |
519 | void |
520 | match_set_tp_src(struct match *match, ovs_be16 tp_src) | |
521 | { | |
b8266395 | 522 | match_set_tp_src_masked(match, tp_src, OVS_BE16_MAX); |
81a76618 BP |
523 | } |
524 | ||
525 | void | |
526 | match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask) | |
527 | { | |
528 | match->flow.tp_src = port & mask; | |
529 | match->wc.masks.tp_src = mask; | |
530 | } | |
531 | ||
532 | void | |
533 | match_set_tp_dst(struct match *match, ovs_be16 tp_dst) | |
534 | { | |
b8266395 | 535 | match_set_tp_dst_masked(match, tp_dst, OVS_BE16_MAX); |
81a76618 BP |
536 | } |
537 | ||
538 | void | |
539 | match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask) | |
540 | { | |
541 | match->flow.tp_dst = port & mask; | |
542 | match->wc.masks.tp_dst = mask; | |
543 | } | |
544 | ||
dc235f7f JR |
545 | void |
546 | match_set_tcp_flags(struct match *match, ovs_be16 flags) | |
547 | { | |
548 | match_set_tcp_flags_masked(match, flags, OVS_BE16_MAX); | |
549 | } | |
550 | ||
551 | void | |
552 | match_set_tcp_flags_masked(struct match *match, ovs_be16 flags, ovs_be16 mask) | |
553 | { | |
554 | match->flow.tcp_flags = flags & mask; | |
555 | match->wc.masks.tcp_flags = mask; | |
556 | } | |
557 | ||
81a76618 BP |
558 | void |
559 | match_set_nw_proto(struct match *match, uint8_t nw_proto) | |
560 | { | |
561 | match->flow.nw_proto = nw_proto; | |
562 | match->wc.masks.nw_proto = UINT8_MAX; | |
563 | } | |
564 | ||
565 | void | |
566 | match_set_nw_src(struct match *match, ovs_be32 nw_src) | |
567 | { | |
568 | match->flow.nw_src = nw_src; | |
b8266395 | 569 | match->wc.masks.nw_src = OVS_BE32_MAX; |
81a76618 BP |
570 | } |
571 | ||
572 | void | |
573 | match_set_nw_src_masked(struct match *match, | |
574 | ovs_be32 nw_src, ovs_be32 mask) | |
575 | { | |
576 | match->flow.nw_src = nw_src & mask; | |
577 | match->wc.masks.nw_src = mask; | |
578 | } | |
579 | ||
580 | void | |
581 | match_set_nw_dst(struct match *match, ovs_be32 nw_dst) | |
582 | { | |
583 | match->flow.nw_dst = nw_dst; | |
b8266395 | 584 | match->wc.masks.nw_dst = OVS_BE32_MAX; |
81a76618 BP |
585 | } |
586 | ||
587 | void | |
588 | match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask) | |
589 | { | |
590 | match->flow.nw_dst = ip & mask; | |
591 | match->wc.masks.nw_dst = mask; | |
592 | } | |
593 | ||
594 | void | |
595 | match_set_nw_dscp(struct match *match, uint8_t nw_dscp) | |
596 | { | |
597 | match->wc.masks.nw_tos |= IP_DSCP_MASK; | |
598 | match->flow.nw_tos &= ~IP_DSCP_MASK; | |
599 | match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK; | |
600 | } | |
601 | ||
602 | void | |
603 | match_set_nw_ecn(struct match *match, uint8_t nw_ecn) | |
604 | { | |
605 | match->wc.masks.nw_tos |= IP_ECN_MASK; | |
606 | match->flow.nw_tos &= ~IP_ECN_MASK; | |
607 | match->flow.nw_tos |= nw_ecn & IP_ECN_MASK; | |
608 | } | |
609 | ||
610 | void | |
611 | match_set_nw_ttl(struct match *match, uint8_t nw_ttl) | |
612 | { | |
613 | match->wc.masks.nw_ttl = UINT8_MAX; | |
614 | match->flow.nw_ttl = nw_ttl; | |
615 | } | |
616 | ||
617 | void | |
618 | match_set_nw_frag(struct match *match, uint8_t nw_frag) | |
619 | { | |
620 | match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK; | |
621 | match->flow.nw_frag = nw_frag; | |
622 | } | |
623 | ||
624 | void | |
625 | match_set_nw_frag_masked(struct match *match, | |
626 | uint8_t nw_frag, uint8_t mask) | |
627 | { | |
628 | match->flow.nw_frag = nw_frag & mask; | |
629 | match->wc.masks.nw_frag = mask; | |
630 | } | |
631 | ||
632 | void | |
633 | match_set_icmp_type(struct match *match, uint8_t icmp_type) | |
634 | { | |
635 | match_set_tp_src(match, htons(icmp_type)); | |
636 | } | |
637 | ||
638 | void | |
639 | match_set_icmp_code(struct match *match, uint8_t icmp_code) | |
640 | { | |
641 | match_set_tp_dst(match, htons(icmp_code)); | |
642 | } | |
643 | ||
644 | void | |
645 | match_set_arp_sha(struct match *match, const uint8_t sha[ETH_ADDR_LEN]) | |
646 | { | |
647 | memcpy(match->flow.arp_sha, sha, ETH_ADDR_LEN); | |
648 | memset(match->wc.masks.arp_sha, UINT8_MAX, ETH_ADDR_LEN); | |
649 | } | |
650 | ||
651 | void | |
652 | match_set_arp_sha_masked(struct match *match, | |
653 | const uint8_t arp_sha[ETH_ADDR_LEN], | |
654 | const uint8_t mask[ETH_ADDR_LEN]) | |
655 | { | |
656 | set_eth_masked(arp_sha, mask, | |
657 | match->flow.arp_sha, match->wc.masks.arp_sha); | |
658 | } | |
659 | ||
660 | void | |
661 | match_set_arp_tha(struct match *match, const uint8_t tha[ETH_ADDR_LEN]) | |
662 | { | |
663 | memcpy(match->flow.arp_tha, tha, ETH_ADDR_LEN); | |
664 | memset(match->wc.masks.arp_tha, UINT8_MAX, ETH_ADDR_LEN); | |
665 | } | |
666 | ||
667 | void | |
668 | match_set_arp_tha_masked(struct match *match, | |
669 | const uint8_t arp_tha[ETH_ADDR_LEN], | |
670 | const uint8_t mask[ETH_ADDR_LEN]) | |
671 | { | |
672 | set_eth_masked(arp_tha, mask, | |
673 | match->flow.arp_tha, match->wc.masks.arp_tha); | |
674 | } | |
675 | ||
676 | void | |
677 | match_set_ipv6_src(struct match *match, const struct in6_addr *src) | |
678 | { | |
679 | match->flow.ipv6_src = *src; | |
680 | match->wc.masks.ipv6_src = in6addr_exact; | |
681 | } | |
682 | ||
683 | void | |
684 | match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src, | |
685 | const struct in6_addr *mask) | |
686 | { | |
687 | match->flow.ipv6_src = ipv6_addr_bitand(src, mask); | |
688 | match->wc.masks.ipv6_src = *mask; | |
689 | } | |
690 | ||
691 | void | |
692 | match_set_ipv6_dst(struct match *match, const struct in6_addr *dst) | |
693 | { | |
694 | match->flow.ipv6_dst = *dst; | |
695 | match->wc.masks.ipv6_dst = in6addr_exact; | |
696 | } | |
697 | ||
698 | void | |
699 | match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst, | |
700 | const struct in6_addr *mask) | |
701 | { | |
702 | match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask); | |
703 | match->wc.masks.ipv6_dst = *mask; | |
704 | } | |
705 | ||
706 | void | |
707 | match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label) | |
708 | { | |
b8266395 | 709 | match->wc.masks.ipv6_label = OVS_BE32_MAX; |
81a76618 BP |
710 | match->flow.ipv6_label = ipv6_label; |
711 | } | |
712 | ||
713 | ||
714 | void | |
715 | match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label, | |
716 | ovs_be32 mask) | |
717 | { | |
718 | match->flow.ipv6_label = ipv6_label & mask; | |
719 | match->wc.masks.ipv6_label = mask; | |
720 | } | |
721 | ||
722 | void | |
723 | match_set_nd_target(struct match *match, const struct in6_addr *target) | |
724 | { | |
725 | match->flow.nd_target = *target; | |
726 | match->wc.masks.nd_target = in6addr_exact; | |
727 | } | |
728 | ||
729 | void | |
730 | match_set_nd_target_masked(struct match *match, | |
731 | const struct in6_addr *target, | |
732 | const struct in6_addr *mask) | |
733 | { | |
734 | match->flow.nd_target = ipv6_addr_bitand(target, mask); | |
735 | match->wc.masks.nd_target = *mask; | |
736 | } | |
737 | ||
738 | /* Returns true if 'a' and 'b' wildcard the same fields and have the same | |
739 | * values for fixed fields, otherwise false. */ | |
740 | bool | |
741 | match_equal(const struct match *a, const struct match *b) | |
742 | { | |
743 | return (flow_wildcards_equal(&a->wc, &b->wc) | |
744 | && flow_equal(&a->flow, &b->flow)); | |
745 | } | |
746 | ||
747 | /* Returns a hash value for the flow and wildcards in 'match', starting from | |
748 | * 'basis'. */ | |
749 | uint32_t | |
750 | match_hash(const struct match *match, uint32_t basis) | |
751 | { | |
752 | return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis)); | |
753 | } | |
754 | ||
adcf00ba AZ |
755 | static bool |
756 | match_has_default_recirc_id(const struct match *m) | |
757 | { | |
758 | return m->flow.recirc_id == 0 && (m->wc.masks.recirc_id == UINT32_MAX || | |
759 | m->wc.masks.recirc_id == 0); | |
760 | } | |
761 | ||
762 | static bool | |
763 | match_has_default_dp_hash(const struct match *m) | |
764 | { | |
765 | return ((m->flow.dp_hash | m->wc.masks.dp_hash) == 0); | |
766 | } | |
767 | ||
768 | /* Return true if the hidden fields of the match are set to the default values. | |
769 | * The default values equals to those set up by match_init_hidden_fields(). */ | |
770 | bool | |
771 | match_has_default_hidden_fields(const struct match *m) | |
772 | { | |
773 | return match_has_default_recirc_id(m) && match_has_default_dp_hash(m); | |
774 | } | |
775 | ||
776 | void | |
777 | match_init_hidden_fields(struct match *m) | |
778 | { | |
779 | match_set_recirc_id(m, 0); | |
780 | match_set_dp_hash_masked(m, 0, 0); | |
781 | } | |
782 | ||
81a76618 | 783 | static void |
3bd0fd39 WSH |
784 | format_eth_masked(struct ds *s, const char *name, |
785 | const uint8_t eth[ETH_ADDR_LEN], | |
786 | const uint8_t mask[ETH_ADDR_LEN]) | |
81a76618 BP |
787 | { |
788 | if (!eth_addr_is_zero(mask)) { | |
789 | ds_put_format(s, "%s=", name); | |
790 | eth_format_masked(eth, mask, s); | |
791 | ds_put_char(s, ','); | |
792 | } | |
793 | } | |
794 | ||
795 | static void | |
796 | format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip, | |
797 | ovs_be32 netmask) | |
798 | { | |
799 | if (netmask) { | |
800 | ds_put_format(s, "%s=", name); | |
801 | ip_format_masked(ip, netmask, s); | |
802 | ds_put_char(s, ','); | |
803 | } | |
804 | } | |
805 | ||
806 | static void | |
807 | format_ipv6_netmask(struct ds *s, const char *name, | |
808 | const struct in6_addr *addr, | |
809 | const struct in6_addr *netmask) | |
810 | { | |
811 | if (!ipv6_mask_is_any(netmask)) { | |
812 | ds_put_format(s, "%s=", name); | |
813 | print_ipv6_masked(s, addr, netmask); | |
814 | ds_put_char(s, ','); | |
815 | } | |
816 | } | |
817 | ||
81a76618 BP |
818 | static void |
819 | format_be16_masked(struct ds *s, const char *name, | |
820 | ovs_be16 value, ovs_be16 mask) | |
821 | { | |
822 | if (mask != htons(0)) { | |
823 | ds_put_format(s, "%s=", name); | |
b8266395 | 824 | if (mask == OVS_BE16_MAX) { |
81a76618 BP |
825 | ds_put_format(s, "%"PRIu16, ntohs(value)); |
826 | } else { | |
827 | ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16, | |
828 | ntohs(value), ntohs(mask)); | |
829 | } | |
830 | ds_put_char(s, ','); | |
831 | } | |
832 | } | |
833 | ||
8bfd0fda BP |
834 | static void |
835 | format_be32_masked(struct ds *s, const char *name, | |
836 | ovs_be32 value, ovs_be32 mask) | |
837 | { | |
838 | if (mask != htonl(0)) { | |
839 | ds_put_format(s, "%s=", name); | |
840 | if (mask == OVS_BE32_MAX) { | |
841 | ds_put_format(s, "%"PRIu32, ntohl(value)); | |
842 | } else { | |
843 | ds_put_format(s, "0x%"PRIx32"/0x%"PRIx32, | |
844 | ntohl(value), ntohl(mask)); | |
845 | } | |
846 | ds_put_char(s, ','); | |
847 | } | |
848 | } | |
849 | ||
b60f804e AZ |
850 | static void |
851 | format_uint32_masked(struct ds *s, const char *name, | |
852 | uint32_t value, uint32_t mask) | |
853 | { | |
854 | if (mask) { | |
855 | ds_put_format(s, "%s=%#"PRIx32, name, value); | |
856 | if (mask != UINT32_MAX) { | |
857 | ds_put_format(s, "/%#"PRIx32, mask); | |
858 | } | |
859 | ds_put_char(s, ','); | |
860 | } | |
861 | } | |
862 | ||
863 | static void | |
864 | format_be64_masked(struct ds *s, const char *name, | |
865 | ovs_be64 value, ovs_be64 mask) | |
866 | { | |
867 | if (mask != htonll(0)) { | |
868 | ds_put_format(s, "%s=%#"PRIx64, name, ntohll(value)); | |
869 | if (mask != OVS_BE64_MAX) { | |
870 | ds_put_format(s, "/%#"PRIx64, ntohll(mask)); | |
871 | } | |
872 | ds_put_char(s, ','); | |
873 | } | |
874 | } | |
875 | ||
4fe3445a PS |
876 | static void |
877 | format_flow_tunnel(struct ds *s, const struct match *match) | |
878 | { | |
879 | const struct flow_wildcards *wc = &match->wc; | |
880 | const struct flow_tnl *tnl = &match->flow.tunnel; | |
881 | ||
b60f804e | 882 | format_be64_masked(s, "tun_id", tnl->tun_id, wc->masks.tunnel.tun_id); |
4fe3445a PS |
883 | format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src); |
884 | format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst); | |
885 | ||
ac6073e3 MC |
886 | if (wc->masks.tunnel.gbp_id) { |
887 | format_be16_masked(s, "tun_gbp_id", tnl->gbp_id, | |
888 | wc->masks.tunnel.gbp_id); | |
889 | } | |
890 | ||
891 | if (wc->masks.tunnel.gbp_flags) { | |
892 | ds_put_format(s, "tun_gbp_flags=%#"PRIx8",", tnl->gbp_flags); | |
893 | } | |
894 | ||
4fe3445a PS |
895 | if (wc->masks.tunnel.ip_tos) { |
896 | ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos); | |
897 | } | |
898 | if (wc->masks.tunnel.ip_ttl) { | |
899 | ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl); | |
900 | } | |
901 | if (wc->masks.tunnel.flags) { | |
902 | format_flags(s, flow_tun_flag_to_string, tnl->flags, '|'); | |
903 | ds_put_char(s, ','); | |
904 | } | |
9558d2a5 | 905 | tun_metadata_match_format(s, match); |
4fe3445a PS |
906 | } |
907 | ||
81a76618 BP |
908 | /* Appends a string representation of 'match' to 's'. If 'priority' is |
909 | * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */ | |
910 | void | |
eb391b76 | 911 | match_format(const struct match *match, struct ds *s, int priority) |
81a76618 BP |
912 | { |
913 | const struct flow_wildcards *wc = &match->wc; | |
914 | size_t start_len = s->length; | |
915 | const struct flow *f = &match->flow; | |
916 | bool skip_type = false; | |
917 | bool skip_proto = false; | |
918 | ||
919 | int i; | |
920 | ||
9558d2a5 | 921 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 32); |
81a76618 BP |
922 | |
923 | if (priority != OFP_DEFAULT_PRIORITY) { | |
eb391b76 | 924 | ds_put_format(s, "priority=%d,", priority); |
81a76618 BP |
925 | } |
926 | ||
b60f804e | 927 | format_uint32_masked(s, "pkt_mark", f->pkt_mark, wc->masks.pkt_mark); |
1b567fb9 | 928 | |
a79f29f2 AZ |
929 | if (wc->masks.recirc_id) { |
930 | format_uint32_masked(s, "recirc_id", f->recirc_id, | |
931 | wc->masks.recirc_id); | |
932 | } | |
933 | ||
330de069 | 934 | if (wc->masks.dp_hash) { |
a79f29f2 AZ |
935 | format_uint32_masked(s, "dp_hash", f->dp_hash, |
936 | wc->masks.dp_hash); | |
937 | } | |
938 | ||
18080541 BP |
939 | if (wc->masks.conj_id) { |
940 | ds_put_format(s, "conj_id=%"PRIu32",", f->conj_id); | |
941 | } | |
942 | ||
1b567fb9 AA |
943 | if (wc->masks.skb_priority) { |
944 | ds_put_format(s, "skb_priority=%#"PRIx32",", f->skb_priority); | |
945 | } | |
946 | ||
c61f3870 BP |
947 | if (wc->masks.actset_output) { |
948 | ds_put_cstr(s, "actset_output="); | |
949 | ofputil_format_port(f->actset_output, s); | |
950 | ds_put_char(s, ','); | |
951 | } | |
952 | ||
81a76618 BP |
953 | if (wc->masks.dl_type) { |
954 | skip_type = true; | |
955 | if (f->dl_type == htons(ETH_TYPE_IP)) { | |
956 | if (wc->masks.nw_proto) { | |
957 | skip_proto = true; | |
958 | if (f->nw_proto == IPPROTO_ICMP) { | |
959 | ds_put_cstr(s, "icmp,"); | |
0e612675 FL |
960 | } else if (f->nw_proto == IPPROTO_IGMP) { |
961 | ds_put_cstr(s, "igmp,"); | |
81a76618 BP |
962 | } else if (f->nw_proto == IPPROTO_TCP) { |
963 | ds_put_cstr(s, "tcp,"); | |
964 | } else if (f->nw_proto == IPPROTO_UDP) { | |
965 | ds_put_cstr(s, "udp,"); | |
0d56eaf2 JS |
966 | } else if (f->nw_proto == IPPROTO_SCTP) { |
967 | ds_put_cstr(s, "sctp,"); | |
81a76618 BP |
968 | } else { |
969 | ds_put_cstr(s, "ip,"); | |
970 | skip_proto = false; | |
971 | } | |
972 | } else { | |
973 | ds_put_cstr(s, "ip,"); | |
974 | } | |
975 | } else if (f->dl_type == htons(ETH_TYPE_IPV6)) { | |
976 | if (wc->masks.nw_proto) { | |
977 | skip_proto = true; | |
978 | if (f->nw_proto == IPPROTO_ICMPV6) { | |
979 | ds_put_cstr(s, "icmp6,"); | |
980 | } else if (f->nw_proto == IPPROTO_TCP) { | |
981 | ds_put_cstr(s, "tcp6,"); | |
982 | } else if (f->nw_proto == IPPROTO_UDP) { | |
983 | ds_put_cstr(s, "udp6,"); | |
0d56eaf2 JS |
984 | } else if (f->nw_proto == IPPROTO_SCTP) { |
985 | ds_put_cstr(s, "sctp6,"); | |
81a76618 BP |
986 | } else { |
987 | ds_put_cstr(s, "ipv6,"); | |
988 | skip_proto = false; | |
989 | } | |
990 | } else { | |
991 | ds_put_cstr(s, "ipv6,"); | |
992 | } | |
993 | } else if (f->dl_type == htons(ETH_TYPE_ARP)) { | |
994 | ds_put_cstr(s, "arp,"); | |
8087f5ff MM |
995 | } else if (f->dl_type == htons(ETH_TYPE_RARP)) { |
996 | ds_put_cstr(s, "rarp,"); | |
392c30ba SH |
997 | } else if (f->dl_type == htons(ETH_TYPE_MPLS)) { |
998 | ds_put_cstr(s, "mpls,"); | |
999 | } else if (f->dl_type == htons(ETH_TYPE_MPLS_MCAST)) { | |
1000 | ds_put_cstr(s, "mplsm,"); | |
81a76618 BP |
1001 | } else { |
1002 | skip_type = false; | |
1003 | } | |
1004 | } | |
1005 | for (i = 0; i < FLOW_N_REGS; i++) { | |
b60f804e AZ |
1006 | #define REGNAME_LEN 20 |
1007 | char regname[REGNAME_LEN]; | |
1008 | if (snprintf(regname, REGNAME_LEN, "reg%d", i) >= REGNAME_LEN) { | |
1009 | strcpy(regname, "reg?"); | |
81a76618 | 1010 | } |
b60f804e | 1011 | format_uint32_masked(s, regname, f->regs[i], wc->masks.regs[i]); |
81a76618 | 1012 | } |
4fe3445a PS |
1013 | |
1014 | format_flow_tunnel(s, match); | |
1015 | ||
b60f804e AZ |
1016 | format_be64_masked(s, "metadata", f->metadata, wc->masks.metadata); |
1017 | ||
4e022ec0 | 1018 | if (wc->masks.in_port.ofp_port) { |
576ec803 | 1019 | ds_put_cstr(s, "in_port="); |
4e022ec0 | 1020 | ofputil_format_port(f->in_port.ofp_port, s); |
576ec803 | 1021 | ds_put_char(s, ','); |
81a76618 BP |
1022 | } |
1023 | if (wc->masks.vlan_tci) { | |
1024 | ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK); | |
1025 | ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK); | |
1026 | ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI); | |
1027 | ||
1028 | if (cfi && f->vlan_tci & htons(VLAN_CFI) | |
1029 | && (!vid_mask || vid_mask == htons(VLAN_VID_MASK)) | |
1030 | && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK)) | |
1031 | && (vid_mask || pcp_mask)) { | |
1032 | if (vid_mask) { | |
1033 | ds_put_format(s, "dl_vlan=%"PRIu16",", | |
1034 | vlan_tci_to_vid(f->vlan_tci)); | |
1035 | } | |
1036 | if (pcp_mask) { | |
1037 | ds_put_format(s, "dl_vlan_pcp=%d,", | |
1038 | vlan_tci_to_pcp(f->vlan_tci)); | |
1039 | } | |
1040 | } else if (wc->masks.vlan_tci == htons(0xffff)) { | |
1041 | ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci)); | |
1042 | } else { | |
1043 | ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",", | |
1044 | ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci)); | |
1045 | } | |
1046 | } | |
1047 | format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src); | |
1048 | format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst); | |
1049 | if (!skip_type && wc->masks.dl_type) { | |
1050 | ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type)); | |
1051 | } | |
1052 | if (f->dl_type == htons(ETH_TYPE_IPV6)) { | |
1053 | format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src); | |
1054 | format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst); | |
1055 | if (wc->masks.ipv6_label) { | |
b8266395 | 1056 | if (wc->masks.ipv6_label == OVS_BE32_MAX) { |
81a76618 BP |
1057 | ds_put_format(s, "ipv6_label=0x%05"PRIx32",", |
1058 | ntohl(f->ipv6_label)); | |
1059 | } else { | |
1060 | ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",", | |
1061 | ntohl(f->ipv6_label), | |
1062 | ntohl(wc->masks.ipv6_label)); | |
1063 | } | |
1064 | } | |
8087f5ff MM |
1065 | } else if (f->dl_type == htons(ETH_TYPE_ARP) || |
1066 | f->dl_type == htons(ETH_TYPE_RARP)) { | |
666d0863 MM |
1067 | format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src); |
1068 | format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst); | |
81a76618 BP |
1069 | } else { |
1070 | format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src); | |
1071 | format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst); | |
1072 | } | |
1073 | if (!skip_proto && wc->masks.nw_proto) { | |
8087f5ff MM |
1074 | if (f->dl_type == htons(ETH_TYPE_ARP) || |
1075 | f->dl_type == htons(ETH_TYPE_RARP)) { | |
81a76618 BP |
1076 | ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto); |
1077 | } else { | |
1078 | ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto); | |
1079 | } | |
1080 | } | |
8087f5ff MM |
1081 | if (f->dl_type == htons(ETH_TYPE_ARP) || |
1082 | f->dl_type == htons(ETH_TYPE_RARP)) { | |
81a76618 BP |
1083 | format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha); |
1084 | format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha); | |
1085 | } | |
1086 | if (wc->masks.nw_tos & IP_DSCP_MASK) { | |
1087 | ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK); | |
1088 | } | |
1089 | if (wc->masks.nw_tos & IP_ECN_MASK) { | |
1090 | ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK); | |
1091 | } | |
1092 | if (wc->masks.nw_ttl) { | |
1093 | ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl); | |
1094 | } | |
8bfd0fda | 1095 | if (wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) { |
b02475c5 | 1096 | ds_put_format(s, "mpls_label=%"PRIu32",", |
78c9486d | 1097 | mpls_lse_to_label(f->mpls_lse[0])); |
b02475c5 | 1098 | } |
8bfd0fda | 1099 | if (wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) { |
b02475c5 | 1100 | ds_put_format(s, "mpls_tc=%"PRIu8",", |
78c9486d | 1101 | mpls_lse_to_tc(f->mpls_lse[0])); |
b02475c5 | 1102 | } |
8bfd0fda | 1103 | if (wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK)) { |
392c30ba | 1104 | ds_put_format(s, "mpls_ttl=%"PRIu8",", |
78c9486d | 1105 | mpls_lse_to_ttl(f->mpls_lse[0])); |
392c30ba | 1106 | } |
8bfd0fda | 1107 | if (wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) { |
b02475c5 | 1108 | ds_put_format(s, "mpls_bos=%"PRIu8",", |
78c9486d | 1109 | mpls_lse_to_bos(f->mpls_lse[0])); |
b02475c5 | 1110 | } |
8bfd0fda BP |
1111 | format_be32_masked(s, "mpls_lse1", f->mpls_lse[1], wc->masks.mpls_lse[1]); |
1112 | format_be32_masked(s, "mpls_lse2", f->mpls_lse[2], wc->masks.mpls_lse[2]); | |
1113 | ||
81a76618 BP |
1114 | switch (wc->masks.nw_frag) { |
1115 | case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER: | |
1116 | ds_put_format(s, "nw_frag=%s,", | |
1117 | f->nw_frag & FLOW_NW_FRAG_ANY | |
1118 | ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first") | |
1119 | : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no")); | |
1120 | break; | |
1121 | ||
1122 | case FLOW_NW_FRAG_ANY: | |
1123 | ds_put_format(s, "nw_frag=%s,", | |
1124 | f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no"); | |
1125 | break; | |
1126 | ||
1127 | case FLOW_NW_FRAG_LATER: | |
1128 | ds_put_format(s, "nw_frag=%s,", | |
1129 | f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later"); | |
1130 | break; | |
1131 | } | |
19564d20 MM |
1132 | if (f->dl_type == htons(ETH_TYPE_IP) && |
1133 | f->nw_proto == IPPROTO_ICMP) { | |
81a76618 BP |
1134 | format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src); |
1135 | format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst); | |
0e612675 FL |
1136 | } else if (f->dl_type == htons(ETH_TYPE_IP) && |
1137 | f->nw_proto == IPPROTO_IGMP) { | |
1138 | format_be16_masked(s, "igmp_type", f->tp_src, wc->masks.tp_src); | |
1139 | format_be16_masked(s, "igmp_code", f->tp_dst, wc->masks.tp_dst); | |
19564d20 MM |
1140 | } else if (f->dl_type == htons(ETH_TYPE_IPV6) && |
1141 | f->nw_proto == IPPROTO_ICMPV6) { | |
81a76618 BP |
1142 | format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src); |
1143 | format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst); | |
1144 | format_ipv6_netmask(s, "nd_target", &f->nd_target, | |
1145 | &wc->masks.nd_target); | |
1146 | format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha); | |
1147 | format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha); | |
1f6d8197 | 1148 | } else { |
81a76618 BP |
1149 | format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src); |
1150 | format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst); | |
1151 | } | |
dc235f7f | 1152 | if (is_ip_any(f) && f->nw_proto == IPPROTO_TCP && wc->masks.tcp_flags) { |
61bf6666 | 1153 | uint16_t mask = TCP_FLAGS(wc->masks.tcp_flags); |
ea2735d3 | 1154 | |
61bf6666 | 1155 | if (mask == TCP_FLAGS(OVS_BE16_MAX)) { |
ea2735d3 JR |
1156 | ds_put_cstr(s, "tcp_flags="); |
1157 | if (f->tcp_flags) { | |
1158 | format_flags(s, packet_tcp_flag_to_string, ntohs(f->tcp_flags), | |
1159 | '|'); | |
1160 | } else { | |
1161 | ds_put_cstr(s, "0"); /* Zero flags. */ | |
1162 | } | |
1163 | } else if (mask) { | |
61bf6666 JR |
1164 | format_flags_masked(s, "tcp_flags", packet_tcp_flag_to_string, |
1165 | ntohs(f->tcp_flags), mask); | |
dc235f7f JR |
1166 | } |
1167 | } | |
81a76618 | 1168 | |
41f562be SH |
1169 | if (s->length > start_len) { |
1170 | ds_chomp(s, ','); | |
81a76618 BP |
1171 | } |
1172 | } | |
1173 | ||
1174 | /* Converts 'match' to a string and returns the string. If 'priority' is | |
1175 | * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller | |
1176 | * must free the string (with free()). */ | |
1177 | char * | |
eb391b76 | 1178 | match_to_string(const struct match *match, int priority) |
81a76618 BP |
1179 | { |
1180 | struct ds s = DS_EMPTY_INITIALIZER; | |
1181 | match_format(match, &s, priority); | |
1182 | return ds_steal_cstr(&s); | |
1183 | } | |
1184 | ||
1185 | void | |
1186 | match_print(const struct match *match) | |
1187 | { | |
1188 | char *s = match_to_string(match, OFP_DEFAULT_PRIORITY); | |
1189 | puts(s); | |
1190 | free(s); | |
1191 | } | |
5cb7a798 BP |
1192 | \f |
1193 | /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst' | |
1194 | * with minimatch_destroy(). */ | |
1195 | void | |
1196 | minimatch_init(struct minimatch *dst, const struct match *src) | |
1197 | { | |
8fd47924 JR |
1198 | dst->mask = minimask_create(&src->wc); |
1199 | dst->flow = miniflow_create_with_minimask(&src->flow, dst->mask); | |
5cb7a798 BP |
1200 | } |
1201 | ||
1202 | /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst' | |
1203 | * with minimatch_destroy(). */ | |
1204 | void | |
1205 | minimatch_clone(struct minimatch *dst, const struct minimatch *src) | |
1206 | { | |
8fd47924 JR |
1207 | dst->flow = miniflow_clone(src->flow); |
1208 | dst->mask = minimask_clone(src->mask); | |
5cb7a798 BP |
1209 | } |
1210 | ||
b2c1f00b BP |
1211 | /* Initializes 'dst' with the data in 'src', destroying 'src'. The caller must |
1212 | * eventually free 'dst' with minimatch_destroy(). */ | |
1213 | void | |
1214 | minimatch_move(struct minimatch *dst, struct minimatch *src) | |
1215 | { | |
8fd47924 JR |
1216 | dst->flow = src->flow; |
1217 | dst->mask = src->mask; | |
b2c1f00b BP |
1218 | } |
1219 | ||
5cb7a798 BP |
1220 | /* Frees any memory owned by 'match'. Does not free the storage in which |
1221 | * 'match' itself resides; the caller is responsible for that. */ | |
1222 | void | |
1223 | minimatch_destroy(struct minimatch *match) | |
1224 | { | |
8fd47924 JR |
1225 | free(match->flow); |
1226 | free(match->mask); | |
5cb7a798 BP |
1227 | } |
1228 | ||
1229 | /* Initializes 'dst' as a copy of 'src'. */ | |
1230 | void | |
1231 | minimatch_expand(const struct minimatch *src, struct match *dst) | |
1232 | { | |
8fd47924 JR |
1233 | miniflow_expand(src->flow, &dst->flow); |
1234 | minimask_expand(src->mask, &dst->wc); | |
9558d2a5 | 1235 | memset(&dst->tun_md, 0, sizeof dst->tun_md); |
5cb7a798 BP |
1236 | } |
1237 | ||
1238 | /* Returns true if 'a' and 'b' match the same packets, false otherwise. */ | |
1239 | bool | |
1240 | minimatch_equal(const struct minimatch *a, const struct minimatch *b) | |
1241 | { | |
8fd47924 JR |
1242 | return minimask_equal(a->mask, b->mask) |
1243 | && miniflow_equal(a->flow, b->flow); | |
5cb7a798 BP |
1244 | } |
1245 | ||
df40c152 BP |
1246 | /* Returns true if 'target' satisifies 'match', that is, if each bit for which |
1247 | * 'match' specifies a particular value has the correct value in 'target'. | |
1248 | * | |
1249 | * This function is equivalent to miniflow_equal_flow_in_minimask(&match->flow, | |
1250 | * target, &match->mask) but it is faster because of the invariant that | |
1251 | * match->flow.map and match->mask.map are the same. */ | |
1252 | bool | |
1253 | minimatch_matches_flow(const struct minimatch *match, | |
1254 | const struct flow *target) | |
1255 | { | |
d70e8c28 | 1256 | const uint64_t *target_u64 = (const uint64_t *) target; |
8fd47924 JR |
1257 | const uint64_t *flowp = match->flow->values; |
1258 | const uint64_t *maskp = match->mask->masks.values; | |
1cea007c | 1259 | int idx; |
df40c152 | 1260 | |
8fd47924 | 1261 | MAP_FOR_EACH_INDEX(idx, match->flow->map) { |
d70e8c28 | 1262 | if ((*flowp++ ^ target_u64[idx]) & *maskp++) { |
080e28d0 | 1263 | return false; |
df40c152 | 1264 | } |
df40c152 BP |
1265 | } |
1266 | ||
1267 | return true; | |
1268 | } | |
1269 | ||
5cb7a798 BP |
1270 | /* Appends a string representation of 'match' to 's'. If 'priority' is |
1271 | * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */ | |
1272 | void | |
eb391b76 | 1273 | minimatch_format(const struct minimatch *match, struct ds *s, int priority) |
5cb7a798 BP |
1274 | { |
1275 | struct match megamatch; | |
1276 | ||
1277 | minimatch_expand(match, &megamatch); | |
1278 | match_format(&megamatch, s, priority); | |
1279 | } | |
1280 | ||
1281 | /* Converts 'match' to a string and returns the string. If 'priority' is | |
1282 | * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller | |
1283 | * must free the string (with free()). */ | |
1284 | char * | |
eb391b76 | 1285 | minimatch_to_string(const struct minimatch *match, int priority) |
5cb7a798 BP |
1286 | { |
1287 | struct match megamatch; | |
1288 | ||
1289 | minimatch_expand(match, &megamatch); | |
1290 | return match_to_string(&megamatch, priority); | |
1291 | } |