forked from intel/intel-cmt-cat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdt.c
2103 lines (1784 loc) · 65 KB
/
rdt.c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* BSD LICENSE
*
* Copyright(c) 2016-2023 Intel Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "rdt.h"
#include "cpu.h"
#include "mba_sc.h"
#include "pqos.h"
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const struct pqos_cap *m_cap = NULL;
static const struct pqos_cpuinfo *m_cpu = NULL;
static const struct pqos_capability *m_cap_l2ca = NULL;
static const struct pqos_capability *m_cap_l3ca = NULL;
static const struct pqos_capability *m_cap_mba = NULL;
/**
* @brief Prints L2, L3 or MBA configuration in \a cfg
*
* @param [in] stream to write output to
* @param [in] cfg COS configuration
*/
static void
rdt_cfg_print(FILE *stream, const struct rdt_cfg cfg)
{
if (NULL == cfg.u.generic_ptr)
return;
switch (cfg.type) {
case PQOS_CAP_TYPE_L2CA:
if (cfg.u.l2->cdp == 1)
fprintf(stream, "code MASK: 0x%llx, data MASK: 0x%llx",
(unsigned long long)cfg.u.l2->u.s.code_mask,
(unsigned long long)cfg.u.l2->u.s.data_mask);
else
fprintf(stream, "MASK: 0x%llx",
(unsigned long long)cfg.u.l2->u.ways_mask);
break;
case PQOS_CAP_TYPE_L3CA:
if (cfg.u.l3->cdp == 1)
fprintf(stream, "code MASK: 0x%llx, data MASK: 0x%llx",
(unsigned long long)cfg.u.l3->u.s.code_mask,
(unsigned long long)cfg.u.l3->u.s.data_mask);
else
fprintf(stream, "MASK: 0x%llx",
(unsigned long long)cfg.u.l3->u.ways_mask);
break;
case PQOS_CAP_TYPE_MBA:
fprintf(stream, "RATE: %u", cfg.u.mba->mb_max);
break;
default:
break;
}
}
/**
* @brief Gets short string representation of configuration type of \a cfg
*
* @param [in] cfg COS allocation configuration
*
* @return String representation of configuration type
*/
static const char *
rdt_cfg_get_type_str(const struct rdt_cfg cfg)
{
if (NULL == cfg.u.generic_ptr)
return "ERROR!";
switch (cfg.type) {
case PQOS_CAP_TYPE_L2CA:
return "L2";
case PQOS_CAP_TYPE_L3CA:
return "L3";
case PQOS_CAP_TYPE_MBA:
return "MBA";
default:
return "ERROR!";
}
}
/**
* @brief Validates configuration in \a cfg
*
* @param [in] cfg COS configuration
*
* @return Result
* @retval 1 on success
* @retval 0 on failure
*/
static int
rdt_cfg_is_valid(const struct rdt_cfg cfg)
{
if (cfg.u.generic_ptr == NULL)
return 0;
switch (cfg.type) {
case PQOS_CAP_TYPE_L2CA:
/* Validate L2 CAT configuration.
* - If CDP is enabled code_mask and data_mask must be set
* - If CDP is not enabled ways_mask must be set
*/
return cfg.u.l2 != NULL &&
((cfg.u.l2->cdp == 1 && cfg.u.l2->u.s.code_mask != 0 &&
cfg.u.l2->u.s.data_mask != 0) ||
(cfg.u.l2->cdp == 0 && cfg.u.l2->u.ways_mask != 0));
case PQOS_CAP_TYPE_L3CA:
/* Validate L3 CAT configuration.
* - If CDP is enabled code_mask and data_mask must be set
* - If CDP is not enabled ways_mask must be set
*/
return cfg.u.l3 != NULL &&
((cfg.u.l3->cdp == 1 && cfg.u.l3->u.s.code_mask != 0 &&
cfg.u.l3->u.s.data_mask != 0) ||
(cfg.u.l3->cdp == 0 && cfg.u.l3->u.ways_mask != 0));
case PQOS_CAP_TYPE_MBA:
return cfg.u.mba != NULL && cfg.u.mba->mb_max > 0;
default:
break;
}
return 0;
}
/**
* @brief Function to get the highest resource ID (socket/cluster)
*
* @param [in] technology used to determine if res ID should be l3cat_id or
* cluster
* @param [out] max_res_id the highest possible resource ID
*
* @return Result
* @retval 0 on success
* @retval negative on error (-errno)
*/
static int
get_max_res_id(unsigned technology, unsigned *max_res_id)
{
unsigned num_ids, max = 0, *ids = NULL;
unsigned i;
if (m_cpu == NULL || technology == 0)
return -EFAULT;
/* get number of L2IDs */
if (technology & (1 << PQOS_CAP_TYPE_L2CA)) {
ids = pqos_cpu_get_l2ids(m_cpu, &num_ids);
if (ids == NULL)
return -EFAULT;
for (i = 0; i < num_ids; i++)
max = ids[i] > max ? ids[i] : max;
free(ids);
}
/* get number of l3cat_ids */
if (technology & (1 << PQOS_CAP_TYPE_L3CA)) {
ids = pqos_cpu_get_l3cat_ids(m_cpu, &num_ids);
if (ids == NULL)
return -EFAULT;
for (i = 0; i < num_ids; i++)
max = ids[i] > max ? ids[i] : max;
free(ids);
}
/* get number of mba_ids */
if (technology & (1 << PQOS_CAP_TYPE_MBA)) {
ids = pqos_cpu_get_mba_ids(m_cpu, &num_ids);
if (ids == NULL)
return -EFAULT;
for (i = 0; i < num_ids; i++)
max = ids[i] > max ? ids[i] : max;
free(ids);
}
*max_res_id = max;
return 0;
}
/**
* @brief Converts string \a str to UINT64
*
* @param [in] str string
* @param [in] base numerical base
* @param [out] value parsed value
*
* @return number of parsed characters
* @retval positive on success
* @retval negative on error (-errno)
*/
static int
str_to_uint64(const char *str, int base, uint64_t *value)
{
const char *str_start = str;
char *str_end = NULL;
unsigned long long val;
if (NULL == str || NULL == value)
return -EINVAL;
while (isblank(*str_start))
str_start++;
if (base == 10 && !isdigit(*str_start))
return -EINVAL;
if (base == 16 && !isxdigit(*str_start))
return -EINVAL;
errno = 0;
val = strtoull(str_start, &str_end, base);
if (errno != 0 || str_end == NULL || *str_end != '\0')
return -EINVAL;
if (val > UINT64_MAX)
return -EINVAL;
*value = val;
return str_end - str;
}
/**
* @brief Parses CBM (Capacity Bit Mask) string \a cbm
* and stores result in \a mask and \a cmask
*
* @param [in] cbm CBM string
* @param [in] cdp two masks separated with ','
* @param [out] data_mask common (L2 or L3 non CDP) or data mask (L3 CDP)
* @param [out] code_mask code mask (L3 CDP)
*
* @return status
* @retval 0 on success
* @retval negative on error (-errno)
*/
static int
parse_mask_set(char *cbm, int *cdp, uint64_t *data_mask, uint64_t *code_mask)
{
char *p = cbm;
char *saveptr = NULL;
uint64_t mask[2] = {0};
unsigned mask_count = 0;
/* skip brackets */
if (*p == '(') {
char *q = strchr(p, ')');
if (q == NULL)
return -EINVAL;
++p;
*q = '\0';
}
for (;; p = NULL) {
int ret;
char *token = NULL;
token = strtok_r(p, ",", &saveptr);
if (token == NULL)
break;
/* more than 2 masks */
if (mask_count > 1)
return -EINVAL;
ret = str_to_uint64(token, 16, &mask[mask_count++]);
if (ret < 0)
return -EINVAL;
}
*data_mask = mask[0];
*code_mask = mask[1];
*cdp = mask_count > 1;
return 0;
}
/**
* @brief This function displays cores association
*
* @param [in] cos_id class of service (COS) identifier
* @param [in] core_num number of cores
* @param [in] core_array array with cores identifiers
*/
static void
print_core_association(const unsigned int cos_id,
const unsigned int core_num,
const unsigned int *core_array)
{
unsigned i;
for (i = 0; i < core_num; i++)
printf("Core %u => COS%u\n", core_array[i], cos_id);
}
/**
* @brief This function displays process identifier (PID) association
*
* @param [in] cos_id class of service identifier (COS)
* @param [in] pid_num number of process identifier (PID)
* @param [in] pid_array array with process identifiers (PID)
*/
static void
print_pid_association(const unsigned int cos_id,
const unsigned int pid_num,
const pid_t *pid_array)
{
unsigned i;
for (i = 0; i < pid_num; i++)
printf("PID %i => COS%u\n", pid_array[i], cos_id);
}
int
parse_reset(const char *cpustr)
{
int ret = 0;
const unsigned cpustr_len = strnlen(cpustr, MAX_OPTARG_LEN);
if (cpustr_len == MAX_OPTARG_LEN)
return -EINVAL;
ret = str_to_cpuset(cpustr, cpustr_len, &g_cfg.reset_cpuset);
return ret > 0 ? 0 : ret;
}
/**
* @brief Parses CBMs string \a param and stores in \a ca
*
* @param [in] param CBMs string
* @param [out] ca to store result
*
* @return status
* @retval 0 on success
* @retval negative on error (-errno)
*/
static int
rdt_ca_str_to_cbm(char *param, struct rdt_cfg ca)
{
uint64_t mask = 0, mask2 = 0;
int cdp;
int ret;
if (!(PQOS_CAP_TYPE_L2CA == ca.type || PQOS_CAP_TYPE_L3CA == ca.type) ||
NULL == ca.u.generic_ptr || NULL == param)
return -EINVAL;
ret = parse_mask_set(param, &cdp, &mask, &mask2);
if (ret < 0)
return -EINVAL;
if (mask == 0)
return -EINVAL;
if (cdp && mask2 == 0)
return -EINVAL;
if (PQOS_CAP_TYPE_L2CA == ca.type) {
if (cdp) {
ca.u.l2->cdp = 1;
ca.u.l2->u.s.data_mask = mask;
ca.u.l2->u.s.code_mask = mask2;
} else
ca.u.l2->u.ways_mask = mask;
} else {
if (cdp) {
ca.u.l3->cdp = 1;
ca.u.l3->u.s.data_mask = mask;
ca.u.l3->u.s.code_mask = mask2;
} else
ca.u.l3->u.ways_mask = mask;
}
return 0;
}
/**
* @brief Parses rate string \a param and stores in \a mba
*
* @param [in] param rate string
* @param [out] mba to store result
*
* @return status
* @retval 0 on success
* @retval negative on error (-errno)
*/
static int
rdt_mba_str_to_rate(const char *param, struct rdt_cfg mba)
{
uint64_t rate;
int ret;
if (PQOS_CAP_TYPE_MBA != mba.type || NULL == mba.u.generic_ptr ||
NULL == param)
return -EINVAL;
ret = str_to_uint64(param, 10, &rate);
if (ret < 0 || rate == 0)
return -EINVAL;
mba.u.mba->ctrl = 0;
mba.u.mba->mb_max = rate;
return 0;
}
/**
* @brief Parses mbps string \a param and stores in \a mba
*
* @param [in] param mbps string
* @param [out] mba to store result
*
* @return status
* @retval 0 on success
* @retval negative on error (-errno)
*/
static int
rdt_mba_str_to_mbps(const char *param, struct rdt_cfg mba)
{
uint64_t mbps;
int ret;
if (PQOS_CAP_TYPE_MBA != mba.type || NULL == mba.u.generic_ptr ||
NULL == param)
return -EINVAL;
ret = str_to_uint64(param, 10, &mbps);
if (ret < 0 || mbps == 0)
return -EINVAL;
mba.u.mba->ctrl = 1;
mba.u.mba->mb_max = mbps;
return 0;
}
/*
* @brief Simplifies feature string
*
* @param [in] feature feature string
*
* @return simplified feature as char
* @retval char representation of feature
* @retval '?' on error
*/
static char
simplify_feature_str(const char *feature)
{
static const struct {
const char *feature_long;
const char feature_char;
} feature_lut[] = {
/* clang-format off */
{"cpu", 'c'},
{"l2", '2'},
{"l3", '3'},
{"mba", 'm'},
{"mba_max", 'b'},
{NULL, 0}
/* clang-format on */
};
if (feature == NULL)
return '?';
if (strlen(feature) > 1) {
unsigned i = 0;
while (feature_lut[i].feature_long != NULL) {
if (strcmp(feature, feature_lut[i].feature_long) == 0)
return feature_lut[i].feature_char;
i++;
}
} else
return feature[0];
return '?';
}
int
parse_rdt(char *rdtstr)
{
char *rdtstr_saveptr = NULL;
char *group = NULL;
unsigned idx = g_cfg.config_count;
const struct rdt_cfg l2ca = wrap_l2ca(&g_cfg.config[idx].l2);
const struct rdt_cfg l3ca = wrap_l3ca(&g_cfg.config[idx].l3);
const struct rdt_cfg mba = wrap_mba(&g_cfg.config[idx].mba);
const unsigned min_len_group = strlen("3=f");
int ret;
if (rdtstr == NULL)
return -EINVAL;
group = strtok_r(rdtstr, ";", &rdtstr_saveptr);
while (group != NULL) {
char *group_saveptr = NULL;
/* Min len check, 1 feature "3=f" */
if (strlen(group) < min_len_group) {
fprintf(stderr, "Invalid option: \"%s\"\n", group);
return -EINVAL;
}
const char *feature = strtok_r(group, "=", &group_saveptr);
char *param = strtok_r(NULL, "=", &group_saveptr);
if (feature == NULL || param == NULL) {
fprintf(stderr, "Invalid option: \"%s\"\n", group);
return -EINVAL;
}
switch (simplify_feature_str(feature)) {
case '2':
if (rdt_cfg_is_valid(l2ca))
return -EINVAL;
ret = rdt_ca_str_to_cbm(param, l2ca);
if (ret < 0)
return ret;
break;
case '3':
if (rdt_cfg_is_valid(l3ca))
return -EINVAL;
ret = rdt_ca_str_to_cbm(param, l3ca);
if (ret < 0)
return ret;
break;
case 'c':
if (CPU_COUNT(&g_cfg.config[idx].cpumask) != 0)
return -EINVAL;
ret = str_to_cpuset(param, strlen(param),
&g_cfg.config[idx].cpumask);
if (ret <= 0)
return -EINVAL;
if (CPU_COUNT(&g_cfg.config[idx].cpumask) == 0)
return -EINVAL;
break;
case 'm':
if (rdt_cfg_is_valid(mba))
return -EINVAL;
ret = rdt_mba_str_to_rate(param, mba);
if (ret < 0)
return ret;
break;
case 'b':
if (rdt_cfg_is_valid(mba))
return -EINVAL;
ret = rdt_mba_str_to_mbps(param, mba);
if (ret < 0)
return ret;
break;
default:
fprintf(stderr, "Invalid option: \"%s\"\n", feature);
return -EINVAL;
}
group = strtok_r(NULL, ";", &rdtstr_saveptr);
}
/* if no cpus specified then set pid flag */
if (CPU_COUNT(&g_cfg.config[idx].cpumask) == 0)
g_cfg.config[idx].pid_cfg = 1;
if (!(rdt_cfg_is_valid(l2ca) || rdt_cfg_is_valid(l3ca) ||
rdt_cfg_is_valid(mba)))
return -EINVAL;
g_cfg.config_count++;
return 0;
}
int
parse_iface(const char *iface)
{
if (strcasecmp(iface, "auto") == 0)
g_cfg.interface = PQOS_INTER_AUTO;
else if (strcasecmp(iface, "msr") == 0)
g_cfg.interface = PQOS_INTER_MSR;
else if (strcasecmp(iface, "os") == 0)
g_cfg.interface = PQOS_INTER_OS;
else {
fprintf(stderr, "Invalid interface: \"%s\"\n", iface);
return -EINVAL;
}
return 0;
}
/**
* @brief Checks if configured CPU sets are overlapping
*
* @return status
* @retval 0 on success
* @retval negative on error (-errno)
*/
static int
check_cpus_overlapping(void)
{
unsigned i = 0;
for (i = 0; i < g_cfg.config_count; i++) {
unsigned j = 0;
for (j = i + 1; j < g_cfg.config_count; j++) {
unsigned overlapping = 0;
#ifdef __linux__
cpu_set_t mask;
CPU_AND(&mask, &g_cfg.config[i].cpumask,
&g_cfg.config[j].cpumask);
overlapping = CPU_COUNT(&mask) != 0;
#endif
#ifdef __FreeBSD__
overlapping = CPU_OVERLAP(&g_cfg.config[i].cpumask,
&g_cfg.config[j].cpumask);
#endif
if (1 == overlapping) {
fprintf(stderr, "Allocation: Requested CPUs "
"sets are overlapping.\n");
return -EINVAL;
}
}
}
return 0;
}
/**
* @brief Checks if configured CPUs are valid and have no COS associated
*
* @return status
* @retval 0 on success
* @retval negative on error (-errno)
*/
static int
check_cpus(void)
{
unsigned i = 0;
int ret = 0;
for (i = 0; i < g_cfg.config_count; i++) {
unsigned cpu_id = 0;
for (cpu_id = 0; cpu_id < CPU_SETSIZE; cpu_id++) {
unsigned cos_id = 0;
if (CPU_ISSET(cpu_id, &g_cfg.config[i].cpumask) == 0)
continue;
ret = pqos_cpu_check_core(m_cpu, cpu_id);
if (ret != PQOS_RETVAL_OK) {
fprintf(stderr,
"Allocation: %u is not a valid "
"logical core id.\n",
cpu_id);
return -ENODEV;
}
ret = pqos_alloc_assoc_get(cpu_id, &cos_id);
if (ret != PQOS_RETVAL_OK) {
fprintf(stderr,
"Allocation: Failed to read "
"cpu %u COS association.\n",
cpu_id);
return -EFAULT;
}
/*
* Check if COS assigned to lcore is different
* then default one (#0)
*/
if (cos_id != 0) {
fprintf(stderr,
"Allocation: cpu %u has "
"already associated COS#%u. Please "
"reset allocation."
"\n",
cpu_id, cos_id);
return -EBUSY;
}
}
}
return 0;
}
/**
* @brief Checks if CPU supports requested L3 CDP configuration
*
* @return status
* @retval 0 on success
* @retval negative on error (-errno)
*/
static int
check_l3cdp_support(void)
{
unsigned i = 0;
int ret;
int l3cdp_supported = 0;
int l3cdp_enabled = 0;
ret = pqos_l3ca_cdp_enabled(m_cap, &l3cdp_supported, &l3cdp_enabled);
if (ret != PQOS_RETVAL_OK && ret != PQOS_RETVAL_RESOURCE)
return -1;
if (l3cdp_enabled)
return 0;
for (i = 0; i < g_cfg.config_count; i++) {
if (0 == g_cfg.config[i].l3.cdp)
continue;
if (!l3cdp_supported)
fprintf(stderr, "Allocation: L3 CDP requested but not "
"supported.\n");
else
fprintf(stderr, "Allocation: L3 CDP requested but not "
"enabled. Please enable L3 CDP.\n");
return -ENOTSUP;
}
return 0;
}
/**
* @brief Checks if CPU supports requested L2 CDP configuration
*
* @return status
* @retval 0 on success
* @retval negative on error (-errno)
*/
static int
check_l2cdp_support(void)
{
unsigned i = 0;
int ret;
int l2cdp_supported = 0;
int l2cdp_enabled = 0;
ret = pqos_l2ca_cdp_enabled(m_cap, &l2cdp_supported, &l2cdp_enabled);
if (ret != PQOS_RETVAL_OK && ret != PQOS_RETVAL_RESOURCE)
return -1;
if (l2cdp_enabled)
return 0;
for (i = 0; i < g_cfg.config_count; i++) {
if (0 == g_cfg.config[i].l2.cdp)
continue;
if (!l2cdp_supported)
fprintf(stderr, "Allocation: L2 CDP requested but not "
"supported.\n");
else
fprintf(stderr, "Allocation: L2 CDP requested but not "
"enabled. Please enable L2 CDP.\n");
return -ENOTSUP;
}
return 0;
}
/**
* @brief Checks if CPU supports requested MBA CTRL configuration
*
* @return status
* @retval 0 on success
* @retval negative on error (-errno)
*/
static int
check_mba_ctrl_support(void)
{
unsigned i = 0;
int ret;
int ctrl_supported = 0;
int ctrl_enabled = 0;
/* MBA Software controller is available */
if (g_cfg.interface == PQOS_INTER_MSR)
return 0;
ret = pqos_mba_ctrl_enabled(m_cap, &ctrl_supported, &ctrl_enabled);
if (ret != PQOS_RETVAL_OK && ret != PQOS_RETVAL_RESOURCE)
return -1;
if (ctrl_enabled != 0)
return 0;
for (i = 0; i < g_cfg.config_count; i++) {
if (0 == g_cfg.config[i].mba.ctrl)
continue;
if (ctrl_supported == 0)
fprintf(stderr, "Allocation: MBA CTRL requested but "
"not supported.\n");
else
fprintf(stderr,
"Allocation: MBA CTRL requested but "
"not enabled. Please enable MBA CTRL.\n");
return -ENOTSUP;
}
return 0;
}
/**
* @brief Checks if CAT/MBA configuration requested by a user via cmd line
* is supported by the system.
*
* @return status
* @retval 0 on success
* @retval negative on error (-errno)
*/
static int
check_supported(void)
{
unsigned i = 0;
for (i = 0; i < g_cfg.config_count; i++) {
if (rdt_cfg_is_valid(wrap_l3ca(&g_cfg.config[i].l3)) &&
NULL == m_cap_l3ca) {
fprintf(stderr, "Allocation: L3CA requested but not "
"supported by system!\n");
return -ENOTSUP;
}
if (rdt_cfg_is_valid(wrap_l2ca(&g_cfg.config[i].l2)) &&
NULL == m_cap_l2ca) {
fprintf(stderr, "Allocation: L2CA requested but not "
"supported by system!\n");
return -ENOTSUP;
}
if (rdt_cfg_is_valid(wrap_mba(&g_cfg.config[i].mba)) &&
NULL == m_cap_mba) {
fprintf(stderr, "Allocation: MBA requested but not "
"supported by system!\n");
return -ENOTSUP;
}
}
return 0;
}
/**
* @brief Returns negation of max CBM for requested \a type (L2 or L3)
*
* @param [in] type type of pqos CAT capability, L2 or L3
*
* @return mask
* @retval UINT64_MAX on error
*/
static uint64_t
get_not_cbm(const enum pqos_cap_type type)
{
if (PQOS_CAP_TYPE_L2CA == type && NULL != m_cap_l2ca)
return (UINT64_MAX << (m_cap_l2ca->u.l2ca->num_ways));
else if (PQOS_CAP_TYPE_L3CA == type && NULL != m_cap_l3ca)
return (UINT64_MAX << (m_cap_l3ca->u.l3ca->num_ways));
else
return UINT64_MAX;
}
/**
* @brief Returns contention mask for requested \a type (L2 or L3)
*
* @param [in] type type of pqos CAT capability, L2 or L3
*
* @return contention mask
* @retval UINT64_MAX on error
*
*/
static uint64_t
get_contention_mask(const enum pqos_cap_type type)
{
if (PQOS_CAP_TYPE_L2CA == type && NULL != m_cap_l2ca)
return m_cap_l2ca->u.l2ca->way_contention;
else if (PQOS_CAP_TYPE_L3CA == type && NULL != m_cap_l3ca)
return m_cap_l3ca->u.l3ca->way_contention;
else
return UINT64_MAX;
}
/**
* @brief Returns cumulative mask for \a ca CAT config
*
* For L2/L3 CDP config returns code_mask | data_mask,
* for L2/L3 non-CDP config returns ways_mask
*
* @param [in] ca CAT COS configuration
*
* @return cumulative mask
* @retval UINT64_MAX on error
*/
static uint64_t
rdt_ca_get_cumulative_cbm(const struct rdt_cfg ca)
{
if (!(PQOS_CAP_TYPE_L2CA == ca.type || PQOS_CAP_TYPE_L3CA == ca.type) ||
NULL == ca.u.generic_ptr || 0 == rdt_cfg_is_valid(ca))
return UINT64_MAX;
if (PQOS_CAP_TYPE_L2CA == ca.type) {
if (ca.u.l2->cdp == 1)
return ca.u.l2->u.s.code_mask | ca.u.l2->u.s.data_mask;
else
return ca.u.l2->u.ways_mask;
}
if (ca.u.l3->cdp == 1)
return ca.u.l3->u.s.code_mask | ca.u.l3->u.s.data_mask;
return ca.u.l3->u.ways_mask;
}
/**
* @brief Checks if requested CBMs of \a type are supported by system.
*
* Warn if CBM overlaps contention mask
*
* @param [in] type type of pqos CAT capability, L2 or L3
*
* @return status
* @retval 0 on success
* @retval negative on error (-errno)
*/
static int
check_cbm_len_and_contention(const enum pqos_cap_type type)
{
unsigned i = 0;
struct rdt_cfg ca;
uint64_t mask = 0;
if (!(PQOS_CAP_TYPE_L2CA == type || PQOS_CAP_TYPE_L3CA == type))
return -EINVAL;