xskxceiver.c (6b3c0821caa49538c49262b041bae59bad523c7c) | xskxceiver.c (f0a249df1b071d6f7177cc615d688a3a5d48423a) |
---|---|
1// SPDX-License-Identifier: GPL-2.0 2/* Copyright(c) 2020 Intel Corporation. */ 3 4/* 5 * Some functions in this program are taken from 6 * Linux kernel samples/bpf/xdpsock* and modified 7 * for use. 8 * --- 1193 unchanged lines hidden (view full) --- 1202 enable_busy_poll(&ifobject->xsk_arr[i]); 1203 } 1204} 1205 1206static void thread_common_ops_tx(struct test_spec *test, struct ifobject *ifobject) 1207{ 1208 xsk_configure_socket(test, ifobject, test->ifobj_rx->umem, true); 1209 ifobject->xsk = &ifobject->xsk_arr[0]; | 1// SPDX-License-Identifier: GPL-2.0 2/* Copyright(c) 2020 Intel Corporation. */ 3 4/* 5 * Some functions in this program are taken from 6 * Linux kernel samples/bpf/xdpsock* and modified 7 * for use. 8 * --- 1193 unchanged lines hidden (view full) --- 1202 enable_busy_poll(&ifobject->xsk_arr[i]); 1203 } 1204} 1205 1206static void thread_common_ops_tx(struct test_spec *test, struct ifobject *ifobject) 1207{ 1208 xsk_configure_socket(test, ifobject, test->ifobj_rx->umem, true); 1209 ifobject->xsk = &ifobject->xsk_arr[0]; |
1210 ifobject->xsk_map_fd = test->ifobj_rx->xsk_map_fd; | 1210 ifobject->xskmap = test->ifobj_rx->xskmap; |
1211 memcpy(ifobject->umem, test->ifobj_rx->umem, sizeof(struct xsk_umem_info)); 1212} 1213 1214static void xsk_populate_fill_ring(struct xsk_umem_info *umem, struct pkt_stream *pkt_stream) 1215{ 1216 u32 idx = 0, i, buffers_to_fill; 1217 int ret; 1218 --- 23 unchanged lines hidden (view full) --- 1242 xsk_ring_prod__submit(&umem->fq, i); 1243} 1244 1245static void thread_common_ops(struct test_spec *test, struct ifobject *ifobject) 1246{ 1247 u64 umem_sz = ifobject->umem->num_frames * ifobject->umem->frame_size; 1248 int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE; 1249 LIBBPF_OPTS(bpf_xdp_query_opts, opts); | 1211 memcpy(ifobject->umem, test->ifobj_rx->umem, sizeof(struct xsk_umem_info)); 1212} 1213 1214static void xsk_populate_fill_ring(struct xsk_umem_info *umem, struct pkt_stream *pkt_stream) 1215{ 1216 u32 idx = 0, i, buffers_to_fill; 1217 int ret; 1218 --- 23 unchanged lines hidden (view full) --- 1242 xsk_ring_prod__submit(&umem->fq, i); 1243} 1244 1245static void thread_common_ops(struct test_spec *test, struct ifobject *ifobject) 1246{ 1247 u64 umem_sz = ifobject->umem->num_frames * ifobject->umem->frame_size; 1248 int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE; 1249 LIBBPF_OPTS(bpf_xdp_query_opts, opts); |
1250 u32 queue_id = 0; 1251 int ret, fd; | |
1252 void *bufs; | 1250 void *bufs; |
1251 int ret; |
|
1253 1254 if (ifobject->umem->unaligned_mode) 1255 mmap_flags |= MAP_HUGETLB; 1256 1257 if (ifobject->shared_umem) 1258 umem_sz *= 2; 1259 1260 bufs = mmap(NULL, umem_sz, PROT_READ | PROT_WRITE, mmap_flags, -1, 0); --- 8 unchanged lines hidden (view full) --- 1269 1270 xsk_configure_socket(test, ifobject, ifobject->umem, false); 1271 1272 ifobject->xsk = &ifobject->xsk_arr[0]; 1273 1274 if (!ifobject->rx_on) 1275 return; 1276 | 1252 1253 if (ifobject->umem->unaligned_mode) 1254 mmap_flags |= MAP_HUGETLB; 1255 1256 if (ifobject->shared_umem) 1257 umem_sz *= 2; 1258 1259 bufs = mmap(NULL, umem_sz, PROT_READ | PROT_WRITE, mmap_flags, -1, 0); --- 8 unchanged lines hidden (view full) --- 1268 1269 xsk_configure_socket(test, ifobject, ifobject->umem, false); 1270 1271 ifobject->xsk = &ifobject->xsk_arr[0]; 1272 1273 if (!ifobject->rx_on) 1274 return; 1275 |
1277 fd = xsk_socket__fd(ifobject->xsk->xsk); 1278 ret = bpf_map_update_elem(ifobject->xsk_map_fd, &queue_id, &fd, 0); | 1276 ret = xsk_update_xskmap(ifobject->xskmap, ifobject->xsk->xsk); |
1279 if (ret) 1280 exit_with_error(errno); 1281} 1282 1283static void *worker_testapp_validate_tx(void *arg) 1284{ 1285 struct test_spec *test = (struct test_spec *)arg; 1286 struct ifobject *ifobject = test->ifobj_tx; --- 17 unchanged lines hidden (view full) --- 1304 1305 pthread_exit(NULL); 1306} 1307 1308static void *worker_testapp_validate_rx(void *arg) 1309{ 1310 struct test_spec *test = (struct test_spec *)arg; 1311 struct ifobject *ifobject = test->ifobj_rx; | 1277 if (ret) 1278 exit_with_error(errno); 1279} 1280 1281static void *worker_testapp_validate_tx(void *arg) 1282{ 1283 struct test_spec *test = (struct test_spec *)arg; 1284 struct ifobject *ifobject = test->ifobj_tx; --- 17 unchanged lines hidden (view full) --- 1302 1303 pthread_exit(NULL); 1304} 1305 1306static void *worker_testapp_validate_rx(void *arg) 1307{ 1308 struct test_spec *test = (struct test_spec *)arg; 1309 struct ifobject *ifobject = test->ifobj_rx; |
1312 int id = 0, err, fd = xsk_socket__fd(ifobject->xsk->xsk); | |
1313 struct pollfd fds = { }; | 1310 struct pollfd fds = { }; |
1314 u32 queue_id = 0; | 1311 int err; |
1315 1316 if (test->current_step == 1) { 1317 thread_common_ops(test, ifobject); 1318 } else { | 1312 1313 if (test->current_step == 1) { 1314 thread_common_ops(test, ifobject); 1315 } else { |
1319 bpf_map_delete_elem(ifobject->xsk_map_fd, &id); 1320 err = bpf_map_update_elem(ifobject->xsk_map_fd, &queue_id, &fd, 0); | 1316 xsk_clear_xskmap(ifobject->xskmap); 1317 err = xsk_update_xskmap(ifobject->xskmap, ifobject->xsk->xsk); |
1321 if (err) { | 1318 if (err) { |
1322 printf("Error: Failed to update xskmap, error %s\n", strerror(err)); 1323 exit_with_error(err); | 1319 printf("Error: Failed to update xskmap, error %s\n", strerror(-err)); 1320 exit_with_error(-err); |
1324 } 1325 } 1326 1327 fds.fd = xsk_socket__fd(ifobject->xsk->xsk); 1328 fds.events = POLLIN; 1329 1330 pthread_barrier_wait(&barr); 1331 --- 53 unchanged lines hidden (view full) --- 1385 1386 if (pthread_barrier_destroy(&barr)) 1387 exit_with_error(errno); 1388 1389 pthread_kill(t0, SIGUSR1); 1390 pthread_join(t0, NULL); 1391 1392 if (test->total_steps == test->current_step || test->fail) { | 1321 } 1322 } 1323 1324 fds.fd = xsk_socket__fd(ifobject->xsk->xsk); 1325 fds.events = POLLIN; 1326 1327 pthread_barrier_wait(&barr); 1328 --- 53 unchanged lines hidden (view full) --- 1382 1383 if (pthread_barrier_destroy(&barr)) 1384 exit_with_error(errno); 1385 1386 pthread_kill(t0, SIGUSR1); 1387 pthread_join(t0, NULL); 1388 1389 if (test->total_steps == test->current_step || test->fail) { |
1393 u32 queue_id = 0; 1394 | |
1395 xsk_socket__delete(ifobj->xsk->xsk); | 1390 xsk_socket__delete(ifobj->xsk->xsk); |
1396 bpf_map_delete_elem(ifobj->xsk_map_fd, &queue_id); | 1391 xsk_clear_xskmap(ifobj->xskmap); |
1397 testapp_clean_xsk_umem(ifobj); 1398 } 1399 1400 test->ifobj_rx->shared_umem = old_shared_umem; 1401 test->ifobj_tx->shared_umem = old_shared_umem; 1402 1403 return !!test->fail; 1404} --- 72 unchanged lines hidden (view full) --- 1477 swap_directions(&test->ifobj_rx, &test->ifobj_tx); 1478 testapp_validate_traffic(test); 1479 1480 swap_directions(&test->ifobj_rx, &test->ifobj_tx); 1481} 1482 1483static void swap_xsk_resources(struct ifobject *ifobj_tx, struct ifobject *ifobj_rx) 1484{ | 1392 testapp_clean_xsk_umem(ifobj); 1393 } 1394 1395 test->ifobj_rx->shared_umem = old_shared_umem; 1396 test->ifobj_tx->shared_umem = old_shared_umem; 1397 1398 return !!test->fail; 1399} --- 72 unchanged lines hidden (view full) --- 1472 swap_directions(&test->ifobj_rx, &test->ifobj_tx); 1473 testapp_validate_traffic(test); 1474 1475 swap_directions(&test->ifobj_rx, &test->ifobj_tx); 1476} 1477 1478static void swap_xsk_resources(struct ifobject *ifobj_tx, struct ifobject *ifobj_rx) 1479{ |
1485 int ret, queue_id = 0, fd = xsk_socket__fd(ifobj_rx->xsk->xsk); | 1480 int ret; |
1486 1487 xsk_socket__delete(ifobj_tx->xsk->xsk); 1488 xsk_socket__delete(ifobj_rx->xsk->xsk); 1489 ifobj_tx->xsk = &ifobj_tx->xsk_arr[1]; 1490 ifobj_rx->xsk = &ifobj_rx->xsk_arr[1]; 1491 | 1481 1482 xsk_socket__delete(ifobj_tx->xsk->xsk); 1483 xsk_socket__delete(ifobj_rx->xsk->xsk); 1484 ifobj_tx->xsk = &ifobj_tx->xsk_arr[1]; 1485 ifobj_rx->xsk = &ifobj_rx->xsk_arr[1]; 1486 |
1492 ret = bpf_map_update_elem(ifobj_rx->xsk_map_fd, &queue_id, &fd, 0); | 1487 ret = xsk_update_xskmap(ifobj_rx->xskmap, ifobj_rx->xsk->xsk); |
1493 if (ret) 1494 exit_with_error(errno); 1495} 1496 1497static void testapp_bpf_res(struct test_spec *test) 1498{ 1499 test_spec_set_name(test, "BPF_RES"); 1500 test->total_steps = 2; --- 145 unchanged lines hidden (view full) --- 1646 pkts[5].addr += UMEM_SIZE; 1647 } 1648 1649 pkt_stream_generate_custom(test, pkts, ARRAY_SIZE(pkts)); 1650 testapp_validate_traffic(test); 1651 pkt_stream_restore_default(test); 1652} 1653 | 1488 if (ret) 1489 exit_with_error(errno); 1490} 1491 1492static void testapp_bpf_res(struct test_spec *test) 1493{ 1494 test_spec_set_name(test, "BPF_RES"); 1495 test->total_steps = 2; --- 145 unchanged lines hidden (view full) --- 1641 pkts[5].addr += UMEM_SIZE; 1642 } 1643 1644 pkt_stream_generate_custom(test, pkts, ARRAY_SIZE(pkts)); 1645 testapp_validate_traffic(test); 1646 pkt_stream_restore_default(test); 1647} 1648 |
1649static int xsk_load_xdp_programs(struct ifobject *ifobj) 1650{ 1651 ifobj->xdp_progs = xsk_xdp_progs__open_and_load(); 1652 if (libbpf_get_error(ifobj->xdp_progs)) 1653 return libbpf_get_error(ifobj->xdp_progs); 1654 1655 return 0; 1656} 1657 1658static void xsk_unload_xdp_programs(struct ifobject *ifobj) 1659{ 1660 xsk_xdp_progs__destroy(ifobj->xdp_progs); 1661} 1662 |
|
1654static void init_iface(struct ifobject *ifobj, const char *dst_mac, const char *src_mac, 1655 const char *dst_ip, const char *src_ip, const u16 dst_port, 1656 const u16 src_port, thread_func_t func_ptr, bool load_xdp) 1657{ | 1663static void init_iface(struct ifobject *ifobj, const char *dst_mac, const char *src_mac, 1664 const char *dst_ip, const char *src_ip, const u16 dst_port, 1665 const u16 src_port, thread_func_t func_ptr, bool load_xdp) 1666{ |
1658 int xsk_map_fd, prog_fd, err; | |
1659 struct in_addr ip; | 1667 struct in_addr ip; |
1668 int err; |
|
1660 1661 memcpy(ifobj->dst_mac, dst_mac, ETH_ALEN); 1662 memcpy(ifobj->src_mac, src_mac, ETH_ALEN); 1663 1664 inet_aton(dst_ip, &ip); 1665 ifobj->dst_ip = ip.s_addr; 1666 1667 inet_aton(src_ip, &ip); 1668 ifobj->src_ip = ip.s_addr; 1669 1670 ifobj->dst_port = dst_port; 1671 ifobj->src_port = src_port; 1672 1673 ifobj->func_ptr = func_ptr; 1674 1675 if (!load_xdp) 1676 return; 1677 | 1669 1670 memcpy(ifobj->dst_mac, dst_mac, ETH_ALEN); 1671 memcpy(ifobj->src_mac, src_mac, ETH_ALEN); 1672 1673 inet_aton(dst_ip, &ip); 1674 ifobj->dst_ip = ip.s_addr; 1675 1676 inet_aton(src_ip, &ip); 1677 ifobj->src_ip = ip.s_addr; 1678 1679 ifobj->dst_port = dst_port; 1680 ifobj->src_port = src_port; 1681 1682 ifobj->func_ptr = func_ptr; 1683 1684 if (!load_xdp) 1685 return; 1686 |
1678 err = xsk_load_xdp_program(&xsk_map_fd, &prog_fd); | 1687 err = xsk_load_xdp_programs(ifobj); |
1679 if (err) { 1680 printf("Error loading XDP program\n"); 1681 exit_with_error(err); 1682 } 1683 | 1688 if (err) { 1689 printf("Error loading XDP program\n"); 1690 exit_with_error(err); 1691 } 1692 |
1684 ifobj->xsk_map_fd = xsk_map_fd; 1685 ifobj->prog_fd = prog_fd; | |
1686 ifobj->xdp_flags = mode_to_xdp_flags(TEST_MODE_SKB); | 1693 ifobj->xdp_flags = mode_to_xdp_flags(TEST_MODE_SKB); |
1687 ifobj->link_fd = xsk_attach_xdp_program(ifobj->ifindex, prog_fd, ifobj->xdp_flags); 1688 if (ifobj->link_fd < 0) { | 1694 err = xsk_attach_xdp_program(ifobj->xdp_progs->progs.xsk_def_prog, ifobj->ifindex, 1695 ifobj->xdp_flags); 1696 if (err) { |
1689 printf("Error attaching XDP program\n"); | 1697 printf("Error attaching XDP program\n"); |
1690 exit_with_error(ifobj->link_fd); | 1698 exit_with_error(-err); |
1691 } | 1699 } |
1700 ifobj->xskmap = ifobj->xdp_progs->maps.xsk; |
|
1692} 1693 1694static void run_pkt_test(struct test_spec *test, enum test_mode mode, enum test_type type) 1695{ 1696 switch (type) { 1697 case TEST_TYPE_STATS_RX_DROPPED: 1698 if (mode == TEST_MODE_ZC) { 1699 ksft_test_result_skip("Can not run RX_DROPPED test for ZC mode\n"); --- 118 unchanged lines hidden (view full) --- 1818 free(ifobj->xsk_arr); 1819out_xsk_arr: 1820 free(ifobj); 1821 return NULL; 1822} 1823 1824static void ifobject_delete(struct ifobject *ifobj) 1825{ | 1701} 1702 1703static void run_pkt_test(struct test_spec *test, enum test_mode mode, enum test_type type) 1704{ 1705 switch (type) { 1706 case TEST_TYPE_STATS_RX_DROPPED: 1707 if (mode == TEST_MODE_ZC) { 1708 ksft_test_result_skip("Can not run RX_DROPPED test for ZC mode\n"); --- 118 unchanged lines hidden (view full) --- 1827 free(ifobj->xsk_arr); 1828out_xsk_arr: 1829 free(ifobj); 1830 return NULL; 1831} 1832 1833static void ifobject_delete(struct ifobject *ifobj) 1834{ |
1826 close(ifobj->prog_fd); 1827 close(ifobj->xsk_map_fd); 1828 | |
1829 free(ifobj->umem); 1830 free(ifobj->xsk_arr); 1831 free(ifobj); 1832} 1833 1834static bool is_xdp_supported(int ifindex) 1835{ 1836 int flags = XDP_FLAGS_DRV_MODE; --- 22 unchanged lines hidden (view full) --- 1859 return true; 1860} 1861 1862static void change_to_drv_mode(struct ifobject *ifobj) 1863{ 1864 LIBBPF_OPTS(bpf_xdp_query_opts, opts); 1865 int ret; 1866 | 1835 free(ifobj->umem); 1836 free(ifobj->xsk_arr); 1837 free(ifobj); 1838} 1839 1840static bool is_xdp_supported(int ifindex) 1841{ 1842 int flags = XDP_FLAGS_DRV_MODE; --- 22 unchanged lines hidden (view full) --- 1865 return true; 1866} 1867 1868static void change_to_drv_mode(struct ifobject *ifobj) 1869{ 1870 LIBBPF_OPTS(bpf_xdp_query_opts, opts); 1871 int ret; 1872 |
1867 close(ifobj->link_fd); 1868 ifobj->link_fd = xsk_attach_xdp_program(ifobj->ifindex, ifobj->prog_fd, 1869 XDP_FLAGS_DRV_MODE); 1870 if (ifobj->link_fd < 0) { | 1873 xsk_detach_xdp_program(ifobj->ifindex, ifobj->xdp_flags); 1874 ifobj->xdp_flags = XDP_FLAGS_DRV_MODE; 1875 ret = xsk_attach_xdp_program(ifobj->xdp_progs->progs.xsk_def_prog, ifobj->ifindex, 1876 ifobj->xdp_flags); 1877 if (ret) { |
1871 ksft_print_msg("Error attaching XDP program\n"); | 1878 ksft_print_msg("Error attaching XDP program\n"); |
1872 exit_with_error(-ifobj->link_fd); | 1879 exit_with_error(-ret); |
1873 } | 1880 } |
1881 ifobj->xskmap = ifobj->xdp_progs->maps.xsk; |
|
1874 1875 ret = bpf_xdp_query(ifobj->ifindex, XDP_FLAGS_DRV_MODE, &opts); 1876 if (ret) 1877 exit_with_error(errno); 1878 1879 if (opts.attach_mode != XDP_ATTACHED_DRV) { 1880 ksft_print_msg("ERROR: XDP prog not in DRV mode\n"); 1881 exit_with_error(EINVAL); --- 68 unchanged lines hidden (view full) --- 1950 1951 if (test.fail) 1952 failed_tests++; 1953 } 1954 } 1955 1956 pkt_stream_delete(tx_pkt_stream_default); 1957 pkt_stream_delete(rx_pkt_stream_default); | 1882 1883 ret = bpf_xdp_query(ifobj->ifindex, XDP_FLAGS_DRV_MODE, &opts); 1884 if (ret) 1885 exit_with_error(errno); 1886 1887 if (opts.attach_mode != XDP_ATTACHED_DRV) { 1888 ksft_print_msg("ERROR: XDP prog not in DRV mode\n"); 1889 exit_with_error(EINVAL); --- 68 unchanged lines hidden (view full) --- 1958 1959 if (test.fail) 1960 failed_tests++; 1961 } 1962 } 1963 1964 pkt_stream_delete(tx_pkt_stream_default); 1965 pkt_stream_delete(rx_pkt_stream_default); |
1966 xsk_unload_xdp_programs(ifobj_tx); 1967 xsk_unload_xdp_programs(ifobj_rx); |
|
1958 ifobject_delete(ifobj_tx); 1959 ifobject_delete(ifobj_rx); 1960 1961 if (failed_tests) 1962 ksft_exit_fail(); 1963 else 1964 ksft_exit_pass(); 1965} | 1968 ifobject_delete(ifobj_tx); 1969 ifobject_delete(ifobj_rx); 1970 1971 if (failed_tests) 1972 ksft_exit_fail(); 1973 else 1974 ksft_exit_pass(); 1975} |