2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #define PORT_ARRAY_H 1
21 #include "openflow/openflow.h"
24 static inline uint16_t
25 port_array_extract_bits__(uint16_t data, int start, int count)
27 return (data >> start) & ((1u << count) - 1);
30 /* Level 1: most-significant bits. */
31 #define PORT_ARRAY_L1_BITS 5
32 #define PORT_ARRAY_L1_SHIFT (PORT_ARRAY_L3_BITS + PORT_ARRAY_L2_BITS)
33 #define PORT_ARRAY_L1_SIZE (1u << PORT_ARRAY_L1_BITS)
34 #define PORT_ARRAY_L1(IDX) \
35 port_array_extract_bits__(IDX, PORT_ARRAY_L1_SHIFT, PORT_ARRAY_L1_BITS)
37 /* Level 2: middle bits. */
38 #define PORT_ARRAY_L2_BITS 5
39 #define PORT_ARRAY_L2_SHIFT PORT_ARRAY_L3_BITS
40 #define PORT_ARRAY_L2_SIZE (1u << PORT_ARRAY_L2_BITS)
41 #define PORT_ARRAY_L2(IDX) \
42 port_array_extract_bits__(IDX, PORT_ARRAY_L2_SHIFT, PORT_ARRAY_L2_BITS)
44 /* Level 3: least-significant bits. */
45 #define PORT_ARRAY_L3_BITS 6
46 #define PORT_ARRAY_L3_SHIFT 0
47 #define PORT_ARRAY_L3_SIZE (1u << PORT_ARRAY_L3_BITS)
48 #define PORT_ARRAY_L3(IDX) \
49 port_array_extract_bits__(IDX, PORT_ARRAY_L3_SHIFT, PORT_ARRAY_L3_BITS)
51 #define PORT_ARRAY_SIZE (1u << (PORT_ARRAY_L1_BITS \
52 + PORT_ARRAY_L2_BITS \
53 + PORT_ARRAY_L3_BITS))
55 BUILD_ASSERT_DECL(PORT_ARRAY_SIZE > 0xffff);
57 /* A "sparse array" of up to 65536 elements (numbered 0...65535), implemented
58 * as a 3-level trie. Most efficient when the elements are clustered
61 struct port_array_l2 *l1[1u << PORT_ARRAY_L1_BITS];
64 struct port_array_l2 {
65 struct port_array_l3 *l2[1u << PORT_ARRAY_L2_BITS];
68 struct port_array_l3 {
69 void *l3[1u << PORT_ARRAY_L3_BITS];
72 /* Returns the value of the element numbered 'idx' in 'pa', or a null pointer
73 * if no element numbered 'idx' has been set. */
75 port_array_get(const struct port_array *pa, uint16_t idx)
77 unsigned int l1_idx = PORT_ARRAY_L1(idx);
78 unsigned int l2_idx = PORT_ARRAY_L2(idx);
79 unsigned int l3_idx = PORT_ARRAY_L3(idx);
80 return pa->l1[l1_idx]->l2[l2_idx]->l3[l3_idx];
83 void port_array_init(struct port_array *);
84 void port_array_destroy(struct port_array *);
85 void port_array_clear(struct port_array *);
86 void port_array_set(struct port_array *, uint16_t idx, void *);
87 void port_array_delete(struct port_array *, uint16_t idx);
88 void *port_array_first(const struct port_array *, unsigned int *);
89 void *port_array_next(const struct port_array *, unsigned int *);
90 unsigned int port_array_count(const struct port_array *);
92 #define PORT_ARRAY_FOR_EACH(DATA, ARRAY, PORT_NO) \
93 for ((DATA) = port_array_first(ARRAY, &(PORT_NO)); (DATA) != NULL; \
94 (DATA) = port_array_next(ARRAY, &(PORT_NO)))
96 #endif /* port-array.h */