Fix minor compilation issue on recent gcc compilers
[samplevnf.git] / VNFs / DPPD-PROX / prox_cfg.c
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
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 <string.h>
18 #include <stdio.h>
19
20 #include "prox_cfg.h"
21
22 #define CM_N_BITS (sizeof(prox_cfg.core_mask[0]) * 8)
23 #define CM_ALL_N_BITS (sizeof(prox_cfg.core_mask) * 8)
24
25 struct prox_cfg prox_cfg = {
26         .update_interval_str = "1"
27 };
28
29 static int prox_cm_isset(const uint32_t lcore_id)
30 {
31         uint64_t cm;
32         uint32_t cm_idx;
33
34         if (lcore_id > CM_ALL_N_BITS)
35                 return -1;
36
37         cm = __UINT64_C(1) << (lcore_id % CM_N_BITS);
38         cm_idx = PROX_CM_DIM - 1 - lcore_id / CM_N_BITS;
39         return !!(prox_cfg.core_mask[cm_idx] & cm);
40 }
41
42 int prox_core_active(const uint32_t lcore_id, const int with_master)
43 {
44         int ret;
45
46         ret = prox_cm_isset(lcore_id);
47         if (ret < 0)
48                 return 0;
49
50         if (with_master)
51                 return ret || lcore_id == prox_cfg.master;
52         else
53                 return ret && lcore_id != prox_cfg.master;
54 }
55
56 int prox_core_next(uint32_t* lcore_id, const int with_master)
57 {
58         for (uint32_t i = *lcore_id + 1; i < CM_ALL_N_BITS; ++i) {
59                 if (prox_core_active(i, with_master)) {
60                         *lcore_id = i;
61                         return 0;
62                 }
63         }
64         return -1;
65 }
66
67 int prox_core_to_hex(char *dst, const size_t size, const int with_master)
68 {
69         uint64_t cm;
70         uint32_t cm_len;
71         uint32_t cm_first = 0;
72         uint32_t master = prox_cfg.master;
73
74         /* Minimum size of the string has to big enough to hold the
75            bitmask in hex (including the prefix "0x"). */
76         if (size < PROX_CM_STR_LEN)
77                 return 0;
78
79         snprintf(dst, size, "0x");
80         for (uint32_t i = 0; i < PROX_CM_DIM; ++i, cm_first = i) {
81                 if ((with_master && ((CM_ALL_N_BITS - 1 - master) / CM_N_BITS == i * CM_N_BITS)) ||
82                     prox_cfg.core_mask[i]) {
83                         break;
84                 }
85         }
86
87         for (uint32_t i = cm_first; i < PROX_CM_DIM; ++i) {
88                 cm = prox_cfg.core_mask[i];
89                 if (with_master && ((CM_ALL_N_BITS - 1 - master) / CM_N_BITS == i)) {
90                         cm |= (__UINT64_C(1) << (master % CM_N_BITS));
91                 }
92
93                 snprintf(dst + strlen(dst), size - strlen(dst), i == cm_first? "%lx" : "%016lx", cm);
94         }
95
96         return 0;
97 }
98
99 int prox_core_to_str(char *dst, const size_t size, const int with_master)
100 {
101         uint32_t lcore_id = -1;
102         uint32_t first = 1;
103
104         *dst = 0;
105         lcore_id - 1;
106         while (prox_core_next(&lcore_id, with_master) == 0) {
107                 /* Stop printing to string if there is not engough
108                    space left. Assume that adding 1 core to the string
109                    will take at most 5 + 1 bytes implying that
110                    lcore_id < 999. Check if ther is space for another
111                    6 bytes to add an elipsis */
112                 if (12 + strlen(dst) > size) {
113                         if (6 + strlen(dst) > size) {
114                                 snprintf(dst + strlen(dst), size - strlen(dst), ", ...");
115                                 return 0;
116                         }
117                         return -1;
118                 }
119
120                 snprintf(dst + strlen(dst), size - strlen(dst), first? "%u" : ", %u", lcore_id);
121                 first = 0;
122         }
123
124         return 0;
125 }
126
127 void prox_core_clr(void)
128 {
129         memset(prox_cfg.core_mask, 0, sizeof(prox_cfg.core_mask));
130 }
131
132 int prox_core_set_active(const uint32_t lcore_id)
133 {
134         uint32_t cm_idx;
135         uint64_t cm;
136
137         if (lcore_id > CM_ALL_N_BITS)
138                 return -1;
139
140         cm = __UINT64_C(1) << (lcore_id % CM_N_BITS);
141         cm_idx = PROX_CM_DIM - 1 - lcore_id / CM_N_BITS;
142         prox_cfg.core_mask[cm_idx] |= cm;
143
144         return 0;
145 }