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