1 // SPDX-License-Identifier: GPL-2.0 2 #include <test_progs.h> 3 #include <bpf/btf.h> 4 #include "bpf/libbpf_internal.h" 5 6 static int duration = 0; 7 8 static void validate_mask(int case_nr, const char *exp, bool *mask, int n) 9 { 10 int i; 11 12 for (i = 0; exp[i]; i++) { 13 if (exp[i] == '1') { 14 if (CHECK(i + 1 > n, "mask_short", 15 "case #%d: mask too short, got n=%d, need at least %d\n", 16 case_nr, n, i + 1)) 17 return; 18 CHECK(!mask[i], "cpu_not_set", 19 "case #%d: mask differs, expected cpu#%d SET\n", 20 case_nr, i); 21 } else { 22 CHECK(i < n && mask[i], "cpu_set", 23 "case #%d: mask differs, expected cpu#%d UNSET\n", 24 case_nr, i); 25 } 26 } 27 CHECK(i < n, "mask_long", 28 "case #%d: mask too long, got n=%d, expected at most %d\n", 29 case_nr, n, i); 30 } 31 32 static struct { 33 const char *cpu_mask; 34 const char *expect; 35 bool fails; 36 } test_cases[] = { 37 { "0\n", "1", false }, 38 { "0,2\n", "101", false }, 39 { "0-2\n", "111", false }, 40 { "0-2,3-4\n", "11111", false }, 41 { "0", "1", false }, 42 { "0-2", "111", false }, 43 { "0,2", "101", false }, 44 { "0,1-3", "1111", false }, 45 { "0,1,2,3", "1111", false }, 46 { "0,2-3,5", "101101", false }, 47 { "3-3", "0001", false }, 48 { "2-4,6,9-10", "00111010011", false }, 49 /* failure cases */ 50 { "", "", true }, 51 { "0-", "", true }, 52 { "0 ", "", true }, 53 { "0_1", "", true }, 54 { "1-0", "", true }, 55 { "-1", "", true }, 56 }; 57 58 void test_cpu_mask() 59 { 60 int i, err, n; 61 bool *mask; 62 63 for (i = 0; i < ARRAY_SIZE(test_cases); i++) { 64 mask = NULL; 65 err = parse_cpu_mask_str(test_cases[i].cpu_mask, &mask, &n); 66 if (test_cases[i].fails) { 67 CHECK(!err, "should_fail", 68 "case #%d: parsing should fail!\n", i + 1); 69 } else { 70 if (CHECK(err, "parse_err", 71 "case #%d: cpu mask parsing failed: %d\n", 72 i + 1, err)) 73 continue; 74 validate_mask(i + 1, test_cases[i].expect, mask, n); 75 } 76 free(mask); 77 } 78 } 79