]> git.proxmox.com Git - mirror_ovs.git/blob - lib/flow.h
flow: New function flow_clear_conntrack().
[mirror_ovs.git] / lib / flow.h
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 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 #ifndef FLOW_H
17 #define FLOW_H 1
18
19 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <netinet/icmp6.h>
22 #include <stdbool.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include "bitmap.h"
26 #include "byte-order.h"
27 #include "openvswitch/compiler.h"
28 #include "openflow/nicira-ext.h"
29 #include "openflow/openflow.h"
30 #include "openvswitch/flow.h"
31 #include "packets.h"
32 #include "hash.h"
33 #include "util.h"
34
35 struct dpif_flow_stats;
36 struct ds;
37 struct flow_wildcards;
38 struct minimask;
39 struct dp_packet;
40 struct pkt_metadata;
41 struct match;
42
43 /* Some flow fields are mutually exclusive or only appear within the flow
44 * pipeline. IPv6 headers are bigger than IPv4 and MPLS, and IPv6 ND packets
45 * are bigger than TCP,UDP and IGMP packets. */
46 #define FLOW_MAX_PACKET_U64S (FLOW_U64S \
47 /* Unused in datapath */ - FLOW_U64_SIZE(regs) \
48 - FLOW_U64_SIZE(metadata) \
49 /* L2.5/3 */ - FLOW_U64_SIZE(nw_src) /* incl. nw_dst */ \
50 - FLOW_U64_SIZE(mpls_lse) \
51 /* L4 */ - FLOW_U64_SIZE(tp_src) \
52 )
53
54 extern const uint8_t flow_segment_u64s[];
55
56 /* Configured maximum VLAN headers. */
57 extern int flow_vlan_limit;
58
59 #define FLOW_U64_OFFSET(FIELD) \
60 (offsetof(struct flow, FIELD) / sizeof(uint64_t))
61 #define FLOW_U64_OFFREM(FIELD) \
62 (offsetof(struct flow, FIELD) % sizeof(uint64_t))
63
64 /* Number of 64-bit units spanned by a 'FIELD'. */
65 #define FLOW_U64_SIZE(FIELD) \
66 DIV_ROUND_UP(FLOW_U64_OFFREM(FIELD) + MEMBER_SIZEOF(struct flow, FIELD), \
67 sizeof(uint64_t))
68
69 void flow_extract(struct dp_packet *, struct flow *);
70
71 void flow_zero_wildcards(struct flow *, const struct flow_wildcards *);
72 void flow_unwildcard_tp_ports(const struct flow *, struct flow_wildcards *);
73 void flow_get_metadata(const struct flow *, struct match *flow_metadata);
74
75 const char *ct_state_to_string(uint32_t state);
76 uint32_t ct_state_from_string(const char *);
77 void flow_clear_conntrack(struct flow *);
78
79 char *flow_to_string(const struct flow *);
80 void format_flags(struct ds *ds, const char *(*bit_to_string)(uint32_t),
81 uint32_t flags, char del);
82 void format_flags_masked(struct ds *ds, const char *name,
83 const char *(*bit_to_string)(uint32_t),
84 uint32_t flags, uint32_t mask, uint32_t max_mask);
85 int parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
86 char end, const char *field_name, char **res_string,
87 uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask);
88
89 void flow_format(struct ds *, const struct flow *);
90 void flow_print(FILE *, const struct flow *);
91 static inline int flow_compare_3way(const struct flow *, const struct flow *);
92 static inline bool flow_equal(const struct flow *, const struct flow *);
93 static inline size_t flow_hash(const struct flow *, uint32_t basis);
94
95 void flow_set_dl_vlan(struct flow *, ovs_be16 vid);
96 void flow_fix_vlan_tpid(struct flow *);
97 void flow_set_vlan_vid(struct flow *, ovs_be16 vid);
98 void flow_set_vlan_pcp(struct flow *, uint8_t pcp);
99
100 void flow_limit_vlans(int vlan_limit);
101 int flow_count_vlan_headers(const struct flow *);
102 void flow_skip_common_vlan_headers(const struct flow *a, int *p_an,
103 const struct flow *b, int *p_bn);
104 void flow_pop_vlan(struct flow*, struct flow_wildcards*);
105 void flow_push_vlan_uninit(struct flow*, struct flow_wildcards*);
106
107 int flow_count_mpls_labels(const struct flow *, struct flow_wildcards *);
108 int flow_count_common_mpls_labels(const struct flow *a, int an,
109 const struct flow *b, int bn,
110 struct flow_wildcards *wc);
111 void flow_push_mpls(struct flow *, int n, ovs_be16 mpls_eth_type,
112 struct flow_wildcards *, bool clear_flow_L3);
113 bool flow_pop_mpls(struct flow *, int n, ovs_be16 eth_type,
114 struct flow_wildcards *);
115 void flow_set_mpls_label(struct flow *, int idx, ovs_be32 label);
116 void flow_set_mpls_ttl(struct flow *, int idx, uint8_t ttl);
117 void flow_set_mpls_tc(struct flow *, int idx, uint8_t tc);
118 void flow_set_mpls_bos(struct flow *, int idx, uint8_t stack);
119 void flow_set_mpls_lse(struct flow *, int idx, ovs_be32 lse);
120
121 void flow_compose(struct dp_packet *, const struct flow *);
122
123 bool parse_ipv6_ext_hdrs(const void **datap, size_t *sizep, uint8_t *nw_proto,
124 uint8_t *nw_frag);
125 ovs_be16 parse_dl_type(const struct eth_header *data_, size_t size);
126
127 static inline uint64_t
128 flow_get_xreg(const struct flow *flow, int idx)
129 {
130 return ((uint64_t) flow->regs[idx * 2] << 32) | flow->regs[idx * 2 + 1];
131 }
132
133 static inline void
134 flow_set_xreg(struct flow *flow, int idx, uint64_t value)
135 {
136 flow->regs[idx * 2] = value >> 32;
137 flow->regs[idx * 2 + 1] = value;
138 }
139
140 static inline ovs_u128
141 flow_get_xxreg(const struct flow *flow, int idx)
142 {
143 ovs_u128 value;
144
145 value.u64.hi = (uint64_t) flow->regs[idx * 4] << 32;
146 value.u64.hi |= flow->regs[idx * 4 + 1];
147 value.u64.lo = (uint64_t) flow->regs[idx * 4 + 2] << 32;
148 value.u64.lo |= flow->regs[idx * 4 + 3];
149
150 return value;
151 }
152
153 static inline void
154 flow_set_xxreg(struct flow *flow, int idx, ovs_u128 value)
155 {
156 flow->regs[idx * 4] = value.u64.hi >> 32;
157 flow->regs[idx * 4 + 1] = value.u64.hi;
158 flow->regs[idx * 4 + 2] = value.u64.lo >> 32;
159 flow->regs[idx * 4 + 3] = value.u64.lo;
160 }
161
162 static inline int
163 flow_compare_3way(const struct flow *a, const struct flow *b)
164 {
165 return memcmp(a, b, sizeof *a);
166 }
167
168 static inline bool
169 flow_equal(const struct flow *a, const struct flow *b)
170 {
171 return !flow_compare_3way(a, b);
172 }
173
174 static inline size_t
175 flow_hash(const struct flow *flow, uint32_t basis)
176 {
177 return hash_bytes64((const uint64_t *)flow, sizeof *flow, basis);
178 }
179
180 static inline uint16_t
181 ofp_to_u16(ofp_port_t ofp_port)
182 {
183 return (OVS_FORCE uint16_t) ofp_port;
184 }
185
186 static inline uint32_t
187 odp_to_u32(odp_port_t odp_port)
188 {
189 return (OVS_FORCE uint32_t) odp_port;
190 }
191
192 static inline uint32_t
193 ofp11_to_u32(ofp11_port_t ofp11_port)
194 {
195 return (OVS_FORCE uint32_t) ofp11_port;
196 }
197
198 static inline ofp_port_t
199 u16_to_ofp(uint16_t port)
200 {
201 return OFP_PORT_C(port);
202 }
203
204 static inline odp_port_t
205 u32_to_odp(uint32_t port)
206 {
207 return ODP_PORT_C(port);
208 }
209
210 static inline ofp11_port_t
211 u32_to_ofp11(uint32_t port)
212 {
213 return OFP11_PORT_C(port);
214 }
215
216 static inline uint32_t
217 hash_ofp_port(ofp_port_t ofp_port)
218 {
219 return hash_int(ofp_to_u16(ofp_port), 0);
220 }
221
222 static inline uint32_t
223 hash_odp_port(odp_port_t odp_port)
224 {
225 return hash_int(odp_to_u32(odp_port), 0);
226 }
227 \f
228 uint32_t flow_hash_5tuple(const struct flow *flow, uint32_t basis);
229 uint32_t flow_hash_symmetric_l4(const struct flow *flow, uint32_t basis);
230 uint32_t flow_hash_symmetric_l3l4(const struct flow *flow, uint32_t basis,
231 bool inc_udp_ports );
232
233 /* Initialize a flow with random fields that matter for nx_hash_fields. */
234 void flow_random_hash_fields(struct flow *);
235 void flow_mask_hash_fields(const struct flow *, struct flow_wildcards *,
236 enum nx_hash_fields);
237 uint32_t flow_hash_fields(const struct flow *, enum nx_hash_fields,
238 uint16_t basis);
239 const char *flow_hash_fields_to_str(enum nx_hash_fields);
240 bool flow_hash_fields_valid(enum nx_hash_fields);
241
242 uint32_t flow_hash_in_wildcards(const struct flow *,
243 const struct flow_wildcards *,
244 uint32_t basis);
245
246 bool flow_equal_except(const struct flow *a, const struct flow *b,
247 const struct flow_wildcards *);
248 \f
249 /* Bitmap for flow values. For each 1-bit the corresponding flow value is
250 * explicitly specified, other values are zeroes.
251 *
252 * map_t must be wide enough to hold any member of struct flow. */
253 typedef unsigned long long map_t;
254 #define MAP_T_BITS (sizeof(map_t) * CHAR_BIT)
255 #define MAP_1 (map_t)1
256 #define MAP_MAX TYPE_MAXIMUM(map_t)
257
258 #define MAP_IS_SET(MAP, IDX) ((MAP) & (MAP_1 << (IDX)))
259
260 /* Iterate through the indices of all 1-bits in 'MAP'. */
261 #define MAP_FOR_EACH_INDEX(IDX, MAP) \
262 ULLONG_FOR_EACH_1(IDX, MAP)
263
264 #define FLOWMAP_UNITS DIV_ROUND_UP(FLOW_U64S, MAP_T_BITS)
265
266 struct flowmap {
267 map_t bits[FLOWMAP_UNITS];
268 };
269
270 #define FLOWMAP_EMPTY_INITIALIZER { { 0 } }
271
272 static inline void flowmap_init(struct flowmap *);
273 static inline bool flowmap_equal(struct flowmap, struct flowmap);
274 static inline bool flowmap_is_set(const struct flowmap *, size_t idx);
275 static inline bool flowmap_are_set(const struct flowmap *, size_t idx,
276 unsigned int n_bits);
277 static inline void flowmap_set(struct flowmap *, size_t idx,
278 unsigned int n_bits);
279 static inline void flowmap_clear(struct flowmap *, size_t idx,
280 unsigned int n_bits);
281 static inline struct flowmap flowmap_or(struct flowmap, struct flowmap);
282 static inline struct flowmap flowmap_and(struct flowmap, struct flowmap);
283 static inline bool flowmap_is_empty(struct flowmap);
284 static inline unsigned int flowmap_n_1bits(struct flowmap);
285
286 #define FLOWMAP_HAS_FIELD(FM, FIELD) \
287 flowmap_are_set(FM, FLOW_U64_OFFSET(FIELD), FLOW_U64_SIZE(FIELD))
288
289 #define FLOWMAP_SET(FM, FIELD) \
290 flowmap_set(FM, FLOW_U64_OFFSET(FIELD), FLOW_U64_SIZE(FIELD))
291
292 #define FLOWMAP_SET__(FM, FIELD, SIZE) \
293 flowmap_set(FM, FLOW_U64_OFFSET(FIELD), \
294 DIV_ROUND_UP(SIZE, sizeof(uint64_t)))
295
296 /* XXX: Only works for full 64-bit units. */
297 #define FLOWMAP_CLEAR(FM, FIELD) \
298 BUILD_ASSERT_DECL(FLOW_U64_OFFREM(FIELD) == 0); \
299 BUILD_ASSERT_DECL(sizeof(((struct flow *)0)->FIELD) % sizeof(uint64_t) == 0); \
300 flowmap_clear(FM, FLOW_U64_OFFSET(FIELD), FLOW_U64_SIZE(FIELD))
301
302 /* Iterate through all units in 'FMAP'. */
303 #define FLOWMAP_FOR_EACH_UNIT(UNIT) \
304 for ((UNIT) = 0; (UNIT) < FLOWMAP_UNITS; (UNIT)++)
305
306 /* Iterate through all map units in 'FMAP'. */
307 #define FLOWMAP_FOR_EACH_MAP(MAP, FLOWMAP) \
308 for (size_t unit__ = 0; \
309 unit__ < FLOWMAP_UNITS && ((MAP) = (FLOWMAP).bits[unit__], true); \
310 unit__++)
311
312 struct flowmap_aux;
313 static inline bool flowmap_next_index(struct flowmap_aux *, size_t *idx);
314
315 #define FLOWMAP_AUX_INITIALIZER(FLOWMAP) { .unit = 0, .map = (FLOWMAP) }
316
317 /* Iterate through all struct flow u64 indices specified by 'MAP'. This is a
318 * slower but easier version of the FLOWMAP_FOR_EACH_MAP() &
319 * MAP_FOR_EACH_INDEX() combination. */
320 #define FLOWMAP_FOR_EACH_INDEX(IDX, MAP) \
321 for (struct flowmap_aux aux__ = FLOWMAP_AUX_INITIALIZER(MAP); \
322 flowmap_next_index(&aux__, &(IDX));)
323
324 /* Flowmap inline implementations. */
325 static inline void
326 flowmap_init(struct flowmap *fm)
327 {
328 memset(fm, 0, sizeof *fm);
329 }
330
331 static inline bool
332 flowmap_equal(struct flowmap a, struct flowmap b)
333 {
334 return !memcmp(&a, &b, sizeof a);
335 }
336
337 static inline bool
338 flowmap_is_set(const struct flowmap *fm, size_t idx)
339 {
340 return (fm->bits[idx / MAP_T_BITS] & (MAP_1 << (idx % MAP_T_BITS))) != 0;
341 }
342
343 /* Returns 'true' if any of the 'n_bits' bits starting at 'idx' are set in
344 * 'fm'. 'n_bits' can be at most MAP_T_BITS. */
345 static inline bool
346 flowmap_are_set(const struct flowmap *fm, size_t idx, unsigned int n_bits)
347 {
348 map_t n_bits_mask = (MAP_1 << n_bits) - 1;
349 size_t unit = idx / MAP_T_BITS;
350
351 idx %= MAP_T_BITS;
352
353 if (fm->bits[unit] & (n_bits_mask << idx)) {
354 return true;
355 }
356 /* The seemingly unnecessary bounds check on 'unit' is a workaround for a
357 * false-positive array out of bounds error by GCC 4.9. */
358 if (unit + 1 < FLOWMAP_UNITS && idx + n_bits > MAP_T_BITS) {
359 /* Check the remaining bits from the next unit. */
360 return fm->bits[unit + 1] & (n_bits_mask >> (MAP_T_BITS - idx));
361 }
362 return false;
363 }
364
365 /* Set the 'n_bits' consecutive bits in 'fm', starting at bit 'idx'.
366 * 'n_bits' can be at most MAP_T_BITS. */
367 static inline void
368 flowmap_set(struct flowmap *fm, size_t idx, unsigned int n_bits)
369 {
370 map_t n_bits_mask = (MAP_1 << n_bits) - 1;
371 size_t unit = idx / MAP_T_BITS;
372
373 idx %= MAP_T_BITS;
374
375 fm->bits[unit] |= n_bits_mask << idx;
376 /* The seemingly unnecessary bounds check on 'unit' is a workaround for a
377 * false-positive array out of bounds error by GCC 4.9. */
378 if (unit + 1 < FLOWMAP_UNITS && idx + n_bits > MAP_T_BITS) {
379 /* 'MAP_T_BITS - idx' bits were set on 'unit', set the remaining
380 * bits from the next unit. */
381 fm->bits[unit + 1] |= n_bits_mask >> (MAP_T_BITS - idx);
382 }
383 }
384
385 /* Clears the 'n_bits' consecutive bits in 'fm', starting at bit 'idx'.
386 * 'n_bits' can be at most MAP_T_BITS. */
387 static inline void
388 flowmap_clear(struct flowmap *fm, size_t idx, unsigned int n_bits)
389 {
390 map_t n_bits_mask = (MAP_1 << n_bits) - 1;
391 size_t unit = idx / MAP_T_BITS;
392
393 idx %= MAP_T_BITS;
394
395 fm->bits[unit] &= ~(n_bits_mask << idx);
396 /* The seemingly unnecessary bounds check on 'unit' is a workaround for a
397 * false-positive array out of bounds error by GCC 4.9. */
398 if (unit + 1 < FLOWMAP_UNITS && idx + n_bits > MAP_T_BITS) {
399 /* 'MAP_T_BITS - idx' bits were cleared on 'unit', clear the
400 * remaining bits from the next unit. */
401 fm->bits[unit + 1] &= ~(n_bits_mask >> (MAP_T_BITS - idx));
402 }
403 }
404
405 /* OR the bits in the flowmaps. */
406 static inline struct flowmap
407 flowmap_or(struct flowmap a, struct flowmap b)
408 {
409 struct flowmap map;
410 size_t unit;
411
412 FLOWMAP_FOR_EACH_UNIT (unit) {
413 map.bits[unit] = a.bits[unit] | b.bits[unit];
414 }
415 return map;
416 }
417
418 /* AND the bits in the flowmaps. */
419 static inline struct flowmap
420 flowmap_and(struct flowmap a, struct flowmap b)
421 {
422 struct flowmap map;
423 size_t unit;
424
425 FLOWMAP_FOR_EACH_UNIT (unit) {
426 map.bits[unit] = a.bits[unit] & b.bits[unit];
427 }
428 return map;
429 }
430
431 static inline bool
432 flowmap_is_empty(struct flowmap fm)
433 {
434 map_t map;
435
436 FLOWMAP_FOR_EACH_MAP (map, fm) {
437 if (map) {
438 return false;
439 }
440 }
441 return true;
442 }
443
444 static inline unsigned int
445 flowmap_n_1bits(struct flowmap fm)
446 {
447 unsigned int n_1bits = 0;
448 size_t unit;
449
450 FLOWMAP_FOR_EACH_UNIT (unit) {
451 n_1bits += count_1bits(fm.bits[unit]);
452 }
453 return n_1bits;
454 }
455
456 struct flowmap_aux {
457 size_t unit;
458 struct flowmap map;
459 };
460
461 static inline bool
462 flowmap_next_index(struct flowmap_aux *aux, size_t *idx)
463 {
464 for (;;) {
465 map_t *map = &aux->map.bits[aux->unit];
466 if (*map) {
467 *idx = aux->unit * MAP_T_BITS + raw_ctz(*map);
468 *map = zero_rightmost_1bit(*map);
469 return true;
470 }
471 if (++aux->unit >= FLOWMAP_UNITS) {
472 return false;
473 }
474 }
475 }
476
477 \f
478 /* Compressed flow. */
479
480 /* A sparse representation of a "struct flow".
481 *
482 * A "struct flow" is fairly large and tends to be mostly zeros. Sparse
483 * representation has two advantages. First, it saves memory and, more
484 * importantly, minimizes the number of accessed cache lines. Second, it saves
485 * time when the goal is to iterate over only the nonzero parts of the struct.
486 *
487 * The map member hold one bit for each uint64_t in a "struct flow". Each
488 * 0-bit indicates that the corresponding uint64_t is zero, each 1-bit that it
489 * *may* be nonzero (see below how this applies to minimasks).
490 *
491 * The values indicated by 'map' always follow the miniflow in memory. The
492 * user of the miniflow is responsible for always having enough storage after
493 * the struct miniflow corresponding to the number of 1-bits in maps.
494 *
495 * Elements in values array are allowed to be zero. This is useful for "struct
496 * minimatch", for which ensuring that the miniflow and minimask members have
497 * same maps allows optimization. This allowance applies only to a miniflow
498 * that is not a mask. That is, a minimask may NOT have zero elements in its
499 * values.
500 *
501 * A miniflow is always dynamically allocated so that the maps are followed by
502 * at least as many elements as there are 1-bits in maps. */
503 struct miniflow {
504 struct flowmap map;
505 /* Followed by:
506 * uint64_t values[n];
507 * where 'n' is miniflow_n_values(miniflow). */
508 };
509 BUILD_ASSERT_DECL(sizeof(struct miniflow) % sizeof(uint64_t) == 0);
510
511 #define MINIFLOW_VALUES_SIZE(COUNT) ((COUNT) * sizeof(uint64_t))
512
513 static inline uint64_t *miniflow_values(struct miniflow *mf)
514 {
515 return (uint64_t *)(mf + 1);
516 }
517
518 static inline const uint64_t *miniflow_get_values(const struct miniflow *mf)
519 {
520 return (const uint64_t *)(mf + 1);
521 }
522
523 struct pkt_metadata;
524
525 /* The 'dst' must follow with buffer space for FLOW_U64S 64-bit units.
526 * 'dst->map' is ignored on input and set on output to indicate which fields
527 * were extracted. */
528 void miniflow_extract(struct dp_packet *packet, struct miniflow *dst);
529 void miniflow_map_init(struct miniflow *, const struct flow *);
530 void flow_wc_map(const struct flow *, struct flowmap *);
531 size_t miniflow_alloc(struct miniflow *dsts[], size_t n,
532 const struct miniflow *src);
533 void miniflow_init(struct miniflow *, const struct flow *);
534 void miniflow_clone(struct miniflow *, const struct miniflow *,
535 size_t n_values);
536 struct miniflow * miniflow_create(const struct flow *);
537
538 void miniflow_expand(const struct miniflow *, struct flow *);
539
540 static inline uint64_t flow_u64_value(const struct flow *flow, size_t index)
541 {
542 return ((uint64_t *)flow)[index];
543 }
544
545 static inline uint64_t *flow_u64_lvalue(struct flow *flow, size_t index)
546 {
547 return &((uint64_t *)flow)[index];
548 }
549
550 static inline size_t
551 miniflow_n_values(const struct miniflow *flow)
552 {
553 return flowmap_n_1bits(flow->map);
554 }
555
556 struct flow_for_each_in_maps_aux {
557 const struct flow *flow;
558 struct flowmap_aux map_aux;
559 };
560
561 static inline bool
562 flow_values_get_next_in_maps(struct flow_for_each_in_maps_aux *aux,
563 uint64_t *value)
564 {
565 size_t idx;
566
567 if (flowmap_next_index(&aux->map_aux, &idx)) {
568 *value = flow_u64_value(aux->flow, idx);
569 return true;
570 }
571 return false;
572 }
573
574 /* Iterate through all flow u64 values specified by 'MAPS'. */
575 #define FLOW_FOR_EACH_IN_MAPS(VALUE, FLOW, MAPS) \
576 for (struct flow_for_each_in_maps_aux aux__ \
577 = { (FLOW), FLOWMAP_AUX_INITIALIZER(MAPS) }; \
578 flow_values_get_next_in_maps(&aux__, &(VALUE));)
579
580 struct mf_for_each_in_map_aux {
581 size_t unit; /* Current 64-bit unit of the flowmaps
582 being processed. */
583 struct flowmap fmap; /* Remaining 1-bits corresponding to the
584 64-bit words in ‘values’ */
585 struct flowmap map; /* Remaining 1-bits corresponding to the
586 64-bit words of interest. */
587 const uint64_t *values; /* 64-bit words corresponding to the
588 1-bits in ‘fmap’. */
589 };
590
591 /* Get the data from ‘aux->values’ corresponding to the next lowest 1-bit
592 * in ‘aux->map’, given that ‘aux->values’ points to an array of 64-bit
593 * words corresponding to the 1-bits in ‘aux->fmap’, starting from the
594 * rightmost 1-bit.
595 *
596 * Returns ’true’ if the traversal is incomplete, ‘false’ otherwise.
597 * ‘aux’ is prepared for the next iteration after each call.
598 *
599 * This is used to traverse through, for example, the values in a miniflow
600 * representation of a flow key selected by non-zero 64-bit words in a
601 * corresponding subtable mask. */
602 static inline bool
603 mf_get_next_in_map(struct mf_for_each_in_map_aux *aux,
604 uint64_t *value)
605 {
606 map_t *map, *fmap;
607 map_t rm1bit;
608
609 /* Skip empty map units. */
610 while (OVS_UNLIKELY(!*(map = &aux->map.bits[aux->unit]))) {
611 /* Skip remaining data in the current unit before advancing
612 * to the next. */
613 aux->values += count_1bits(aux->fmap.bits[aux->unit]);
614 if (++aux->unit == FLOWMAP_UNITS) {
615 return false;
616 }
617 }
618
619 rm1bit = rightmost_1bit(*map);
620 *map -= rm1bit;
621 fmap = &aux->fmap.bits[aux->unit];
622
623 /* If the rightmost 1-bit found from the current unit in ‘aux->map’
624 * (‘rm1bit’) is also present in ‘aux->fmap’, store the corresponding
625 * value from ‘aux->values’ to ‘*value', otherwise store 0. */
626 if (OVS_LIKELY(*fmap & rm1bit)) {
627 /* Skip all 64-bit words in ‘values’ preceding the one corresponding
628 * to ‘rm1bit’. */
629 map_t trash = *fmap & (rm1bit - 1);
630
631 /* Avoid resetting 'fmap' and calling count_1bits() when trash is
632 * zero. */
633 if (trash) {
634 *fmap -= trash;
635 aux->values += count_1bits(trash);
636 }
637
638 *value = *aux->values;
639 } else {
640 *value = 0;
641 }
642 return true;
643 }
644
645 /* Iterate through miniflow u64 values specified by 'FLOWMAP'. */
646 #define MINIFLOW_FOR_EACH_IN_FLOWMAP(VALUE, FLOW, FLOWMAP) \
647 for (struct mf_for_each_in_map_aux aux__ = \
648 { 0, (FLOW)->map, (FLOWMAP), miniflow_get_values(FLOW) }; \
649 mf_get_next_in_map(&aux__, &(VALUE));)
650
651 /* This can be used when it is known that 'idx' is set in 'map'. */
652 static inline const uint64_t *
653 miniflow_values_get__(const uint64_t *values, map_t map, size_t idx)
654 {
655 return values + count_1bits(map & ((MAP_1 << idx) - 1));
656 }
657
658 /* This can be used when it is known that 'u64_idx' is set in
659 * the map of 'mf'. */
660 static inline const uint64_t *
661 miniflow_get__(const struct miniflow *mf, size_t idx)
662 {
663 const uint64_t *values = miniflow_get_values(mf);
664 const map_t *map = mf->map.bits;
665
666 while (idx >= MAP_T_BITS) {
667 idx -= MAP_T_BITS;
668 values += count_1bits(*map++);
669 }
670 return miniflow_values_get__(values, *map, idx);
671 }
672
673 #define MINIFLOW_IN_MAP(MF, IDX) flowmap_is_set(&(MF)->map, IDX)
674
675 /* Get the value of the struct flow 'FIELD' as up to 8 byte wide integer type
676 * 'TYPE' from miniflow 'MF'. */
677 #define MINIFLOW_GET_TYPE(MF, TYPE, FIELD) \
678 (MINIFLOW_IN_MAP(MF, FLOW_U64_OFFSET(FIELD)) \
679 ? ((OVS_FORCE const TYPE *)miniflow_get__(MF, FLOW_U64_OFFSET(FIELD))) \
680 [FLOW_U64_OFFREM(FIELD) / sizeof(TYPE)] \
681 : 0)
682
683 #define MINIFLOW_GET_U128(FLOW, FIELD) \
684 (ovs_u128) { .u64 = { \
685 (MINIFLOW_IN_MAP(FLOW, FLOW_U64_OFFSET(FIELD)) ? \
686 *miniflow_get__(FLOW, FLOW_U64_OFFSET(FIELD)) : 0), \
687 (MINIFLOW_IN_MAP(FLOW, FLOW_U64_OFFSET(FIELD) + 1) ? \
688 *miniflow_get__(FLOW, FLOW_U64_OFFSET(FIELD) + 1) : 0) } }
689
690 #define MINIFLOW_GET_U8(FLOW, FIELD) \
691 MINIFLOW_GET_TYPE(FLOW, uint8_t, FIELD)
692 #define MINIFLOW_GET_U16(FLOW, FIELD) \
693 MINIFLOW_GET_TYPE(FLOW, uint16_t, FIELD)
694 #define MINIFLOW_GET_BE16(FLOW, FIELD) \
695 MINIFLOW_GET_TYPE(FLOW, ovs_be16, FIELD)
696 #define MINIFLOW_GET_U32(FLOW, FIELD) \
697 MINIFLOW_GET_TYPE(FLOW, uint32_t, FIELD)
698 #define MINIFLOW_GET_BE32(FLOW, FIELD) \
699 MINIFLOW_GET_TYPE(FLOW, ovs_be32, FIELD)
700 #define MINIFLOW_GET_U64(FLOW, FIELD) \
701 MINIFLOW_GET_TYPE(FLOW, uint64_t, FIELD)
702 #define MINIFLOW_GET_BE64(FLOW, FIELD) \
703 MINIFLOW_GET_TYPE(FLOW, ovs_be64, FIELD)
704
705 static inline uint64_t miniflow_get(const struct miniflow *,
706 unsigned int u64_ofs);
707 static inline uint32_t miniflow_get_u32(const struct miniflow *,
708 unsigned int u32_ofs);
709 static inline ovs_be32 miniflow_get_be32(const struct miniflow *,
710 unsigned int be32_ofs);
711 static inline uint16_t miniflow_get_vid(const struct miniflow *, size_t);
712 static inline uint16_t miniflow_get_tcp_flags(const struct miniflow *);
713 static inline ovs_be64 miniflow_get_metadata(const struct miniflow *);
714
715 bool miniflow_equal(const struct miniflow *a, const struct miniflow *b);
716 bool miniflow_equal_in_minimask(const struct miniflow *a,
717 const struct miniflow *b,
718 const struct minimask *);
719 bool miniflow_equal_flow_in_minimask(const struct miniflow *a,
720 const struct flow *b,
721 const struct minimask *);
722 uint32_t miniflow_hash_5tuple(const struct miniflow *flow, uint32_t basis);
723
724 \f
725 /* Compressed flow wildcards. */
726
727 /* A sparse representation of a "struct flow_wildcards".
728 *
729 * See the large comment on struct miniflow for details.
730 *
731 * Note: While miniflow can have zero data for a 1-bit in the map,
732 * a minimask may not! We rely on this in the implementation. */
733 struct minimask {
734 struct miniflow masks;
735 };
736
737 void minimask_init(struct minimask *, const struct flow_wildcards *);
738 struct minimask * minimask_create(const struct flow_wildcards *);
739 void minimask_combine(struct minimask *dst,
740 const struct minimask *a, const struct minimask *b,
741 uint64_t storage[FLOW_U64S]);
742
743 void minimask_expand(const struct minimask *, struct flow_wildcards *);
744
745 static inline uint32_t minimask_get_u32(const struct minimask *,
746 unsigned int u32_ofs);
747 static inline ovs_be32 minimask_get_be32(const struct minimask *,
748 unsigned int be32_ofs);
749 static inline uint16_t minimask_get_vid_mask(const struct minimask *, size_t);
750 static inline ovs_be64 minimask_get_metadata_mask(const struct minimask *);
751
752 bool minimask_equal(const struct minimask *a, const struct minimask *b);
753 bool minimask_has_extra(const struct minimask *, const struct minimask *);
754
755 \f
756 /* Returns true if 'mask' matches every packet, false if 'mask' fixes any bits
757 * or fields. */
758 static inline bool
759 minimask_is_catchall(const struct minimask *mask)
760 {
761 /* For every 1-bit in mask's map, the corresponding value is non-zero,
762 * so the only way the mask can not fix any bits or fields is for the
763 * map the be zero. */
764 return flowmap_is_empty(mask->masks.map);
765 }
766
767 /* Returns the uint64_t that would be at byte offset '8 * u64_ofs' if 'flow'
768 * were expanded into a "struct flow". */
769 static inline uint64_t miniflow_get(const struct miniflow *flow,
770 unsigned int u64_ofs)
771 {
772 return MINIFLOW_IN_MAP(flow, u64_ofs) ? *miniflow_get__(flow, u64_ofs) : 0;
773 }
774
775 static inline uint32_t miniflow_get_u32(const struct miniflow *flow,
776 unsigned int u32_ofs)
777 {
778 uint64_t value = miniflow_get(flow, u32_ofs / 2);
779
780 #if WORDS_BIGENDIAN
781 return (u32_ofs & 1) ? value : value >> 32;
782 #else
783 return (u32_ofs & 1) ? value >> 32 : value;
784 #endif
785 }
786
787 static inline ovs_be32 miniflow_get_be32(const struct miniflow *flow,
788 unsigned int be32_ofs)
789 {
790 return (OVS_FORCE ovs_be32)miniflow_get_u32(flow, be32_ofs);
791 }
792
793 /* Returns the VID within the vlan_tci member of the "struct flow" represented
794 * by 'flow'. */
795 static inline uint16_t
796 miniflow_get_vid(const struct miniflow *flow, size_t n)
797 {
798 if (n < FLOW_MAX_VLAN_HEADERS) {
799 union flow_vlan_hdr hdr = {
800 .qtag = MINIFLOW_GET_BE32(flow, vlans[n])
801 };
802 return vlan_tci_to_vid(hdr.tci);
803 }
804 return 0;
805 }
806
807 /* Returns the uint32_t that would be at byte offset '4 * u32_ofs' if 'mask'
808 * were expanded into a "struct flow_wildcards". */
809 static inline uint32_t
810 minimask_get_u32(const struct minimask *mask, unsigned int u32_ofs)
811 {
812 return miniflow_get_u32(&mask->masks, u32_ofs);
813 }
814
815 static inline ovs_be32
816 minimask_get_be32(const struct minimask *mask, unsigned int be32_ofs)
817 {
818 return (OVS_FORCE ovs_be32)minimask_get_u32(mask, be32_ofs);
819 }
820
821 /* Returns the VID mask within the vlan_tci member of the "struct
822 * flow_wildcards" represented by 'mask'. */
823 static inline uint16_t
824 minimask_get_vid_mask(const struct minimask *mask, size_t n)
825 {
826 return miniflow_get_vid(&mask->masks, n);
827 }
828
829 /* Returns the value of the "tcp_flags" field in 'flow'. */
830 static inline uint16_t
831 miniflow_get_tcp_flags(const struct miniflow *flow)
832 {
833 return ntohs(MINIFLOW_GET_BE16(flow, tcp_flags));
834 }
835
836 /* Returns the value of the OpenFlow 1.1+ "metadata" field in 'flow'. */
837 static inline ovs_be64
838 miniflow_get_metadata(const struct miniflow *flow)
839 {
840 return MINIFLOW_GET_BE64(flow, metadata);
841 }
842
843 /* Returns the mask for the OpenFlow 1.1+ "metadata" field in 'mask'.
844 *
845 * The return value is all-1-bits if 'mask' matches on the whole value of the
846 * metadata field, all-0-bits if 'mask' entirely wildcards the metadata field,
847 * or some other value if the metadata field is partially matched, partially
848 * wildcarded. */
849 static inline ovs_be64
850 minimask_get_metadata_mask(const struct minimask *mask)
851 {
852 return MINIFLOW_GET_BE64(&mask->masks, metadata);
853 }
854
855 /* Perform a bitwise OR of miniflow 'src' flow data specified in 'subset' with
856 * the equivalent fields in 'dst', storing the result in 'dst'. 'subset' must
857 * be a subset of 'src's map. */
858 static inline void
859 flow_union_with_miniflow_subset(struct flow *dst, const struct miniflow *src,
860 struct flowmap subset)
861 {
862 uint64_t *dst_u64 = (uint64_t *) dst;
863 const uint64_t *p = miniflow_get_values(src);
864 map_t map;
865
866 FLOWMAP_FOR_EACH_MAP (map, subset) {
867 size_t idx;
868
869 MAP_FOR_EACH_INDEX(idx, map) {
870 dst_u64[idx] |= *p++;
871 }
872 dst_u64 += MAP_T_BITS;
873 }
874 }
875
876 /* Perform a bitwise OR of miniflow 'src' flow data with the equivalent
877 * fields in 'dst', storing the result in 'dst'. */
878 static inline void
879 flow_union_with_miniflow(struct flow *dst, const struct miniflow *src)
880 {
881 flow_union_with_miniflow_subset(dst, src, src->map);
882 }
883
884 static inline bool is_ct_valid(const struct flow *flow,
885 const struct flow_wildcards *mask,
886 struct flow_wildcards *wc)
887 {
888 /* Matches are checked with 'mask' and without 'wc'. */
889 if (mask && !wc) {
890 /* Must match at least one of the bits that implies a valid
891 * conntrack entry, or an explicit not-invalid. */
892 return flow->ct_state & (CS_NEW | CS_ESTABLISHED | CS_RELATED
893 | CS_REPLY_DIR | CS_SRC_NAT | CS_DST_NAT)
894 || (flow->ct_state & CS_TRACKED
895 && mask->masks.ct_state & CS_INVALID
896 && !(flow->ct_state & CS_INVALID));
897 }
898 /* Else we are checking a fully extracted flow, where valid CT state always
899 * has either 'new', 'established', or 'reply_dir' bit set. */
900 #define CS_VALID_MASK (CS_NEW | CS_ESTABLISHED | CS_REPLY_DIR)
901 if (wc) {
902 wc->masks.ct_state |= CS_VALID_MASK;
903 }
904 return flow->ct_state & CS_VALID_MASK;
905 }
906
907 static inline void
908 pkt_metadata_from_flow(struct pkt_metadata *md, const struct flow *flow)
909 {
910 /* Update this function whenever struct flow changes. */
911 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 38);
912
913 md->recirc_id = flow->recirc_id;
914 md->dp_hash = flow->dp_hash;
915 flow_tnl_copy__(&md->tunnel, &flow->tunnel);
916 md->skb_priority = flow->skb_priority;
917 md->pkt_mark = flow->pkt_mark;
918 md->in_port = flow->in_port;
919 md->ct_state = flow->ct_state;
920 md->ct_zone = flow->ct_zone;
921 md->ct_mark = flow->ct_mark;
922 md->ct_label = flow->ct_label;
923
924 md->ct_orig_tuple_ipv6 = false;
925 if (is_ct_valid(flow, NULL, NULL)) {
926 if (flow->dl_type == htons(ETH_TYPE_IP)) {
927 md->ct_orig_tuple.ipv4 = (struct ovs_key_ct_tuple_ipv4) {
928 flow->ct_nw_src,
929 flow->ct_nw_dst,
930 flow->ct_tp_src,
931 flow->ct_tp_dst,
932 flow->ct_nw_proto,
933 };
934 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
935 md->ct_orig_tuple_ipv6 = true;
936 md->ct_orig_tuple.ipv6 = (struct ovs_key_ct_tuple_ipv6) {
937 flow->ct_ipv6_src,
938 flow->ct_ipv6_dst,
939 flow->ct_tp_src,
940 flow->ct_tp_dst,
941 flow->ct_nw_proto,
942 };
943 }
944 } else {
945 memset(&md->ct_orig_tuple, 0, sizeof md->ct_orig_tuple);
946 }
947 }
948
949 /* Often, during translation we need to read a value from a flow('FLOW') and
950 * unwildcard the corresponding bits in the wildcards('WC'). This macro makes
951 * it easier to do that. */
952
953 #define FLOW_WC_GET_AND_MASK_WC(FLOW, WC, FIELD) \
954 (((WC) ? WC_MASK_FIELD(WC, FIELD) : NULL), ((FLOW)->FIELD))
955
956 static inline bool is_vlan(const struct flow *flow,
957 struct flow_wildcards *wc)
958 {
959 if (wc) {
960 WC_MASK_FIELD_MASK(wc, vlans[0].tci, htons(VLAN_CFI));
961 }
962 return (flow->vlans[0].tci & htons(VLAN_CFI)) != 0;
963 }
964
965 static inline bool is_ip_any(const struct flow *flow)
966 {
967 return dl_type_is_ip_any(flow->dl_type);
968 }
969
970 static inline bool is_ip_proto(const struct flow *flow, uint8_t ip_proto,
971 struct flow_wildcards *wc)
972 {
973 if (is_ip_any(flow)) {
974 if (wc) {
975 WC_MASK_FIELD(wc, nw_proto);
976 }
977 return flow->nw_proto == ip_proto;
978 }
979 return false;
980 }
981
982 static inline bool is_tcp(const struct flow *flow,
983 struct flow_wildcards *wc)
984 {
985 return is_ip_proto(flow, IPPROTO_TCP, wc);
986 }
987
988 static inline bool is_udp(const struct flow *flow,
989 struct flow_wildcards *wc)
990 {
991 return is_ip_proto(flow, IPPROTO_UDP, wc);
992 }
993
994 static inline bool is_sctp(const struct flow *flow,
995 struct flow_wildcards *wc)
996 {
997 return is_ip_proto(flow, IPPROTO_SCTP, wc);
998 }
999
1000 static inline bool is_icmpv4(const struct flow *flow,
1001 struct flow_wildcards *wc)
1002 {
1003 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1004 if (wc) {
1005 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1006 }
1007 return flow->nw_proto == IPPROTO_ICMP;
1008 }
1009 return false;
1010 }
1011
1012 static inline bool is_icmpv6(const struct flow *flow,
1013 struct flow_wildcards *wc)
1014 {
1015 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1016 if (wc) {
1017 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1018 }
1019 return flow->nw_proto == IPPROTO_ICMPV6;
1020 }
1021 return false;
1022 }
1023
1024 static inline bool is_nd(const struct flow *flow,
1025 struct flow_wildcards *wc)
1026 {
1027 if (is_icmpv6(flow, wc)) {
1028 if (wc) {
1029 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
1030 }
1031 if (flow->tp_dst != htons(0)) {
1032 return false;
1033 }
1034
1035 if (wc) {
1036 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
1037 }
1038 return (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1039 flow->tp_src == htons(ND_NEIGHBOR_ADVERT));
1040 }
1041 return false;
1042 }
1043
1044 static inline bool is_igmp(const struct flow *flow, struct flow_wildcards *wc)
1045 {
1046 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1047 if (wc) {
1048 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1049 }
1050 return flow->nw_proto == IPPROTO_IGMP;
1051 }
1052 return false;
1053 }
1054
1055 static inline bool is_mld(const struct flow *flow,
1056 struct flow_wildcards *wc)
1057 {
1058 if (is_icmpv6(flow, wc)) {
1059 if (wc) {
1060 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
1061 }
1062 return (flow->tp_src == htons(MLD_QUERY)
1063 || flow->tp_src == htons(MLD_REPORT)
1064 || flow->tp_src == htons(MLD_DONE)
1065 || flow->tp_src == htons(MLD2_REPORT));
1066 }
1067 return false;
1068 }
1069
1070 static inline bool is_mld_query(const struct flow *flow,
1071 struct flow_wildcards *wc)
1072 {
1073 if (is_icmpv6(flow, wc)) {
1074 if (wc) {
1075 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
1076 }
1077 return flow->tp_src == htons(MLD_QUERY);
1078 }
1079 return false;
1080 }
1081
1082 static inline bool is_mld_report(const struct flow *flow,
1083 struct flow_wildcards *wc)
1084 {
1085 return is_mld(flow, wc) && !is_mld_query(flow, wc);
1086 }
1087
1088 static inline bool is_stp(const struct flow *flow)
1089 {
1090 return (eth_addr_equals(flow->dl_dst, eth_addr_stp)
1091 && flow->dl_type == htons(FLOW_DL_TYPE_NONE));
1092 }
1093
1094 #endif /* flow.h */