xskxceiver.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2020 Intel Corporation. */
  3. /*
  4. * Some functions in this program are taken from
  5. * Linux kernel samples/bpf/xdpsock* and modified
  6. * for use.
  7. *
  8. * See test_xsk.sh for detailed information on test topology
  9. * and prerequisite network setup.
  10. *
  11. * This test program contains two threads, each thread is single socket with
  12. * a unique UMEM. It validates in-order packet delivery and packet content
  13. * by sending packets to each other.
  14. *
  15. * Tests Information:
  16. * ------------------
  17. * These selftests test AF_XDP SKB and Native/DRV modes using veth
  18. * Virtual Ethernet interfaces.
  19. *
  20. * For each mode, the following tests are run:
  21. * a. nopoll - soft-irq processing in run-to-completion mode
  22. * b. poll - using poll() syscall
  23. * c. Socket Teardown
  24. * Create a Tx and a Rx socket, Tx from one socket, Rx on another. Destroy
  25. * both sockets, then repeat multiple times. Only nopoll mode is used
  26. * d. Bi-directional sockets
  27. * Configure sockets as bi-directional tx/rx sockets, sets up fill and
  28. * completion rings on each socket, tx/rx in both directions. Only nopoll
  29. * mode is used
  30. * e. Statistics
  31. * Trigger some error conditions and ensure that the appropriate statistics
  32. * are incremented. Within this test, the following statistics are tested:
  33. * i. rx dropped
  34. * Increase the UMEM frame headroom to a value which results in
  35. * insufficient space in the rx buffer for both the packet and the headroom.
  36. * ii. tx invalid
  37. * Set the 'len' field of tx descriptors to an invalid value (umem frame
  38. * size + 1).
  39. * iii. rx ring full
  40. * Reduce the size of the RX ring to a fraction of the fill ring size.
  41. * iv. fill queue empty
  42. * Do not populate the fill queue and then try to receive pkts.
  43. * f. bpf_link resource persistence
  44. * Configure sockets at indexes 0 and 1, run a traffic on queue ids 0,
  45. * then remove xsk sockets from queue 0 on both veth interfaces and
  46. * finally run a traffic on queues ids 1
  47. * g. unaligned mode
  48. * h. tests for invalid and corner case Tx descriptors so that the correct ones
  49. * are discarded and let through, respectively.
  50. * i. 2K frame size tests
  51. * j. If multi-buffer is supported, send 9k packets divided into 3 frames
  52. * k. If multi-buffer and huge pages are supported, send 9k packets in a single frame
  53. * using unaligned mode
  54. * l. If multi-buffer is supported, try various nasty combinations of descriptors to
  55. * check if they pass the validation or not
  56. *
  57. * Flow:
  58. * -----
  59. * - Single process spawns two threads: Tx and Rx
  60. * - Each of these two threads attach to a veth interface
  61. * - Each thread creates one AF_XDP socket connected to a unique umem for each
  62. * veth interface
  63. * - Tx thread Transmits a number of packets from veth<xxxx> to veth<yyyy>
  64. * - Rx thread verifies if all packets were received and delivered in-order,
  65. * and have the right content
  66. *
  67. * Enable/disable packet dump mode:
  68. * --------------------------
  69. * To enable L2 - L4 headers and payload dump of each packet on STDOUT, add
  70. * parameter -D to params array in test_xsk.sh, i.e. params=("-S" "-D")
  71. */
  72. #define _GNU_SOURCE
  73. #include <assert.h>
  74. #include <fcntl.h>
  75. #include <errno.h>
  76. #include <getopt.h>
  77. #include <linux/if_link.h>
  78. #include <linux/if_ether.h>
  79. #include <linux/mman.h>
  80. #include <linux/netdev.h>
  81. #include <linux/bitmap.h>
  82. #include <linux/ethtool.h>
  83. #include <arpa/inet.h>
  84. #include <net/if.h>
  85. #include <locale.h>
  86. #include <poll.h>
  87. #include <pthread.h>
  88. #include <signal.h>
  89. #include <stdio.h>
  90. #include <stdlib.h>
  91. #include <libgen.h>
  92. #include <string.h>
  93. #include <stddef.h>
  94. #include <sys/mman.h>
  95. #include <sys/socket.h>
  96. #include <sys/time.h>
  97. #include <sys/types.h>
  98. #include <unistd.h>
  99. #include "xsk_xdp_progs.skel.h"
  100. #include "xsk.h"
  101. #include "xskxceiver.h"
  102. #include <bpf/bpf.h>
  103. #include <linux/filter.h>
  104. #include "../kselftest.h"
  105. #include "xsk_xdp_common.h"
  106. #include <network_helpers.h>
  107. static bool opt_verbose;
  108. static bool opt_print_tests;
  109. static enum test_mode opt_mode = TEST_MODE_ALL;
  110. static u32 opt_run_test = RUN_ALL_TESTS;
  111. void test__fail(void) { /* for network_helpers.c */ }
  112. static void __exit_with_error(int error, const char *file, const char *func, int line)
  113. {
  114. ksft_test_result_fail("[%s:%s:%i]: ERROR: %d/\"%s\"\n", file, func, line, error,
  115. strerror(error));
  116. ksft_exit_xfail();
  117. }
  118. #define exit_with_error(error) __exit_with_error(error, __FILE__, __func__, __LINE__)
  119. #define busy_poll_string(test) (test)->ifobj_tx->busy_poll ? "BUSY-POLL " : ""
  120. static char *mode_string(struct test_spec *test)
  121. {
  122. switch (test->mode) {
  123. case TEST_MODE_SKB:
  124. return "SKB";
  125. case TEST_MODE_DRV:
  126. return "DRV";
  127. case TEST_MODE_ZC:
  128. return "ZC";
  129. default:
  130. return "BOGUS";
  131. }
  132. }
  133. static void report_failure(struct test_spec *test)
  134. {
  135. if (test->fail)
  136. return;
  137. ksft_test_result_fail("FAIL: %s %s%s\n", mode_string(test), busy_poll_string(test),
  138. test->name);
  139. test->fail = true;
  140. }
  141. /* The payload is a word consisting of a packet sequence number in the upper
  142. * 16-bits and a intra packet data sequence number in the lower 16 bits. So the 3rd packet's
  143. * 5th word of data will contain the number (2<<16) | 4 as they are numbered from 0.
  144. */
  145. static void write_payload(void *dest, u32 pkt_nb, u32 start, u32 size)
  146. {
  147. u32 *ptr = (u32 *)dest, i;
  148. start /= sizeof(*ptr);
  149. size /= sizeof(*ptr);
  150. for (i = 0; i < size; i++)
  151. ptr[i] = htonl(pkt_nb << 16 | (i + start));
  152. }
  153. static void gen_eth_hdr(struct xsk_socket_info *xsk, struct ethhdr *eth_hdr)
  154. {
  155. memcpy(eth_hdr->h_dest, xsk->dst_mac, ETH_ALEN);
  156. memcpy(eth_hdr->h_source, xsk->src_mac, ETH_ALEN);
  157. eth_hdr->h_proto = htons(ETH_P_LOOPBACK);
  158. }
  159. static bool is_umem_valid(struct ifobject *ifobj)
  160. {
  161. return !!ifobj->umem->umem;
  162. }
  163. static u32 mode_to_xdp_flags(enum test_mode mode)
  164. {
  165. return (mode == TEST_MODE_SKB) ? XDP_FLAGS_SKB_MODE : XDP_FLAGS_DRV_MODE;
  166. }
  167. static u64 umem_size(struct xsk_umem_info *umem)
  168. {
  169. return umem->num_frames * umem->frame_size;
  170. }
  171. static int xsk_configure_umem(struct ifobject *ifobj, struct xsk_umem_info *umem, void *buffer,
  172. u64 size)
  173. {
  174. struct xsk_umem_config cfg = {
  175. .fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS,
  176. .comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
  177. .frame_size = umem->frame_size,
  178. .frame_headroom = umem->frame_headroom,
  179. .flags = XSK_UMEM__DEFAULT_FLAGS
  180. };
  181. int ret;
  182. if (umem->fill_size)
  183. cfg.fill_size = umem->fill_size;
  184. if (umem->comp_size)
  185. cfg.comp_size = umem->comp_size;
  186. if (umem->unaligned_mode)
  187. cfg.flags |= XDP_UMEM_UNALIGNED_CHUNK_FLAG;
  188. ret = xsk_umem__create(&umem->umem, buffer, size,
  189. &umem->fq, &umem->cq, &cfg);
  190. if (ret)
  191. return ret;
  192. umem->buffer = buffer;
  193. if (ifobj->shared_umem && ifobj->rx_on) {
  194. umem->base_addr = umem_size(umem);
  195. umem->next_buffer = umem_size(umem);
  196. }
  197. return 0;
  198. }
  199. static u64 umem_alloc_buffer(struct xsk_umem_info *umem)
  200. {
  201. u64 addr;
  202. addr = umem->next_buffer;
  203. umem->next_buffer += umem->frame_size;
  204. if (umem->next_buffer >= umem->base_addr + umem_size(umem))
  205. umem->next_buffer = umem->base_addr;
  206. return addr;
  207. }
  208. static void umem_reset_alloc(struct xsk_umem_info *umem)
  209. {
  210. umem->next_buffer = 0;
  211. }
  212. static void enable_busy_poll(struct xsk_socket_info *xsk)
  213. {
  214. int sock_opt;
  215. sock_opt = 1;
  216. if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_PREFER_BUSY_POLL,
  217. (void *)&sock_opt, sizeof(sock_opt)) < 0)
  218. exit_with_error(errno);
  219. sock_opt = 20;
  220. if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_BUSY_POLL,
  221. (void *)&sock_opt, sizeof(sock_opt)) < 0)
  222. exit_with_error(errno);
  223. sock_opt = xsk->batch_size;
  224. if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_BUSY_POLL_BUDGET,
  225. (void *)&sock_opt, sizeof(sock_opt)) < 0)
  226. exit_with_error(errno);
  227. }
  228. static int __xsk_configure_socket(struct xsk_socket_info *xsk, struct xsk_umem_info *umem,
  229. struct ifobject *ifobject, bool shared)
  230. {
  231. struct xsk_socket_config cfg = {};
  232. struct xsk_ring_cons *rxr;
  233. struct xsk_ring_prod *txr;
  234. xsk->umem = umem;
  235. cfg.rx_size = xsk->rxqsize;
  236. cfg.tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
  237. cfg.bind_flags = ifobject->bind_flags;
  238. if (shared)
  239. cfg.bind_flags |= XDP_SHARED_UMEM;
  240. if (ifobject->mtu > MAX_ETH_PKT_SIZE)
  241. cfg.bind_flags |= XDP_USE_SG;
  242. if (umem->comp_size)
  243. cfg.tx_size = umem->comp_size;
  244. if (umem->fill_size)
  245. cfg.rx_size = umem->fill_size;
  246. txr = ifobject->tx_on ? &xsk->tx : NULL;
  247. rxr = ifobject->rx_on ? &xsk->rx : NULL;
  248. return xsk_socket__create(&xsk->xsk, ifobject->ifindex, 0, umem->umem, rxr, txr, &cfg);
  249. }
  250. static bool ifobj_zc_avail(struct ifobject *ifobject)
  251. {
  252. size_t umem_sz = DEFAULT_UMEM_BUFFERS * XSK_UMEM__DEFAULT_FRAME_SIZE;
  253. int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
  254. struct xsk_socket_info *xsk;
  255. struct xsk_umem_info *umem;
  256. bool zc_avail = false;
  257. void *bufs;
  258. int ret;
  259. bufs = mmap(NULL, umem_sz, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
  260. if (bufs == MAP_FAILED)
  261. exit_with_error(errno);
  262. umem = calloc(1, sizeof(struct xsk_umem_info));
  263. if (!umem) {
  264. munmap(bufs, umem_sz);
  265. exit_with_error(ENOMEM);
  266. }
  267. umem->frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE;
  268. ret = xsk_configure_umem(ifobject, umem, bufs, umem_sz);
  269. if (ret)
  270. exit_with_error(-ret);
  271. xsk = calloc(1, sizeof(struct xsk_socket_info));
  272. if (!xsk)
  273. goto out;
  274. ifobject->bind_flags = XDP_USE_NEED_WAKEUP | XDP_ZEROCOPY;
  275. ifobject->rx_on = true;
  276. xsk->rxqsize = XSK_RING_CONS__DEFAULT_NUM_DESCS;
  277. ret = __xsk_configure_socket(xsk, umem, ifobject, false);
  278. if (!ret)
  279. zc_avail = true;
  280. xsk_socket__delete(xsk->xsk);
  281. free(xsk);
  282. out:
  283. munmap(umem->buffer, umem_sz);
  284. xsk_umem__delete(umem->umem);
  285. free(umem);
  286. return zc_avail;
  287. }
  288. #define MAX_SKB_FRAGS_PATH "/proc/sys/net/core/max_skb_frags"
  289. static unsigned int get_max_skb_frags(void)
  290. {
  291. unsigned int max_skb_frags = 0;
  292. FILE *file;
  293. file = fopen(MAX_SKB_FRAGS_PATH, "r");
  294. if (!file) {
  295. ksft_print_msg("Error opening %s\n", MAX_SKB_FRAGS_PATH);
  296. return 0;
  297. }
  298. if (fscanf(file, "%u", &max_skb_frags) != 1)
  299. ksft_print_msg("Error reading %s\n", MAX_SKB_FRAGS_PATH);
  300. fclose(file);
  301. return max_skb_frags;
  302. }
  303. static struct option long_options[] = {
  304. {"interface", required_argument, 0, 'i'},
  305. {"busy-poll", no_argument, 0, 'b'},
  306. {"verbose", no_argument, 0, 'v'},
  307. {"mode", required_argument, 0, 'm'},
  308. {"list", no_argument, 0, 'l'},
  309. {"test", required_argument, 0, 't'},
  310. {"help", no_argument, 0, 'h'},
  311. {0, 0, 0, 0}
  312. };
  313. static void print_usage(char **argv)
  314. {
  315. const char *str =
  316. " Usage: xskxceiver [OPTIONS]\n"
  317. " Options:\n"
  318. " -i, --interface Use interface\n"
  319. " -v, --verbose Verbose output\n"
  320. " -b, --busy-poll Enable busy poll\n"
  321. " -m, --mode Run only mode skb, drv, or zc\n"
  322. " -l, --list List all available tests\n"
  323. " -t, --test Run a specific test. Enter number from -l option.\n"
  324. " -h, --help Display this help and exit\n";
  325. ksft_print_msg(str, basename(argv[0]));
  326. ksft_exit_xfail();
  327. }
  328. static bool validate_interface(struct ifobject *ifobj)
  329. {
  330. if (!strcmp(ifobj->ifname, ""))
  331. return false;
  332. return true;
  333. }
  334. static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj_rx, int argc,
  335. char **argv)
  336. {
  337. struct ifobject *ifobj;
  338. u32 interface_nb = 0;
  339. int option_index, c;
  340. opterr = 0;
  341. for (;;) {
  342. c = getopt_long(argc, argv, "i:vbm:lt:", long_options, &option_index);
  343. if (c == -1)
  344. break;
  345. switch (c) {
  346. case 'i':
  347. if (interface_nb == 0)
  348. ifobj = ifobj_tx;
  349. else if (interface_nb == 1)
  350. ifobj = ifobj_rx;
  351. else
  352. break;
  353. memcpy(ifobj->ifname, optarg,
  354. min_t(size_t, MAX_INTERFACE_NAME_CHARS, strlen(optarg)));
  355. ifobj->ifindex = if_nametoindex(ifobj->ifname);
  356. if (!ifobj->ifindex)
  357. exit_with_error(errno);
  358. interface_nb++;
  359. break;
  360. case 'v':
  361. opt_verbose = true;
  362. break;
  363. case 'b':
  364. ifobj_tx->busy_poll = true;
  365. ifobj_rx->busy_poll = true;
  366. break;
  367. case 'm':
  368. if (!strncmp("skb", optarg, strlen(optarg)))
  369. opt_mode = TEST_MODE_SKB;
  370. else if (!strncmp("drv", optarg, strlen(optarg)))
  371. opt_mode = TEST_MODE_DRV;
  372. else if (!strncmp("zc", optarg, strlen(optarg)))
  373. opt_mode = TEST_MODE_ZC;
  374. else
  375. print_usage(argv);
  376. break;
  377. case 'l':
  378. opt_print_tests = true;
  379. break;
  380. case 't':
  381. errno = 0;
  382. opt_run_test = strtol(optarg, NULL, 0);
  383. if (errno)
  384. print_usage(argv);
  385. break;
  386. case 'h':
  387. default:
  388. print_usage(argv);
  389. }
  390. }
  391. }
  392. static int set_ring_size(struct ifobject *ifobj)
  393. {
  394. int ret;
  395. u32 ctr = 0;
  396. while (ctr++ < SOCK_RECONF_CTR) {
  397. ret = set_hw_ring_size(ifobj->ifname, &ifobj->ring);
  398. if (!ret)
  399. break;
  400. /* Retry if it fails */
  401. if (ctr >= SOCK_RECONF_CTR || errno != EBUSY)
  402. return -errno;
  403. usleep(USLEEP_MAX);
  404. }
  405. return ret;
  406. }
  407. static int hw_ring_size_reset(struct ifobject *ifobj)
  408. {
  409. ifobj->ring.tx_pending = ifobj->set_ring.default_tx;
  410. ifobj->ring.rx_pending = ifobj->set_ring.default_rx;
  411. return set_ring_size(ifobj);
  412. }
  413. static void __test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx,
  414. struct ifobject *ifobj_rx)
  415. {
  416. u32 i, j;
  417. for (i = 0; i < MAX_INTERFACES; i++) {
  418. struct ifobject *ifobj = i ? ifobj_rx : ifobj_tx;
  419. ifobj->xsk = &ifobj->xsk_arr[0];
  420. ifobj->use_poll = false;
  421. ifobj->use_fill_ring = true;
  422. ifobj->release_rx = true;
  423. ifobj->validation_func = NULL;
  424. ifobj->use_metadata = false;
  425. if (i == 0) {
  426. ifobj->rx_on = false;
  427. ifobj->tx_on = true;
  428. } else {
  429. ifobj->rx_on = true;
  430. ifobj->tx_on = false;
  431. }
  432. memset(ifobj->umem, 0, sizeof(*ifobj->umem));
  433. ifobj->umem->num_frames = DEFAULT_UMEM_BUFFERS;
  434. ifobj->umem->frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE;
  435. for (j = 0; j < MAX_SOCKETS; j++) {
  436. memset(&ifobj->xsk_arr[j], 0, sizeof(ifobj->xsk_arr[j]));
  437. ifobj->xsk_arr[j].rxqsize = XSK_RING_CONS__DEFAULT_NUM_DESCS;
  438. ifobj->xsk_arr[j].batch_size = DEFAULT_BATCH_SIZE;
  439. if (i == 0)
  440. ifobj->xsk_arr[j].pkt_stream = test->tx_pkt_stream_default;
  441. else
  442. ifobj->xsk_arr[j].pkt_stream = test->rx_pkt_stream_default;
  443. memcpy(ifobj->xsk_arr[j].src_mac, g_mac, ETH_ALEN);
  444. memcpy(ifobj->xsk_arr[j].dst_mac, g_mac, ETH_ALEN);
  445. ifobj->xsk_arr[j].src_mac[5] += ((j * 2) + 0);
  446. ifobj->xsk_arr[j].dst_mac[5] += ((j * 2) + 1);
  447. }
  448. }
  449. if (ifobj_tx->hw_ring_size_supp)
  450. hw_ring_size_reset(ifobj_tx);
  451. test->ifobj_tx = ifobj_tx;
  452. test->ifobj_rx = ifobj_rx;
  453. test->current_step = 0;
  454. test->total_steps = 1;
  455. test->nb_sockets = 1;
  456. test->fail = false;
  457. test->set_ring = false;
  458. test->mtu = MAX_ETH_PKT_SIZE;
  459. test->xdp_prog_rx = ifobj_rx->xdp_progs->progs.xsk_def_prog;
  460. test->xskmap_rx = ifobj_rx->xdp_progs->maps.xsk;
  461. test->xdp_prog_tx = ifobj_tx->xdp_progs->progs.xsk_def_prog;
  462. test->xskmap_tx = ifobj_tx->xdp_progs->maps.xsk;
  463. }
  464. static void test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx,
  465. struct ifobject *ifobj_rx, enum test_mode mode,
  466. const struct test_spec *test_to_run)
  467. {
  468. struct pkt_stream *tx_pkt_stream;
  469. struct pkt_stream *rx_pkt_stream;
  470. u32 i;
  471. tx_pkt_stream = test->tx_pkt_stream_default;
  472. rx_pkt_stream = test->rx_pkt_stream_default;
  473. memset(test, 0, sizeof(*test));
  474. test->tx_pkt_stream_default = tx_pkt_stream;
  475. test->rx_pkt_stream_default = rx_pkt_stream;
  476. for (i = 0; i < MAX_INTERFACES; i++) {
  477. struct ifobject *ifobj = i ? ifobj_rx : ifobj_tx;
  478. ifobj->bind_flags = XDP_USE_NEED_WAKEUP;
  479. if (mode == TEST_MODE_ZC)
  480. ifobj->bind_flags |= XDP_ZEROCOPY;
  481. else
  482. ifobj->bind_flags |= XDP_COPY;
  483. }
  484. strncpy(test->name, test_to_run->name, MAX_TEST_NAME_SIZE);
  485. test->test_func = test_to_run->test_func;
  486. test->mode = mode;
  487. __test_spec_init(test, ifobj_tx, ifobj_rx);
  488. }
  489. static void test_spec_reset(struct test_spec *test)
  490. {
  491. __test_spec_init(test, test->ifobj_tx, test->ifobj_rx);
  492. }
  493. static void test_spec_set_xdp_prog(struct test_spec *test, struct bpf_program *xdp_prog_rx,
  494. struct bpf_program *xdp_prog_tx, struct bpf_map *xskmap_rx,
  495. struct bpf_map *xskmap_tx)
  496. {
  497. test->xdp_prog_rx = xdp_prog_rx;
  498. test->xdp_prog_tx = xdp_prog_tx;
  499. test->xskmap_rx = xskmap_rx;
  500. test->xskmap_tx = xskmap_tx;
  501. }
  502. static int test_spec_set_mtu(struct test_spec *test, int mtu)
  503. {
  504. int err;
  505. if (test->ifobj_rx->mtu != mtu) {
  506. err = xsk_set_mtu(test->ifobj_rx->ifindex, mtu);
  507. if (err)
  508. return err;
  509. test->ifobj_rx->mtu = mtu;
  510. }
  511. if (test->ifobj_tx->mtu != mtu) {
  512. err = xsk_set_mtu(test->ifobj_tx->ifindex, mtu);
  513. if (err)
  514. return err;
  515. test->ifobj_tx->mtu = mtu;
  516. }
  517. return 0;
  518. }
  519. static void pkt_stream_reset(struct pkt_stream *pkt_stream)
  520. {
  521. if (pkt_stream) {
  522. pkt_stream->current_pkt_nb = 0;
  523. pkt_stream->nb_rx_pkts = 0;
  524. }
  525. }
  526. static struct pkt *pkt_stream_get_next_tx_pkt(struct pkt_stream *pkt_stream)
  527. {
  528. if (pkt_stream->current_pkt_nb >= pkt_stream->nb_pkts)
  529. return NULL;
  530. return &pkt_stream->pkts[pkt_stream->current_pkt_nb++];
  531. }
  532. static struct pkt *pkt_stream_get_next_rx_pkt(struct pkt_stream *pkt_stream, u32 *pkts_sent)
  533. {
  534. while (pkt_stream->current_pkt_nb < pkt_stream->nb_pkts) {
  535. (*pkts_sent)++;
  536. if (pkt_stream->pkts[pkt_stream->current_pkt_nb].valid)
  537. return &pkt_stream->pkts[pkt_stream->current_pkt_nb++];
  538. pkt_stream->current_pkt_nb++;
  539. }
  540. return NULL;
  541. }
  542. static void pkt_stream_delete(struct pkt_stream *pkt_stream)
  543. {
  544. free(pkt_stream->pkts);
  545. free(pkt_stream);
  546. }
  547. static void pkt_stream_restore_default(struct test_spec *test)
  548. {
  549. struct pkt_stream *tx_pkt_stream = test->ifobj_tx->xsk->pkt_stream;
  550. struct pkt_stream *rx_pkt_stream = test->ifobj_rx->xsk->pkt_stream;
  551. if (tx_pkt_stream != test->tx_pkt_stream_default) {
  552. pkt_stream_delete(test->ifobj_tx->xsk->pkt_stream);
  553. test->ifobj_tx->xsk->pkt_stream = test->tx_pkt_stream_default;
  554. }
  555. if (rx_pkt_stream != test->rx_pkt_stream_default) {
  556. pkt_stream_delete(test->ifobj_rx->xsk->pkt_stream);
  557. test->ifobj_rx->xsk->pkt_stream = test->rx_pkt_stream_default;
  558. }
  559. }
  560. static struct pkt_stream *__pkt_stream_alloc(u32 nb_pkts)
  561. {
  562. struct pkt_stream *pkt_stream;
  563. pkt_stream = calloc(1, sizeof(*pkt_stream));
  564. if (!pkt_stream)
  565. return NULL;
  566. pkt_stream->pkts = calloc(nb_pkts, sizeof(*pkt_stream->pkts));
  567. if (!pkt_stream->pkts) {
  568. free(pkt_stream);
  569. return NULL;
  570. }
  571. pkt_stream->nb_pkts = nb_pkts;
  572. return pkt_stream;
  573. }
  574. static bool pkt_continues(u32 options)
  575. {
  576. return options & XDP_PKT_CONTD;
  577. }
  578. static u32 ceil_u32(u32 a, u32 b)
  579. {
  580. return (a + b - 1) / b;
  581. }
  582. static u32 pkt_nb_frags(u32 frame_size, struct pkt_stream *pkt_stream, struct pkt *pkt)
  583. {
  584. u32 nb_frags = 1, next_frag;
  585. if (!pkt)
  586. return 1;
  587. if (!pkt_stream->verbatim) {
  588. if (!pkt->valid || !pkt->len)
  589. return 1;
  590. return ceil_u32(pkt->len, frame_size);
  591. }
  592. /* Search for the end of the packet in verbatim mode */
  593. if (!pkt_continues(pkt->options))
  594. return nb_frags;
  595. next_frag = pkt_stream->current_pkt_nb;
  596. pkt++;
  597. while (next_frag++ < pkt_stream->nb_pkts) {
  598. nb_frags++;
  599. if (!pkt_continues(pkt->options) || !pkt->valid)
  600. break;
  601. pkt++;
  602. }
  603. return nb_frags;
  604. }
  605. static bool set_pkt_valid(int offset, u32 len)
  606. {
  607. return len <= MAX_ETH_JUMBO_SIZE;
  608. }
  609. static void pkt_set(struct pkt_stream *pkt_stream, struct pkt *pkt, int offset, u32 len)
  610. {
  611. pkt->offset = offset;
  612. pkt->len = len;
  613. pkt->valid = set_pkt_valid(offset, len);
  614. }
  615. static void pkt_stream_pkt_set(struct pkt_stream *pkt_stream, struct pkt *pkt, int offset, u32 len)
  616. {
  617. bool prev_pkt_valid = pkt->valid;
  618. pkt_set(pkt_stream, pkt, offset, len);
  619. pkt_stream->nb_valid_entries += pkt->valid - prev_pkt_valid;
  620. }
  621. static u32 pkt_get_buffer_len(struct xsk_umem_info *umem, u32 len)
  622. {
  623. return ceil_u32(len, umem->frame_size) * umem->frame_size;
  624. }
  625. static struct pkt_stream *__pkt_stream_generate(u32 nb_pkts, u32 pkt_len, u32 nb_start, u32 nb_off)
  626. {
  627. struct pkt_stream *pkt_stream;
  628. u32 i;
  629. pkt_stream = __pkt_stream_alloc(nb_pkts);
  630. if (!pkt_stream)
  631. exit_with_error(ENOMEM);
  632. pkt_stream->nb_pkts = nb_pkts;
  633. pkt_stream->max_pkt_len = pkt_len;
  634. for (i = 0; i < nb_pkts; i++) {
  635. struct pkt *pkt = &pkt_stream->pkts[i];
  636. pkt_stream_pkt_set(pkt_stream, pkt, 0, pkt_len);
  637. pkt->pkt_nb = nb_start + i * nb_off;
  638. }
  639. return pkt_stream;
  640. }
  641. static struct pkt_stream *pkt_stream_generate(u32 nb_pkts, u32 pkt_len)
  642. {
  643. return __pkt_stream_generate(nb_pkts, pkt_len, 0, 1);
  644. }
  645. static struct pkt_stream *pkt_stream_clone(struct pkt_stream *pkt_stream)
  646. {
  647. return pkt_stream_generate(pkt_stream->nb_pkts, pkt_stream->pkts[0].len);
  648. }
  649. static void pkt_stream_replace(struct test_spec *test, u32 nb_pkts, u32 pkt_len)
  650. {
  651. struct pkt_stream *pkt_stream;
  652. pkt_stream = pkt_stream_generate(nb_pkts, pkt_len);
  653. test->ifobj_tx->xsk->pkt_stream = pkt_stream;
  654. pkt_stream = pkt_stream_generate(nb_pkts, pkt_len);
  655. test->ifobj_rx->xsk->pkt_stream = pkt_stream;
  656. }
  657. static void __pkt_stream_replace_half(struct ifobject *ifobj, u32 pkt_len,
  658. int offset)
  659. {
  660. struct pkt_stream *pkt_stream;
  661. u32 i;
  662. pkt_stream = pkt_stream_clone(ifobj->xsk->pkt_stream);
  663. for (i = 1; i < ifobj->xsk->pkt_stream->nb_pkts; i += 2)
  664. pkt_stream_pkt_set(pkt_stream, &pkt_stream->pkts[i], offset, pkt_len);
  665. ifobj->xsk->pkt_stream = pkt_stream;
  666. }
  667. static void pkt_stream_replace_half(struct test_spec *test, u32 pkt_len, int offset)
  668. {
  669. __pkt_stream_replace_half(test->ifobj_tx, pkt_len, offset);
  670. __pkt_stream_replace_half(test->ifobj_rx, pkt_len, offset);
  671. }
  672. static void pkt_stream_receive_half(struct test_spec *test)
  673. {
  674. struct pkt_stream *pkt_stream = test->ifobj_tx->xsk->pkt_stream;
  675. u32 i;
  676. test->ifobj_rx->xsk->pkt_stream = pkt_stream_generate(pkt_stream->nb_pkts,
  677. pkt_stream->pkts[0].len);
  678. pkt_stream = test->ifobj_rx->xsk->pkt_stream;
  679. for (i = 1; i < pkt_stream->nb_pkts; i += 2)
  680. pkt_stream->pkts[i].valid = false;
  681. pkt_stream->nb_valid_entries /= 2;
  682. }
  683. static void pkt_stream_even_odd_sequence(struct test_spec *test)
  684. {
  685. struct pkt_stream *pkt_stream;
  686. u32 i;
  687. for (i = 0; i < test->nb_sockets; i++) {
  688. pkt_stream = test->ifobj_tx->xsk_arr[i].pkt_stream;
  689. pkt_stream = __pkt_stream_generate(pkt_stream->nb_pkts / 2,
  690. pkt_stream->pkts[0].len, i, 2);
  691. test->ifobj_tx->xsk_arr[i].pkt_stream = pkt_stream;
  692. pkt_stream = test->ifobj_rx->xsk_arr[i].pkt_stream;
  693. pkt_stream = __pkt_stream_generate(pkt_stream->nb_pkts / 2,
  694. pkt_stream->pkts[0].len, i, 2);
  695. test->ifobj_rx->xsk_arr[i].pkt_stream = pkt_stream;
  696. }
  697. }
  698. static u64 pkt_get_addr(struct pkt *pkt, struct xsk_umem_info *umem)
  699. {
  700. if (!pkt->valid)
  701. return pkt->offset;
  702. return pkt->offset + umem_alloc_buffer(umem);
  703. }
  704. static void pkt_stream_cancel(struct pkt_stream *pkt_stream)
  705. {
  706. pkt_stream->current_pkt_nb--;
  707. }
  708. static void pkt_generate(struct xsk_socket_info *xsk, struct xsk_umem_info *umem, u64 addr, u32 len,
  709. u32 pkt_nb, u32 bytes_written)
  710. {
  711. void *data = xsk_umem__get_data(umem->buffer, addr);
  712. if (len < MIN_PKT_SIZE)
  713. return;
  714. if (!bytes_written) {
  715. gen_eth_hdr(xsk, data);
  716. len -= PKT_HDR_SIZE;
  717. data += PKT_HDR_SIZE;
  718. } else {
  719. bytes_written -= PKT_HDR_SIZE;
  720. }
  721. write_payload(data, pkt_nb, bytes_written, len);
  722. }
  723. static struct pkt_stream *__pkt_stream_generate_custom(struct ifobject *ifobj, struct pkt *frames,
  724. u32 nb_frames, bool verbatim)
  725. {
  726. u32 i, len = 0, pkt_nb = 0, payload = 0;
  727. struct pkt_stream *pkt_stream;
  728. pkt_stream = __pkt_stream_alloc(nb_frames);
  729. if (!pkt_stream)
  730. exit_with_error(ENOMEM);
  731. for (i = 0; i < nb_frames; i++) {
  732. struct pkt *pkt = &pkt_stream->pkts[pkt_nb];
  733. struct pkt *frame = &frames[i];
  734. pkt->offset = frame->offset;
  735. if (verbatim) {
  736. *pkt = *frame;
  737. pkt->pkt_nb = payload;
  738. if (!frame->valid || !pkt_continues(frame->options))
  739. payload++;
  740. } else {
  741. if (frame->valid)
  742. len += frame->len;
  743. if (frame->valid && pkt_continues(frame->options))
  744. continue;
  745. pkt->pkt_nb = pkt_nb;
  746. pkt->len = len;
  747. pkt->valid = frame->valid;
  748. pkt->options = 0;
  749. len = 0;
  750. }
  751. print_verbose("offset: %d len: %u valid: %u options: %u pkt_nb: %u\n",
  752. pkt->offset, pkt->len, pkt->valid, pkt->options, pkt->pkt_nb);
  753. if (pkt->valid && pkt->len > pkt_stream->max_pkt_len)
  754. pkt_stream->max_pkt_len = pkt->len;
  755. if (pkt->valid)
  756. pkt_stream->nb_valid_entries++;
  757. pkt_nb++;
  758. }
  759. pkt_stream->nb_pkts = pkt_nb;
  760. pkt_stream->verbatim = verbatim;
  761. return pkt_stream;
  762. }
  763. static void pkt_stream_generate_custom(struct test_spec *test, struct pkt *pkts, u32 nb_pkts)
  764. {
  765. struct pkt_stream *pkt_stream;
  766. pkt_stream = __pkt_stream_generate_custom(test->ifobj_tx, pkts, nb_pkts, true);
  767. test->ifobj_tx->xsk->pkt_stream = pkt_stream;
  768. pkt_stream = __pkt_stream_generate_custom(test->ifobj_rx, pkts, nb_pkts, false);
  769. test->ifobj_rx->xsk->pkt_stream = pkt_stream;
  770. }
  771. static void pkt_print_data(u32 *data, u32 cnt)
  772. {
  773. u32 i;
  774. for (i = 0; i < cnt; i++) {
  775. u32 seqnum, pkt_nb;
  776. seqnum = ntohl(*data) & 0xffff;
  777. pkt_nb = ntohl(*data) >> 16;
  778. ksft_print_msg("%u:%u ", pkt_nb, seqnum);
  779. data++;
  780. }
  781. }
  782. static void pkt_dump(void *pkt, u32 len, bool eth_header)
  783. {
  784. struct ethhdr *ethhdr = pkt;
  785. u32 i, *data;
  786. if (eth_header) {
  787. /*extract L2 frame */
  788. ksft_print_msg("DEBUG>> L2: dst mac: ");
  789. for (i = 0; i < ETH_ALEN; i++)
  790. ksft_print_msg("%02X", ethhdr->h_dest[i]);
  791. ksft_print_msg("\nDEBUG>> L2: src mac: ");
  792. for (i = 0; i < ETH_ALEN; i++)
  793. ksft_print_msg("%02X", ethhdr->h_source[i]);
  794. data = pkt + PKT_HDR_SIZE;
  795. } else {
  796. data = pkt;
  797. }
  798. /*extract L5 frame */
  799. ksft_print_msg("\nDEBUG>> L5: seqnum: ");
  800. pkt_print_data(data, PKT_DUMP_NB_TO_PRINT);
  801. ksft_print_msg("....");
  802. if (len > PKT_DUMP_NB_TO_PRINT * sizeof(u32)) {
  803. ksft_print_msg("\n.... ");
  804. pkt_print_data(data + len / sizeof(u32) - PKT_DUMP_NB_TO_PRINT,
  805. PKT_DUMP_NB_TO_PRINT);
  806. }
  807. ksft_print_msg("\n---------------------------------------\n");
  808. }
  809. static bool is_offset_correct(struct xsk_umem_info *umem, struct pkt *pkt, u64 addr)
  810. {
  811. u32 headroom = umem->unaligned_mode ? 0 : umem->frame_headroom;
  812. u32 offset = addr % umem->frame_size, expected_offset;
  813. int pkt_offset = pkt->valid ? pkt->offset : 0;
  814. if (!umem->unaligned_mode)
  815. pkt_offset = 0;
  816. expected_offset = (pkt_offset + headroom + XDP_PACKET_HEADROOM) % umem->frame_size;
  817. if (offset == expected_offset)
  818. return true;
  819. ksft_print_msg("[%s] expected [%u], got [%u]\n", __func__, expected_offset, offset);
  820. return false;
  821. }
  822. static bool is_metadata_correct(struct pkt *pkt, void *buffer, u64 addr)
  823. {
  824. void *data = xsk_umem__get_data(buffer, addr);
  825. struct xdp_info *meta = data - sizeof(struct xdp_info);
  826. if (meta->count != pkt->pkt_nb) {
  827. ksft_print_msg("[%s] expected meta_count [%d], got meta_count [%llu]\n",
  828. __func__, pkt->pkt_nb,
  829. (unsigned long long)meta->count);
  830. return false;
  831. }
  832. return true;
  833. }
  834. static bool is_frag_valid(struct xsk_umem_info *umem, u64 addr, u32 len, u32 expected_pkt_nb,
  835. u32 bytes_processed)
  836. {
  837. u32 seqnum, pkt_nb, *pkt_data, words_to_end, expected_seqnum;
  838. void *data = xsk_umem__get_data(umem->buffer, addr);
  839. addr -= umem->base_addr;
  840. if (addr >= umem->num_frames * umem->frame_size ||
  841. addr + len > umem->num_frames * umem->frame_size) {
  842. ksft_print_msg("Frag invalid addr: %llx len: %u\n",
  843. (unsigned long long)addr, len);
  844. return false;
  845. }
  846. if (!umem->unaligned_mode && addr % umem->frame_size + len > umem->frame_size) {
  847. ksft_print_msg("Frag crosses frame boundary addr: %llx len: %u\n",
  848. (unsigned long long)addr, len);
  849. return false;
  850. }
  851. pkt_data = data;
  852. if (!bytes_processed) {
  853. pkt_data += PKT_HDR_SIZE / sizeof(*pkt_data);
  854. len -= PKT_HDR_SIZE;
  855. } else {
  856. bytes_processed -= PKT_HDR_SIZE;
  857. }
  858. expected_seqnum = bytes_processed / sizeof(*pkt_data);
  859. seqnum = ntohl(*pkt_data) & 0xffff;
  860. pkt_nb = ntohl(*pkt_data) >> 16;
  861. if (expected_pkt_nb != pkt_nb) {
  862. ksft_print_msg("[%s] expected pkt_nb [%u], got pkt_nb [%u]\n",
  863. __func__, expected_pkt_nb, pkt_nb);
  864. goto error;
  865. }
  866. if (expected_seqnum != seqnum) {
  867. ksft_print_msg("[%s] expected seqnum at start [%u], got seqnum [%u]\n",
  868. __func__, expected_seqnum, seqnum);
  869. goto error;
  870. }
  871. words_to_end = len / sizeof(*pkt_data) - 1;
  872. pkt_data += words_to_end;
  873. seqnum = ntohl(*pkt_data) & 0xffff;
  874. expected_seqnum += words_to_end;
  875. if (expected_seqnum != seqnum) {
  876. ksft_print_msg("[%s] expected seqnum at end [%u], got seqnum [%u]\n",
  877. __func__, expected_seqnum, seqnum);
  878. goto error;
  879. }
  880. return true;
  881. error:
  882. pkt_dump(data, len, !bytes_processed);
  883. return false;
  884. }
  885. static bool is_pkt_valid(struct pkt *pkt, void *buffer, u64 addr, u32 len)
  886. {
  887. if (pkt->len != len) {
  888. ksft_print_msg("[%s] expected packet length [%d], got length [%d]\n",
  889. __func__, pkt->len, len);
  890. pkt_dump(xsk_umem__get_data(buffer, addr), len, true);
  891. return false;
  892. }
  893. return true;
  894. }
  895. static int kick_tx(struct xsk_socket_info *xsk)
  896. {
  897. int ret;
  898. ret = sendto(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, 0);
  899. if (ret >= 0)
  900. return TEST_PASS;
  901. if (errno == ENOBUFS || errno == EAGAIN || errno == EBUSY || errno == ENETDOWN) {
  902. usleep(100);
  903. return TEST_PASS;
  904. }
  905. return TEST_FAILURE;
  906. }
  907. static int kick_rx(struct xsk_socket_info *xsk)
  908. {
  909. int ret;
  910. ret = recvfrom(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, NULL);
  911. if (ret < 0)
  912. return TEST_FAILURE;
  913. return TEST_PASS;
  914. }
  915. static int complete_pkts(struct xsk_socket_info *xsk, int batch_size)
  916. {
  917. unsigned int rcvd;
  918. u32 idx;
  919. int ret;
  920. if (xsk_ring_prod__needs_wakeup(&xsk->tx)) {
  921. ret = kick_tx(xsk);
  922. if (ret)
  923. return TEST_FAILURE;
  924. }
  925. rcvd = xsk_ring_cons__peek(&xsk->umem->cq, batch_size, &idx);
  926. if (rcvd) {
  927. if (rcvd > xsk->outstanding_tx) {
  928. u64 addr = *xsk_ring_cons__comp_addr(&xsk->umem->cq, idx + rcvd - 1);
  929. ksft_print_msg("[%s] Too many packets completed\n", __func__);
  930. ksft_print_msg("Last completion address: %llx\n",
  931. (unsigned long long)addr);
  932. return TEST_FAILURE;
  933. }
  934. xsk_ring_cons__release(&xsk->umem->cq, rcvd);
  935. xsk->outstanding_tx -= rcvd;
  936. }
  937. return TEST_PASS;
  938. }
  939. static int __receive_pkts(struct test_spec *test, struct xsk_socket_info *xsk)
  940. {
  941. u32 frags_processed = 0, nb_frags = 0, pkt_len = 0;
  942. u32 idx_rx = 0, idx_fq = 0, rcvd, pkts_sent = 0;
  943. struct pkt_stream *pkt_stream = xsk->pkt_stream;
  944. struct ifobject *ifobj = test->ifobj_rx;
  945. struct xsk_umem_info *umem = xsk->umem;
  946. struct pollfd fds = { };
  947. struct pkt *pkt;
  948. u64 first_addr = 0;
  949. int ret;
  950. fds.fd = xsk_socket__fd(xsk->xsk);
  951. fds.events = POLLIN;
  952. ret = kick_rx(xsk);
  953. if (ret)
  954. return TEST_FAILURE;
  955. if (ifobj->use_poll) {
  956. ret = poll(&fds, 1, POLL_TMOUT);
  957. if (ret < 0)
  958. return TEST_FAILURE;
  959. if (!ret) {
  960. if (!is_umem_valid(test->ifobj_tx))
  961. return TEST_PASS;
  962. ksft_print_msg("ERROR: [%s] Poll timed out\n", __func__);
  963. return TEST_CONTINUE;
  964. }
  965. if (!(fds.revents & POLLIN))
  966. return TEST_CONTINUE;
  967. }
  968. rcvd = xsk_ring_cons__peek(&xsk->rx, xsk->batch_size, &idx_rx);
  969. if (!rcvd)
  970. return TEST_CONTINUE;
  971. if (ifobj->use_fill_ring) {
  972. ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq);
  973. while (ret != rcvd) {
  974. if (xsk_ring_prod__needs_wakeup(&umem->fq)) {
  975. ret = poll(&fds, 1, POLL_TMOUT);
  976. if (ret < 0)
  977. return TEST_FAILURE;
  978. }
  979. ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq);
  980. }
  981. }
  982. while (frags_processed < rcvd) {
  983. const struct xdp_desc *desc = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++);
  984. u64 addr = desc->addr, orig;
  985. orig = xsk_umem__extract_addr(addr);
  986. addr = xsk_umem__add_offset_to_addr(addr);
  987. if (!nb_frags) {
  988. pkt = pkt_stream_get_next_rx_pkt(pkt_stream, &pkts_sent);
  989. if (!pkt) {
  990. ksft_print_msg("[%s] received too many packets addr: %lx len %u\n",
  991. __func__, addr, desc->len);
  992. return TEST_FAILURE;
  993. }
  994. }
  995. print_verbose("Rx: addr: %lx len: %u options: %u pkt_nb: %u valid: %u\n",
  996. addr, desc->len, desc->options, pkt->pkt_nb, pkt->valid);
  997. if (!is_frag_valid(umem, addr, desc->len, pkt->pkt_nb, pkt_len) ||
  998. !is_offset_correct(umem, pkt, addr) || (ifobj->use_metadata &&
  999. !is_metadata_correct(pkt, umem->buffer, addr)))
  1000. return TEST_FAILURE;
  1001. if (!nb_frags++)
  1002. first_addr = addr;
  1003. frags_processed++;
  1004. pkt_len += desc->len;
  1005. if (ifobj->use_fill_ring)
  1006. *xsk_ring_prod__fill_addr(&umem->fq, idx_fq++) = orig;
  1007. if (pkt_continues(desc->options))
  1008. continue;
  1009. /* The complete packet has been received */
  1010. if (!is_pkt_valid(pkt, umem->buffer, first_addr, pkt_len) ||
  1011. !is_offset_correct(umem, pkt, addr))
  1012. return TEST_FAILURE;
  1013. pkt_stream->nb_rx_pkts++;
  1014. nb_frags = 0;
  1015. pkt_len = 0;
  1016. }
  1017. if (nb_frags) {
  1018. /* In the middle of a packet. Start over from beginning of packet. */
  1019. idx_rx -= nb_frags;
  1020. xsk_ring_cons__cancel(&xsk->rx, nb_frags);
  1021. if (ifobj->use_fill_ring) {
  1022. idx_fq -= nb_frags;
  1023. xsk_ring_prod__cancel(&umem->fq, nb_frags);
  1024. }
  1025. frags_processed -= nb_frags;
  1026. }
  1027. if (ifobj->use_fill_ring)
  1028. xsk_ring_prod__submit(&umem->fq, frags_processed);
  1029. if (ifobj->release_rx)
  1030. xsk_ring_cons__release(&xsk->rx, frags_processed);
  1031. pthread_mutex_lock(&pacing_mutex);
  1032. pkts_in_flight -= pkts_sent;
  1033. pthread_mutex_unlock(&pacing_mutex);
  1034. pkts_sent = 0;
  1035. return TEST_CONTINUE;
  1036. }
  1037. bool all_packets_received(struct test_spec *test, struct xsk_socket_info *xsk, u32 sock_num,
  1038. unsigned long *bitmap)
  1039. {
  1040. struct pkt_stream *pkt_stream = xsk->pkt_stream;
  1041. if (!pkt_stream) {
  1042. __set_bit(sock_num, bitmap);
  1043. return false;
  1044. }
  1045. if (pkt_stream->nb_rx_pkts == pkt_stream->nb_valid_entries) {
  1046. __set_bit(sock_num, bitmap);
  1047. if (bitmap_full(bitmap, test->nb_sockets))
  1048. return true;
  1049. }
  1050. return false;
  1051. }
  1052. static int receive_pkts(struct test_spec *test)
  1053. {
  1054. struct timeval tv_end, tv_now, tv_timeout = {THREAD_TMOUT, 0};
  1055. DECLARE_BITMAP(bitmap, test->nb_sockets);
  1056. struct xsk_socket_info *xsk;
  1057. u32 sock_num = 0;
  1058. int res, ret;
  1059. ret = gettimeofday(&tv_now, NULL);
  1060. if (ret)
  1061. exit_with_error(errno);
  1062. timeradd(&tv_now, &tv_timeout, &tv_end);
  1063. while (1) {
  1064. xsk = &test->ifobj_rx->xsk_arr[sock_num];
  1065. if ((all_packets_received(test, xsk, sock_num, bitmap)))
  1066. break;
  1067. res = __receive_pkts(test, xsk);
  1068. if (!(res == TEST_PASS || res == TEST_CONTINUE))
  1069. return res;
  1070. ret = gettimeofday(&tv_now, NULL);
  1071. if (ret)
  1072. exit_with_error(errno);
  1073. if (timercmp(&tv_now, &tv_end, >)) {
  1074. ksft_print_msg("ERROR: [%s] Receive loop timed out\n", __func__);
  1075. return TEST_FAILURE;
  1076. }
  1077. sock_num = (sock_num + 1) % test->nb_sockets;
  1078. }
  1079. return TEST_PASS;
  1080. }
  1081. static int __send_pkts(struct ifobject *ifobject, struct xsk_socket_info *xsk, bool timeout)
  1082. {
  1083. u32 i, idx = 0, valid_pkts = 0, valid_frags = 0, buffer_len;
  1084. struct pkt_stream *pkt_stream = xsk->pkt_stream;
  1085. struct xsk_umem_info *umem = ifobject->umem;
  1086. bool use_poll = ifobject->use_poll;
  1087. struct pollfd fds = { };
  1088. int ret;
  1089. buffer_len = pkt_get_buffer_len(umem, pkt_stream->max_pkt_len);
  1090. /* pkts_in_flight might be negative if many invalid packets are sent */
  1091. if (pkts_in_flight >= (int)((umem_size(umem) - xsk->batch_size * buffer_len) /
  1092. buffer_len)) {
  1093. ret = kick_tx(xsk);
  1094. if (ret)
  1095. return TEST_FAILURE;
  1096. return TEST_CONTINUE;
  1097. }
  1098. fds.fd = xsk_socket__fd(xsk->xsk);
  1099. fds.events = POLLOUT;
  1100. while (xsk_ring_prod__reserve(&xsk->tx, xsk->batch_size, &idx) < xsk->batch_size) {
  1101. if (use_poll) {
  1102. ret = poll(&fds, 1, POLL_TMOUT);
  1103. if (timeout) {
  1104. if (ret < 0) {
  1105. ksft_print_msg("ERROR: [%s] Poll error %d\n",
  1106. __func__, errno);
  1107. return TEST_FAILURE;
  1108. }
  1109. if (ret == 0)
  1110. return TEST_PASS;
  1111. break;
  1112. }
  1113. if (ret <= 0) {
  1114. ksft_print_msg("ERROR: [%s] Poll error %d\n",
  1115. __func__, errno);
  1116. return TEST_FAILURE;
  1117. }
  1118. }
  1119. complete_pkts(xsk, xsk->batch_size);
  1120. }
  1121. for (i = 0; i < xsk->batch_size; i++) {
  1122. struct pkt *pkt = pkt_stream_get_next_tx_pkt(pkt_stream);
  1123. u32 nb_frags_left, nb_frags, bytes_written = 0;
  1124. if (!pkt)
  1125. break;
  1126. nb_frags = pkt_nb_frags(umem->frame_size, pkt_stream, pkt);
  1127. if (nb_frags > xsk->batch_size - i) {
  1128. pkt_stream_cancel(pkt_stream);
  1129. xsk_ring_prod__cancel(&xsk->tx, xsk->batch_size - i);
  1130. break;
  1131. }
  1132. nb_frags_left = nb_frags;
  1133. while (nb_frags_left--) {
  1134. struct xdp_desc *tx_desc = xsk_ring_prod__tx_desc(&xsk->tx, idx + i);
  1135. tx_desc->addr = pkt_get_addr(pkt, ifobject->umem);
  1136. if (pkt_stream->verbatim) {
  1137. tx_desc->len = pkt->len;
  1138. tx_desc->options = pkt->options;
  1139. } else if (nb_frags_left) {
  1140. tx_desc->len = umem->frame_size;
  1141. tx_desc->options = XDP_PKT_CONTD;
  1142. } else {
  1143. tx_desc->len = pkt->len - bytes_written;
  1144. tx_desc->options = 0;
  1145. }
  1146. if (pkt->valid)
  1147. pkt_generate(xsk, umem, tx_desc->addr, tx_desc->len, pkt->pkt_nb,
  1148. bytes_written);
  1149. bytes_written += tx_desc->len;
  1150. print_verbose("Tx addr: %llx len: %u options: %u pkt_nb: %u\n",
  1151. tx_desc->addr, tx_desc->len, tx_desc->options, pkt->pkt_nb);
  1152. if (nb_frags_left) {
  1153. i++;
  1154. if (pkt_stream->verbatim)
  1155. pkt = pkt_stream_get_next_tx_pkt(pkt_stream);
  1156. }
  1157. }
  1158. if (pkt && pkt->valid) {
  1159. valid_pkts++;
  1160. valid_frags += nb_frags;
  1161. }
  1162. }
  1163. pthread_mutex_lock(&pacing_mutex);
  1164. pkts_in_flight += valid_pkts;
  1165. pthread_mutex_unlock(&pacing_mutex);
  1166. xsk_ring_prod__submit(&xsk->tx, i);
  1167. xsk->outstanding_tx += valid_frags;
  1168. if (use_poll) {
  1169. ret = poll(&fds, 1, POLL_TMOUT);
  1170. if (ret <= 0) {
  1171. if (ret == 0 && timeout)
  1172. return TEST_PASS;
  1173. ksft_print_msg("ERROR: [%s] Poll error %d\n", __func__, ret);
  1174. return TEST_FAILURE;
  1175. }
  1176. }
  1177. if (!timeout) {
  1178. if (complete_pkts(xsk, i))
  1179. return TEST_FAILURE;
  1180. usleep(10);
  1181. return TEST_PASS;
  1182. }
  1183. return TEST_CONTINUE;
  1184. }
  1185. static int wait_for_tx_completion(struct xsk_socket_info *xsk)
  1186. {
  1187. struct timeval tv_end, tv_now, tv_timeout = {THREAD_TMOUT, 0};
  1188. int ret;
  1189. ret = gettimeofday(&tv_now, NULL);
  1190. if (ret)
  1191. exit_with_error(errno);
  1192. timeradd(&tv_now, &tv_timeout, &tv_end);
  1193. while (xsk->outstanding_tx) {
  1194. ret = gettimeofday(&tv_now, NULL);
  1195. if (ret)
  1196. exit_with_error(errno);
  1197. if (timercmp(&tv_now, &tv_end, >)) {
  1198. ksft_print_msg("ERROR: [%s] Transmission loop timed out\n", __func__);
  1199. return TEST_FAILURE;
  1200. }
  1201. complete_pkts(xsk, xsk->batch_size);
  1202. }
  1203. return TEST_PASS;
  1204. }
  1205. bool all_packets_sent(struct test_spec *test, unsigned long *bitmap)
  1206. {
  1207. return bitmap_full(bitmap, test->nb_sockets);
  1208. }
  1209. static int send_pkts(struct test_spec *test, struct ifobject *ifobject)
  1210. {
  1211. bool timeout = !is_umem_valid(test->ifobj_rx);
  1212. DECLARE_BITMAP(bitmap, test->nb_sockets);
  1213. u32 i, ret;
  1214. while (!(all_packets_sent(test, bitmap))) {
  1215. for (i = 0; i < test->nb_sockets; i++) {
  1216. struct pkt_stream *pkt_stream;
  1217. pkt_stream = ifobject->xsk_arr[i].pkt_stream;
  1218. if (!pkt_stream || pkt_stream->current_pkt_nb >= pkt_stream->nb_pkts) {
  1219. __set_bit(i, bitmap);
  1220. continue;
  1221. }
  1222. ret = __send_pkts(ifobject, &ifobject->xsk_arr[i], timeout);
  1223. if (ret == TEST_CONTINUE && !test->fail)
  1224. continue;
  1225. if ((ret || test->fail) && !timeout)
  1226. return TEST_FAILURE;
  1227. if (ret == TEST_PASS && timeout)
  1228. return ret;
  1229. ret = wait_for_tx_completion(&ifobject->xsk_arr[i]);
  1230. if (ret)
  1231. return TEST_FAILURE;
  1232. }
  1233. }
  1234. return TEST_PASS;
  1235. }
  1236. static int get_xsk_stats(struct xsk_socket *xsk, struct xdp_statistics *stats)
  1237. {
  1238. int fd = xsk_socket__fd(xsk), err;
  1239. socklen_t optlen, expected_len;
  1240. optlen = sizeof(*stats);
  1241. err = getsockopt(fd, SOL_XDP, XDP_STATISTICS, stats, &optlen);
  1242. if (err) {
  1243. ksft_print_msg("[%s] getsockopt(XDP_STATISTICS) error %u %s\n",
  1244. __func__, -err, strerror(-err));
  1245. return TEST_FAILURE;
  1246. }
  1247. expected_len = sizeof(struct xdp_statistics);
  1248. if (optlen != expected_len) {
  1249. ksft_print_msg("[%s] getsockopt optlen error. Expected: %u got: %u\n",
  1250. __func__, expected_len, optlen);
  1251. return TEST_FAILURE;
  1252. }
  1253. return TEST_PASS;
  1254. }
  1255. static int validate_rx_dropped(struct ifobject *ifobject)
  1256. {
  1257. struct xsk_socket *xsk = ifobject->xsk->xsk;
  1258. struct xdp_statistics stats;
  1259. int err;
  1260. err = kick_rx(ifobject->xsk);
  1261. if (err)
  1262. return TEST_FAILURE;
  1263. err = get_xsk_stats(xsk, &stats);
  1264. if (err)
  1265. return TEST_FAILURE;
  1266. /* The receiver calls getsockopt after receiving the last (valid)
  1267. * packet which is not the final packet sent in this test (valid and
  1268. * invalid packets are sent in alternating fashion with the final
  1269. * packet being invalid). Since the last packet may or may not have
  1270. * been dropped already, both outcomes must be allowed.
  1271. */
  1272. if (stats.rx_dropped == ifobject->xsk->pkt_stream->nb_pkts / 2 ||
  1273. stats.rx_dropped == ifobject->xsk->pkt_stream->nb_pkts / 2 - 1)
  1274. return TEST_PASS;
  1275. return TEST_FAILURE;
  1276. }
  1277. static int validate_rx_full(struct ifobject *ifobject)
  1278. {
  1279. struct xsk_socket *xsk = ifobject->xsk->xsk;
  1280. struct xdp_statistics stats;
  1281. int err;
  1282. usleep(1000);
  1283. err = kick_rx(ifobject->xsk);
  1284. if (err)
  1285. return TEST_FAILURE;
  1286. err = get_xsk_stats(xsk, &stats);
  1287. if (err)
  1288. return TEST_FAILURE;
  1289. if (stats.rx_ring_full)
  1290. return TEST_PASS;
  1291. return TEST_FAILURE;
  1292. }
  1293. static int validate_fill_empty(struct ifobject *ifobject)
  1294. {
  1295. struct xsk_socket *xsk = ifobject->xsk->xsk;
  1296. struct xdp_statistics stats;
  1297. int err;
  1298. usleep(1000);
  1299. err = kick_rx(ifobject->xsk);
  1300. if (err)
  1301. return TEST_FAILURE;
  1302. err = get_xsk_stats(xsk, &stats);
  1303. if (err)
  1304. return TEST_FAILURE;
  1305. if (stats.rx_fill_ring_empty_descs)
  1306. return TEST_PASS;
  1307. return TEST_FAILURE;
  1308. }
  1309. static int validate_tx_invalid_descs(struct ifobject *ifobject)
  1310. {
  1311. struct xsk_socket *xsk = ifobject->xsk->xsk;
  1312. int fd = xsk_socket__fd(xsk);
  1313. struct xdp_statistics stats;
  1314. socklen_t optlen;
  1315. int err;
  1316. optlen = sizeof(stats);
  1317. err = getsockopt(fd, SOL_XDP, XDP_STATISTICS, &stats, &optlen);
  1318. if (err) {
  1319. ksft_print_msg("[%s] getsockopt(XDP_STATISTICS) error %u %s\n",
  1320. __func__, -err, strerror(-err));
  1321. return TEST_FAILURE;
  1322. }
  1323. if (stats.tx_invalid_descs != ifobject->xsk->pkt_stream->nb_pkts / 2) {
  1324. ksft_print_msg("[%s] tx_invalid_descs incorrect. Got [%llu] expected [%u]\n",
  1325. __func__,
  1326. (unsigned long long)stats.tx_invalid_descs,
  1327. ifobject->xsk->pkt_stream->nb_pkts);
  1328. return TEST_FAILURE;
  1329. }
  1330. return TEST_PASS;
  1331. }
  1332. static void xsk_configure_socket(struct test_spec *test, struct ifobject *ifobject,
  1333. struct xsk_umem_info *umem, bool tx)
  1334. {
  1335. int i, ret;
  1336. for (i = 0; i < test->nb_sockets; i++) {
  1337. bool shared = (ifobject->shared_umem && tx) ? true : !!i;
  1338. u32 ctr = 0;
  1339. while (ctr++ < SOCK_RECONF_CTR) {
  1340. ret = __xsk_configure_socket(&ifobject->xsk_arr[i], umem,
  1341. ifobject, shared);
  1342. if (!ret)
  1343. break;
  1344. /* Retry if it fails as xsk_socket__create() is asynchronous */
  1345. if (ctr >= SOCK_RECONF_CTR)
  1346. exit_with_error(-ret);
  1347. usleep(USLEEP_MAX);
  1348. }
  1349. if (ifobject->busy_poll)
  1350. enable_busy_poll(&ifobject->xsk_arr[i]);
  1351. }
  1352. }
  1353. static void thread_common_ops_tx(struct test_spec *test, struct ifobject *ifobject)
  1354. {
  1355. xsk_configure_socket(test, ifobject, test->ifobj_rx->umem, true);
  1356. ifobject->xsk = &ifobject->xsk_arr[0];
  1357. ifobject->xskmap = test->ifobj_rx->xskmap;
  1358. memcpy(ifobject->umem, test->ifobj_rx->umem, sizeof(struct xsk_umem_info));
  1359. ifobject->umem->base_addr = 0;
  1360. }
  1361. static void xsk_populate_fill_ring(struct xsk_umem_info *umem, struct pkt_stream *pkt_stream,
  1362. bool fill_up)
  1363. {
  1364. u32 rx_frame_size = umem->frame_size - XDP_PACKET_HEADROOM;
  1365. u32 idx = 0, filled = 0, buffers_to_fill, nb_pkts;
  1366. int ret;
  1367. if (umem->num_frames < XSK_RING_PROD__DEFAULT_NUM_DESCS)
  1368. buffers_to_fill = umem->num_frames;
  1369. else
  1370. buffers_to_fill = umem->fill_size;
  1371. ret = xsk_ring_prod__reserve(&umem->fq, buffers_to_fill, &idx);
  1372. if (ret != buffers_to_fill)
  1373. exit_with_error(ENOSPC);
  1374. while (filled < buffers_to_fill) {
  1375. struct pkt *pkt = pkt_stream_get_next_rx_pkt(pkt_stream, &nb_pkts);
  1376. u64 addr;
  1377. u32 i;
  1378. for (i = 0; i < pkt_nb_frags(rx_frame_size, pkt_stream, pkt); i++) {
  1379. if (!pkt) {
  1380. if (!fill_up)
  1381. break;
  1382. addr = filled * umem->frame_size + umem->base_addr;
  1383. } else if (pkt->offset >= 0) {
  1384. addr = pkt->offset % umem->frame_size + umem_alloc_buffer(umem);
  1385. } else {
  1386. addr = pkt->offset + umem_alloc_buffer(umem);
  1387. }
  1388. *xsk_ring_prod__fill_addr(&umem->fq, idx++) = addr;
  1389. if (++filled >= buffers_to_fill)
  1390. break;
  1391. }
  1392. }
  1393. xsk_ring_prod__submit(&umem->fq, filled);
  1394. xsk_ring_prod__cancel(&umem->fq, buffers_to_fill - filled);
  1395. pkt_stream_reset(pkt_stream);
  1396. umem_reset_alloc(umem);
  1397. }
  1398. static void thread_common_ops(struct test_spec *test, struct ifobject *ifobject)
  1399. {
  1400. u64 umem_sz = ifobject->umem->num_frames * ifobject->umem->frame_size;
  1401. int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
  1402. LIBBPF_OPTS(bpf_xdp_query_opts, opts);
  1403. void *bufs;
  1404. int ret;
  1405. u32 i;
  1406. if (ifobject->umem->unaligned_mode)
  1407. mmap_flags |= MAP_HUGETLB | MAP_HUGE_2MB;
  1408. if (ifobject->shared_umem)
  1409. umem_sz *= 2;
  1410. bufs = mmap(NULL, umem_sz, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
  1411. if (bufs == MAP_FAILED)
  1412. exit_with_error(errno);
  1413. ret = xsk_configure_umem(ifobject, ifobject->umem, bufs, umem_sz);
  1414. if (ret)
  1415. exit_with_error(-ret);
  1416. xsk_configure_socket(test, ifobject, ifobject->umem, false);
  1417. ifobject->xsk = &ifobject->xsk_arr[0];
  1418. if (!ifobject->rx_on)
  1419. return;
  1420. xsk_populate_fill_ring(ifobject->umem, ifobject->xsk->pkt_stream, ifobject->use_fill_ring);
  1421. for (i = 0; i < test->nb_sockets; i++) {
  1422. ifobject->xsk = &ifobject->xsk_arr[i];
  1423. ret = xsk_update_xskmap(ifobject->xskmap, ifobject->xsk->xsk, i);
  1424. if (ret)
  1425. exit_with_error(errno);
  1426. }
  1427. }
  1428. static void *worker_testapp_validate_tx(void *arg)
  1429. {
  1430. struct test_spec *test = (struct test_spec *)arg;
  1431. struct ifobject *ifobject = test->ifobj_tx;
  1432. int err;
  1433. if (test->current_step == 1) {
  1434. if (!ifobject->shared_umem)
  1435. thread_common_ops(test, ifobject);
  1436. else
  1437. thread_common_ops_tx(test, ifobject);
  1438. }
  1439. err = send_pkts(test, ifobject);
  1440. if (!err && ifobject->validation_func)
  1441. err = ifobject->validation_func(ifobject);
  1442. if (err)
  1443. report_failure(test);
  1444. pthread_exit(NULL);
  1445. }
  1446. static void *worker_testapp_validate_rx(void *arg)
  1447. {
  1448. struct test_spec *test = (struct test_spec *)arg;
  1449. struct ifobject *ifobject = test->ifobj_rx;
  1450. int err;
  1451. if (test->current_step == 1) {
  1452. thread_common_ops(test, ifobject);
  1453. } else {
  1454. xsk_clear_xskmap(ifobject->xskmap);
  1455. err = xsk_update_xskmap(ifobject->xskmap, ifobject->xsk->xsk, 0);
  1456. if (err) {
  1457. ksft_print_msg("Error: Failed to update xskmap, error %s\n",
  1458. strerror(-err));
  1459. exit_with_error(-err);
  1460. }
  1461. }
  1462. pthread_barrier_wait(&barr);
  1463. err = receive_pkts(test);
  1464. if (!err && ifobject->validation_func)
  1465. err = ifobject->validation_func(ifobject);
  1466. if (err)
  1467. report_failure(test);
  1468. pthread_exit(NULL);
  1469. }
  1470. static u64 ceil_u64(u64 a, u64 b)
  1471. {
  1472. return (a + b - 1) / b;
  1473. }
  1474. static void testapp_clean_xsk_umem(struct ifobject *ifobj)
  1475. {
  1476. u64 umem_sz = ifobj->umem->num_frames * ifobj->umem->frame_size;
  1477. if (ifobj->shared_umem)
  1478. umem_sz *= 2;
  1479. umem_sz = ceil_u64(umem_sz, HUGEPAGE_SIZE) * HUGEPAGE_SIZE;
  1480. xsk_umem__delete(ifobj->umem->umem);
  1481. munmap(ifobj->umem->buffer, umem_sz);
  1482. }
  1483. static void handler(int signum)
  1484. {
  1485. pthread_exit(NULL);
  1486. }
  1487. static bool xdp_prog_changed_rx(struct test_spec *test)
  1488. {
  1489. struct ifobject *ifobj = test->ifobj_rx;
  1490. return ifobj->xdp_prog != test->xdp_prog_rx || ifobj->mode != test->mode;
  1491. }
  1492. static bool xdp_prog_changed_tx(struct test_spec *test)
  1493. {
  1494. struct ifobject *ifobj = test->ifobj_tx;
  1495. return ifobj->xdp_prog != test->xdp_prog_tx || ifobj->mode != test->mode;
  1496. }
  1497. static void xsk_reattach_xdp(struct ifobject *ifobj, struct bpf_program *xdp_prog,
  1498. struct bpf_map *xskmap, enum test_mode mode)
  1499. {
  1500. int err;
  1501. xsk_detach_xdp_program(ifobj->ifindex, mode_to_xdp_flags(ifobj->mode));
  1502. err = xsk_attach_xdp_program(xdp_prog, ifobj->ifindex, mode_to_xdp_flags(mode));
  1503. if (err) {
  1504. ksft_print_msg("Error attaching XDP program\n");
  1505. exit_with_error(-err);
  1506. }
  1507. if (ifobj->mode != mode && (mode == TEST_MODE_DRV || mode == TEST_MODE_ZC))
  1508. if (!xsk_is_in_mode(ifobj->ifindex, XDP_FLAGS_DRV_MODE)) {
  1509. ksft_print_msg("ERROR: XDP prog not in DRV mode\n");
  1510. exit_with_error(EINVAL);
  1511. }
  1512. ifobj->xdp_prog = xdp_prog;
  1513. ifobj->xskmap = xskmap;
  1514. ifobj->mode = mode;
  1515. }
  1516. static void xsk_attach_xdp_progs(struct test_spec *test, struct ifobject *ifobj_rx,
  1517. struct ifobject *ifobj_tx)
  1518. {
  1519. if (xdp_prog_changed_rx(test))
  1520. xsk_reattach_xdp(ifobj_rx, test->xdp_prog_rx, test->xskmap_rx, test->mode);
  1521. if (!ifobj_tx || ifobj_tx->shared_umem)
  1522. return;
  1523. if (xdp_prog_changed_tx(test))
  1524. xsk_reattach_xdp(ifobj_tx, test->xdp_prog_tx, test->xskmap_tx, test->mode);
  1525. }
  1526. static int __testapp_validate_traffic(struct test_spec *test, struct ifobject *ifobj1,
  1527. struct ifobject *ifobj2)
  1528. {
  1529. pthread_t t0, t1;
  1530. int err;
  1531. if (test->mtu > MAX_ETH_PKT_SIZE) {
  1532. if (test->mode == TEST_MODE_ZC && (!ifobj1->multi_buff_zc_supp ||
  1533. (ifobj2 && !ifobj2->multi_buff_zc_supp))) {
  1534. ksft_test_result_skip("Multi buffer for zero-copy not supported.\n");
  1535. return TEST_SKIP;
  1536. }
  1537. if (test->mode != TEST_MODE_ZC && (!ifobj1->multi_buff_supp ||
  1538. (ifobj2 && !ifobj2->multi_buff_supp))) {
  1539. ksft_test_result_skip("Multi buffer not supported.\n");
  1540. return TEST_SKIP;
  1541. }
  1542. }
  1543. err = test_spec_set_mtu(test, test->mtu);
  1544. if (err) {
  1545. ksft_print_msg("Error, could not set mtu.\n");
  1546. exit_with_error(err);
  1547. }
  1548. if (ifobj2) {
  1549. if (pthread_barrier_init(&barr, NULL, 2))
  1550. exit_with_error(errno);
  1551. pkt_stream_reset(ifobj2->xsk->pkt_stream);
  1552. }
  1553. test->current_step++;
  1554. pkt_stream_reset(ifobj1->xsk->pkt_stream);
  1555. pkts_in_flight = 0;
  1556. signal(SIGUSR1, handler);
  1557. /*Spawn RX thread */
  1558. pthread_create(&t0, NULL, ifobj1->func_ptr, test);
  1559. if (ifobj2) {
  1560. pthread_barrier_wait(&barr);
  1561. if (pthread_barrier_destroy(&barr))
  1562. exit_with_error(errno);
  1563. /*Spawn TX thread */
  1564. pthread_create(&t1, NULL, ifobj2->func_ptr, test);
  1565. pthread_join(t1, NULL);
  1566. }
  1567. if (!ifobj2)
  1568. pthread_kill(t0, SIGUSR1);
  1569. else
  1570. pthread_join(t0, NULL);
  1571. if (test->total_steps == test->current_step || test->fail) {
  1572. u32 i;
  1573. if (ifobj2)
  1574. for (i = 0; i < test->nb_sockets; i++)
  1575. xsk_socket__delete(ifobj2->xsk_arr[i].xsk);
  1576. for (i = 0; i < test->nb_sockets; i++)
  1577. xsk_socket__delete(ifobj1->xsk_arr[i].xsk);
  1578. testapp_clean_xsk_umem(ifobj1);
  1579. if (ifobj2 && !ifobj2->shared_umem)
  1580. testapp_clean_xsk_umem(ifobj2);
  1581. }
  1582. return !!test->fail;
  1583. }
  1584. static int testapp_validate_traffic(struct test_spec *test)
  1585. {
  1586. struct ifobject *ifobj_rx = test->ifobj_rx;
  1587. struct ifobject *ifobj_tx = test->ifobj_tx;
  1588. if ((ifobj_rx->umem->unaligned_mode && !ifobj_rx->unaligned_supp) ||
  1589. (ifobj_tx->umem->unaligned_mode && !ifobj_tx->unaligned_supp)) {
  1590. ksft_test_result_skip("No huge pages present.\n");
  1591. return TEST_SKIP;
  1592. }
  1593. if (test->set_ring) {
  1594. if (ifobj_tx->hw_ring_size_supp) {
  1595. if (set_ring_size(ifobj_tx)) {
  1596. ksft_test_result_skip("Failed to change HW ring size.\n");
  1597. return TEST_FAILURE;
  1598. }
  1599. } else {
  1600. ksft_test_result_skip("Changing HW ring size not supported.\n");
  1601. return TEST_SKIP;
  1602. }
  1603. }
  1604. xsk_attach_xdp_progs(test, ifobj_rx, ifobj_tx);
  1605. return __testapp_validate_traffic(test, ifobj_rx, ifobj_tx);
  1606. }
  1607. static int testapp_validate_traffic_single_thread(struct test_spec *test, struct ifobject *ifobj)
  1608. {
  1609. return __testapp_validate_traffic(test, ifobj, NULL);
  1610. }
  1611. static int testapp_teardown(struct test_spec *test)
  1612. {
  1613. int i;
  1614. for (i = 0; i < MAX_TEARDOWN_ITER; i++) {
  1615. if (testapp_validate_traffic(test))
  1616. return TEST_FAILURE;
  1617. test_spec_reset(test);
  1618. }
  1619. return TEST_PASS;
  1620. }
  1621. static void swap_directions(struct ifobject **ifobj1, struct ifobject **ifobj2)
  1622. {
  1623. thread_func_t tmp_func_ptr = (*ifobj1)->func_ptr;
  1624. struct ifobject *tmp_ifobj = (*ifobj1);
  1625. (*ifobj1)->func_ptr = (*ifobj2)->func_ptr;
  1626. (*ifobj2)->func_ptr = tmp_func_ptr;
  1627. *ifobj1 = *ifobj2;
  1628. *ifobj2 = tmp_ifobj;
  1629. }
  1630. static int testapp_bidirectional(struct test_spec *test)
  1631. {
  1632. int res;
  1633. test->ifobj_tx->rx_on = true;
  1634. test->ifobj_rx->tx_on = true;
  1635. test->total_steps = 2;
  1636. if (testapp_validate_traffic(test))
  1637. return TEST_FAILURE;
  1638. print_verbose("Switching Tx/Rx direction\n");
  1639. swap_directions(&test->ifobj_rx, &test->ifobj_tx);
  1640. res = __testapp_validate_traffic(test, test->ifobj_rx, test->ifobj_tx);
  1641. swap_directions(&test->ifobj_rx, &test->ifobj_tx);
  1642. return res;
  1643. }
  1644. static int swap_xsk_resources(struct test_spec *test)
  1645. {
  1646. int ret;
  1647. test->ifobj_tx->xsk_arr[0].pkt_stream = NULL;
  1648. test->ifobj_rx->xsk_arr[0].pkt_stream = NULL;
  1649. test->ifobj_tx->xsk_arr[1].pkt_stream = test->tx_pkt_stream_default;
  1650. test->ifobj_rx->xsk_arr[1].pkt_stream = test->rx_pkt_stream_default;
  1651. test->ifobj_tx->xsk = &test->ifobj_tx->xsk_arr[1];
  1652. test->ifobj_rx->xsk = &test->ifobj_rx->xsk_arr[1];
  1653. ret = xsk_update_xskmap(test->ifobj_rx->xskmap, test->ifobj_rx->xsk->xsk, 0);
  1654. if (ret)
  1655. return TEST_FAILURE;
  1656. return TEST_PASS;
  1657. }
  1658. static int testapp_xdp_prog_cleanup(struct test_spec *test)
  1659. {
  1660. test->total_steps = 2;
  1661. test->nb_sockets = 2;
  1662. if (testapp_validate_traffic(test))
  1663. return TEST_FAILURE;
  1664. if (swap_xsk_resources(test))
  1665. return TEST_FAILURE;
  1666. return testapp_validate_traffic(test);
  1667. }
  1668. static int testapp_headroom(struct test_spec *test)
  1669. {
  1670. test->ifobj_rx->umem->frame_headroom = UMEM_HEADROOM_TEST_SIZE;
  1671. return testapp_validate_traffic(test);
  1672. }
  1673. static int testapp_stats_rx_dropped(struct test_spec *test)
  1674. {
  1675. if (test->mode == TEST_MODE_ZC) {
  1676. ksft_test_result_skip("Can not run RX_DROPPED test for ZC mode\n");
  1677. return TEST_SKIP;
  1678. }
  1679. pkt_stream_replace_half(test, MIN_PKT_SIZE * 4, 0);
  1680. test->ifobj_rx->umem->frame_headroom = test->ifobj_rx->umem->frame_size -
  1681. XDP_PACKET_HEADROOM - MIN_PKT_SIZE * 3;
  1682. pkt_stream_receive_half(test);
  1683. test->ifobj_rx->validation_func = validate_rx_dropped;
  1684. return testapp_validate_traffic(test);
  1685. }
  1686. static int testapp_stats_tx_invalid_descs(struct test_spec *test)
  1687. {
  1688. pkt_stream_replace_half(test, XSK_UMEM__INVALID_FRAME_SIZE, 0);
  1689. test->ifobj_tx->validation_func = validate_tx_invalid_descs;
  1690. return testapp_validate_traffic(test);
  1691. }
  1692. static int testapp_stats_rx_full(struct test_spec *test)
  1693. {
  1694. pkt_stream_replace(test, DEFAULT_UMEM_BUFFERS + DEFAULT_UMEM_BUFFERS / 2, MIN_PKT_SIZE);
  1695. test->ifobj_rx->xsk->pkt_stream = pkt_stream_generate(DEFAULT_UMEM_BUFFERS, MIN_PKT_SIZE);
  1696. test->ifobj_rx->xsk->rxqsize = DEFAULT_UMEM_BUFFERS;
  1697. test->ifobj_rx->release_rx = false;
  1698. test->ifobj_rx->validation_func = validate_rx_full;
  1699. return testapp_validate_traffic(test);
  1700. }
  1701. static int testapp_stats_fill_empty(struct test_spec *test)
  1702. {
  1703. pkt_stream_replace(test, DEFAULT_UMEM_BUFFERS + DEFAULT_UMEM_BUFFERS / 2, MIN_PKT_SIZE);
  1704. test->ifobj_rx->xsk->pkt_stream = pkt_stream_generate(DEFAULT_UMEM_BUFFERS, MIN_PKT_SIZE);
  1705. test->ifobj_rx->use_fill_ring = false;
  1706. test->ifobj_rx->validation_func = validate_fill_empty;
  1707. return testapp_validate_traffic(test);
  1708. }
  1709. static int testapp_send_receive_unaligned(struct test_spec *test)
  1710. {
  1711. test->ifobj_tx->umem->unaligned_mode = true;
  1712. test->ifobj_rx->umem->unaligned_mode = true;
  1713. /* Let half of the packets straddle a 4K buffer boundary */
  1714. pkt_stream_replace_half(test, MIN_PKT_SIZE, -MIN_PKT_SIZE / 2);
  1715. return testapp_validate_traffic(test);
  1716. }
  1717. static int testapp_send_receive_unaligned_mb(struct test_spec *test)
  1718. {
  1719. test->mtu = MAX_ETH_JUMBO_SIZE;
  1720. test->ifobj_tx->umem->unaligned_mode = true;
  1721. test->ifobj_rx->umem->unaligned_mode = true;
  1722. pkt_stream_replace(test, DEFAULT_PKT_CNT, MAX_ETH_JUMBO_SIZE);
  1723. return testapp_validate_traffic(test);
  1724. }
  1725. static int testapp_single_pkt(struct test_spec *test)
  1726. {
  1727. struct pkt pkts[] = {{0, MIN_PKT_SIZE, 0, true}};
  1728. pkt_stream_generate_custom(test, pkts, ARRAY_SIZE(pkts));
  1729. return testapp_validate_traffic(test);
  1730. }
  1731. static int testapp_send_receive_mb(struct test_spec *test)
  1732. {
  1733. test->mtu = MAX_ETH_JUMBO_SIZE;
  1734. pkt_stream_replace(test, DEFAULT_PKT_CNT, MAX_ETH_JUMBO_SIZE);
  1735. return testapp_validate_traffic(test);
  1736. }
  1737. static int testapp_invalid_desc_mb(struct test_spec *test)
  1738. {
  1739. struct xsk_umem_info *umem = test->ifobj_tx->umem;
  1740. u64 umem_size = umem->num_frames * umem->frame_size;
  1741. struct pkt pkts[] = {
  1742. /* Valid packet for synch to start with */
  1743. {0, MIN_PKT_SIZE, 0, true, 0},
  1744. /* Zero frame len is not legal */
  1745. {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD},
  1746. {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD},
  1747. {0, 0, 0, false, 0},
  1748. /* Invalid address in the second frame */
  1749. {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD},
  1750. {umem_size, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD},
  1751. /* Invalid len in the middle */
  1752. {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD},
  1753. {0, XSK_UMEM__INVALID_FRAME_SIZE, 0, false, XDP_PKT_CONTD},
  1754. /* Invalid options in the middle */
  1755. {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD},
  1756. {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XSK_DESC__INVALID_OPTION},
  1757. /* Transmit 2 frags, receive 3 */
  1758. {0, XSK_UMEM__MAX_FRAME_SIZE, 0, true, XDP_PKT_CONTD},
  1759. {0, XSK_UMEM__MAX_FRAME_SIZE, 0, true, 0},
  1760. /* Middle frame crosses chunk boundary with small length */
  1761. {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD},
  1762. {-MIN_PKT_SIZE / 2, MIN_PKT_SIZE, 0, false, 0},
  1763. /* Valid packet for synch so that something is received */
  1764. {0, MIN_PKT_SIZE, 0, true, 0}};
  1765. if (umem->unaligned_mode) {
  1766. /* Crossing a chunk boundary allowed */
  1767. pkts[12].valid = true;
  1768. pkts[13].valid = true;
  1769. }
  1770. test->mtu = MAX_ETH_JUMBO_SIZE;
  1771. pkt_stream_generate_custom(test, pkts, ARRAY_SIZE(pkts));
  1772. return testapp_validate_traffic(test);
  1773. }
  1774. static int testapp_invalid_desc(struct test_spec *test)
  1775. {
  1776. struct xsk_umem_info *umem = test->ifobj_tx->umem;
  1777. u64 umem_size = umem->num_frames * umem->frame_size;
  1778. struct pkt pkts[] = {
  1779. /* Zero packet address allowed */
  1780. {0, MIN_PKT_SIZE, 0, true},
  1781. /* Allowed packet */
  1782. {0, MIN_PKT_SIZE, 0, true},
  1783. /* Straddling the start of umem */
  1784. {-2, MIN_PKT_SIZE, 0, false},
  1785. /* Packet too large */
  1786. {0, XSK_UMEM__INVALID_FRAME_SIZE, 0, false},
  1787. /* Up to end of umem allowed */
  1788. {umem_size - MIN_PKT_SIZE - 2 * umem->frame_size, MIN_PKT_SIZE, 0, true},
  1789. /* After umem ends */
  1790. {umem_size, MIN_PKT_SIZE, 0, false},
  1791. /* Straddle the end of umem */
  1792. {umem_size - MIN_PKT_SIZE / 2, MIN_PKT_SIZE, 0, false},
  1793. /* Straddle a 4K boundary */
  1794. {0x1000 - MIN_PKT_SIZE / 2, MIN_PKT_SIZE, 0, false},
  1795. /* Straddle a 2K boundary */
  1796. {0x800 - MIN_PKT_SIZE / 2, MIN_PKT_SIZE, 0, true},
  1797. /* Valid packet for synch so that something is received */
  1798. {0, MIN_PKT_SIZE, 0, true}};
  1799. if (umem->unaligned_mode) {
  1800. /* Crossing a page boundary allowed */
  1801. pkts[7].valid = true;
  1802. }
  1803. if (umem->frame_size == XSK_UMEM__DEFAULT_FRAME_SIZE / 2) {
  1804. /* Crossing a 2K frame size boundary not allowed */
  1805. pkts[8].valid = false;
  1806. }
  1807. if (test->ifobj_tx->shared_umem) {
  1808. pkts[4].offset += umem_size;
  1809. pkts[5].offset += umem_size;
  1810. pkts[6].offset += umem_size;
  1811. }
  1812. pkt_stream_generate_custom(test, pkts, ARRAY_SIZE(pkts));
  1813. return testapp_validate_traffic(test);
  1814. }
  1815. static int testapp_xdp_drop(struct test_spec *test)
  1816. {
  1817. struct xsk_xdp_progs *skel_rx = test->ifobj_rx->xdp_progs;
  1818. struct xsk_xdp_progs *skel_tx = test->ifobj_tx->xdp_progs;
  1819. test_spec_set_xdp_prog(test, skel_rx->progs.xsk_xdp_drop, skel_tx->progs.xsk_xdp_drop,
  1820. skel_rx->maps.xsk, skel_tx->maps.xsk);
  1821. pkt_stream_receive_half(test);
  1822. return testapp_validate_traffic(test);
  1823. }
  1824. static int testapp_xdp_metadata_copy(struct test_spec *test)
  1825. {
  1826. struct xsk_xdp_progs *skel_rx = test->ifobj_rx->xdp_progs;
  1827. struct xsk_xdp_progs *skel_tx = test->ifobj_tx->xdp_progs;
  1828. struct bpf_map *data_map;
  1829. int count = 0;
  1830. int key = 0;
  1831. test_spec_set_xdp_prog(test, skel_rx->progs.xsk_xdp_populate_metadata,
  1832. skel_tx->progs.xsk_xdp_populate_metadata,
  1833. skel_rx->maps.xsk, skel_tx->maps.xsk);
  1834. test->ifobj_rx->use_metadata = true;
  1835. data_map = bpf_object__find_map_by_name(skel_rx->obj, "xsk_xdp_.bss");
  1836. if (!data_map || !bpf_map__is_internal(data_map)) {
  1837. ksft_print_msg("Error: could not find bss section of XDP program\n");
  1838. return TEST_FAILURE;
  1839. }
  1840. if (bpf_map_update_elem(bpf_map__fd(data_map), &key, &count, BPF_ANY)) {
  1841. ksft_print_msg("Error: could not update count element\n");
  1842. return TEST_FAILURE;
  1843. }
  1844. return testapp_validate_traffic(test);
  1845. }
  1846. static int testapp_xdp_shared_umem(struct test_spec *test)
  1847. {
  1848. struct xsk_xdp_progs *skel_rx = test->ifobj_rx->xdp_progs;
  1849. struct xsk_xdp_progs *skel_tx = test->ifobj_tx->xdp_progs;
  1850. test->total_steps = 1;
  1851. test->nb_sockets = 2;
  1852. test_spec_set_xdp_prog(test, skel_rx->progs.xsk_xdp_shared_umem,
  1853. skel_tx->progs.xsk_xdp_shared_umem,
  1854. skel_rx->maps.xsk, skel_tx->maps.xsk);
  1855. pkt_stream_even_odd_sequence(test);
  1856. return testapp_validate_traffic(test);
  1857. }
  1858. static int testapp_poll_txq_tmout(struct test_spec *test)
  1859. {
  1860. test->ifobj_tx->use_poll = true;
  1861. /* create invalid frame by set umem frame_size and pkt length equal to 2048 */
  1862. test->ifobj_tx->umem->frame_size = 2048;
  1863. pkt_stream_replace(test, 2 * DEFAULT_PKT_CNT, 2048);
  1864. return testapp_validate_traffic_single_thread(test, test->ifobj_tx);
  1865. }
  1866. static int testapp_poll_rxq_tmout(struct test_spec *test)
  1867. {
  1868. test->ifobj_rx->use_poll = true;
  1869. return testapp_validate_traffic_single_thread(test, test->ifobj_rx);
  1870. }
  1871. static int testapp_too_many_frags(struct test_spec *test)
  1872. {
  1873. struct pkt *pkts;
  1874. u32 max_frags, i;
  1875. int ret;
  1876. if (test->mode == TEST_MODE_ZC) {
  1877. max_frags = test->ifobj_tx->xdp_zc_max_segs;
  1878. } else {
  1879. max_frags = get_max_skb_frags();
  1880. if (!max_frags) {
  1881. ksft_print_msg("Couldn't retrieve MAX_SKB_FRAGS from system, using default (17) value\n");
  1882. max_frags = 17;
  1883. }
  1884. max_frags += 1;
  1885. }
  1886. pkts = calloc(2 * max_frags + 2, sizeof(struct pkt));
  1887. if (!pkts)
  1888. return TEST_FAILURE;
  1889. test->mtu = MAX_ETH_JUMBO_SIZE;
  1890. /* Valid packet for synch */
  1891. pkts[0].len = MIN_PKT_SIZE;
  1892. pkts[0].valid = true;
  1893. /* One valid packet with the max amount of frags */
  1894. for (i = 1; i < max_frags + 1; i++) {
  1895. pkts[i].len = MIN_PKT_SIZE;
  1896. pkts[i].options = XDP_PKT_CONTD;
  1897. pkts[i].valid = true;
  1898. }
  1899. pkts[max_frags].options = 0;
  1900. /* An invalid packet with the max amount of frags but signals packet
  1901. * continues on the last frag
  1902. */
  1903. for (i = max_frags + 1; i < 2 * max_frags + 1; i++) {
  1904. pkts[i].len = MIN_PKT_SIZE;
  1905. pkts[i].options = XDP_PKT_CONTD;
  1906. pkts[i].valid = false;
  1907. }
  1908. /* Valid packet for synch */
  1909. pkts[2 * max_frags + 1].len = MIN_PKT_SIZE;
  1910. pkts[2 * max_frags + 1].valid = true;
  1911. pkt_stream_generate_custom(test, pkts, 2 * max_frags + 2);
  1912. ret = testapp_validate_traffic(test);
  1913. free(pkts);
  1914. return ret;
  1915. }
  1916. static int xsk_load_xdp_programs(struct ifobject *ifobj)
  1917. {
  1918. ifobj->xdp_progs = xsk_xdp_progs__open_and_load();
  1919. if (libbpf_get_error(ifobj->xdp_progs))
  1920. return libbpf_get_error(ifobj->xdp_progs);
  1921. return 0;
  1922. }
  1923. static void xsk_unload_xdp_programs(struct ifobject *ifobj)
  1924. {
  1925. xsk_xdp_progs__destroy(ifobj->xdp_progs);
  1926. }
  1927. /* Simple test */
  1928. static bool hugepages_present(void)
  1929. {
  1930. size_t mmap_sz = 2 * DEFAULT_UMEM_BUFFERS * XSK_UMEM__DEFAULT_FRAME_SIZE;
  1931. void *bufs;
  1932. bufs = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE,
  1933. MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, MAP_HUGE_2MB);
  1934. if (bufs == MAP_FAILED)
  1935. return false;
  1936. mmap_sz = ceil_u64(mmap_sz, HUGEPAGE_SIZE) * HUGEPAGE_SIZE;
  1937. munmap(bufs, mmap_sz);
  1938. return true;
  1939. }
  1940. static void init_iface(struct ifobject *ifobj, thread_func_t func_ptr)
  1941. {
  1942. LIBBPF_OPTS(bpf_xdp_query_opts, query_opts);
  1943. int err;
  1944. ifobj->func_ptr = func_ptr;
  1945. err = xsk_load_xdp_programs(ifobj);
  1946. if (err) {
  1947. ksft_print_msg("Error loading XDP program\n");
  1948. exit_with_error(err);
  1949. }
  1950. if (hugepages_present())
  1951. ifobj->unaligned_supp = true;
  1952. err = bpf_xdp_query(ifobj->ifindex, XDP_FLAGS_DRV_MODE, &query_opts);
  1953. if (err) {
  1954. ksft_print_msg("Error querying XDP capabilities\n");
  1955. exit_with_error(-err);
  1956. }
  1957. if (query_opts.feature_flags & NETDEV_XDP_ACT_RX_SG)
  1958. ifobj->multi_buff_supp = true;
  1959. if (query_opts.feature_flags & NETDEV_XDP_ACT_XSK_ZEROCOPY) {
  1960. if (query_opts.xdp_zc_max_segs > 1) {
  1961. ifobj->multi_buff_zc_supp = true;
  1962. ifobj->xdp_zc_max_segs = query_opts.xdp_zc_max_segs;
  1963. } else {
  1964. ifobj->xdp_zc_max_segs = 0;
  1965. }
  1966. }
  1967. }
  1968. static int testapp_send_receive(struct test_spec *test)
  1969. {
  1970. return testapp_validate_traffic(test);
  1971. }
  1972. static int testapp_send_receive_2k_frame(struct test_spec *test)
  1973. {
  1974. test->ifobj_tx->umem->frame_size = 2048;
  1975. test->ifobj_rx->umem->frame_size = 2048;
  1976. pkt_stream_replace(test, DEFAULT_PKT_CNT, MIN_PKT_SIZE);
  1977. return testapp_validate_traffic(test);
  1978. }
  1979. static int testapp_poll_rx(struct test_spec *test)
  1980. {
  1981. test->ifobj_rx->use_poll = true;
  1982. return testapp_validate_traffic(test);
  1983. }
  1984. static int testapp_poll_tx(struct test_spec *test)
  1985. {
  1986. test->ifobj_tx->use_poll = true;
  1987. return testapp_validate_traffic(test);
  1988. }
  1989. static int testapp_aligned_inv_desc(struct test_spec *test)
  1990. {
  1991. return testapp_invalid_desc(test);
  1992. }
  1993. static int testapp_aligned_inv_desc_2k_frame(struct test_spec *test)
  1994. {
  1995. test->ifobj_tx->umem->frame_size = 2048;
  1996. test->ifobj_rx->umem->frame_size = 2048;
  1997. return testapp_invalid_desc(test);
  1998. }
  1999. static int testapp_unaligned_inv_desc(struct test_spec *test)
  2000. {
  2001. test->ifobj_tx->umem->unaligned_mode = true;
  2002. test->ifobj_rx->umem->unaligned_mode = true;
  2003. return testapp_invalid_desc(test);
  2004. }
  2005. static int testapp_unaligned_inv_desc_4001_frame(struct test_spec *test)
  2006. {
  2007. u64 page_size, umem_size;
  2008. /* Odd frame size so the UMEM doesn't end near a page boundary. */
  2009. test->ifobj_tx->umem->frame_size = 4001;
  2010. test->ifobj_rx->umem->frame_size = 4001;
  2011. test->ifobj_tx->umem->unaligned_mode = true;
  2012. test->ifobj_rx->umem->unaligned_mode = true;
  2013. /* This test exists to test descriptors that staddle the end of
  2014. * the UMEM but not a page.
  2015. */
  2016. page_size = sysconf(_SC_PAGESIZE);
  2017. umem_size = test->ifobj_tx->umem->num_frames * test->ifobj_tx->umem->frame_size;
  2018. assert(umem_size % page_size > MIN_PKT_SIZE);
  2019. assert(umem_size % page_size < page_size - MIN_PKT_SIZE);
  2020. return testapp_invalid_desc(test);
  2021. }
  2022. static int testapp_aligned_inv_desc_mb(struct test_spec *test)
  2023. {
  2024. return testapp_invalid_desc_mb(test);
  2025. }
  2026. static int testapp_unaligned_inv_desc_mb(struct test_spec *test)
  2027. {
  2028. test->ifobj_tx->umem->unaligned_mode = true;
  2029. test->ifobj_rx->umem->unaligned_mode = true;
  2030. return testapp_invalid_desc_mb(test);
  2031. }
  2032. static int testapp_xdp_metadata(struct test_spec *test)
  2033. {
  2034. return testapp_xdp_metadata_copy(test);
  2035. }
  2036. static int testapp_xdp_metadata_mb(struct test_spec *test)
  2037. {
  2038. test->mtu = MAX_ETH_JUMBO_SIZE;
  2039. return testapp_xdp_metadata_copy(test);
  2040. }
  2041. static int testapp_hw_sw_min_ring_size(struct test_spec *test)
  2042. {
  2043. int ret;
  2044. test->set_ring = true;
  2045. test->total_steps = 2;
  2046. test->ifobj_tx->ring.tx_pending = DEFAULT_BATCH_SIZE;
  2047. test->ifobj_tx->ring.rx_pending = DEFAULT_BATCH_SIZE * 2;
  2048. test->ifobj_tx->xsk->batch_size = 1;
  2049. test->ifobj_rx->xsk->batch_size = 1;
  2050. ret = testapp_validate_traffic(test);
  2051. if (ret)
  2052. return ret;
  2053. /* Set batch size to hw_ring_size - 1 */
  2054. test->ifobj_tx->xsk->batch_size = DEFAULT_BATCH_SIZE - 1;
  2055. test->ifobj_rx->xsk->batch_size = DEFAULT_BATCH_SIZE - 1;
  2056. return testapp_validate_traffic(test);
  2057. }
  2058. static int testapp_hw_sw_max_ring_size(struct test_spec *test)
  2059. {
  2060. u32 max_descs = XSK_RING_PROD__DEFAULT_NUM_DESCS * 4;
  2061. int ret;
  2062. test->set_ring = true;
  2063. test->total_steps = 2;
  2064. test->ifobj_tx->ring.tx_pending = test->ifobj_tx->ring.tx_max_pending;
  2065. test->ifobj_tx->ring.rx_pending = test->ifobj_tx->ring.rx_max_pending;
  2066. test->ifobj_rx->umem->num_frames = max_descs;
  2067. test->ifobj_rx->umem->fill_size = max_descs;
  2068. test->ifobj_rx->umem->comp_size = max_descs;
  2069. test->ifobj_tx->xsk->batch_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
  2070. test->ifobj_rx->xsk->batch_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
  2071. ret = testapp_validate_traffic(test);
  2072. if (ret)
  2073. return ret;
  2074. /* Set batch_size to 8152 for testing, as the ice HW ignores the 3 lowest bits when
  2075. * updating the Rx HW tail register.
  2076. */
  2077. test->ifobj_tx->xsk->batch_size = test->ifobj_tx->ring.tx_max_pending - 8;
  2078. test->ifobj_rx->xsk->batch_size = test->ifobj_tx->ring.tx_max_pending - 8;
  2079. pkt_stream_replace(test, max_descs, MIN_PKT_SIZE);
  2080. return testapp_validate_traffic(test);
  2081. }
  2082. static void run_pkt_test(struct test_spec *test)
  2083. {
  2084. int ret;
  2085. ret = test->test_func(test);
  2086. if (ret == TEST_PASS)
  2087. ksft_test_result_pass("PASS: %s %s%s\n", mode_string(test), busy_poll_string(test),
  2088. test->name);
  2089. pkt_stream_restore_default(test);
  2090. }
  2091. static struct ifobject *ifobject_create(void)
  2092. {
  2093. struct ifobject *ifobj;
  2094. ifobj = calloc(1, sizeof(struct ifobject));
  2095. if (!ifobj)
  2096. return NULL;
  2097. ifobj->xsk_arr = calloc(MAX_SOCKETS, sizeof(*ifobj->xsk_arr));
  2098. if (!ifobj->xsk_arr)
  2099. goto out_xsk_arr;
  2100. ifobj->umem = calloc(1, sizeof(*ifobj->umem));
  2101. if (!ifobj->umem)
  2102. goto out_umem;
  2103. return ifobj;
  2104. out_umem:
  2105. free(ifobj->xsk_arr);
  2106. out_xsk_arr:
  2107. free(ifobj);
  2108. return NULL;
  2109. }
  2110. static void ifobject_delete(struct ifobject *ifobj)
  2111. {
  2112. free(ifobj->umem);
  2113. free(ifobj->xsk_arr);
  2114. free(ifobj);
  2115. }
  2116. static bool is_xdp_supported(int ifindex)
  2117. {
  2118. int flags = XDP_FLAGS_DRV_MODE;
  2119. LIBBPF_OPTS(bpf_link_create_opts, opts, .flags = flags);
  2120. struct bpf_insn insns[2] = {
  2121. BPF_MOV64_IMM(BPF_REG_0, XDP_PASS),
  2122. BPF_EXIT_INSN()
  2123. };
  2124. int prog_fd, insn_cnt = ARRAY_SIZE(insns);
  2125. int err;
  2126. prog_fd = bpf_prog_load(BPF_PROG_TYPE_XDP, NULL, "GPL", insns, insn_cnt, NULL);
  2127. if (prog_fd < 0)
  2128. return false;
  2129. err = bpf_xdp_attach(ifindex, prog_fd, flags, NULL);
  2130. if (err) {
  2131. close(prog_fd);
  2132. return false;
  2133. }
  2134. bpf_xdp_detach(ifindex, flags, NULL);
  2135. close(prog_fd);
  2136. return true;
  2137. }
  2138. static const struct test_spec tests[] = {
  2139. {.name = "SEND_RECEIVE", .test_func = testapp_send_receive},
  2140. {.name = "SEND_RECEIVE_2K_FRAME", .test_func = testapp_send_receive_2k_frame},
  2141. {.name = "SEND_RECEIVE_SINGLE_PKT", .test_func = testapp_single_pkt},
  2142. {.name = "POLL_RX", .test_func = testapp_poll_rx},
  2143. {.name = "POLL_TX", .test_func = testapp_poll_tx},
  2144. {.name = "POLL_RXQ_FULL", .test_func = testapp_poll_rxq_tmout},
  2145. {.name = "POLL_TXQ_FULL", .test_func = testapp_poll_txq_tmout},
  2146. {.name = "SEND_RECEIVE_UNALIGNED", .test_func = testapp_send_receive_unaligned},
  2147. {.name = "ALIGNED_INV_DESC", .test_func = testapp_aligned_inv_desc},
  2148. {.name = "ALIGNED_INV_DESC_2K_FRAME_SIZE", .test_func = testapp_aligned_inv_desc_2k_frame},
  2149. {.name = "UNALIGNED_INV_DESC", .test_func = testapp_unaligned_inv_desc},
  2150. {.name = "UNALIGNED_INV_DESC_4001_FRAME_SIZE",
  2151. .test_func = testapp_unaligned_inv_desc_4001_frame},
  2152. {.name = "UMEM_HEADROOM", .test_func = testapp_headroom},
  2153. {.name = "TEARDOWN", .test_func = testapp_teardown},
  2154. {.name = "BIDIRECTIONAL", .test_func = testapp_bidirectional},
  2155. {.name = "STAT_RX_DROPPED", .test_func = testapp_stats_rx_dropped},
  2156. {.name = "STAT_TX_INVALID", .test_func = testapp_stats_tx_invalid_descs},
  2157. {.name = "STAT_RX_FULL", .test_func = testapp_stats_rx_full},
  2158. {.name = "STAT_FILL_EMPTY", .test_func = testapp_stats_fill_empty},
  2159. {.name = "XDP_PROG_CLEANUP", .test_func = testapp_xdp_prog_cleanup},
  2160. {.name = "XDP_DROP_HALF", .test_func = testapp_xdp_drop},
  2161. {.name = "XDP_SHARED_UMEM", .test_func = testapp_xdp_shared_umem},
  2162. {.name = "XDP_METADATA_COPY", .test_func = testapp_xdp_metadata},
  2163. {.name = "XDP_METADATA_COPY_MULTI_BUFF", .test_func = testapp_xdp_metadata_mb},
  2164. {.name = "SEND_RECEIVE_9K_PACKETS", .test_func = testapp_send_receive_mb},
  2165. {.name = "SEND_RECEIVE_UNALIGNED_9K_PACKETS",
  2166. .test_func = testapp_send_receive_unaligned_mb},
  2167. {.name = "ALIGNED_INV_DESC_MULTI_BUFF", .test_func = testapp_aligned_inv_desc_mb},
  2168. {.name = "UNALIGNED_INV_DESC_MULTI_BUFF", .test_func = testapp_unaligned_inv_desc_mb},
  2169. {.name = "TOO_MANY_FRAGS", .test_func = testapp_too_many_frags},
  2170. {.name = "HW_SW_MIN_RING_SIZE", .test_func = testapp_hw_sw_min_ring_size},
  2171. {.name = "HW_SW_MAX_RING_SIZE", .test_func = testapp_hw_sw_max_ring_size},
  2172. };
  2173. static void print_tests(void)
  2174. {
  2175. u32 i;
  2176. printf("Tests:\n");
  2177. for (i = 0; i < ARRAY_SIZE(tests); i++)
  2178. printf("%u: %s\n", i, tests[i].name);
  2179. }
  2180. int main(int argc, char **argv)
  2181. {
  2182. struct pkt_stream *rx_pkt_stream_default;
  2183. struct pkt_stream *tx_pkt_stream_default;
  2184. struct ifobject *ifobj_tx, *ifobj_rx;
  2185. u32 i, j, failed_tests = 0, nb_tests;
  2186. int modes = TEST_MODE_SKB + 1;
  2187. struct test_spec test;
  2188. bool shared_netdev;
  2189. int ret;
  2190. /* Use libbpf 1.0 API mode */
  2191. libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
  2192. ifobj_tx = ifobject_create();
  2193. if (!ifobj_tx)
  2194. exit_with_error(ENOMEM);
  2195. ifobj_rx = ifobject_create();
  2196. if (!ifobj_rx)
  2197. exit_with_error(ENOMEM);
  2198. setlocale(LC_ALL, "");
  2199. parse_command_line(ifobj_tx, ifobj_rx, argc, argv);
  2200. if (opt_print_tests) {
  2201. print_tests();
  2202. ksft_exit_xpass();
  2203. }
  2204. if (opt_run_test != RUN_ALL_TESTS && opt_run_test >= ARRAY_SIZE(tests)) {
  2205. ksft_print_msg("Error: test %u does not exist.\n", opt_run_test);
  2206. ksft_exit_xfail();
  2207. }
  2208. shared_netdev = (ifobj_tx->ifindex == ifobj_rx->ifindex);
  2209. ifobj_tx->shared_umem = shared_netdev;
  2210. ifobj_rx->shared_umem = shared_netdev;
  2211. if (!validate_interface(ifobj_tx) || !validate_interface(ifobj_rx))
  2212. print_usage(argv);
  2213. if (is_xdp_supported(ifobj_tx->ifindex)) {
  2214. modes++;
  2215. if (ifobj_zc_avail(ifobj_tx))
  2216. modes++;
  2217. }
  2218. ret = get_hw_ring_size(ifobj_tx->ifname, &ifobj_tx->ring);
  2219. if (!ret) {
  2220. ifobj_tx->hw_ring_size_supp = true;
  2221. ifobj_tx->set_ring.default_tx = ifobj_tx->ring.tx_pending;
  2222. ifobj_tx->set_ring.default_rx = ifobj_tx->ring.rx_pending;
  2223. }
  2224. init_iface(ifobj_rx, worker_testapp_validate_rx);
  2225. init_iface(ifobj_tx, worker_testapp_validate_tx);
  2226. test_spec_init(&test, ifobj_tx, ifobj_rx, 0, &tests[0]);
  2227. tx_pkt_stream_default = pkt_stream_generate(DEFAULT_PKT_CNT, MIN_PKT_SIZE);
  2228. rx_pkt_stream_default = pkt_stream_generate(DEFAULT_PKT_CNT, MIN_PKT_SIZE);
  2229. if (!tx_pkt_stream_default || !rx_pkt_stream_default)
  2230. exit_with_error(ENOMEM);
  2231. test.tx_pkt_stream_default = tx_pkt_stream_default;
  2232. test.rx_pkt_stream_default = rx_pkt_stream_default;
  2233. if (opt_run_test == RUN_ALL_TESTS)
  2234. nb_tests = ARRAY_SIZE(tests);
  2235. else
  2236. nb_tests = 1;
  2237. if (opt_mode == TEST_MODE_ALL) {
  2238. ksft_set_plan(modes * nb_tests);
  2239. } else {
  2240. if (opt_mode == TEST_MODE_DRV && modes <= TEST_MODE_DRV) {
  2241. ksft_print_msg("Error: XDP_DRV mode not supported.\n");
  2242. ksft_exit_xfail();
  2243. }
  2244. if (opt_mode == TEST_MODE_ZC && modes <= TEST_MODE_ZC) {
  2245. ksft_print_msg("Error: zero-copy mode not supported.\n");
  2246. ksft_exit_xfail();
  2247. }
  2248. ksft_set_plan(nb_tests);
  2249. }
  2250. for (i = 0; i < modes; i++) {
  2251. if (opt_mode != TEST_MODE_ALL && i != opt_mode)
  2252. continue;
  2253. for (j = 0; j < ARRAY_SIZE(tests); j++) {
  2254. if (opt_run_test != RUN_ALL_TESTS && j != opt_run_test)
  2255. continue;
  2256. test_spec_init(&test, ifobj_tx, ifobj_rx, i, &tests[j]);
  2257. run_pkt_test(&test);
  2258. usleep(USLEEP_MAX);
  2259. if (test.fail)
  2260. failed_tests++;
  2261. }
  2262. }
  2263. if (ifobj_tx->hw_ring_size_supp)
  2264. hw_ring_size_reset(ifobj_tx);
  2265. pkt_stream_delete(tx_pkt_stream_default);
  2266. pkt_stream_delete(rx_pkt_stream_default);
  2267. xsk_unload_xdp_programs(ifobj_tx);
  2268. xsk_unload_xdp_programs(ifobj_rx);
  2269. ifobject_delete(ifobj_tx);
  2270. ifobject_delete(ifobj_rx);
  2271. if (failed_tests)
  2272. ksft_exit_fail();
  2273. else
  2274. ksft_exit_pass();
  2275. }