]>
Commit | Line | Data |
---|---|---|
6a885fd0 | 1 | /* |
e0edde6f | 2 | * Copyright (c) 2011, 2012 Nicira, Inc. |
6a885fd0 BP |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
18 | ||
19 | #include "meta-flow.h" | |
20 | ||
21 | #include <assert.h> | |
22 | #include <errno.h> | |
23 | #include <limits.h> | |
24 | #include <netinet/icmp6.h> | |
25 | #include <netinet/ip6.h> | |
26 | ||
27 | #include "classifier.h" | |
28 | #include "dynamic-string.h" | |
816fd533 | 29 | #include "ofp-errors.h" |
6a885fd0 BP |
30 | #include "ofp-util.h" |
31 | #include "packets.h" | |
32 | #include "random.h" | |
33 | #include "shash.h" | |
34 | #include "socket-util.h" | |
35 | #include "unaligned.h" | |
816fd533 BP |
36 | #include "vlog.h" |
37 | ||
38 | VLOG_DEFINE_THIS_MODULE(meta_flow); | |
6a885fd0 BP |
39 | |
40 | #define MF_FIELD_SIZES(MEMBER) \ | |
41 | sizeof ((union mf_value *)0)->MEMBER, \ | |
42 | 8 * sizeof ((union mf_value *)0)->MEMBER | |
43 | ||
44 | static const struct mf_field mf_fields[MFF_N_IDS] = { | |
45 | /* ## -------- ## */ | |
46 | /* ## metadata ## */ | |
47 | /* ## -------- ## */ | |
48 | ||
49 | { | |
50 | MFF_TUN_ID, "tun_id", NULL, | |
51 | MF_FIELD_SIZES(be64), | |
52 | MFM_FULLY, 0, | |
53 | MFS_HEXADECIMAL, | |
54 | MFP_NONE, | |
28da1f8f BP |
55 | true, |
56 | NXM_NX_TUN_ID, "NXM_NX_TUN_ID", | |
b5e5143b | 57 | NXM_NX_TUN_ID, "NXM_NX_TUN_ID", |
969fc56c JS |
58 | }, { |
59 | MFF_METADATA, "metadata", NULL, | |
60 | MF_FIELD_SIZES(be64), | |
61 | MFM_FULLY, 0, | |
62 | MFS_HEXADECIMAL, | |
63 | MFP_NONE, | |
64 | true, | |
65 | OXM_OF_METADATA, "OXM_OF_METADATA", | |
66 | OXM_OF_METADATA, "OXM_OF_METADATA", | |
6a885fd0 BP |
67 | }, { |
68 | MFF_IN_PORT, "in_port", NULL, | |
69 | MF_FIELD_SIZES(be16), | |
70 | MFM_NONE, FWW_IN_PORT, | |
71 | MFS_OFP_PORT, | |
72 | MFP_NONE, | |
28da1f8f BP |
73 | false, |
74 | NXM_OF_IN_PORT, "NXM_OF_IN_PORT", | |
36f3999b | 75 | OXM_OF_IN_PORT, "OXM_OF_IN_PORT", |
6a885fd0 BP |
76 | }, |
77 | ||
78 | #define REGISTER(IDX) \ | |
79 | { \ | |
80 | MFF_REG##IDX, "reg" #IDX, NULL, \ | |
81 | MF_FIELD_SIZES(be32), \ | |
82 | MFM_FULLY, 0, \ | |
83 | MFS_HEXADECIMAL, \ | |
84 | MFP_NONE, \ | |
28da1f8f | 85 | true, \ |
b5e5143b BP |
86 | NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \ |
87 | NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \ | |
6a885fd0 BP |
88 | } |
89 | #if FLOW_N_REGS > 0 | |
90 | REGISTER(0), | |
91 | #endif | |
92 | #if FLOW_N_REGS > 1 | |
93 | REGISTER(1), | |
94 | #endif | |
95 | #if FLOW_N_REGS > 2 | |
96 | REGISTER(2), | |
97 | #endif | |
98 | #if FLOW_N_REGS > 3 | |
99 | REGISTER(3), | |
100 | #endif | |
101 | #if FLOW_N_REGS > 4 | |
d2c0fed9 JP |
102 | REGISTER(4), |
103 | #endif | |
104 | #if FLOW_N_REGS > 5 | |
e9358af6 EJ |
105 | REGISTER(5), |
106 | #endif | |
107 | #if FLOW_N_REGS > 6 | |
108 | REGISTER(6), | |
109 | #endif | |
110 | #if FLOW_N_REGS > 7 | |
111 | REGISTER(7), | |
112 | #endif | |
113 | #if FLOW_N_REGS > 8 | |
6a885fd0 BP |
114 | #error |
115 | #endif | |
116 | ||
117 | /* ## -- ## */ | |
118 | /* ## L2 ## */ | |
119 | /* ## -- ## */ | |
120 | ||
121 | { | |
122 | MFF_ETH_SRC, "eth_src", "dl_src", | |
123 | MF_FIELD_SIZES(mac), | |
73c0ce34 | 124 | MFM_FULLY, 0, |
6a885fd0 BP |
125 | MFS_ETHERNET, |
126 | MFP_NONE, | |
28da1f8f BP |
127 | true, |
128 | NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC", | |
36f3999b | 129 | OXM_OF_ETH_SRC, "OXM_OF_ETH_SRC", |
6a885fd0 BP |
130 | }, { |
131 | MFF_ETH_DST, "eth_dst", "dl_dst", | |
132 | MF_FIELD_SIZES(mac), | |
73c0ce34 | 133 | MFM_FULLY, 0, |
6a885fd0 BP |
134 | MFS_ETHERNET, |
135 | MFP_NONE, | |
28da1f8f BP |
136 | true, |
137 | NXM_OF_ETH_DST, "NXM_OF_ETH_DST", | |
36f3999b | 138 | OXM_OF_ETH_DST, "OXM_OF_ETH_DST", |
6a885fd0 BP |
139 | }, { |
140 | MFF_ETH_TYPE, "eth_type", "dl_type", | |
141 | MF_FIELD_SIZES(be16), | |
142 | MFM_NONE, FWW_DL_TYPE, | |
143 | MFS_HEXADECIMAL, | |
144 | MFP_NONE, | |
28da1f8f BP |
145 | false, |
146 | NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE", | |
36f3999b | 147 | OXM_OF_ETH_TYPE, "OXM_OF_ETH_TYPE", |
6a885fd0 BP |
148 | }, |
149 | ||
150 | { | |
151 | MFF_VLAN_TCI, "vlan_tci", NULL, | |
152 | MF_FIELD_SIZES(be16), | |
153 | MFM_FULLY, 0, | |
154 | MFS_HEXADECIMAL, | |
155 | MFP_NONE, | |
28da1f8f BP |
156 | true, |
157 | NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI", | |
b5e5143b | 158 | NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI", |
6a885fd0 | 159 | }, { |
441c57a9 | 160 | MFF_DL_VLAN, "dl_vlan", NULL, |
6a885fd0 BP |
161 | sizeof(ovs_be16), 12, |
162 | MFM_NONE, 0, | |
163 | MFS_DECIMAL, | |
164 | MFP_NONE, | |
28da1f8f | 165 | true, |
b5e5143b | 166 | OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID", |
36f3999b | 167 | OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID", |
6a885fd0 | 168 | }, { |
441c57a9 | 169 | MFF_DL_VLAN_PCP, "dl_vlan_pcp", NULL, |
6a885fd0 BP |
170 | 1, 3, |
171 | MFM_NONE, 0, | |
172 | MFS_DECIMAL, | |
173 | MFP_NONE, | |
28da1f8f | 174 | true, |
b5e5143b | 175 | OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP", |
36f3999b | 176 | OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP", |
6a885fd0 BP |
177 | }, |
178 | ||
179 | /* ## -- ## */ | |
180 | /* ## L3 ## */ | |
181 | /* ## -- ## */ | |
182 | ||
183 | { | |
184 | MFF_IPV4_SRC, "ip_src", "nw_src", | |
185 | MF_FIELD_SIZES(be32), | |
c08201d6 | 186 | MFM_FULLY, 0, |
6a885fd0 BP |
187 | MFS_IPV4, |
188 | MFP_IPV4, | |
28da1f8f BP |
189 | true, |
190 | NXM_OF_IP_SRC, "NXM_OF_IP_SRC", | |
36f3999b | 191 | OXM_OF_IPV4_SRC, "OXM_OF_IPV4_SRC", |
6a885fd0 BP |
192 | }, { |
193 | MFF_IPV4_DST, "ip_dst", "nw_dst", | |
194 | MF_FIELD_SIZES(be32), | |
c08201d6 | 195 | MFM_FULLY, 0, |
6a885fd0 BP |
196 | MFS_IPV4, |
197 | MFP_IPV4, | |
28da1f8f BP |
198 | true, |
199 | NXM_OF_IP_DST, "NXM_OF_IP_DST", | |
36f3999b | 200 | OXM_OF_IPV4_DST, "OXM_OF_IPV4_DST", |
6a885fd0 BP |
201 | }, |
202 | ||
203 | { | |
204 | MFF_IPV6_SRC, "ipv6_src", NULL, | |
205 | MF_FIELD_SIZES(ipv6), | |
ff0b06ee | 206 | MFM_FULLY, 0, |
6a885fd0 BP |
207 | MFS_IPV6, |
208 | MFP_IPV6, | |
28da1f8f BP |
209 | true, |
210 | NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC", | |
36f3999b | 211 | OXM_OF_IPV6_SRC, "OXM_OF_IPV6_SRC", |
6a885fd0 BP |
212 | }, { |
213 | MFF_IPV6_DST, "ipv6_dst", NULL, | |
214 | MF_FIELD_SIZES(ipv6), | |
ff0b06ee | 215 | MFM_FULLY, 0, |
6a885fd0 BP |
216 | MFS_IPV6, |
217 | MFP_IPV6, | |
28da1f8f BP |
218 | true, |
219 | NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST", | |
36f3999b | 220 | OXM_OF_IPV6_DST, "OXM_OF_IPV6_DST", |
6a885fd0 | 221 | }, |
fa8223b7 JP |
222 | { |
223 | MFF_IPV6_LABEL, "ipv6_label", NULL, | |
224 | 4, 20, | |
32455024 | 225 | MFM_FULLY, 0, |
fa8223b7 JP |
226 | MFS_HEXADECIMAL, |
227 | MFP_IPV6, | |
28da1f8f BP |
228 | false, |
229 | NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL", | |
36f3999b | 230 | OXM_OF_IPV6_FLABEL, "OXM_OF_IPV6_FLABEL", |
fa8223b7 | 231 | }, |
6a885fd0 BP |
232 | |
233 | { | |
234 | MFF_IP_PROTO, "nw_proto", NULL, | |
235 | MF_FIELD_SIZES(u8), | |
236 | MFM_NONE, FWW_NW_PROTO, | |
237 | MFS_DECIMAL, | |
238 | MFP_IP_ANY, | |
28da1f8f BP |
239 | false, |
240 | NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO", | |
36f3999b | 241 | OXM_OF_IP_PROTO, "OXM_OF_IP_PROTO", |
6a885fd0 | 242 | }, { |
530180fd | 243 | MFF_IP_DSCP, "nw_tos", NULL, |
6a885fd0 | 244 | MF_FIELD_SIZES(u8), |
2486e66a | 245 | MFM_NONE, FWW_NW_DSCP, |
6a885fd0 BP |
246 | MFS_DECIMAL, |
247 | MFP_IP_ANY, | |
28da1f8f | 248 | true, |
36f3999b SH |
249 | NXM_OF_IP_TOS, "NXM_OF_IP_TOS", |
250 | OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP", | |
530180fd JP |
251 | }, { |
252 | MFF_IP_ECN, "nw_ecn", NULL, | |
253 | 1, 2, | |
2486e66a | 254 | MFM_NONE, FWW_NW_ECN, |
530180fd JP |
255 | MFS_DECIMAL, |
256 | MFP_IP_ANY, | |
28da1f8f BP |
257 | true, |
258 | NXM_NX_IP_ECN, "NXM_NX_IP_ECN", | |
36f3999b | 259 | OXM_OF_IP_ECN, "OXM_OF_IP_ECN", |
a61680c6 JP |
260 | }, { |
261 | MFF_IP_TTL, "nw_ttl", NULL, | |
262 | MF_FIELD_SIZES(u8), | |
263 | MFM_NONE, FWW_NW_TTL, | |
264 | MFS_DECIMAL, | |
265 | MFP_IP_ANY, | |
28da1f8f | 266 | true, |
36f3999b | 267 | NXM_NX_IP_TTL, "NXM_NX_IP_TTL", |
b5e5143b | 268 | NXM_NX_IP_TTL, "NXM_NX_IP_TTL", |
7257b535 BP |
269 | }, { |
270 | MFF_IP_FRAG, "ip_frag", NULL, | |
271 | 1, 2, | |
272 | MFM_FULLY, 0, | |
273 | MFS_FRAG, | |
274 | MFP_IP_ANY, | |
28da1f8f | 275 | false, |
36f3999b | 276 | NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG", |
b5e5143b | 277 | NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG", |
6a885fd0 BP |
278 | }, |
279 | ||
280 | { | |
281 | MFF_ARP_OP, "arp_op", NULL, | |
282 | MF_FIELD_SIZES(be16), | |
283 | MFM_NONE, FWW_NW_PROTO, | |
284 | MFS_DECIMAL, | |
285 | MFP_ARP, | |
28da1f8f BP |
286 | false, |
287 | NXM_OF_ARP_OP, "NXM_OF_ARP_OP", | |
36f3999b | 288 | OXM_OF_ARP_OP, "OXM_OF_ARP_OP", |
6a885fd0 BP |
289 | }, { |
290 | MFF_ARP_SPA, "arp_spa", NULL, | |
291 | MF_FIELD_SIZES(be32), | |
c08201d6 | 292 | MFM_FULLY, 0, |
6a885fd0 BP |
293 | MFS_IPV4, |
294 | MFP_ARP, | |
28da1f8f BP |
295 | false, |
296 | NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA", | |
36f3999b | 297 | OXM_OF_ARP_SPA, "OXM_OF_ARP_SPA", |
6a885fd0 BP |
298 | }, { |
299 | MFF_ARP_TPA, "arp_tpa", NULL, | |
300 | MF_FIELD_SIZES(be32), | |
c08201d6 | 301 | MFM_FULLY, 0, |
6a885fd0 BP |
302 | MFS_IPV4, |
303 | MFP_ARP, | |
28da1f8f BP |
304 | false, |
305 | NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA", | |
36f3999b | 306 | OXM_OF_ARP_TPA, "OXM_OF_ARP_TPA", |
6a885fd0 BP |
307 | }, { |
308 | MFF_ARP_SHA, "arp_sha", NULL, | |
309 | MF_FIELD_SIZES(mac), | |
e878338b | 310 | MFM_FULLY, 0, |
6a885fd0 BP |
311 | MFS_ETHERNET, |
312 | MFP_ARP, | |
28da1f8f BP |
313 | false, |
314 | NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA", | |
36f3999b | 315 | OXM_OF_ARP_SHA, "OXM_OF_ARP_SHA", |
6a885fd0 BP |
316 | }, { |
317 | MFF_ARP_THA, "arp_tha", NULL, | |
318 | MF_FIELD_SIZES(mac), | |
e878338b | 319 | MFM_FULLY, 0, |
6a885fd0 BP |
320 | MFS_ETHERNET, |
321 | MFP_ARP, | |
28da1f8f BP |
322 | false, |
323 | NXM_NX_ARP_THA, "NXM_NX_ARP_THA", | |
36f3999b | 324 | OXM_OF_ARP_THA, "OXM_OF_ARP_THA", |
6a885fd0 BP |
325 | }, |
326 | ||
327 | /* ## -- ## */ | |
328 | /* ## L4 ## */ | |
329 | /* ## -- ## */ | |
330 | ||
331 | { | |
332 | MFF_TCP_SRC, "tcp_src", "tp_src", | |
333 | MF_FIELD_SIZES(be16), | |
73f33563 | 334 | MFM_FULLY, 0, |
6a885fd0 BP |
335 | MFS_DECIMAL, |
336 | MFP_TCP, | |
28da1f8f BP |
337 | true, |
338 | NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC", | |
36f3999b | 339 | OXM_OF_TCP_SRC, "OXM_OF_TCP_SRC", |
6a885fd0 BP |
340 | }, { |
341 | MFF_TCP_DST, "tcp_dst", "tp_dst", | |
342 | MF_FIELD_SIZES(be16), | |
73f33563 | 343 | MFM_FULLY, 0, |
6a885fd0 BP |
344 | MFS_DECIMAL, |
345 | MFP_TCP, | |
28da1f8f BP |
346 | true, |
347 | NXM_OF_TCP_DST, "NXM_OF_TCP_DST", | |
36f3999b | 348 | OXM_OF_TCP_DST, "OXM_OF_TCP_DST", |
6a885fd0 BP |
349 | }, |
350 | ||
351 | { | |
352 | MFF_UDP_SRC, "udp_src", NULL, | |
353 | MF_FIELD_SIZES(be16), | |
73f33563 | 354 | MFM_FULLY, 0, |
6a885fd0 BP |
355 | MFS_DECIMAL, |
356 | MFP_UDP, | |
28da1f8f BP |
357 | true, |
358 | NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC", | |
36f3999b | 359 | OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC", |
6a885fd0 BP |
360 | }, { |
361 | MFF_UDP_DST, "udp_dst", NULL, | |
362 | MF_FIELD_SIZES(be16), | |
73f33563 | 363 | MFM_FULLY, 0, |
6a885fd0 BP |
364 | MFS_DECIMAL, |
365 | MFP_UDP, | |
28da1f8f BP |
366 | true, |
367 | NXM_OF_UDP_DST, "NXM_OF_UDP_DST", | |
36f3999b | 368 | OXM_OF_UDP_DST, "OXM_OF_UDP_DST", |
6a885fd0 BP |
369 | }, |
370 | ||
371 | { | |
268a95e0 | 372 | MFF_ICMPV4_TYPE, "icmp_type", NULL, |
6a885fd0 | 373 | MF_FIELD_SIZES(u8), |
73f33563 | 374 | MFM_NONE, 0, |
6a885fd0 | 375 | MFS_DECIMAL, |
268a95e0 | 376 | MFP_ICMPV4, |
28da1f8f BP |
377 | false, |
378 | NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE", | |
36f3999b | 379 | OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE", |
6a885fd0 | 380 | }, { |
268a95e0 | 381 | MFF_ICMPV4_CODE, "icmp_code", NULL, |
6a885fd0 | 382 | MF_FIELD_SIZES(u8), |
73f33563 | 383 | MFM_NONE, 0, |
6a885fd0 | 384 | MFS_DECIMAL, |
268a95e0 | 385 | MFP_ICMPV4, |
28da1f8f BP |
386 | false, |
387 | NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE", | |
36f3999b | 388 | OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE", |
28da1f8f BP |
389 | }, |
390 | ||
391 | { | |
268a95e0 BP |
392 | MFF_ICMPV6_TYPE, "icmpv6_type", NULL, |
393 | MF_FIELD_SIZES(u8), | |
73f33563 | 394 | MFM_NONE, 0, |
268a95e0 BP |
395 | MFS_DECIMAL, |
396 | MFP_ICMPV6, | |
28da1f8f BP |
397 | false, |
398 | NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE", | |
36f3999b | 399 | OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE", |
268a95e0 BP |
400 | }, { |
401 | MFF_ICMPV6_CODE, "icmpv6_code", NULL, | |
402 | MF_FIELD_SIZES(u8), | |
73f33563 | 403 | MFM_NONE, 0, |
268a95e0 BP |
404 | MFS_DECIMAL, |
405 | MFP_ICMPV6, | |
28da1f8f BP |
406 | false, |
407 | NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE", | |
36f3999b | 408 | OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE", |
6a885fd0 BP |
409 | }, |
410 | ||
411 | /* ## ---- ## */ | |
412 | /* ## L"5" ## */ | |
413 | /* ## ---- ## */ | |
414 | ||
415 | { | |
416 | MFF_ND_TARGET, "nd_target", NULL, | |
417 | MF_FIELD_SIZES(ipv6), | |
ff0b06ee | 418 | MFM_FULLY, 0, |
6a885fd0 BP |
419 | MFS_IPV6, |
420 | MFP_ND, | |
28da1f8f BP |
421 | false, |
422 | NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET", | |
36f3999b | 423 | OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET", |
6a885fd0 BP |
424 | }, { |
425 | MFF_ND_SLL, "nd_sll", NULL, | |
426 | MF_FIELD_SIZES(mac), | |
e878338b | 427 | MFM_FULLY, 0, |
6a885fd0 BP |
428 | MFS_ETHERNET, |
429 | MFP_ND_SOLICIT, | |
28da1f8f BP |
430 | false, |
431 | NXM_NX_ND_SLL, "NXM_NX_ND_SLL", | |
36f3999b | 432 | OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL", |
6a885fd0 BP |
433 | }, { |
434 | MFF_ND_TLL, "nd_tll", NULL, | |
435 | MF_FIELD_SIZES(mac), | |
e878338b | 436 | MFM_FULLY, 0, |
6a885fd0 BP |
437 | MFS_ETHERNET, |
438 | MFP_ND_ADVERT, | |
28da1f8f BP |
439 | false, |
440 | NXM_NX_ND_TLL, "NXM_NX_ND_TLL", | |
36f3999b | 441 | OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL", |
6a885fd0 BP |
442 | } |
443 | }; | |
444 | ||
b5e5143b | 445 | /* Maps an NXM or OXM header value to an mf_field. */ |
28da1f8f | 446 | struct nxm_field { |
b5e5143b BP |
447 | struct hmap_node hmap_node; /* In 'all_fields' hmap. */ |
448 | uint32_t header; /* NXM or OXM header value. */ | |
28da1f8f BP |
449 | const struct mf_field *mf; |
450 | }; | |
451 | ||
b5e5143b BP |
452 | /* Contains 'struct nxm_field's. */ |
453 | static struct hmap all_fields = HMAP_INITIALIZER(&all_fields); | |
28da1f8f | 454 | |
816fd533 BP |
455 | /* Rate limit for parse errors. These always indicate a bug in an OpenFlow |
456 | * controller and so there's not much point in showing a lot of them. */ | |
457 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
458 | ||
b5e5143b BP |
459 | const struct mf_field *mf_from_nxm_header__(uint32_t header); |
460 | ||
6a885fd0 BP |
461 | /* Returns the field with the given 'id'. */ |
462 | const struct mf_field * | |
463 | mf_from_id(enum mf_field_id id) | |
464 | { | |
465 | assert((unsigned int) id < MFF_N_IDS); | |
466 | return &mf_fields[id]; | |
467 | } | |
468 | ||
469 | /* Returns the field with the given 'name', or a null pointer if no field has | |
470 | * that name. */ | |
471 | const struct mf_field * | |
472 | mf_from_name(const char *name) | |
473 | { | |
474 | static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name); | |
475 | ||
476 | if (shash_is_empty(&mf_by_name)) { | |
477 | const struct mf_field *mf; | |
478 | ||
479 | for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) { | |
480 | shash_add_once(&mf_by_name, mf->name, mf); | |
481 | if (mf->extra_name) { | |
482 | shash_add_once(&mf_by_name, mf->extra_name, mf); | |
483 | } | |
484 | } | |
485 | } | |
486 | ||
487 | return shash_find_data(&mf_by_name, name); | |
488 | } | |
489 | ||
28da1f8f | 490 | static void |
b5e5143b | 491 | add_nxm_field(uint32_t header, const struct mf_field *mf) |
28da1f8f BP |
492 | { |
493 | struct nxm_field *f; | |
494 | ||
495 | f = xmalloc(sizeof *f); | |
b5e5143b BP |
496 | hmap_insert(&all_fields, &f->hmap_node, hash_int(header, 0)); |
497 | f->header = header; | |
28da1f8f BP |
498 | f->mf = mf; |
499 | } | |
500 | ||
b5ae8913 SH |
501 | static void |
502 | nxm_init_add_field(const struct mf_field *mf, uint32_t header) | |
503 | { | |
b5e5143b BP |
504 | if (header) { |
505 | assert(!mf_from_nxm_header__(header)); | |
506 | add_nxm_field(header, mf); | |
507 | if (mf->maskable != MFM_NONE) { | |
508 | add_nxm_field(NXM_MAKE_WILD_HEADER(header), mf); | |
509 | } | |
b5ae8913 | 510 | } |
b5ae8913 | 511 | } |
b5ae8913 | 512 | |
28da1f8f BP |
513 | static void |
514 | nxm_init(void) | |
515 | { | |
516 | const struct mf_field *mf; | |
517 | ||
518 | for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) { | |
b5ae8913 | 519 | nxm_init_add_field(mf, mf->nxm_header); |
b5e5143b BP |
520 | if (mf->oxm_header != mf->nxm_header) { |
521 | nxm_init_add_field(mf, mf->oxm_header); | |
522 | } | |
28da1f8f | 523 | } |
28da1f8f BP |
524 | } |
525 | ||
526 | const struct mf_field * | |
527 | mf_from_nxm_header(uint32_t header) | |
528 | { | |
b5e5143b | 529 | if (hmap_is_empty(&all_fields)) { |
28da1f8f BP |
530 | nxm_init(); |
531 | } | |
b5e5143b BP |
532 | return mf_from_nxm_header__(header); |
533 | } | |
28da1f8f | 534 | |
b5e5143b BP |
535 | const struct mf_field * |
536 | mf_from_nxm_header__(uint32_t header) | |
537 | { | |
538 | const struct nxm_field *f; | |
539 | ||
540 | HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0), &all_fields) { | |
541 | if (f->header == header) { | |
28da1f8f BP |
542 | return f->mf; |
543 | } | |
544 | } | |
545 | ||
546 | return NULL; | |
547 | } | |
548 | ||
6a885fd0 BP |
549 | /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc' |
550 | * specifies at least one bit in the field. | |
551 | * | |
552 | * The caller is responsible for ensuring that 'wc' corresponds to a flow that | |
553 | * meets 'mf''s prerequisites. */ | |
554 | bool | |
555 | mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc) | |
556 | { | |
557 | switch (mf->id) { | |
558 | case MFF_IN_PORT: | |
6a885fd0 BP |
559 | case MFF_ETH_TYPE: |
560 | case MFF_IP_PROTO: | |
2486e66a JP |
561 | case MFF_IP_DSCP: |
562 | case MFF_IP_ECN: | |
a61680c6 | 563 | case MFF_IP_TTL: |
6a885fd0 | 564 | case MFF_ARP_OP: |
6a885fd0 BP |
565 | assert(mf->fww_bit != 0); |
566 | return (wc->wildcards & mf->fww_bit) != 0; | |
567 | ||
568 | case MFF_TUN_ID: | |
569 | return !wc->tun_id_mask; | |
969fc56c JS |
570 | case MFF_METADATA: |
571 | return !wc->metadata_mask; | |
6a885fd0 | 572 | |
0d7e2fe4 | 573 | CASE_MFF_REGS: |
6a885fd0 BP |
574 | return !wc->reg_masks[mf->id - MFF_REG0]; |
575 | ||
73c0ce34 JS |
576 | case MFF_ETH_SRC: |
577 | return eth_addr_is_zero(wc->dl_src_mask); | |
6a885fd0 | 578 | case MFF_ETH_DST: |
73c0ce34 | 579 | return eth_addr_is_zero(wc->dl_dst_mask); |
6a885fd0 | 580 | |
e878338b SH |
581 | case MFF_ARP_SHA: |
582 | case MFF_ND_SLL: | |
583 | return eth_addr_is_zero(wc->arp_sha_mask); | |
584 | ||
585 | case MFF_ARP_THA: | |
586 | case MFF_ND_TLL: | |
587 | return eth_addr_is_zero(wc->arp_tha_mask); | |
588 | ||
6a885fd0 BP |
589 | case MFF_VLAN_TCI: |
590 | return !wc->vlan_tci_mask; | |
441c57a9 | 591 | case MFF_DL_VLAN: |
6a885fd0 | 592 | return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK)); |
441c57a9 | 593 | case MFF_DL_VLAN_PCP: |
6a885fd0 BP |
594 | return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK)); |
595 | ||
596 | case MFF_IPV4_SRC: | |
597 | return !wc->nw_src_mask; | |
598 | case MFF_IPV4_DST: | |
599 | return !wc->nw_dst_mask; | |
600 | ||
601 | case MFF_IPV6_SRC: | |
602 | return ipv6_mask_is_any(&wc->ipv6_src_mask); | |
603 | case MFF_IPV6_DST: | |
604 | return ipv6_mask_is_any(&wc->ipv6_dst_mask); | |
605 | ||
32455024 SH |
606 | case MFF_IPV6_LABEL: |
607 | return !wc->ipv6_label_mask; | |
608 | ||
47284b1f AA |
609 | case MFF_ND_TARGET: |
610 | return ipv6_mask_is_any(&wc->nd_target_mask); | |
611 | ||
7257b535 | 612 | case MFF_IP_FRAG: |
eadef313 | 613 | return !(wc->nw_frag_mask & FLOW_NW_FRAG_MASK); |
7257b535 | 614 | |
6a885fd0 BP |
615 | case MFF_ARP_SPA: |
616 | return !wc->nw_src_mask; | |
617 | case MFF_ARP_TPA: | |
618 | return !wc->nw_dst_mask; | |
619 | ||
73f33563 BP |
620 | case MFF_TCP_SRC: |
621 | case MFF_UDP_SRC: | |
622 | case MFF_ICMPV4_TYPE: | |
623 | case MFF_ICMPV6_TYPE: | |
624 | return !wc->tp_src_mask; | |
625 | case MFF_TCP_DST: | |
626 | case MFF_UDP_DST: | |
627 | case MFF_ICMPV4_CODE: | |
628 | case MFF_ICMPV6_CODE: | |
629 | return !wc->tp_dst_mask; | |
630 | ||
6a885fd0 BP |
631 | case MFF_N_IDS: |
632 | default: | |
633 | NOT_REACHED(); | |
634 | } | |
635 | } | |
636 | ||
637 | /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'. | |
638 | * Each bit in 'mask' will be set to 1 if the bit is significant for matching | |
639 | * purposes, or to 0 if it is wildcarded. | |
640 | * | |
641 | * The caller is responsible for ensuring that 'wc' corresponds to a flow that | |
642 | * meets 'mf''s prerequisites. */ | |
643 | void | |
644 | mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc, | |
645 | union mf_value *mask) | |
646 | { | |
647 | switch (mf->id) { | |
648 | case MFF_IN_PORT: | |
6a885fd0 BP |
649 | case MFF_ETH_TYPE: |
650 | case MFF_IP_PROTO: | |
2486e66a JP |
651 | case MFF_IP_DSCP: |
652 | case MFF_IP_ECN: | |
a61680c6 | 653 | case MFF_IP_TTL: |
6a885fd0 | 654 | case MFF_ARP_OP: |
6a885fd0 BP |
655 | assert(mf->fww_bit != 0); |
656 | memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes); | |
657 | break; | |
658 | ||
659 | case MFF_TUN_ID: | |
660 | mask->be64 = wc->tun_id_mask; | |
661 | break; | |
969fc56c JS |
662 | case MFF_METADATA: |
663 | mask->be64 = wc->metadata_mask; | |
664 | break; | |
6a885fd0 | 665 | |
0d7e2fe4 | 666 | CASE_MFF_REGS: |
6a885fd0 BP |
667 | mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]); |
668 | break; | |
669 | ||
670 | case MFF_ETH_DST: | |
73c0ce34 JS |
671 | memcpy(mask->mac, wc->dl_dst_mask, ETH_ADDR_LEN); |
672 | break; | |
673 | ||
674 | case MFF_ETH_SRC: | |
675 | memcpy(mask->mac, wc->dl_src_mask, ETH_ADDR_LEN); | |
6a885fd0 BP |
676 | break; |
677 | ||
678 | case MFF_VLAN_TCI: | |
679 | mask->be16 = wc->vlan_tci_mask; | |
680 | break; | |
441c57a9 | 681 | case MFF_DL_VLAN: |
6a885fd0 BP |
682 | mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK); |
683 | break; | |
441c57a9 | 684 | case MFF_DL_VLAN_PCP: |
6a885fd0 BP |
685 | mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask); |
686 | break; | |
687 | ||
688 | case MFF_IPV4_SRC: | |
689 | mask->be32 = wc->nw_src_mask; | |
690 | break; | |
691 | case MFF_IPV4_DST: | |
692 | mask->be32 = wc->nw_dst_mask; | |
693 | break; | |
694 | ||
695 | case MFF_IPV6_SRC: | |
696 | mask->ipv6 = wc->ipv6_src_mask; | |
697 | break; | |
698 | case MFF_IPV6_DST: | |
699 | mask->ipv6 = wc->ipv6_dst_mask; | |
700 | break; | |
465d9a8f BP |
701 | case MFF_IPV6_LABEL: |
702 | mask->be32 = wc->ipv6_label_mask; | |
703 | break; | |
6a885fd0 | 704 | |
47284b1f AA |
705 | case MFF_ND_TARGET: |
706 | mask->ipv6 = wc->nd_target_mask; | |
707 | break; | |
708 | ||
7257b535 | 709 | case MFF_IP_FRAG: |
eadef313 | 710 | mask->u8 = wc->nw_frag_mask & FLOW_NW_FRAG_MASK; |
7257b535 BP |
711 | break; |
712 | ||
6a885fd0 BP |
713 | case MFF_ARP_SPA: |
714 | mask->be32 = wc->nw_src_mask; | |
715 | break; | |
716 | case MFF_ARP_TPA: | |
717 | mask->be32 = wc->nw_dst_mask; | |
718 | break; | |
e878338b SH |
719 | case MFF_ARP_SHA: |
720 | case MFF_ND_SLL: | |
721 | memcpy(mask->mac, wc->arp_sha_mask, ETH_ADDR_LEN); | |
722 | break; | |
723 | case MFF_ARP_THA: | |
724 | case MFF_ND_TLL: | |
725 | memcpy(mask->mac, wc->arp_tha_mask, ETH_ADDR_LEN); | |
726 | break; | |
6a885fd0 | 727 | |
73f33563 BP |
728 | case MFF_TCP_SRC: |
729 | case MFF_UDP_SRC: | |
730 | mask->be16 = wc->tp_src_mask; | |
731 | break; | |
732 | case MFF_TCP_DST: | |
733 | case MFF_UDP_DST: | |
734 | mask->be16 = wc->tp_dst_mask; | |
735 | break; | |
736 | ||
737 | case MFF_ICMPV4_TYPE: | |
738 | case MFF_ICMPV6_TYPE: | |
739 | mask->u8 = ntohs(wc->tp_src_mask); | |
740 | break; | |
741 | case MFF_ICMPV4_CODE: | |
742 | case MFF_ICMPV6_CODE: | |
743 | mask->u8 = ntohs(wc->tp_dst_mask); | |
744 | break; | |
745 | ||
6a885fd0 BP |
746 | case MFF_N_IDS: |
747 | default: | |
748 | NOT_REACHED(); | |
749 | } | |
750 | } | |
751 | ||
752 | /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true | |
753 | * if the mask is valid, false otherwise. */ | |
754 | bool | |
755 | mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask) | |
756 | { | |
757 | switch (mf->maskable) { | |
758 | case MFM_NONE: | |
759 | return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) || | |
760 | is_all_ones((const uint8_t *) mask, mf->n_bytes)); | |
761 | ||
762 | case MFM_FULLY: | |
763 | return true; | |
6a885fd0 BP |
764 | } |
765 | ||
766 | NOT_REACHED(); | |
767 | } | |
768 | ||
769 | static bool | |
770 | is_ip_any(const struct flow *flow) | |
771 | { | |
772 | return (flow->dl_type == htons(ETH_TYPE_IP) || | |
773 | flow->dl_type == htons(ETH_TYPE_IPV6)); | |
774 | } | |
775 | ||
776 | static bool | |
777 | is_icmpv4(const struct flow *flow) | |
778 | { | |
779 | return (flow->dl_type == htons(ETH_TYPE_IP) | |
780 | && flow->nw_proto == IPPROTO_ICMP); | |
781 | } | |
782 | ||
783 | static bool | |
784 | is_icmpv6(const struct flow *flow) | |
785 | { | |
786 | return (flow->dl_type == htons(ETH_TYPE_IPV6) | |
787 | && flow->nw_proto == IPPROTO_ICMPV6); | |
788 | } | |
789 | ||
790 | /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */ | |
791 | bool | |
792 | mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow) | |
793 | { | |
794 | switch (mf->prereqs) { | |
795 | case MFP_NONE: | |
796 | return true; | |
797 | ||
798 | case MFP_ARP: | |
799 | return flow->dl_type == htons(ETH_TYPE_ARP); | |
800 | case MFP_IPV4: | |
801 | return flow->dl_type == htons(ETH_TYPE_IP); | |
802 | case MFP_IPV6: | |
803 | return flow->dl_type == htons(ETH_TYPE_IPV6); | |
804 | case MFP_IP_ANY: | |
805 | return is_ip_any(flow); | |
806 | ||
807 | case MFP_TCP: | |
808 | return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP; | |
809 | case MFP_UDP: | |
810 | return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP; | |
268a95e0 BP |
811 | case MFP_ICMPV4: |
812 | return is_icmpv4(flow); | |
6a885fd0 BP |
813 | case MFP_ICMPV6: |
814 | return is_icmpv6(flow); | |
6a885fd0 BP |
815 | |
816 | case MFP_ND: | |
817 | return (is_icmpv6(flow) | |
3ee8a9f0 BP |
818 | && flow->tp_dst == htons(0) |
819 | && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) || | |
820 | flow->tp_src == htons(ND_NEIGHBOR_ADVERT))); | |
6a885fd0 BP |
821 | case MFP_ND_SOLICIT: |
822 | return (is_icmpv6(flow) | |
3ee8a9f0 BP |
823 | && flow->tp_dst == htons(0) |
824 | && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT))); | |
6a885fd0 BP |
825 | case MFP_ND_ADVERT: |
826 | return (is_icmpv6(flow) | |
3ee8a9f0 BP |
827 | && flow->tp_dst == htons(0) |
828 | && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT))); | |
6a885fd0 BP |
829 | } |
830 | ||
831 | NOT_REACHED(); | |
832 | } | |
833 | ||
834 | /* Returns true if 'value' may be a valid value *as part of a masked match*, | |
835 | * false otherwise. | |
836 | * | |
837 | * A value is not rejected just because it is not valid for the field in | |
838 | * question, but only if it doesn't make sense to test the bits in question at | |
839 | * all. For example, the MFF_VLAN_TCI field will never have a nonzero value | |
840 | * without the VLAN_CFI bit being set, but we can't reject those values because | |
841 | * it is still legitimate to test just for those bits (see the documentation | |
842 | * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a | |
530180fd | 843 | * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */ |
6a885fd0 BP |
844 | bool |
845 | mf_is_value_valid(const struct mf_field *mf, const union mf_value *value) | |
846 | { | |
847 | switch (mf->id) { | |
848 | case MFF_TUN_ID: | |
969fc56c | 849 | case MFF_METADATA: |
6a885fd0 | 850 | case MFF_IN_PORT: |
0d7e2fe4 | 851 | CASE_MFF_REGS: |
6a885fd0 BP |
852 | case MFF_ETH_SRC: |
853 | case MFF_ETH_DST: | |
854 | case MFF_ETH_TYPE: | |
855 | case MFF_VLAN_TCI: | |
856 | case MFF_IPV4_SRC: | |
857 | case MFF_IPV4_DST: | |
858 | case MFF_IPV6_SRC: | |
859 | case MFF_IPV6_DST: | |
860 | case MFF_IP_PROTO: | |
a61680c6 | 861 | case MFF_IP_TTL: |
6a885fd0 BP |
862 | case MFF_ARP_SPA: |
863 | case MFF_ARP_TPA: | |
864 | case MFF_ARP_SHA: | |
865 | case MFF_ARP_THA: | |
866 | case MFF_TCP_SRC: | |
867 | case MFF_TCP_DST: | |
868 | case MFF_UDP_SRC: | |
869 | case MFF_UDP_DST: | |
268a95e0 BP |
870 | case MFF_ICMPV4_TYPE: |
871 | case MFF_ICMPV4_CODE: | |
872 | case MFF_ICMPV6_TYPE: | |
873 | case MFF_ICMPV6_CODE: | |
6a885fd0 BP |
874 | case MFF_ND_TARGET: |
875 | case MFF_ND_SLL: | |
876 | case MFF_ND_TLL: | |
877 | return true; | |
878 | ||
530180fd | 879 | case MFF_IP_DSCP: |
7257b535 | 880 | return !(value->u8 & ~IP_DSCP_MASK); |
530180fd JP |
881 | case MFF_IP_ECN: |
882 | return !(value->u8 & ~IP_ECN_MASK); | |
7257b535 | 883 | case MFF_IP_FRAG: |
eadef313 | 884 | return !(value->u8 & ~FLOW_NW_FRAG_MASK); |
6a885fd0 BP |
885 | |
886 | case MFF_ARP_OP: | |
887 | return !(value->be16 & htons(0xff00)); | |
888 | ||
441c57a9 | 889 | case MFF_DL_VLAN: |
6a885fd0 BP |
890 | return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK)); |
891 | ||
441c57a9 | 892 | case MFF_DL_VLAN_PCP: |
24016f5a | 893 | return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT)); |
6a885fd0 | 894 | |
fa8223b7 JP |
895 | case MFF_IPV6_LABEL: |
896 | return !(value->be32 & ~htonl(IPV6_LABEL_MASK)); | |
897 | ||
6a885fd0 BP |
898 | case MFF_N_IDS: |
899 | default: | |
900 | NOT_REACHED(); | |
901 | } | |
902 | } | |
903 | ||
904 | /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is | |
905 | * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */ | |
906 | void | |
907 | mf_get_value(const struct mf_field *mf, const struct flow *flow, | |
908 | union mf_value *value) | |
909 | { | |
910 | switch (mf->id) { | |
911 | case MFF_TUN_ID: | |
912 | value->be64 = flow->tun_id; | |
913 | break; | |
969fc56c JS |
914 | case MFF_METADATA: |
915 | value->be64 = flow->metadata; | |
916 | break; | |
6a885fd0 BP |
917 | |
918 | case MFF_IN_PORT: | |
919 | value->be16 = htons(flow->in_port); | |
920 | break; | |
921 | ||
0d7e2fe4 | 922 | CASE_MFF_REGS: |
2f98b0b7 | 923 | value->be32 = htonl(flow->regs[mf->id - MFF_REG0]); |
6a885fd0 BP |
924 | break; |
925 | ||
926 | case MFF_ETH_SRC: | |
927 | memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN); | |
928 | break; | |
929 | ||
930 | case MFF_ETH_DST: | |
931 | memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN); | |
932 | break; | |
933 | ||
934 | case MFF_ETH_TYPE: | |
935 | value->be16 = flow->dl_type; | |
936 | break; | |
937 | ||
938 | case MFF_VLAN_TCI: | |
939 | value->be16 = flow->vlan_tci; | |
940 | break; | |
941 | ||
441c57a9 | 942 | case MFF_DL_VLAN: |
6a885fd0 BP |
943 | value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK); |
944 | break; | |
945 | ||
441c57a9 | 946 | case MFF_DL_VLAN_PCP: |
6a885fd0 BP |
947 | value->u8 = vlan_tci_to_pcp(flow->vlan_tci); |
948 | break; | |
949 | ||
950 | case MFF_IPV4_SRC: | |
951 | value->be32 = flow->nw_src; | |
952 | break; | |
953 | ||
954 | case MFF_IPV4_DST: | |
955 | value->be32 = flow->nw_dst; | |
956 | break; | |
957 | ||
958 | case MFF_IPV6_SRC: | |
959 | value->ipv6 = flow->ipv6_src; | |
960 | break; | |
961 | ||
962 | case MFF_IPV6_DST: | |
963 | value->ipv6 = flow->ipv6_dst; | |
964 | break; | |
965 | ||
fa8223b7 JP |
966 | case MFF_IPV6_LABEL: |
967 | value->be32 = flow->ipv6_label; | |
968 | break; | |
969 | ||
6a885fd0 BP |
970 | case MFF_IP_PROTO: |
971 | value->u8 = flow->nw_proto; | |
972 | break; | |
973 | ||
530180fd | 974 | case MFF_IP_DSCP: |
eadef313 | 975 | value->u8 = flow->nw_tos & IP_DSCP_MASK; |
7257b535 BP |
976 | break; |
977 | ||
530180fd | 978 | case MFF_IP_ECN: |
eadef313 | 979 | value->u8 = flow->nw_tos & IP_ECN_MASK; |
530180fd JP |
980 | break; |
981 | ||
a61680c6 JP |
982 | case MFF_IP_TTL: |
983 | value->u8 = flow->nw_ttl; | |
984 | break; | |
985 | ||
7257b535 | 986 | case MFF_IP_FRAG: |
eadef313 | 987 | value->u8 = flow->nw_frag; |
6a885fd0 BP |
988 | break; |
989 | ||
990 | case MFF_ARP_OP: | |
991 | value->be16 = htons(flow->nw_proto); | |
992 | break; | |
993 | ||
994 | case MFF_ARP_SPA: | |
995 | value->be32 = flow->nw_src; | |
996 | break; | |
997 | ||
998 | case MFF_ARP_TPA: | |
999 | value->be32 = flow->nw_dst; | |
1000 | break; | |
1001 | ||
1002 | case MFF_ARP_SHA: | |
1003 | case MFF_ND_SLL: | |
1004 | memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN); | |
1005 | break; | |
1006 | ||
1007 | case MFF_ARP_THA: | |
1008 | case MFF_ND_TLL: | |
1009 | memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN); | |
1010 | break; | |
1011 | ||
1012 | case MFF_TCP_SRC: | |
6a885fd0 BP |
1013 | case MFF_UDP_SRC: |
1014 | value->be16 = flow->tp_src; | |
1015 | break; | |
1016 | ||
71baec06 | 1017 | case MFF_TCP_DST: |
6a885fd0 BP |
1018 | case MFF_UDP_DST: |
1019 | value->be16 = flow->tp_dst; | |
1020 | break; | |
1021 | ||
268a95e0 BP |
1022 | case MFF_ICMPV4_TYPE: |
1023 | case MFF_ICMPV6_TYPE: | |
6a885fd0 BP |
1024 | value->u8 = ntohs(flow->tp_src); |
1025 | break; | |
1026 | ||
268a95e0 BP |
1027 | case MFF_ICMPV4_CODE: |
1028 | case MFF_ICMPV6_CODE: | |
6a885fd0 BP |
1029 | value->u8 = ntohs(flow->tp_dst); |
1030 | break; | |
1031 | ||
1032 | case MFF_ND_TARGET: | |
1033 | value->ipv6 = flow->nd_target; | |
1034 | break; | |
1035 | ||
1036 | case MFF_N_IDS: | |
1037 | default: | |
1038 | NOT_REACHED(); | |
1039 | } | |
1040 | } | |
1041 | ||
1042 | /* Makes 'rule' match field 'mf' exactly, with the value matched taken from | |
1043 | * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s | |
1044 | * prerequisites. */ | |
1045 | void | |
1046 | mf_set_value(const struct mf_field *mf, | |
1047 | const union mf_value *value, struct cls_rule *rule) | |
1048 | { | |
1049 | switch (mf->id) { | |
1050 | case MFF_TUN_ID: | |
1051 | cls_rule_set_tun_id(rule, value->be64); | |
1052 | break; | |
969fc56c JS |
1053 | case MFF_METADATA: |
1054 | cls_rule_set_metadata(rule, value->be64); | |
1055 | break; | |
6a885fd0 BP |
1056 | |
1057 | case MFF_IN_PORT: | |
1058 | cls_rule_set_in_port(rule, ntohs(value->be16)); | |
1059 | break; | |
1060 | ||
0d7e2fe4 | 1061 | CASE_MFF_REGS: |
6a885fd0 BP |
1062 | cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32)); |
1063 | break; | |
6a885fd0 BP |
1064 | |
1065 | case MFF_ETH_SRC: | |
1066 | cls_rule_set_dl_src(rule, value->mac); | |
1067 | break; | |
1068 | ||
1069 | case MFF_ETH_DST: | |
1070 | cls_rule_set_dl_dst(rule, value->mac); | |
1071 | break; | |
1072 | ||
1073 | case MFF_ETH_TYPE: | |
1074 | cls_rule_set_dl_type(rule, value->be16); | |
1075 | break; | |
1076 | ||
1077 | case MFF_VLAN_TCI: | |
1078 | cls_rule_set_dl_tci(rule, value->be16); | |
1079 | break; | |
1080 | ||
441c57a9 | 1081 | case MFF_DL_VLAN: |
6a885fd0 BP |
1082 | cls_rule_set_dl_vlan(rule, value->be16); |
1083 | break; | |
1084 | ||
441c57a9 | 1085 | case MFF_DL_VLAN_PCP: |
6a885fd0 BP |
1086 | cls_rule_set_dl_vlan_pcp(rule, value->u8); |
1087 | break; | |
1088 | ||
1089 | case MFF_IPV4_SRC: | |
1090 | cls_rule_set_nw_src(rule, value->be32); | |
1091 | break; | |
1092 | ||
1093 | case MFF_IPV4_DST: | |
1094 | cls_rule_set_nw_dst(rule, value->be32); | |
1095 | break; | |
1096 | ||
1097 | case MFF_IPV6_SRC: | |
1098 | cls_rule_set_ipv6_src(rule, &value->ipv6); | |
1099 | break; | |
1100 | ||
1101 | case MFF_IPV6_DST: | |
1102 | cls_rule_set_ipv6_dst(rule, &value->ipv6); | |
1103 | break; | |
1104 | ||
fa8223b7 JP |
1105 | case MFF_IPV6_LABEL: |
1106 | cls_rule_set_ipv6_label(rule, value->be32); | |
1107 | break; | |
1108 | ||
6a885fd0 BP |
1109 | case MFF_IP_PROTO: |
1110 | cls_rule_set_nw_proto(rule, value->u8); | |
1111 | break; | |
1112 | ||
530180fd JP |
1113 | case MFF_IP_DSCP: |
1114 | cls_rule_set_nw_dscp(rule, value->u8); | |
1115 | break; | |
1116 | ||
1117 | case MFF_IP_ECN: | |
1118 | cls_rule_set_nw_ecn(rule, value->u8); | |
6a885fd0 BP |
1119 | break; |
1120 | ||
a61680c6 JP |
1121 | case MFF_IP_TTL: |
1122 | cls_rule_set_nw_ttl(rule, value->u8); | |
1123 | break; | |
1124 | ||
7257b535 | 1125 | case MFF_IP_FRAG: |
eadef313 | 1126 | cls_rule_set_nw_frag(rule, value->u8); |
7257b535 BP |
1127 | break; |
1128 | ||
6a885fd0 BP |
1129 | case MFF_ARP_OP: |
1130 | cls_rule_set_nw_proto(rule, ntohs(value->be16)); | |
1131 | break; | |
1132 | ||
1133 | case MFF_ARP_SPA: | |
1134 | cls_rule_set_nw_src(rule, value->be32); | |
1135 | break; | |
1136 | ||
1137 | case MFF_ARP_TPA: | |
1138 | cls_rule_set_nw_dst(rule, value->be32); | |
1139 | break; | |
1140 | ||
1141 | case MFF_ARP_SHA: | |
1142 | case MFF_ND_SLL: | |
1143 | cls_rule_set_arp_sha(rule, value->mac); | |
1144 | break; | |
1145 | ||
1146 | case MFF_ARP_THA: | |
1147 | case MFF_ND_TLL: | |
1148 | cls_rule_set_arp_tha(rule, value->mac); | |
1149 | break; | |
1150 | ||
1151 | case MFF_TCP_SRC: | |
6a885fd0 BP |
1152 | case MFF_UDP_SRC: |
1153 | cls_rule_set_tp_src(rule, value->be16); | |
1154 | break; | |
1155 | ||
71baec06 | 1156 | case MFF_TCP_DST: |
6a885fd0 BP |
1157 | case MFF_UDP_DST: |
1158 | cls_rule_set_tp_dst(rule, value->be16); | |
1159 | break; | |
1160 | ||
268a95e0 BP |
1161 | case MFF_ICMPV4_TYPE: |
1162 | case MFF_ICMPV6_TYPE: | |
6a885fd0 BP |
1163 | cls_rule_set_icmp_type(rule, value->u8); |
1164 | break; | |
1165 | ||
268a95e0 BP |
1166 | case MFF_ICMPV4_CODE: |
1167 | case MFF_ICMPV6_CODE: | |
6a885fd0 BP |
1168 | cls_rule_set_icmp_code(rule, value->u8); |
1169 | break; | |
1170 | ||
1171 | case MFF_ND_TARGET: | |
1172 | cls_rule_set_nd_target(rule, &value->ipv6); | |
1173 | break; | |
1174 | ||
1175 | case MFF_N_IDS: | |
1176 | default: | |
1177 | NOT_REACHED(); | |
1178 | } | |
1179 | } | |
1180 | ||
28da1f8f BP |
1181 | /* Makes 'rule' match field 'mf' exactly, with the value matched taken from |
1182 | * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s | |
1183 | * prerequisites. */ | |
1184 | void | |
1185 | mf_set_flow_value(const struct mf_field *mf, | |
1186 | const union mf_value *value, struct flow *flow) | |
1187 | { | |
1188 | switch (mf->id) { | |
1189 | case MFF_TUN_ID: | |
1190 | flow->tun_id = value->be64; | |
1191 | break; | |
969fc56c JS |
1192 | case MFF_METADATA: |
1193 | flow->metadata = value->be64; | |
1194 | break; | |
28da1f8f BP |
1195 | |
1196 | case MFF_IN_PORT: | |
1197 | flow->in_port = ntohs(value->be16); | |
1198 | break; | |
1199 | ||
0d7e2fe4 | 1200 | CASE_MFF_REGS: |
28da1f8f BP |
1201 | flow->regs[mf->id - MFF_REG0] = ntohl(value->be32); |
1202 | break; | |
28da1f8f BP |
1203 | |
1204 | case MFF_ETH_SRC: | |
1205 | memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN); | |
1206 | break; | |
1207 | ||
1208 | case MFF_ETH_DST: | |
b5a36e38 | 1209 | memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN); |
28da1f8f BP |
1210 | break; |
1211 | ||
1212 | case MFF_ETH_TYPE: | |
1213 | flow->dl_type = value->be16; | |
1214 | break; | |
1215 | ||
1216 | case MFF_VLAN_TCI: | |
1217 | flow->vlan_tci = value->be16; | |
1218 | break; | |
1219 | ||
441c57a9 | 1220 | case MFF_DL_VLAN: |
28da1f8f BP |
1221 | flow_set_vlan_vid(flow, value->be16); |
1222 | break; | |
1223 | ||
441c57a9 | 1224 | case MFF_DL_VLAN_PCP: |
28da1f8f BP |
1225 | flow_set_vlan_pcp(flow, value->u8); |
1226 | break; | |
1227 | ||
1228 | case MFF_IPV4_SRC: | |
1229 | flow->nw_src = value->be32; | |
1230 | break; | |
1231 | ||
1232 | case MFF_IPV4_DST: | |
1233 | flow->nw_dst = value->be32; | |
1234 | break; | |
1235 | ||
1236 | case MFF_IPV6_SRC: | |
1237 | flow->ipv6_src = value->ipv6; | |
1238 | break; | |
1239 | ||
1240 | case MFF_IPV6_DST: | |
1241 | flow->ipv6_dst = value->ipv6; | |
1242 | break; | |
1243 | ||
1244 | case MFF_IPV6_LABEL: | |
1245 | flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK); | |
1246 | break; | |
1247 | ||
1248 | case MFF_IP_PROTO: | |
1249 | flow->nw_proto = value->u8; | |
1250 | break; | |
1251 | ||
1252 | case MFF_IP_DSCP: | |
1253 | flow->nw_tos &= ~IP_DSCP_MASK; | |
1254 | flow->nw_tos |= value->u8 & IP_DSCP_MASK; | |
1255 | break; | |
1256 | ||
1257 | case MFF_IP_ECN: | |
1258 | flow->nw_tos &= ~IP_ECN_MASK; | |
1259 | flow->nw_tos |= value->u8 & IP_ECN_MASK; | |
1260 | break; | |
1261 | ||
1262 | case MFF_IP_TTL: | |
1263 | flow->nw_ttl = value->u8; | |
1264 | break; | |
1265 | ||
1266 | case MFF_IP_FRAG: | |
1267 | flow->nw_frag &= value->u8; | |
1268 | break; | |
1269 | ||
1270 | case MFF_ARP_OP: | |
1271 | flow->nw_proto = ntohs(value->be16); | |
1272 | break; | |
1273 | ||
1274 | case MFF_ARP_SPA: | |
1275 | flow->nw_src = value->be32; | |
1276 | break; | |
1277 | ||
1278 | case MFF_ARP_TPA: | |
1279 | flow->nw_dst = value->be32; | |
1280 | break; | |
1281 | ||
1282 | case MFF_ARP_SHA: | |
1283 | case MFF_ND_SLL: | |
1284 | memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN); | |
1285 | break; | |
1286 | ||
1287 | case MFF_ARP_THA: | |
1288 | case MFF_ND_TLL: | |
1289 | memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN); | |
1290 | break; | |
1291 | ||
1292 | case MFF_TCP_SRC: | |
1293 | case MFF_UDP_SRC: | |
1294 | flow->tp_src = value->be16; | |
1295 | break; | |
1296 | ||
1297 | case MFF_TCP_DST: | |
1298 | case MFF_UDP_DST: | |
1299 | flow->tp_dst = value->be16; | |
1300 | break; | |
1301 | ||
1302 | case MFF_ICMPV4_TYPE: | |
1303 | case MFF_ICMPV6_TYPE: | |
1304 | flow->tp_src = htons(value->u8); | |
1305 | break; | |
1306 | ||
1307 | case MFF_ICMPV4_CODE: | |
1308 | case MFF_ICMPV6_CODE: | |
1309 | flow->tp_dst = htons(value->u8); | |
1310 | break; | |
1311 | ||
1312 | case MFF_ND_TARGET: | |
1313 | flow->nd_target = value->ipv6; | |
1314 | break; | |
1315 | ||
1316 | case MFF_N_IDS: | |
1317 | default: | |
1318 | NOT_REACHED(); | |
1319 | } | |
1320 | } | |
1321 | ||
ccbe50f8 BP |
1322 | /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero. |
1323 | * | |
1324 | * The caller is responsible for ensuring that 'flow' meets 'mf''s | |
1325 | * prerequisites. */ | |
1326 | bool | |
1327 | mf_is_zero(const struct mf_field *mf, const struct flow *flow) | |
1328 | { | |
1329 | union mf_value value; | |
1330 | ||
1331 | mf_get_value(mf, flow, &value); | |
1332 | return is_all_zeros((const uint8_t *) &value, mf->n_bytes); | |
1333 | } | |
1334 | ||
6a885fd0 BP |
1335 | /* Makes 'rule' wildcard field 'mf'. |
1336 | * | |
1337 | * The caller is responsible for ensuring that 'rule' meets 'mf''s | |
1338 | * prerequisites. */ | |
1339 | void | |
1340 | mf_set_wild(const struct mf_field *mf, struct cls_rule *rule) | |
1341 | { | |
1342 | switch (mf->id) { | |
1343 | case MFF_TUN_ID: | |
1344 | cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0)); | |
1345 | break; | |
969fc56c JS |
1346 | case MFF_METADATA: |
1347 | cls_rule_set_metadata_masked(rule, htonll(0), htonll(0)); | |
6a885fd0 BP |
1348 | |
1349 | case MFF_IN_PORT: | |
1350 | rule->wc.wildcards |= FWW_IN_PORT; | |
1351 | rule->flow.in_port = 0; | |
1352 | break; | |
1353 | ||
0d7e2fe4 IY |
1354 | CASE_MFF_REGS: |
1355 | cls_rule_set_reg_masked(rule, mf->id - MFF_REG0, 0, 0); | |
e9358af6 | 1356 | break; |
6a885fd0 BP |
1357 | |
1358 | case MFF_ETH_SRC: | |
73c0ce34 JS |
1359 | memset(rule->flow.dl_src, 0, ETH_ADDR_LEN); |
1360 | memset(rule->wc.dl_src_mask, 0, ETH_ADDR_LEN); | |
6a885fd0 BP |
1361 | break; |
1362 | ||
1363 | case MFF_ETH_DST: | |
73c0ce34 JS |
1364 | memset(rule->flow.dl_dst, 0, ETH_ADDR_LEN); |
1365 | memset(rule->wc.dl_dst_mask, 0, ETH_ADDR_LEN); | |
6a885fd0 BP |
1366 | break; |
1367 | ||
1368 | case MFF_ETH_TYPE: | |
1369 | rule->wc.wildcards |= FWW_DL_TYPE; | |
1370 | rule->flow.dl_type = htons(0); | |
1371 | break; | |
1372 | ||
1373 | case MFF_VLAN_TCI: | |
1374 | cls_rule_set_dl_tci_masked(rule, htons(0), htons(0)); | |
1375 | break; | |
1376 | ||
441c57a9 | 1377 | case MFF_DL_VLAN: |
6a885fd0 BP |
1378 | cls_rule_set_any_vid(rule); |
1379 | break; | |
1380 | ||
441c57a9 | 1381 | case MFF_DL_VLAN_PCP: |
6a885fd0 BP |
1382 | cls_rule_set_any_pcp(rule); |
1383 | break; | |
1384 | ||
1385 | case MFF_IPV4_SRC: | |
1386 | case MFF_ARP_SPA: | |
1387 | cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0)); | |
1388 | break; | |
1389 | ||
1390 | case MFF_IPV4_DST: | |
1391 | case MFF_ARP_TPA: | |
1392 | cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0)); | |
1393 | break; | |
1394 | ||
1395 | case MFF_IPV6_SRC: | |
1396 | memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask); | |
1397 | memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src); | |
1398 | break; | |
1399 | ||
1400 | case MFF_IPV6_DST: | |
1401 | memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask); | |
1402 | memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst); | |
1403 | break; | |
1404 | ||
fa8223b7 | 1405 | case MFF_IPV6_LABEL: |
32455024 | 1406 | rule->wc.ipv6_label_mask = 0; |
fa8223b7 JP |
1407 | rule->flow.ipv6_label = 0; |
1408 | break; | |
1409 | ||
6a885fd0 BP |
1410 | case MFF_IP_PROTO: |
1411 | rule->wc.wildcards |= FWW_NW_PROTO; | |
1412 | rule->flow.nw_proto = 0; | |
1413 | break; | |
1414 | ||
530180fd | 1415 | case MFF_IP_DSCP: |
2486e66a | 1416 | rule->wc.wildcards |= FWW_NW_DSCP; |
eadef313 | 1417 | rule->flow.nw_tos &= ~IP_DSCP_MASK; |
7257b535 BP |
1418 | break; |
1419 | ||
530180fd | 1420 | case MFF_IP_ECN: |
2486e66a | 1421 | rule->wc.wildcards |= FWW_NW_ECN; |
eadef313 | 1422 | rule->flow.nw_tos &= ~IP_ECN_MASK; |
530180fd JP |
1423 | break; |
1424 | ||
a61680c6 JP |
1425 | case MFF_IP_TTL: |
1426 | rule->wc.wildcards |= FWW_NW_TTL; | |
1427 | rule->flow.nw_ttl = 0; | |
1428 | break; | |
1429 | ||
7257b535 | 1430 | case MFF_IP_FRAG: |
eadef313 JP |
1431 | rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK; |
1432 | rule->flow.nw_frag &= ~FLOW_NW_FRAG_MASK; | |
6a885fd0 BP |
1433 | break; |
1434 | ||
1435 | case MFF_ARP_OP: | |
1436 | rule->wc.wildcards |= FWW_NW_PROTO; | |
1437 | rule->flow.nw_proto = 0; | |
1438 | break; | |
1439 | ||
1440 | case MFF_ARP_SHA: | |
1441 | case MFF_ND_SLL: | |
e878338b SH |
1442 | memset(rule->flow.arp_sha, 0, ETH_ADDR_LEN); |
1443 | memset(rule->wc.arp_sha_mask, 0, ETH_ADDR_LEN); | |
6a885fd0 BP |
1444 | break; |
1445 | ||
1446 | case MFF_ARP_THA: | |
1447 | case MFF_ND_TLL: | |
e878338b SH |
1448 | memset(rule->flow.arp_tha, 0, ETH_ADDR_LEN); |
1449 | memset(rule->wc.arp_tha_mask, 0, ETH_ADDR_LEN); | |
6a885fd0 BP |
1450 | break; |
1451 | ||
1452 | case MFF_TCP_SRC: | |
1453 | case MFF_UDP_SRC: | |
268a95e0 BP |
1454 | case MFF_ICMPV4_TYPE: |
1455 | case MFF_ICMPV6_TYPE: | |
73f33563 | 1456 | rule->wc.tp_src_mask = htons(0); |
6a885fd0 BP |
1457 | rule->flow.tp_src = htons(0); |
1458 | break; | |
1459 | ||
1460 | case MFF_TCP_DST: | |
1461 | case MFF_UDP_DST: | |
268a95e0 BP |
1462 | case MFF_ICMPV4_CODE: |
1463 | case MFF_ICMPV6_CODE: | |
73f33563 | 1464 | rule->wc.tp_dst_mask = htons(0); |
6a885fd0 BP |
1465 | rule->flow.tp_dst = htons(0); |
1466 | break; | |
1467 | ||
1468 | case MFF_ND_TARGET: | |
47284b1f | 1469 | memset(&rule->wc.nd_target_mask, 0, sizeof rule->wc.nd_target_mask); |
6a885fd0 BP |
1470 | memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target); |
1471 | break; | |
1472 | ||
1473 | case MFF_N_IDS: | |
1474 | default: | |
1475 | NOT_REACHED(); | |
1476 | } | |
1477 | } | |
1478 | ||
1479 | /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'. | |
1480 | * 'value' specifies a value to match and 'mask' specifies a wildcard pattern, | |
1481 | * with a 1-bit indicating that the corresponding value bit must match and a | |
1482 | * 0-bit indicating a don't-care. | |
1483 | * | |
1484 | * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to | |
1485 | * mf_set_value(mf, value, rule). If 'mask' points to all-0-bits, then this | |
1486 | * call is equivalent to mf_set_wild(mf, rule). | |
1487 | * | |
1488 | * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller | |
1489 | * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */ | |
1490 | void | |
1491 | mf_set(const struct mf_field *mf, | |
1492 | const union mf_value *value, const union mf_value *mask, | |
1493 | struct cls_rule *rule) | |
1494 | { | |
1495 | if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) { | |
1496 | mf_set_value(mf, value, rule); | |
1497 | return; | |
1498 | } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) { | |
1499 | mf_set_wild(mf, rule); | |
1500 | return; | |
1501 | } | |
1502 | ||
1503 | switch (mf->id) { | |
1504 | case MFF_IN_PORT: | |
6a885fd0 | 1505 | case MFF_ETH_TYPE: |
441c57a9 BP |
1506 | case MFF_DL_VLAN: |
1507 | case MFF_DL_VLAN_PCP: | |
6a885fd0 | 1508 | case MFF_IP_PROTO: |
a61680c6 | 1509 | case MFF_IP_TTL: |
530180fd JP |
1510 | case MFF_IP_DSCP: |
1511 | case MFF_IP_ECN: | |
6a885fd0 | 1512 | case MFF_ARP_OP: |
268a95e0 BP |
1513 | case MFF_ICMPV4_TYPE: |
1514 | case MFF_ICMPV4_CODE: | |
1515 | case MFF_ICMPV6_TYPE: | |
1516 | case MFF_ICMPV6_CODE: | |
6a885fd0 BP |
1517 | NOT_REACHED(); |
1518 | ||
1519 | case MFF_TUN_ID: | |
1520 | cls_rule_set_tun_id_masked(rule, value->be64, mask->be64); | |
1521 | break; | |
969fc56c JS |
1522 | case MFF_METADATA: |
1523 | cls_rule_set_metadata_masked(rule, value->be64, mask->be64); | |
1524 | break; | |
6a885fd0 | 1525 | |
0d7e2fe4 | 1526 | CASE_MFF_REGS: |
6a885fd0 BP |
1527 | cls_rule_set_reg_masked(rule, mf->id - MFF_REG0, |
1528 | ntohl(value->be32), ntohl(mask->be32)); | |
1529 | break; | |
1530 | ||
1531 | case MFF_ETH_DST: | |
73c0ce34 JS |
1532 | cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac); |
1533 | break; | |
1534 | ||
1535 | case MFF_ETH_SRC: | |
1536 | cls_rule_set_dl_src_masked(rule, value->mac, mask->mac); | |
6a885fd0 BP |
1537 | break; |
1538 | ||
e878338b SH |
1539 | case MFF_ARP_SHA: |
1540 | case MFF_ND_SLL: | |
1541 | cls_rule_set_arp_sha_masked(rule, value->mac, mask->mac); | |
1542 | break; | |
1543 | ||
1544 | case MFF_ARP_THA: | |
1545 | case MFF_ND_TLL: | |
1546 | cls_rule_set_arp_tha_masked(rule, value->mac, mask->mac); | |
1547 | break; | |
1548 | ||
6a885fd0 BP |
1549 | case MFF_VLAN_TCI: |
1550 | cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16); | |
1551 | break; | |
1552 | ||
1553 | case MFF_IPV4_SRC: | |
1554 | cls_rule_set_nw_src_masked(rule, value->be32, mask->be32); | |
1555 | break; | |
1556 | ||
1557 | case MFF_IPV4_DST: | |
1558 | cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32); | |
1559 | break; | |
1560 | ||
1561 | case MFF_IPV6_SRC: | |
1562 | cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6); | |
1563 | break; | |
1564 | ||
1565 | case MFF_IPV6_DST: | |
1566 | cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6); | |
1567 | break; | |
1568 | ||
32455024 SH |
1569 | case MFF_IPV6_LABEL: |
1570 | if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) { | |
1571 | mf_set_value(mf, value, rule); | |
1572 | } else { | |
1573 | cls_rule_set_ipv6_label_masked(rule, value->be32, mask->be32); | |
1574 | } | |
1575 | break; | |
1576 | ||
47284b1f AA |
1577 | case MFF_ND_TARGET: |
1578 | cls_rule_set_nd_target_masked(rule, &value->ipv6, &mask->ipv6); | |
1579 | break; | |
1580 | ||
7257b535 | 1581 | case MFF_IP_FRAG: |
eadef313 | 1582 | cls_rule_set_nw_frag_masked(rule, value->u8, mask->u8); |
7257b535 BP |
1583 | break; |
1584 | ||
6a885fd0 BP |
1585 | case MFF_ARP_SPA: |
1586 | cls_rule_set_nw_src_masked(rule, value->be32, mask->be32); | |
1587 | break; | |
1588 | ||
1589 | case MFF_ARP_TPA: | |
1590 | cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32); | |
1591 | break; | |
1592 | ||
73f33563 BP |
1593 | case MFF_TCP_SRC: |
1594 | case MFF_UDP_SRC: | |
1595 | cls_rule_set_tp_src_masked(rule, value->be16, mask->be16); | |
1596 | break; | |
1597 | ||
1598 | case MFF_TCP_DST: | |
1599 | case MFF_UDP_DST: | |
1600 | cls_rule_set_tp_dst_masked(rule, value->be16, mask->be16); | |
1601 | break; | |
1602 | ||
6a885fd0 BP |
1603 | case MFF_N_IDS: |
1604 | default: | |
1605 | NOT_REACHED(); | |
1606 | } | |
1607 | } | |
1608 | ||
816fd533 BP |
1609 | static enum ofperr |
1610 | mf_check__(const struct mf_subfield *sf, const struct flow *flow, | |
1611 | const char *type) | |
6a885fd0 | 1612 | { |
816fd533 BP |
1613 | if (!sf->field) { |
1614 | VLOG_WARN_RL(&rl, "unknown %s field", type); | |
1615 | } else if (!sf->n_bits) { | |
1616 | VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name); | |
1617 | } else if (sf->ofs >= sf->field->n_bits) { | |
1618 | VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s", | |
1619 | sf->ofs, sf->field->n_bits, type, sf->field->name); | |
1620 | } else if (sf->ofs + sf->n_bits > sf->field->n_bits) { | |
1621 | VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width " | |
1622 | "of %s field %s", sf->ofs, sf->n_bits, | |
1623 | sf->field->n_bits, type, sf->field->name); | |
1624 | } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) { | |
1625 | VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites", | |
1626 | type, sf->field->name); | |
6a885fd0 | 1627 | } else { |
816fd533 BP |
1628 | return 0; |
1629 | } | |
6a885fd0 | 1630 | |
816fd533 BP |
1631 | return OFPERR_OFPBAC_BAD_ARGUMENT; |
1632 | } | |
6a885fd0 | 1633 | |
816fd533 BP |
1634 | /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns |
1635 | * 0 if so, otherwise an OpenFlow error code (e.g. as returned by | |
1636 | * ofp_mkerr()). */ | |
1637 | enum ofperr | |
1638 | mf_check_src(const struct mf_subfield *sf, const struct flow *flow) | |
1639 | { | |
1640 | return mf_check__(sf, flow, "source"); | |
1641 | } | |
6a885fd0 | 1642 | |
816fd533 BP |
1643 | /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0 |
1644 | * if so, otherwise an OpenFlow error code (e.g. as returned by | |
1645 | * ofp_mkerr()). */ | |
1646 | enum ofperr | |
1647 | mf_check_dst(const struct mf_subfield *sf, const struct flow *flow) | |
1648 | { | |
1649 | int error = mf_check__(sf, flow, "destination"); | |
1650 | if (!error && !sf->field->writable) { | |
1651 | VLOG_WARN_RL(&rl, "destination field %s is not writable", | |
1652 | sf->field->name); | |
1653 | return OFPERR_OFPBAC_BAD_ARGUMENT; | |
6a885fd0 | 1654 | } |
816fd533 | 1655 | return error; |
6a885fd0 BP |
1656 | } |
1657 | ||
1658 | /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the | |
1659 | * 'value' and 'mask', respectively. */ | |
1660 | void | |
1661 | mf_get(const struct mf_field *mf, const struct cls_rule *rule, | |
1662 | union mf_value *value, union mf_value *mask) | |
1663 | { | |
1664 | mf_get_value(mf, &rule->flow, value); | |
1665 | mf_get_mask(mf, &rule->wc, mask); | |
1666 | } | |
1667 | ||
1668 | /* Assigns a random value for field 'mf' to 'value'. */ | |
1669 | void | |
1670 | mf_random_value(const struct mf_field *mf, union mf_value *value) | |
1671 | { | |
1672 | random_bytes(value, mf->n_bytes); | |
1673 | ||
1674 | switch (mf->id) { | |
1675 | case MFF_TUN_ID: | |
969fc56c | 1676 | case MFF_METADATA: |
6a885fd0 | 1677 | case MFF_IN_PORT: |
0d7e2fe4 | 1678 | CASE_MFF_REGS: |
6a885fd0 BP |
1679 | case MFF_ETH_SRC: |
1680 | case MFF_ETH_DST: | |
1681 | case MFF_ETH_TYPE: | |
1682 | case MFF_VLAN_TCI: | |
1683 | case MFF_IPV4_SRC: | |
1684 | case MFF_IPV4_DST: | |
1685 | case MFF_IPV6_SRC: | |
1686 | case MFF_IPV6_DST: | |
1687 | case MFF_IP_PROTO: | |
a61680c6 | 1688 | case MFF_IP_TTL: |
6a885fd0 BP |
1689 | case MFF_ARP_SPA: |
1690 | case MFF_ARP_TPA: | |
1691 | case MFF_ARP_SHA: | |
1692 | case MFF_ARP_THA: | |
1693 | case MFF_TCP_SRC: | |
1694 | case MFF_TCP_DST: | |
1695 | case MFF_UDP_SRC: | |
1696 | case MFF_UDP_DST: | |
268a95e0 BP |
1697 | case MFF_ICMPV4_TYPE: |
1698 | case MFF_ICMPV4_CODE: | |
1699 | case MFF_ICMPV6_TYPE: | |
1700 | case MFF_ICMPV6_CODE: | |
6a885fd0 BP |
1701 | case MFF_ND_TARGET: |
1702 | case MFF_ND_SLL: | |
1703 | case MFF_ND_TLL: | |
1704 | break; | |
1705 | ||
fa8223b7 JP |
1706 | case MFF_IPV6_LABEL: |
1707 | value->be32 &= ~htonl(IPV6_LABEL_MASK); | |
1708 | break; | |
1709 | ||
530180fd JP |
1710 | case MFF_IP_DSCP: |
1711 | value->u8 &= IP_DSCP_MASK; | |
1712 | break; | |
1713 | ||
1714 | case MFF_IP_ECN: | |
1715 | value->u8 &= IP_ECN_MASK; | |
6a885fd0 BP |
1716 | break; |
1717 | ||
7257b535 | 1718 | case MFF_IP_FRAG: |
eadef313 | 1719 | value->u8 &= FLOW_NW_FRAG_MASK; |
7257b535 BP |
1720 | break; |
1721 | ||
6a885fd0 BP |
1722 | case MFF_ARP_OP: |
1723 | value->be16 &= htons(0xff); | |
1724 | break; | |
1725 | ||
441c57a9 | 1726 | case MFF_DL_VLAN: |
6a885fd0 BP |
1727 | value->be16 &= htons(VLAN_VID_MASK); |
1728 | break; | |
1729 | ||
441c57a9 | 1730 | case MFF_DL_VLAN_PCP: |
6a885fd0 BP |
1731 | value->u8 &= 0x07; |
1732 | break; | |
1733 | ||
1734 | case MFF_N_IDS: | |
1735 | default: | |
1736 | NOT_REACHED(); | |
1737 | } | |
1738 | } | |
1739 | ||
1740 | static char * | |
1741 | mf_from_integer_string(const struct mf_field *mf, const char *s, | |
1742 | uint8_t *valuep, uint8_t *maskp) | |
1743 | { | |
1744 | unsigned long long int integer, mask; | |
1745 | char *tail; | |
1746 | int i; | |
1747 | ||
1748 | errno = 0; | |
1749 | integer = strtoull(s, &tail, 0); | |
1750 | if (errno || (*tail != '\0' && *tail != '/')) { | |
1751 | goto syntax_error; | |
1752 | } | |
1753 | ||
1754 | if (*tail == '/') { | |
1755 | mask = strtoull(tail + 1, &tail, 0); | |
1756 | if (errno || *tail != '\0') { | |
1757 | goto syntax_error; | |
1758 | } | |
1759 | } else { | |
1760 | mask = ULLONG_MAX; | |
1761 | } | |
1762 | ||
1763 | for (i = mf->n_bytes - 1; i >= 0; i--) { | |
1764 | valuep[i] = integer; | |
1765 | maskp[i] = mask; | |
1766 | integer >>= 8; | |
1767 | mask >>= 8; | |
1768 | } | |
1769 | if (integer) { | |
1770 | return xasprintf("%s: value too large for %u-byte field %s", | |
1771 | s, mf->n_bytes, mf->name); | |
1772 | } | |
1773 | return NULL; | |
1774 | ||
1775 | syntax_error: | |
1776 | return xasprintf("%s: bad syntax for %s", s, mf->name); | |
1777 | } | |
1778 | ||
1779 | static char * | |
1780 | mf_from_ethernet_string(const struct mf_field *mf, const char *s, | |
1781 | uint8_t mac[ETH_ADDR_LEN], | |
1782 | uint8_t mask[ETH_ADDR_LEN]) | |
1783 | { | |
1784 | assert(mf->n_bytes == ETH_ADDR_LEN); | |
1785 | ||
1786 | switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT, | |
1787 | ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){ | |
1788 | case ETH_ADDR_SCAN_COUNT * 2: | |
1789 | return NULL; | |
1790 | ||
1791 | case ETH_ADDR_SCAN_COUNT: | |
1792 | memset(mask, 0xff, ETH_ADDR_LEN); | |
1793 | return NULL; | |
1794 | ||
1795 | default: | |
1796 | return xasprintf("%s: invalid Ethernet address", s); | |
1797 | } | |
1798 | } | |
1799 | ||
1800 | static char * | |
1801 | mf_from_ipv4_string(const struct mf_field *mf, const char *s, | |
1802 | ovs_be32 *ip, ovs_be32 *mask) | |
1803 | { | |
1804 | int prefix; | |
1805 | ||
1806 | assert(mf->n_bytes == sizeof *ip); | |
1807 | ||
1808 | if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT, | |
1809 | IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) { | |
1810 | /* OK. */ | |
1811 | } else if (sscanf(s, IP_SCAN_FMT"/%d", | |
1812 | IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) { | |
1813 | if (prefix <= 0 || prefix > 32) { | |
1814 | return xasprintf("%s: network prefix bits not between 1 and " | |
1815 | "32", s); | |
1816 | } else if (prefix == 32) { | |
1817 | *mask = htonl(UINT32_MAX); | |
1818 | } else { | |
1819 | *mask = htonl(((1u << prefix) - 1) << (32 - prefix)); | |
1820 | } | |
1821 | } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) { | |
1822 | *mask = htonl(UINT32_MAX); | |
1823 | } else { | |
1824 | return xasprintf("%s: invalid IP address", s); | |
1825 | } | |
1826 | return NULL; | |
1827 | } | |
1828 | ||
1829 | static char * | |
1830 | mf_from_ipv6_string(const struct mf_field *mf, const char *s, | |
1831 | struct in6_addr *value, struct in6_addr *mask) | |
1832 | { | |
1833 | char *str = xstrdup(s); | |
1834 | char *save_ptr = NULL; | |
1835 | const char *name, *netmask; | |
1836 | int retval; | |
1837 | ||
1838 | assert(mf->n_bytes == sizeof *value); | |
1839 | ||
1840 | name = strtok_r(str, "/", &save_ptr); | |
1841 | retval = name ? lookup_ipv6(name, value) : EINVAL; | |
1842 | if (retval) { | |
1843 | char *err; | |
1844 | ||
1845 | err = xasprintf("%s: could not convert to IPv6 address", str); | |
1846 | free(str); | |
1847 | ||
1848 | return err; | |
1849 | } | |
1850 | ||
1851 | netmask = strtok_r(NULL, "/", &save_ptr); | |
1852 | if (netmask) { | |
ff0b06ee BP |
1853 | if (inet_pton(AF_INET6, netmask, mask) != 1) { |
1854 | int prefix = atoi(netmask); | |
1855 | if (prefix <= 0 || prefix > 128) { | |
1856 | free(str); | |
1857 | return xasprintf("%s: prefix bits not between 1 and 128", s); | |
1858 | } else { | |
1859 | *mask = ipv6_create_mask(prefix); | |
1860 | } | |
6a885fd0 BP |
1861 | } |
1862 | } else { | |
1863 | *mask = in6addr_exact; | |
1864 | } | |
1865 | free(str); | |
1866 | ||
1867 | return NULL; | |
1868 | } | |
1869 | ||
1870 | static char * | |
1871 | mf_from_ofp_port_string(const struct mf_field *mf, const char *s, | |
1872 | ovs_be16 *valuep, ovs_be16 *maskp) | |
1873 | { | |
1874 | uint16_t port; | |
1875 | ||
1876 | assert(mf->n_bytes == sizeof(ovs_be16)); | |
1877 | if (ofputil_port_from_string(s, &port)) { | |
1878 | *valuep = htons(port); | |
1879 | *maskp = htons(UINT16_MAX); | |
1880 | return NULL; | |
1881 | } else { | |
1882 | return mf_from_integer_string(mf, s, | |
1883 | (uint8_t *) valuep, (uint8_t *) maskp); | |
1884 | } | |
1885 | } | |
1886 | ||
7257b535 BP |
1887 | struct frag_handling { |
1888 | const char *name; | |
1889 | uint8_t mask; | |
1890 | uint8_t value; | |
1891 | }; | |
1892 | ||
1893 | static const struct frag_handling all_frags[] = { | |
eadef313 JP |
1894 | #define A FLOW_NW_FRAG_ANY |
1895 | #define L FLOW_NW_FRAG_LATER | |
7257b535 BP |
1896 | /* name mask value */ |
1897 | ||
1898 | { "no", A|L, 0 }, | |
1899 | { "first", A|L, A }, | |
1900 | { "later", A|L, A|L }, | |
1901 | ||
1902 | { "no", A, 0 }, | |
1903 | { "yes", A, A }, | |
1904 | ||
1905 | { "not_later", L, 0 }, | |
1906 | { "later", L, L }, | |
1907 | #undef A | |
1908 | #undef L | |
1909 | }; | |
1910 | ||
1911 | static char * | |
1912 | mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp) | |
1913 | { | |
1914 | const struct frag_handling *h; | |
1915 | ||
1916 | for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) { | |
1917 | if (!strcasecmp(s, h->name)) { | |
1918 | /* We force the upper bits of the mask on to make mf_parse_value() | |
1919 | * happy (otherwise it will never think it's an exact match.) */ | |
eadef313 | 1920 | *maskp = h->mask | ~FLOW_NW_FRAG_MASK; |
7257b535 BP |
1921 | *valuep = h->value; |
1922 | return NULL; | |
1923 | } | |
1924 | } | |
1925 | ||
1926 | return xasprintf("%s: unknown fragment type (valid types are \"no\", " | |
1927 | "\"yes\", \"first\", \"later\", \"not_first\"", s); | |
1928 | } | |
1929 | ||
6a885fd0 BP |
1930 | /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns |
1931 | * NULL if successful, otherwise a malloc()'d string describing the error. */ | |
1932 | char * | |
1933 | mf_parse(const struct mf_field *mf, const char *s, | |
1934 | union mf_value *value, union mf_value *mask) | |
1935 | { | |
1936 | if (!strcasecmp(s, "any") || !strcmp(s, "*")) { | |
1937 | memset(value, 0, mf->n_bytes); | |
1938 | memset(mask, 0, mf->n_bytes); | |
1939 | return NULL; | |
1940 | } | |
1941 | ||
1942 | switch (mf->string) { | |
1943 | case MFS_DECIMAL: | |
1944 | case MFS_HEXADECIMAL: | |
1945 | return mf_from_integer_string(mf, s, | |
1946 | (uint8_t *) value, (uint8_t *) mask); | |
1947 | ||
1948 | case MFS_ETHERNET: | |
1949 | return mf_from_ethernet_string(mf, s, value->mac, mask->mac); | |
1950 | ||
1951 | case MFS_IPV4: | |
1952 | return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32); | |
1953 | ||
1954 | case MFS_IPV6: | |
1955 | return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6); | |
1956 | ||
1957 | case MFS_OFP_PORT: | |
1958 | return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16); | |
7257b535 BP |
1959 | |
1960 | case MFS_FRAG: | |
1961 | return mf_from_frag_string(s, &value->u8, &mask->u8); | |
6a885fd0 BP |
1962 | } |
1963 | NOT_REACHED(); | |
1964 | } | |
1965 | ||
1966 | /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if | |
1967 | * successful, otherwise a malloc()'d string describing the error. */ | |
1968 | char * | |
1969 | mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value) | |
1970 | { | |
1971 | union mf_value mask; | |
1972 | char *error; | |
1973 | ||
1974 | error = mf_parse(mf, s, value, &mask); | |
1975 | if (error) { | |
1976 | return error; | |
1977 | } | |
1978 | ||
1979 | if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) { | |
1980 | return xasprintf("%s: wildcards not allowed here", s); | |
1981 | } | |
1982 | return NULL; | |
1983 | } | |
1984 | ||
1985 | static void | |
1986 | mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep, | |
1987 | const uint8_t *maskp, struct ds *s) | |
1988 | { | |
1989 | unsigned long long int integer; | |
1990 | int i; | |
1991 | ||
1992 | assert(mf->n_bytes <= 8); | |
1993 | ||
1994 | integer = 0; | |
1995 | for (i = 0; i < mf->n_bytes; i++) { | |
1996 | integer = (integer << 8) | valuep[i]; | |
1997 | } | |
1998 | if (mf->string == MFS_HEXADECIMAL) { | |
1999 | ds_put_format(s, "%#llx", integer); | |
2000 | } else { | |
2001 | ds_put_format(s, "%lld", integer); | |
2002 | } | |
2003 | ||
2004 | if (maskp) { | |
2005 | unsigned long long int mask; | |
2006 | ||
2007 | mask = 0; | |
2008 | for (i = 0; i < mf->n_bytes; i++) { | |
2009 | mask = (mask << 8) | maskp[i]; | |
2010 | } | |
2011 | ||
2012 | /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm | |
2013 | * not sure that that a bit-mask written in decimal is ever easier to | |
2014 | * understand than the same bit-mask written in hexadecimal. */ | |
2015 | ds_put_format(s, "/%#llx", mask); | |
2016 | } | |
2017 | } | |
2018 | ||
7257b535 BP |
2019 | static void |
2020 | mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp, | |
2021 | struct ds *s) | |
2022 | { | |
2023 | const struct frag_handling *h; | |
2024 | uint8_t value = *valuep; | |
2025 | uint8_t mask = *maskp; | |
2026 | ||
2027 | value &= mask; | |
eadef313 | 2028 | mask &= FLOW_NW_FRAG_MASK; |
7257b535 BP |
2029 | |
2030 | for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) { | |
2031 | if (value == h->value && mask == h->mask) { | |
2032 | ds_put_cstr(s, h->name); | |
2033 | return; | |
2034 | } | |
2035 | } | |
2036 | ds_put_cstr(s, "<error>"); | |
2037 | } | |
2038 | ||
6a885fd0 BP |
2039 | /* Appends to 's' a string representation of field 'mf' whose value is in |
2040 | * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */ | |
2041 | void | |
2042 | mf_format(const struct mf_field *mf, | |
2043 | const union mf_value *value, const union mf_value *mask, | |
2044 | struct ds *s) | |
2045 | { | |
2046 | if (mask) { | |
2047 | if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) { | |
2048 | ds_put_cstr(s, "ANY"); | |
2049 | return; | |
2050 | } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) { | |
2051 | mask = NULL; | |
2052 | } | |
2053 | } | |
2054 | ||
2055 | switch (mf->string) { | |
2056 | case MFS_OFP_PORT: | |
2057 | if (!mask) { | |
2058 | ofputil_format_port(ntohs(value->be16), s); | |
2059 | break; | |
2060 | } | |
2061 | /* fall through */ | |
2062 | case MFS_DECIMAL: | |
2063 | case MFS_HEXADECIMAL: | |
2064 | mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s); | |
2065 | break; | |
2066 | ||
2067 | case MFS_ETHERNET: | |
3b4d8ad3 | 2068 | eth_format_masked(value->mac, mask->mac, s); |
6a885fd0 BP |
2069 | break; |
2070 | ||
2071 | case MFS_IPV4: | |
2072 | ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX), | |
2073 | s); | |
2074 | break; | |
2075 | ||
2076 | case MFS_IPV6: | |
2077 | print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL); | |
2078 | break; | |
2079 | ||
7257b535 BP |
2080 | case MFS_FRAG: |
2081 | mf_format_frag_string(&value->u8, &mask->u8, s); | |
2082 | break; | |
2083 | ||
6a885fd0 BP |
2084 | default: |
2085 | NOT_REACHED(); | |
2086 | } | |
2087 | } | |
816fd533 | 2088 | \f |
1b35df45 BP |
2089 | /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits' |
2090 | * least-significant bits in 'x'. | |
615660a9 | 2091 | */ |
1b35df45 BP |
2092 | void |
2093 | mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x, | |
2094 | struct cls_rule *rule) | |
2095 | { | |
2096 | const struct mf_field *field = sf->field; | |
2097 | union mf_value value, mask; | |
2098 | ||
2099 | mf_get(field, rule, &value, &mask); | |
2100 | bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits); | |
2101 | bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits); | |
2102 | mf_set(field, &value, &mask, rule); | |
2103 | } | |
2104 | ||
1b35df45 BP |
2105 | /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for |
2106 | * reading 'flow', e.g. as checked by mf_check_src(). */ | |
2107 | void | |
2108 | mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow, | |
2109 | union mf_subvalue *x) | |
2110 | { | |
2111 | union mf_value value; | |
2112 | ||
2113 | mf_get_value(sf->field, flow, &value); | |
2114 | ||
2115 | memset(x, 0, sizeof *x); | |
2116 | bitwise_copy(&value, sf->field->n_bytes, sf->ofs, | |
2117 | x, sizeof *x, 0, | |
2118 | sf->n_bits); | |
2119 | } | |
2120 | ||
816fd533 BP |
2121 | /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading |
2122 | * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or | |
2123 | * less. */ | |
2124 | uint64_t | |
2125 | mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow) | |
2126 | { | |
2127 | union mf_value value; | |
2128 | ||
2129 | mf_get_value(sf->field, flow, &value); | |
2130 | return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits); | |
2131 | } | |
2132 | ||
2133 | /* Formats 'sf' into 's' in a format normally acceptable to | |
2134 | * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if | |
2135 | * sf->field has no NXM name.) */ | |
2136 | void | |
2137 | mf_format_subfield(const struct mf_subfield *sf, struct ds *s) | |
2138 | { | |
2139 | if (!sf->field) { | |
2140 | ds_put_cstr(s, "<unknown>"); | |
2141 | } else if (sf->field->nxm_name) { | |
2142 | ds_put_cstr(s, sf->field->nxm_name); | |
2143 | } else if (sf->field->nxm_header) { | |
2144 | uint32_t header = sf->field->nxm_header; | |
2145 | ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header)); | |
2146 | } else { | |
2147 | ds_put_cstr(s, sf->field->name); | |
2148 | } | |
2149 | ||
33500edd | 2150 | if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) { |
816fd533 BP |
2151 | ds_put_cstr(s, "[]"); |
2152 | } else if (sf->n_bits == 1) { | |
2153 | ds_put_format(s, "[%d]", sf->ofs); | |
2154 | } else { | |
2155 | ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1); | |
2156 | } | |
2157 | } | |
2158 | ||
2159 | static const struct mf_field * | |
2160 | mf_parse_subfield_name(const char *name, int name_len, bool *wild) | |
2161 | { | |
2162 | int i; | |
2163 | ||
2164 | *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2); | |
2165 | if (*wild) { | |
2166 | name_len -= 2; | |
2167 | } | |
2168 | ||
2169 | for (i = 0; i < MFF_N_IDS; i++) { | |
2170 | const struct mf_field *mf = mf_from_id(i); | |
2171 | ||
2172 | if (mf->nxm_name | |
2173 | && !strncmp(mf->nxm_name, name, name_len) | |
2174 | && mf->nxm_name[name_len] == '\0') { | |
2175 | return mf; | |
2176 | } | |
b5e5143b BP |
2177 | if (mf->oxm_name |
2178 | && !strncmp(mf->oxm_name, name, name_len) | |
2179 | && mf->oxm_name[name_len] == '\0') { | |
2180 | return mf; | |
2181 | } | |
816fd533 BP |
2182 | } |
2183 | ||
2184 | return NULL; | |
2185 | } | |
2186 | ||
2187 | /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful, | |
2188 | * returns NULL and advances '*sp' to the first byte following the parsed | |
2189 | * string. On failure, returns a malloc()'d error message, does not modify | |
2190 | * '*sp', and does not properly initialize 'sf'. | |
2191 | * | |
2192 | * The syntax parsed from '*sp' takes the form "header[start..end]" where | |
2193 | * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive) | |
2194 | * bit indexes. "..end" may be omitted to indicate a single bit. "start..end" | |
2195 | * may both be omitted (the [] are still required) to indicate an entire | |
2196 | * field. */ | |
2197 | char * | |
2198 | mf_parse_subfield__(struct mf_subfield *sf, const char **sp) | |
2199 | { | |
2200 | const struct mf_field *field; | |
2201 | const char *name; | |
2202 | int start, end; | |
2203 | const char *s; | |
2204 | int name_len; | |
2205 | bool wild; | |
2206 | ||
2207 | s = *sp; | |
2208 | name = s; | |
2209 | name_len = strcspn(s, "["); | |
2210 | if (s[name_len] != '[') { | |
2211 | return xasprintf("%s: missing [ looking for field name", *sp); | |
2212 | } | |
2213 | ||
2214 | field = mf_parse_subfield_name(name, name_len, &wild); | |
2215 | if (!field) { | |
2216 | return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s); | |
2217 | } | |
2218 | ||
2219 | s += name_len; | |
2220 | if (sscanf(s, "[%d..%d]", &start, &end) == 2) { | |
2221 | /* Nothing to do. */ | |
2222 | } else if (sscanf(s, "[%d]", &start) == 1) { | |
2223 | end = start; | |
2224 | } else if (!strncmp(s, "[]", 2)) { | |
2225 | start = 0; | |
2226 | end = field->n_bits - 1; | |
2227 | } else { | |
2228 | return xasprintf("%s: syntax error expecting [] or [<bit>] or " | |
2229 | "[<start>..<end>]", *sp); | |
2230 | } | |
2231 | s = strchr(s, ']') + 1; | |
2232 | ||
2233 | if (start > end) { | |
2234 | return xasprintf("%s: starting bit %d is after ending bit %d", | |
2235 | *sp, start, end); | |
2236 | } else if (start >= field->n_bits) { | |
2237 | return xasprintf("%s: starting bit %d is not valid because field is " | |
2238 | "only %d bits wide", *sp, start, field->n_bits); | |
2239 | } else if (end >= field->n_bits){ | |
2240 | return xasprintf("%s: ending bit %d is not valid because field is " | |
2241 | "only %d bits wide", *sp, end, field->n_bits); | |
2242 | } | |
2243 | ||
2244 | sf->field = field; | |
2245 | sf->ofs = start; | |
2246 | sf->n_bits = end - start + 1; | |
2247 | ||
2248 | *sp = s; | |
2249 | return NULL; | |
2250 | } | |
2251 | ||
2252 | /* Parses a subfield from the beginning of 's' into 'sf'. Returns the first | |
2253 | * byte in 's' following the parsed string. | |
2254 | * | |
2255 | * Exits with an error message if 's' has incorrect syntax. | |
2256 | * | |
2257 | * The syntax parsed from 's' takes the form "header[start..end]" where | |
2258 | * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive) | |
2259 | * bit indexes. "..end" may be omitted to indicate a single bit. "start..end" | |
2260 | * may both be omitted (the [] are still required) to indicate an entire | |
2261 | * field. */ | |
2262 | const char * | |
2263 | mf_parse_subfield(struct mf_subfield *sf, const char *s) | |
2264 | { | |
2265 | char *msg = mf_parse_subfield__(sf, &s); | |
2266 | if (msg) { | |
2267 | ovs_fatal(0, "%s", msg); | |
2268 | } | |
2269 | return s; | |
2270 | } |