aboutsummaryrefslogtreecommitdiffstats
path: root/lib/test_bpf.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/test_bpf.c')
-rw-r--r--lib/test_bpf.c89
1 files changed, 74 insertions, 15 deletions
diff --git a/lib/test_bpf.c b/lib/test_bpf.c
index c579e0f58818..23e070bcf72d 100644
--- a/lib/test_bpf.c
+++ b/lib/test_bpf.c
@@ -66,7 +66,7 @@ struct bpf_test {
66 const char *descr; 66 const char *descr;
67 union { 67 union {
68 struct sock_filter insns[MAX_INSNS]; 68 struct sock_filter insns[MAX_INSNS];
69 struct sock_filter_int insns_int[MAX_INSNS]; 69 struct bpf_insn insns_int[MAX_INSNS];
70 } u; 70 } u;
71 __u8 aux; 71 __u8 aux;
72 __u8 data[MAX_DATA]; 72 __u8 data[MAX_DATA];
@@ -1342,6 +1342,44 @@ static struct bpf_test tests[] = {
1342 { { 0, -1 } } 1342 { { 0, -1 } }
1343 }, 1343 },
1344 { 1344 {
1345 "INT: shifts by register",
1346 .u.insns_int = {
1347 BPF_MOV64_IMM(R0, -1234),
1348 BPF_MOV64_IMM(R1, 1),
1349 BPF_ALU32_REG(BPF_RSH, R0, R1),
1350 BPF_JMP_IMM(BPF_JEQ, R0, 0x7ffffd97, 1),
1351 BPF_EXIT_INSN(),
1352 BPF_MOV64_IMM(R2, 1),
1353 BPF_ALU64_REG(BPF_LSH, R0, R2),
1354 BPF_MOV32_IMM(R4, -1234),
1355 BPF_JMP_REG(BPF_JEQ, R0, R4, 1),
1356 BPF_EXIT_INSN(),
1357 BPF_ALU64_IMM(BPF_AND, R4, 63),
1358 BPF_ALU64_REG(BPF_LSH, R0, R4), /* R0 <= 46 */
1359 BPF_MOV64_IMM(R3, 47),
1360 BPF_ALU64_REG(BPF_ARSH, R0, R3),
1361 BPF_JMP_IMM(BPF_JEQ, R0, -617, 1),
1362 BPF_EXIT_INSN(),
1363 BPF_MOV64_IMM(R2, 1),
1364 BPF_ALU64_REG(BPF_LSH, R4, R2), /* R4 = 46 << 1 */
1365 BPF_JMP_IMM(BPF_JEQ, R4, 92, 1),
1366 BPF_EXIT_INSN(),
1367 BPF_MOV64_IMM(R4, 4),
1368 BPF_ALU64_REG(BPF_LSH, R4, R4), /* R4 = 4 << 4 */
1369 BPF_JMP_IMM(BPF_JEQ, R4, 64, 1),
1370 BPF_EXIT_INSN(),
1371 BPF_MOV64_IMM(R4, 5),
1372 BPF_ALU32_REG(BPF_LSH, R4, R4), /* R4 = 5 << 5 */
1373 BPF_JMP_IMM(BPF_JEQ, R4, 160, 1),
1374 BPF_EXIT_INSN(),
1375 BPF_MOV64_IMM(R0, -1),
1376 BPF_EXIT_INSN(),
1377 },
1378 INTERNAL,
1379 { },
1380 { { 0, -1 } }
1381 },
1382 {
1345 "INT: DIV + ABS", 1383 "INT: DIV + ABS",
1346 .u.insns_int = { 1384 .u.insns_int = {
1347 BPF_ALU64_REG(BPF_MOV, R6, R1), 1385 BPF_ALU64_REG(BPF_MOV, R6, R1),
@@ -1697,6 +1735,27 @@ static struct bpf_test tests[] = {
1697 { }, 1735 { },
1698 { { 1, 0 } }, 1736 { { 1, 0 } },
1699 }, 1737 },
1738 {
1739 "load 64-bit immediate",
1740 .u.insns_int = {
1741 BPF_LD_IMM64(R1, 0x567800001234LL),
1742 BPF_MOV64_REG(R2, R1),
1743 BPF_MOV64_REG(R3, R2),
1744 BPF_ALU64_IMM(BPF_RSH, R2, 32),
1745 BPF_ALU64_IMM(BPF_LSH, R3, 32),
1746 BPF_ALU64_IMM(BPF_RSH, R3, 32),
1747 BPF_ALU64_IMM(BPF_MOV, R0, 0),
1748 BPF_JMP_IMM(BPF_JEQ, R2, 0x5678, 1),
1749 BPF_EXIT_INSN(),
1750 BPF_JMP_IMM(BPF_JEQ, R3, 0x1234, 1),
1751 BPF_EXIT_INSN(),
1752 BPF_ALU64_IMM(BPF_MOV, R0, 1),
1753 BPF_EXIT_INSN(),
1754 },
1755 INTERNAL,
1756 { },
1757 { { 0, 1 } }
1758 },
1700}; 1759};
1701 1760
1702static struct net_device dev; 1761static struct net_device dev;
@@ -1761,9 +1820,9 @@ static int probe_filter_length(struct sock_filter *fp)
1761 return len + 1; 1820 return len + 1;
1762} 1821}
1763 1822
1764static struct sk_filter *generate_filter(int which, int *err) 1823static struct bpf_prog *generate_filter(int which, int *err)
1765{ 1824{
1766 struct sk_filter *fp; 1825 struct bpf_prog *fp;
1767 struct sock_fprog_kern fprog; 1826 struct sock_fprog_kern fprog;
1768 unsigned int flen = probe_filter_length(tests[which].u.insns); 1827 unsigned int flen = probe_filter_length(tests[which].u.insns);
1769 __u8 test_type = tests[which].aux & TEST_TYPE_MASK; 1828 __u8 test_type = tests[which].aux & TEST_TYPE_MASK;
@@ -1773,7 +1832,7 @@ static struct sk_filter *generate_filter(int which, int *err)
1773 fprog.filter = tests[which].u.insns; 1832 fprog.filter = tests[which].u.insns;
1774 fprog.len = flen; 1833 fprog.len = flen;
1775 1834
1776 *err = sk_unattached_filter_create(&fp, &fprog); 1835 *err = bpf_prog_create(&fp, &fprog);
1777 if (tests[which].aux & FLAG_EXPECTED_FAIL) { 1836 if (tests[which].aux & FLAG_EXPECTED_FAIL) {
1778 if (*err == -EINVAL) { 1837 if (*err == -EINVAL) {
1779 pr_cont("PASS\n"); 1838 pr_cont("PASS\n");
@@ -1798,7 +1857,7 @@ static struct sk_filter *generate_filter(int which, int *err)
1798 break; 1857 break;
1799 1858
1800 case INTERNAL: 1859 case INTERNAL:
1801 fp = kzalloc(sk_filter_size(flen), GFP_KERNEL); 1860 fp = bpf_prog_alloc(bpf_prog_size(flen), 0);
1802 if (fp == NULL) { 1861 if (fp == NULL) {
1803 pr_cont("UNEXPECTED_FAIL no memory left\n"); 1862 pr_cont("UNEXPECTED_FAIL no memory left\n");
1804 *err = -ENOMEM; 1863 *err = -ENOMEM;
@@ -1807,9 +1866,9 @@ static struct sk_filter *generate_filter(int which, int *err)
1807 1866
1808 fp->len = flen; 1867 fp->len = flen;
1809 memcpy(fp->insnsi, tests[which].u.insns_int, 1868 memcpy(fp->insnsi, tests[which].u.insns_int,
1810 fp->len * sizeof(struct sock_filter_int)); 1869 fp->len * sizeof(struct bpf_insn));
1811 1870
1812 sk_filter_select_runtime(fp); 1871 bpf_prog_select_runtime(fp);
1813 break; 1872 break;
1814 } 1873 }
1815 1874
@@ -1817,30 +1876,30 @@ static struct sk_filter *generate_filter(int which, int *err)
1817 return fp; 1876 return fp;
1818} 1877}
1819 1878
1820static void release_filter(struct sk_filter *fp, int which) 1879static void release_filter(struct bpf_prog *fp, int which)
1821{ 1880{
1822 __u8 test_type = tests[which].aux & TEST_TYPE_MASK; 1881 __u8 test_type = tests[which].aux & TEST_TYPE_MASK;
1823 1882
1824 switch (test_type) { 1883 switch (test_type) {
1825 case CLASSIC: 1884 case CLASSIC:
1826 sk_unattached_filter_destroy(fp); 1885 bpf_prog_destroy(fp);
1827 break; 1886 break;
1828 case INTERNAL: 1887 case INTERNAL:
1829 sk_filter_free(fp); 1888 bpf_prog_free(fp);
1830 break; 1889 break;
1831 } 1890 }
1832} 1891}
1833 1892
1834static int __run_one(const struct sk_filter *fp, const void *data, 1893static int __run_one(const struct bpf_prog *fp, const void *data,
1835 int runs, u64 *duration) 1894 int runs, u64 *duration)
1836{ 1895{
1837 u64 start, finish; 1896 u64 start, finish;
1838 int ret, i; 1897 int ret = 0, i;
1839 1898
1840 start = ktime_to_us(ktime_get()); 1899 start = ktime_to_us(ktime_get());
1841 1900
1842 for (i = 0; i < runs; i++) 1901 for (i = 0; i < runs; i++)
1843 ret = SK_RUN_FILTER(fp, data); 1902 ret = BPF_PROG_RUN(fp, data);
1844 1903
1845 finish = ktime_to_us(ktime_get()); 1904 finish = ktime_to_us(ktime_get());
1846 1905
@@ -1850,7 +1909,7 @@ static int __run_one(const struct sk_filter *fp, const void *data,
1850 return ret; 1909 return ret;
1851} 1910}
1852 1911
1853static int run_one(const struct sk_filter *fp, struct bpf_test *test) 1912static int run_one(const struct bpf_prog *fp, struct bpf_test *test)
1854{ 1913{
1855 int err_cnt = 0, i, runs = MAX_TESTRUNS; 1914 int err_cnt = 0, i, runs = MAX_TESTRUNS;
1856 1915
@@ -1884,7 +1943,7 @@ static __init int test_bpf(void)
1884 int i, err_cnt = 0, pass_cnt = 0; 1943 int i, err_cnt = 0, pass_cnt = 0;
1885 1944
1886 for (i = 0; i < ARRAY_SIZE(tests); i++) { 1945 for (i = 0; i < ARRAY_SIZE(tests); i++) {
1887 struct sk_filter *fp; 1946 struct bpf_prog *fp;
1888 int err; 1947 int err;
1889 1948
1890 pr_info("#%d %s ", i, tests[i].descr); 1949 pr_info("#%d %s ", i, tests[i].descr);