]>
Commit | Line | Data |
---|---|---|
81a76618 BP |
1 | /* |
2 | * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc. | |
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" | |
19 | #include <assert.h> | |
20 | #include <stdlib.h> | |
21 | #include "byte-order.h" | |
22 | #include "dynamic-string.h" | |
23 | #include "packets.h" | |
24 | ||
25 | /* Converts the flow in 'flow' into a match in 'match', with the given | |
26 | * 'wildcards'. */ | |
27 | void | |
28 | match_init(struct match *match, | |
29 | const struct flow *flow, const struct flow_wildcards *wc) | |
30 | { | |
31 | match->flow = *flow; | |
32 | match->wc = *wc; | |
33 | match_zero_wildcarded_fields(match); | |
34 | } | |
35 | ||
36 | /* Converts the flow in 'flow' into an exact-match match in 'match'. */ | |
37 | void | |
38 | match_init_exact(struct match *match, const struct flow *flow) | |
39 | { | |
40 | match->flow = *flow; | |
41 | match->flow.skb_priority = 0; | |
42 | flow_wildcards_init_exact(&match->wc); | |
43 | } | |
44 | ||
45 | /* Initializes 'match' as a "catch-all" match that matches every packet. */ | |
46 | void | |
47 | match_init_catchall(struct match *match) | |
48 | { | |
49 | memset(&match->flow, 0, sizeof match->flow); | |
50 | flow_wildcards_init_catchall(&match->wc); | |
51 | } | |
52 | ||
53 | /* For each bit or field wildcarded in 'match', sets the corresponding bit or | |
54 | * field in 'flow' to all-0-bits. It is important to maintain this invariant | |
55 | * in a match that might be inserted into a classifier. | |
56 | * | |
57 | * It is never necessary to call this function directly for a match that is | |
58 | * initialized or modified only by match_*() functions. It is useful to | |
59 | * restore the invariant in a match whose 'wc' member is modified by hand. | |
60 | */ | |
61 | void | |
62 | match_zero_wildcarded_fields(struct match *match) | |
63 | { | |
64 | flow_zero_wildcards(&match->flow, &match->wc); | |
65 | } | |
66 | ||
67 | void | |
68 | match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value) | |
69 | { | |
70 | match_set_reg_masked(match, reg_idx, value, UINT32_MAX); | |
71 | } | |
72 | ||
73 | void | |
74 | match_set_reg_masked(struct match *match, unsigned int reg_idx, | |
75 | uint32_t value, uint32_t mask) | |
76 | { | |
77 | assert(reg_idx < FLOW_N_REGS); | |
78 | flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask); | |
79 | match->flow.regs[reg_idx] = value & mask; | |
80 | } | |
81 | ||
82 | void | |
83 | match_set_metadata(struct match *match, ovs_be64 metadata) | |
84 | { | |
85 | match_set_metadata_masked(match, metadata, htonll(UINT64_MAX)); | |
86 | } | |
87 | ||
88 | void | |
89 | match_set_metadata_masked(struct match *match, | |
90 | ovs_be64 metadata, ovs_be64 mask) | |
91 | { | |
92 | match->wc.masks.metadata = mask; | |
93 | match->flow.metadata = metadata & mask; | |
94 | } | |
95 | ||
96 | void | |
97 | match_set_tun_id(struct match *match, ovs_be64 tun_id) | |
98 | { | |
99 | match_set_tun_id_masked(match, tun_id, htonll(UINT64_MAX)); | |
100 | } | |
101 | ||
102 | void | |
103 | match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask) | |
104 | { | |
105 | match->wc.masks.tun_id = mask; | |
106 | match->flow.tun_id = tun_id & mask; | |
107 | } | |
108 | ||
109 | void | |
110 | match_set_in_port(struct match *match, uint16_t ofp_port) | |
111 | { | |
112 | match->wc.masks.in_port = UINT16_MAX; | |
113 | match->flow.in_port = ofp_port; | |
114 | } | |
115 | ||
116 | void | |
117 | match_set_dl_type(struct match *match, ovs_be16 dl_type) | |
118 | { | |
119 | match->wc.masks.dl_type = htons(UINT16_MAX); | |
120 | match->flow.dl_type = dl_type; | |
121 | } | |
122 | ||
123 | /* Modifies 'value_src' so that the Ethernet address must match 'value_dst' | |
124 | * exactly. 'mask_dst' is set to all 1s. */ | |
125 | static void | |
126 | set_eth(const uint8_t value_src[ETH_ADDR_LEN], | |
127 | uint8_t value_dst[ETH_ADDR_LEN], | |
128 | uint8_t mask_dst[ETH_ADDR_LEN]) | |
129 | { | |
130 | memcpy(value_dst, value_src, ETH_ADDR_LEN); | |
131 | memset(mask_dst, 0xff, ETH_ADDR_LEN); | |
132 | } | |
133 | ||
134 | /* Modifies 'value_src' so that the Ethernet address must match 'value_src' | |
135 | * after each byte is ANDed with the appropriate byte in 'mask_src'. | |
136 | * 'mask_dst' is set to 'mask_src' */ | |
137 | static void | |
138 | set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN], | |
139 | const uint8_t mask_src[ETH_ADDR_LEN], | |
140 | uint8_t value_dst[ETH_ADDR_LEN], | |
141 | uint8_t mask_dst[ETH_ADDR_LEN]) | |
142 | { | |
143 | size_t i; | |
144 | ||
145 | for (i = 0; i < ETH_ADDR_LEN; i++) { | |
146 | value_dst[i] = value_src[i] & mask_src[i]; | |
147 | mask_dst[i] = mask_src[i]; | |
148 | } | |
149 | } | |
150 | ||
151 | /* Modifies 'rule' so that the source Ethernet address must match 'dl_src' | |
152 | * exactly. */ | |
153 | void | |
154 | match_set_dl_src(struct match *match, const uint8_t dl_src[ETH_ADDR_LEN]) | |
155 | { | |
156 | set_eth(dl_src, match->flow.dl_src, match->wc.masks.dl_src); | |
157 | } | |
158 | ||
159 | /* Modifies 'rule' so that the source Ethernet address must match 'dl_src' | |
160 | * after each byte is ANDed with the appropriate byte in 'mask'. */ | |
161 | void | |
162 | match_set_dl_src_masked(struct match *match, | |
163 | const uint8_t dl_src[ETH_ADDR_LEN], | |
164 | const uint8_t mask[ETH_ADDR_LEN]) | |
165 | { | |
166 | set_eth_masked(dl_src, mask, match->flow.dl_src, match->wc.masks.dl_src); | |
167 | } | |
168 | ||
169 | /* Modifies 'match' so that the Ethernet address must match 'dl_dst' | |
170 | * exactly. */ | |
171 | void | |
172 | match_set_dl_dst(struct match *match, const uint8_t dl_dst[ETH_ADDR_LEN]) | |
173 | { | |
174 | set_eth(dl_dst, match->flow.dl_dst, match->wc.masks.dl_dst); | |
175 | } | |
176 | ||
177 | /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each | |
178 | * byte is ANDed with the appropriate byte in 'mask'. | |
179 | * | |
180 | * This function will assert-fail if 'mask' is invalid. Only 'mask' values | |
181 | * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */ | |
182 | void | |
183 | match_set_dl_dst_masked(struct match *match, | |
184 | const uint8_t dl_dst[ETH_ADDR_LEN], | |
185 | const uint8_t mask[ETH_ADDR_LEN]) | |
186 | { | |
187 | set_eth_masked(dl_dst, mask, match->flow.dl_dst, match->wc.masks.dl_dst); | |
188 | } | |
189 | ||
190 | void | |
191 | match_set_dl_tci(struct match *match, ovs_be16 tci) | |
192 | { | |
193 | match_set_dl_tci_masked(match, tci, htons(0xffff)); | |
194 | } | |
195 | ||
196 | void | |
197 | match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask) | |
198 | { | |
199 | match->flow.vlan_tci = tci & mask; | |
200 | match->wc.masks.vlan_tci = mask; | |
201 | } | |
202 | ||
203 | /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already | |
204 | * wildcarded, then 'match' will match a packet regardless of whether it has an | |
205 | * 802.1Q header or not. */ | |
206 | void | |
207 | match_set_any_vid(struct match *match) | |
208 | { | |
209 | if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) { | |
210 | match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK); | |
211 | match->flow.vlan_tci &= ~htons(VLAN_VID_MASK); | |
212 | } else { | |
213 | match_set_dl_tci_masked(match, htons(0), htons(0)); | |
214 | } | |
215 | } | |
216 | ||
217 | /* Modifies 'match' depending on 'dl_vlan': | |
218 | * | |
219 | * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets | |
220 | * without an 802.1Q header. | |
221 | * | |
222 | * - Otherwise, makes 'match' match only packets with an 802.1Q header whose | |
223 | * VID equals the low 12 bits of 'dl_vlan'. | |
224 | */ | |
225 | void | |
226 | match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan) | |
227 | { | |
228 | flow_set_dl_vlan(&match->flow, dl_vlan); | |
229 | if (dl_vlan == htons(OFP10_VLAN_NONE)) { | |
230 | match->wc.masks.vlan_tci = htons(UINT16_MAX); | |
231 | } else { | |
232 | match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI); | |
233 | } | |
234 | } | |
235 | ||
236 | /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an | |
237 | * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID | |
238 | * plus CFI). */ | |
239 | void | |
240 | match_set_vlan_vid(struct match *match, ovs_be16 vid) | |
241 | { | |
242 | match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI)); | |
243 | } | |
244 | ||
245 | ||
246 | /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an | |
247 | * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID | |
248 | * plus CFI), with the corresponding 'mask'. */ | |
249 | void | |
250 | match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask) | |
251 | { | |
252 | ovs_be16 pcp_mask = htons(VLAN_PCP_MASK); | |
253 | ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI); | |
254 | ||
255 | mask &= vid_mask; | |
256 | flow_set_vlan_vid(&match->flow, vid & mask); | |
257 | match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask); | |
258 | } | |
259 | ||
260 | /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already | |
261 | * wildcarded, then 'match' will match a packet regardless of whether it has an | |
262 | * 802.1Q header or not. */ | |
263 | void | |
264 | match_set_any_pcp(struct match *match) | |
265 | { | |
266 | if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) { | |
267 | match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK); | |
268 | match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK); | |
269 | } else { | |
270 | match_set_dl_tci_masked(match, htons(0), htons(0)); | |
271 | } | |
272 | } | |
273 | ||
274 | /* Modifies 'match' so that it matches only packets with an 802.1Q header whose | |
275 | * PCP equals the low 3 bits of 'dl_vlan_pcp'. */ | |
276 | void | |
277 | match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp) | |
278 | { | |
279 | flow_set_vlan_pcp(&match->flow, dl_vlan_pcp); | |
280 | match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK); | |
281 | } | |
282 | ||
283 | void | |
284 | match_set_tp_src(struct match *match, ovs_be16 tp_src) | |
285 | { | |
286 | match_set_tp_src_masked(match, tp_src, htons(UINT16_MAX)); | |
287 | } | |
288 | ||
289 | void | |
290 | match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask) | |
291 | { | |
292 | match->flow.tp_src = port & mask; | |
293 | match->wc.masks.tp_src = mask; | |
294 | } | |
295 | ||
296 | void | |
297 | match_set_tp_dst(struct match *match, ovs_be16 tp_dst) | |
298 | { | |
299 | match_set_tp_dst_masked(match, tp_dst, htons(UINT16_MAX)); | |
300 | } | |
301 | ||
302 | void | |
303 | match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask) | |
304 | { | |
305 | match->flow.tp_dst = port & mask; | |
306 | match->wc.masks.tp_dst = mask; | |
307 | } | |
308 | ||
309 | void | |
310 | match_set_nw_proto(struct match *match, uint8_t nw_proto) | |
311 | { | |
312 | match->flow.nw_proto = nw_proto; | |
313 | match->wc.masks.nw_proto = UINT8_MAX; | |
314 | } | |
315 | ||
316 | void | |
317 | match_set_nw_src(struct match *match, ovs_be32 nw_src) | |
318 | { | |
319 | match->flow.nw_src = nw_src; | |
320 | match->wc.masks.nw_src = htonl(UINT32_MAX); | |
321 | } | |
322 | ||
323 | void | |
324 | match_set_nw_src_masked(struct match *match, | |
325 | ovs_be32 nw_src, ovs_be32 mask) | |
326 | { | |
327 | match->flow.nw_src = nw_src & mask; | |
328 | match->wc.masks.nw_src = mask; | |
329 | } | |
330 | ||
331 | void | |
332 | match_set_nw_dst(struct match *match, ovs_be32 nw_dst) | |
333 | { | |
334 | match->flow.nw_dst = nw_dst; | |
335 | match->wc.masks.nw_dst = htonl(UINT32_MAX); | |
336 | } | |
337 | ||
338 | void | |
339 | match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask) | |
340 | { | |
341 | match->flow.nw_dst = ip & mask; | |
342 | match->wc.masks.nw_dst = mask; | |
343 | } | |
344 | ||
345 | void | |
346 | match_set_nw_dscp(struct match *match, uint8_t nw_dscp) | |
347 | { | |
348 | match->wc.masks.nw_tos |= IP_DSCP_MASK; | |
349 | match->flow.nw_tos &= ~IP_DSCP_MASK; | |
350 | match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK; | |
351 | } | |
352 | ||
353 | void | |
354 | match_set_nw_ecn(struct match *match, uint8_t nw_ecn) | |
355 | { | |
356 | match->wc.masks.nw_tos |= IP_ECN_MASK; | |
357 | match->flow.nw_tos &= ~IP_ECN_MASK; | |
358 | match->flow.nw_tos |= nw_ecn & IP_ECN_MASK; | |
359 | } | |
360 | ||
361 | void | |
362 | match_set_nw_ttl(struct match *match, uint8_t nw_ttl) | |
363 | { | |
364 | match->wc.masks.nw_ttl = UINT8_MAX; | |
365 | match->flow.nw_ttl = nw_ttl; | |
366 | } | |
367 | ||
368 | void | |
369 | match_set_nw_frag(struct match *match, uint8_t nw_frag) | |
370 | { | |
371 | match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK; | |
372 | match->flow.nw_frag = nw_frag; | |
373 | } | |
374 | ||
375 | void | |
376 | match_set_nw_frag_masked(struct match *match, | |
377 | uint8_t nw_frag, uint8_t mask) | |
378 | { | |
379 | match->flow.nw_frag = nw_frag & mask; | |
380 | match->wc.masks.nw_frag = mask; | |
381 | } | |
382 | ||
383 | void | |
384 | match_set_icmp_type(struct match *match, uint8_t icmp_type) | |
385 | { | |
386 | match_set_tp_src(match, htons(icmp_type)); | |
387 | } | |
388 | ||
389 | void | |
390 | match_set_icmp_code(struct match *match, uint8_t icmp_code) | |
391 | { | |
392 | match_set_tp_dst(match, htons(icmp_code)); | |
393 | } | |
394 | ||
395 | void | |
396 | match_set_arp_sha(struct match *match, const uint8_t sha[ETH_ADDR_LEN]) | |
397 | { | |
398 | memcpy(match->flow.arp_sha, sha, ETH_ADDR_LEN); | |
399 | memset(match->wc.masks.arp_sha, UINT8_MAX, ETH_ADDR_LEN); | |
400 | } | |
401 | ||
402 | void | |
403 | match_set_arp_sha_masked(struct match *match, | |
404 | const uint8_t arp_sha[ETH_ADDR_LEN], | |
405 | const uint8_t mask[ETH_ADDR_LEN]) | |
406 | { | |
407 | set_eth_masked(arp_sha, mask, | |
408 | match->flow.arp_sha, match->wc.masks.arp_sha); | |
409 | } | |
410 | ||
411 | void | |
412 | match_set_arp_tha(struct match *match, const uint8_t tha[ETH_ADDR_LEN]) | |
413 | { | |
414 | memcpy(match->flow.arp_tha, tha, ETH_ADDR_LEN); | |
415 | memset(match->wc.masks.arp_tha, UINT8_MAX, ETH_ADDR_LEN); | |
416 | } | |
417 | ||
418 | void | |
419 | match_set_arp_tha_masked(struct match *match, | |
420 | const uint8_t arp_tha[ETH_ADDR_LEN], | |
421 | const uint8_t mask[ETH_ADDR_LEN]) | |
422 | { | |
423 | set_eth_masked(arp_tha, mask, | |
424 | match->flow.arp_tha, match->wc.masks.arp_tha); | |
425 | } | |
426 | ||
427 | void | |
428 | match_set_ipv6_src(struct match *match, const struct in6_addr *src) | |
429 | { | |
430 | match->flow.ipv6_src = *src; | |
431 | match->wc.masks.ipv6_src = in6addr_exact; | |
432 | } | |
433 | ||
434 | void | |
435 | match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src, | |
436 | const struct in6_addr *mask) | |
437 | { | |
438 | match->flow.ipv6_src = ipv6_addr_bitand(src, mask); | |
439 | match->wc.masks.ipv6_src = *mask; | |
440 | } | |
441 | ||
442 | void | |
443 | match_set_ipv6_dst(struct match *match, const struct in6_addr *dst) | |
444 | { | |
445 | match->flow.ipv6_dst = *dst; | |
446 | match->wc.masks.ipv6_dst = in6addr_exact; | |
447 | } | |
448 | ||
449 | void | |
450 | match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst, | |
451 | const struct in6_addr *mask) | |
452 | { | |
453 | match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask); | |
454 | match->wc.masks.ipv6_dst = *mask; | |
455 | } | |
456 | ||
457 | void | |
458 | match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label) | |
459 | { | |
460 | match->wc.masks.ipv6_label = htonl(UINT32_MAX); | |
461 | match->flow.ipv6_label = ipv6_label; | |
462 | } | |
463 | ||
464 | ||
465 | void | |
466 | match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label, | |
467 | ovs_be32 mask) | |
468 | { | |
469 | match->flow.ipv6_label = ipv6_label & mask; | |
470 | match->wc.masks.ipv6_label = mask; | |
471 | } | |
472 | ||
473 | void | |
474 | match_set_nd_target(struct match *match, const struct in6_addr *target) | |
475 | { | |
476 | match->flow.nd_target = *target; | |
477 | match->wc.masks.nd_target = in6addr_exact; | |
478 | } | |
479 | ||
480 | void | |
481 | match_set_nd_target_masked(struct match *match, | |
482 | const struct in6_addr *target, | |
483 | const struct in6_addr *mask) | |
484 | { | |
485 | match->flow.nd_target = ipv6_addr_bitand(target, mask); | |
486 | match->wc.masks.nd_target = *mask; | |
487 | } | |
488 | ||
489 | /* Returns true if 'a' and 'b' wildcard the same fields and have the same | |
490 | * values for fixed fields, otherwise false. */ | |
491 | bool | |
492 | match_equal(const struct match *a, const struct match *b) | |
493 | { | |
494 | return (flow_wildcards_equal(&a->wc, &b->wc) | |
495 | && flow_equal(&a->flow, &b->flow)); | |
496 | } | |
497 | ||
498 | /* Returns a hash value for the flow and wildcards in 'match', starting from | |
499 | * 'basis'. */ | |
500 | uint32_t | |
501 | match_hash(const struct match *match, uint32_t basis) | |
502 | { | |
503 | return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis)); | |
504 | } | |
505 | ||
506 | static void | |
507 | format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6], | |
508 | const uint8_t mask[6]) | |
509 | { | |
510 | if (!eth_addr_is_zero(mask)) { | |
511 | ds_put_format(s, "%s=", name); | |
512 | eth_format_masked(eth, mask, s); | |
513 | ds_put_char(s, ','); | |
514 | } | |
515 | } | |
516 | ||
517 | static void | |
518 | format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip, | |
519 | ovs_be32 netmask) | |
520 | { | |
521 | if (netmask) { | |
522 | ds_put_format(s, "%s=", name); | |
523 | ip_format_masked(ip, netmask, s); | |
524 | ds_put_char(s, ','); | |
525 | } | |
526 | } | |
527 | ||
528 | static void | |
529 | format_ipv6_netmask(struct ds *s, const char *name, | |
530 | const struct in6_addr *addr, | |
531 | const struct in6_addr *netmask) | |
532 | { | |
533 | if (!ipv6_mask_is_any(netmask)) { | |
534 | ds_put_format(s, "%s=", name); | |
535 | print_ipv6_masked(s, addr, netmask); | |
536 | ds_put_char(s, ','); | |
537 | } | |
538 | } | |
539 | ||
540 | ||
541 | static void | |
542 | format_be16_masked(struct ds *s, const char *name, | |
543 | ovs_be16 value, ovs_be16 mask) | |
544 | { | |
545 | if (mask != htons(0)) { | |
546 | ds_put_format(s, "%s=", name); | |
547 | if (mask == htons(UINT16_MAX)) { | |
548 | ds_put_format(s, "%"PRIu16, ntohs(value)); | |
549 | } else { | |
550 | ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16, | |
551 | ntohs(value), ntohs(mask)); | |
552 | } | |
553 | ds_put_char(s, ','); | |
554 | } | |
555 | } | |
556 | ||
557 | /* Appends a string representation of 'match' to 's'. If 'priority' is | |
558 | * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */ | |
559 | void | |
560 | match_format(const struct match *match, struct ds *s, unsigned int priority) | |
561 | { | |
562 | const struct flow_wildcards *wc = &match->wc; | |
563 | size_t start_len = s->length; | |
564 | const struct flow *f = &match->flow; | |
565 | bool skip_type = false; | |
566 | bool skip_proto = false; | |
567 | ||
568 | int i; | |
569 | ||
570 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 17); | |
571 | ||
572 | if (priority != OFP_DEFAULT_PRIORITY) { | |
573 | ds_put_format(s, "priority=%u,", priority); | |
574 | } | |
575 | ||
576 | if (wc->masks.dl_type) { | |
577 | skip_type = true; | |
578 | if (f->dl_type == htons(ETH_TYPE_IP)) { | |
579 | if (wc->masks.nw_proto) { | |
580 | skip_proto = true; | |
581 | if (f->nw_proto == IPPROTO_ICMP) { | |
582 | ds_put_cstr(s, "icmp,"); | |
583 | } else if (f->nw_proto == IPPROTO_TCP) { | |
584 | ds_put_cstr(s, "tcp,"); | |
585 | } else if (f->nw_proto == IPPROTO_UDP) { | |
586 | ds_put_cstr(s, "udp,"); | |
587 | } else { | |
588 | ds_put_cstr(s, "ip,"); | |
589 | skip_proto = false; | |
590 | } | |
591 | } else { | |
592 | ds_put_cstr(s, "ip,"); | |
593 | } | |
594 | } else if (f->dl_type == htons(ETH_TYPE_IPV6)) { | |
595 | if (wc->masks.nw_proto) { | |
596 | skip_proto = true; | |
597 | if (f->nw_proto == IPPROTO_ICMPV6) { | |
598 | ds_put_cstr(s, "icmp6,"); | |
599 | } else if (f->nw_proto == IPPROTO_TCP) { | |
600 | ds_put_cstr(s, "tcp6,"); | |
601 | } else if (f->nw_proto == IPPROTO_UDP) { | |
602 | ds_put_cstr(s, "udp6,"); | |
603 | } else { | |
604 | ds_put_cstr(s, "ipv6,"); | |
605 | skip_proto = false; | |
606 | } | |
607 | } else { | |
608 | ds_put_cstr(s, "ipv6,"); | |
609 | } | |
610 | } else if (f->dl_type == htons(ETH_TYPE_ARP)) { | |
611 | ds_put_cstr(s, "arp,"); | |
612 | } else { | |
613 | skip_type = false; | |
614 | } | |
615 | } | |
616 | for (i = 0; i < FLOW_N_REGS; i++) { | |
617 | switch (wc->masks.regs[i]) { | |
618 | case 0: | |
619 | break; | |
620 | case UINT32_MAX: | |
621 | ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]); | |
622 | break; | |
623 | default: | |
624 | ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",", | |
625 | i, f->regs[i], wc->masks.regs[i]); | |
626 | break; | |
627 | } | |
628 | } | |
629 | switch (wc->masks.tun_id) { | |
630 | case 0: | |
631 | break; | |
632 | case CONSTANT_HTONLL(UINT64_MAX): | |
633 | ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(f->tun_id)); | |
634 | break; | |
635 | default: | |
636 | ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",", | |
637 | ntohll(f->tun_id), ntohll(wc->masks.tun_id)); | |
638 | break; | |
639 | } | |
640 | switch (wc->masks.metadata) { | |
641 | case 0: | |
642 | break; | |
643 | case CONSTANT_HTONLL(UINT64_MAX): | |
644 | ds_put_format(s, "metadata=%#"PRIx64",", ntohll(f->metadata)); | |
645 | break; | |
646 | default: | |
647 | ds_put_format(s, "metadata=%#"PRIx64"/%#"PRIx64",", | |
648 | ntohll(f->metadata), ntohll(wc->masks.metadata)); | |
649 | break; | |
650 | } | |
651 | if (wc->masks.in_port) { | |
652 | ds_put_format(s, "in_port=%"PRIu16",", f->in_port); | |
653 | } | |
654 | if (wc->masks.vlan_tci) { | |
655 | ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK); | |
656 | ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK); | |
657 | ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI); | |
658 | ||
659 | if (cfi && f->vlan_tci & htons(VLAN_CFI) | |
660 | && (!vid_mask || vid_mask == htons(VLAN_VID_MASK)) | |
661 | && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK)) | |
662 | && (vid_mask || pcp_mask)) { | |
663 | if (vid_mask) { | |
664 | ds_put_format(s, "dl_vlan=%"PRIu16",", | |
665 | vlan_tci_to_vid(f->vlan_tci)); | |
666 | } | |
667 | if (pcp_mask) { | |
668 | ds_put_format(s, "dl_vlan_pcp=%d,", | |
669 | vlan_tci_to_pcp(f->vlan_tci)); | |
670 | } | |
671 | } else if (wc->masks.vlan_tci == htons(0xffff)) { | |
672 | ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci)); | |
673 | } else { | |
674 | ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",", | |
675 | ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci)); | |
676 | } | |
677 | } | |
678 | format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src); | |
679 | format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst); | |
680 | if (!skip_type && wc->masks.dl_type) { | |
681 | ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type)); | |
682 | } | |
683 | if (f->dl_type == htons(ETH_TYPE_IPV6)) { | |
684 | format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src); | |
685 | format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst); | |
686 | if (wc->masks.ipv6_label) { | |
687 | if (wc->masks.ipv6_label == htonl(UINT32_MAX)) { | |
688 | ds_put_format(s, "ipv6_label=0x%05"PRIx32",", | |
689 | ntohl(f->ipv6_label)); | |
690 | } else { | |
691 | ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",", | |
692 | ntohl(f->ipv6_label), | |
693 | ntohl(wc->masks.ipv6_label)); | |
694 | } | |
695 | } | |
696 | } else { | |
697 | format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src); | |
698 | format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst); | |
699 | } | |
700 | if (!skip_proto && wc->masks.nw_proto) { | |
701 | if (f->dl_type == htons(ETH_TYPE_ARP)) { | |
702 | ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto); | |
703 | } else { | |
704 | ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto); | |
705 | } | |
706 | } | |
707 | if (f->dl_type == htons(ETH_TYPE_ARP)) { | |
708 | format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha); | |
709 | format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha); | |
710 | } | |
711 | if (wc->masks.nw_tos & IP_DSCP_MASK) { | |
712 | ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK); | |
713 | } | |
714 | if (wc->masks.nw_tos & IP_ECN_MASK) { | |
715 | ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK); | |
716 | } | |
717 | if (wc->masks.nw_ttl) { | |
718 | ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl); | |
719 | } | |
720 | switch (wc->masks.nw_frag) { | |
721 | case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER: | |
722 | ds_put_format(s, "nw_frag=%s,", | |
723 | f->nw_frag & FLOW_NW_FRAG_ANY | |
724 | ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first") | |
725 | : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no")); | |
726 | break; | |
727 | ||
728 | case FLOW_NW_FRAG_ANY: | |
729 | ds_put_format(s, "nw_frag=%s,", | |
730 | f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no"); | |
731 | break; | |
732 | ||
733 | case FLOW_NW_FRAG_LATER: | |
734 | ds_put_format(s, "nw_frag=%s,", | |
735 | f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later"); | |
736 | break; | |
737 | } | |
738 | if (f->nw_proto == IPPROTO_ICMP) { | |
739 | format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src); | |
740 | format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst); | |
741 | } else if (f->nw_proto == IPPROTO_ICMPV6) { | |
742 | format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src); | |
743 | format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst); | |
744 | format_ipv6_netmask(s, "nd_target", &f->nd_target, | |
745 | &wc->masks.nd_target); | |
746 | format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha); | |
747 | format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha); | |
748 | } else { | |
749 | format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src); | |
750 | format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst); | |
751 | } | |
752 | ||
753 | if (s->length > start_len && ds_last(s) == ',') { | |
754 | s->length--; | |
755 | } | |
756 | } | |
757 | ||
758 | /* Converts 'match' to a string and returns the string. If 'priority' is | |
759 | * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller | |
760 | * must free the string (with free()). */ | |
761 | char * | |
762 | match_to_string(const struct match *match, unsigned int priority) | |
763 | { | |
764 | struct ds s = DS_EMPTY_INITIALIZER; | |
765 | match_format(match, &s, priority); | |
766 | return ds_steal_cstr(&s); | |
767 | } | |
768 | ||
769 | void | |
770 | match_print(const struct match *match) | |
771 | { | |
772 | char *s = match_to_string(match, OFP_DEFAULT_PRIORITY); | |
773 | puts(s); | |
774 | free(s); | |
775 | } |