2 * Copyright (c) 2016 Intel Corporation.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #ifndef _PORT_BITMAP_H_
17 #define _PORT_BITMAP_H_
24 * Simple implementation of bitmap for all possible L4 ports [0-UINT16_MAX].
27 #define MAX_PORT_NUM (UINT16_MAX + 1)
29 #define PORT_BLK(p) ((p) / (sizeof(uint32_t) * CHAR_BIT))
30 #define PORT_IDX(p) ((p) % (sizeof(uint32_t) * CHAR_BIT))
32 #define MAX_PORT_BLK PORT_BLK(MAX_PORT_NUM)
35 uint32_t nb_set; /* number of bits set. */
36 uint32_t blk; /* last block with free entry. */
37 uint32_t bm[MAX_PORT_BLK];
41 tle_pbm_init(struct tle_pbm *pbm, uint32_t blk)
49 tle_pbm_set(struct tle_pbm *pbm, uint16_t port)
54 b = 1 << PORT_IDX(port);
57 pbm->nb_set += (v & b) == 0;
61 tle_pbm_clear(struct tle_pbm *pbm, uint16_t port)
66 b = 1 << PORT_IDX(port);
69 pbm->nb_set -= (v & b) != 0;
73 static inline uint32_t
74 tle_pbm_check(const struct tle_pbm *pbm, uint16_t port)
79 v = pbm->bm[i] >> PORT_IDX(port);
83 static inline uint16_t
84 tle_pbm_find_range(struct tle_pbm *pbm, uint32_t start_blk, uint32_t end_blk)
89 if (pbm->nb_set == MAX_PORT_NUM)
93 for (i = start_blk; i != end_blk; i++) {
94 i %= RTE_DIM(pbm->bm);
96 if (v != UINT32_MAX) {
97 for (p = i * (sizeof(pbm->bm[0]) * CHAR_BIT);
98 (v & 1) != 0; v >>= 1, p++)
112 #endif /* _PORT_BITMAP_H_ */