summaryrefslogtreecommitdiffstats
path: root/VNFs/DPPD-PROX/prox_cfg.c
blob: a2cf7953a40656155cdcaf33f1ee1e6a855e408d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
// Copyright (c) 2010-2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/

#include <string.h>
#include <stdio.h>

#include "prox_cfg.h"

#define CM_N_BITS (sizeof(prox_cfg.core_mask[0]) * 8)
#define CM_ALL_N_BITS (sizeof(prox_cfg.core_mask) * 8)

struct prox_cfg prox_cfg = {
	.update_interval_str = "1"
};

static int prox_cm_isset(const uint32_t lcore_id)
{
	uint64_t cm;
	uint32_t cm_idx;

	if (lcore_id > CM_ALL_N_BITS)
		return -1;

	cm = __UINT64_C(1) << (lcore_id % CM_N_BITS);
	cm_idx = PROX_CM_DIM - 1 - lcore_id / CM_N_BITS;
	return !!(prox_cfg.core_mask[cm_idx] & cm);
}

int prox_core_active(const uint32_t lcore_id, const int with_master)
{
	int ret;

	ret = prox_cm_isset(lcore_id);
	if (ret < 0)
		return 0;

	if (with_master)
		return ret || lcore_id == prox_cfg.master;
	else
		return ret && lcore_id != prox_cfg.master;
}

int prox_core_next(uint32_t* lcore_id, const int with_master)
{
	for (uint32_t i = *lcore_id + 1; i < CM_ALL_N_BITS; ++i) {
		if (prox_core_active(i, with_master)) {
			*lcore_id = i;
			return 0;
		}
	}
	return -1;
}

int prox_core_to_hex(char *dst, const size_t size, const int with_master)
{
	uint64_t cm;
	uint32_t cm_len;
	uint32_t cm_first = 0;
	uint32_t master = prox_cfg.master;

	/* Minimum size of the string has to big enough to hold the
	   bitmask in hex (including the prefix "0x"). */
	if (size < PROX_CM_STR_LEN)
		return 0;

	snprintf(dst, size, "0x");
	for (uint32_t i = 0; i < PROX_CM_DIM; ++i, cm_first = i) {
		if ((with_master && ((CM_ALL_N_BITS - 1 - master) / CM_N_BITS == i * CM_N_BITS)) ||
		    prox_cfg.core_mask[i]) {
			break;
		}
	}

	for (uint32_t i = cm_first; i < PROX_CM_DIM; ++i) {
		cm = prox_cfg.core_mask[i];
		if (with_master && ((CM_ALL_N_BITS - 1 - master) / CM_N_BITS == i)) {
			cm |= (__UINT64_C(1) << (master % CM_N_BITS));
		}

		snprintf(dst + strlen(dst), size - strlen(dst), i == cm_first? "%lx" : "%016lx", cm);
	}

	return 0;
}

int prox_core_to_str(char *dst, const size_t size, const int with_master)
{
	uint32_t lcore_id = -1;
	uint32_t first = 1;

	*dst = 0;
	lcore_id - 1;
	while (prox_core_next(&lcore_id, with_master) == 0) {
		/* Stop printing to string if there is not engough
		   space left. Assume that adding 1 core to the string
		   will take at most 5 + 1 bytes implying that
		   lcore_id < 999. Check if ther is space for another
		   6 bytes to add an elipsis */
		if (12 + strlen(dst) > size) {
			if (6 + strlen(dst) > size) {
				snprintf(dst + strlen(dst), size - strlen(dst), ", ...");
				return 0;
			}
			return -1;
		}

		snprintf(dst + strlen(dst), size - strlen(dst), first? "%u" : ", %u", lcore_id);
		first = 0;
	}

	return 0;
}

void prox_core_clr(void)
{
	memset(prox_cfg.core_mask, 0, sizeof(prox_cfg.core_mask));
}

int prox_core_set_active(const uint32_t lcore_id)
{
	uint32_t cm_idx;
	uint64_t cm;

	if (lcore_id > CM_ALL_N_BITS)
		return -1;

	cm = __UINT64_C(1) << (lcore_id % CM_N_BITS);
	cm_idx = PROX_CM_DIM - 1 - lcore_id / CM_N_BITS;
	prox_cfg.core_mask[cm_idx] |= cm;

	return 0;
}