2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009-2011 Spectra Logic Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * substantially similar to the "NO WARRANTY" disclaimer below
15 * ("Disclaimer") and any redistribution must be conditioned upon
16 * including a substantially similar Disclaimer requirement for further
17 * binary redistribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGES.
32 * Authors: Justin T. Gibbs (Spectra Logic Corporation)
33 * Alan Somers (Spectra Logic Corporation)
34 * John Suykerbuyk (Spectra Logic Corporation)
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 * \file netback_unit_tests.c
43 * \brief Unit tests for the Xen netback driver.
45 * Due to the driver's use of static functions, these tests cannot be compiled
46 * standalone; they must be #include'd from the driver's .c file.
50 /** Helper macro used to snprintf to a buffer and update the buffer pointer */
51 #define SNCATF(buffer, buflen, ...) do { \
52 size_t new_chars = snprintf(buffer, buflen, __VA_ARGS__); \
53 buffer += new_chars; \
54 /* be careful; snprintf's return value can be > buflen */ \
55 buflen -= MIN(buflen, new_chars); \
58 /* STRINGIFY and TOSTRING are used only to help turn __LINE__ into a string */
59 #define STRINGIFY(x) #x
60 #define TOSTRING(x) STRINGIFY(x)
63 * Writes an error message to buffer if cond is false
64 * Note the implied parameters buffer and
67 #define XNB_ASSERT(cond) ({ \
68 int passed = (cond); \
69 char *_buffer = (buffer); \
70 size_t _buflen = (buflen); \
72 strlcat(_buffer, __func__, _buflen); \
73 strlcat(_buffer, ":" TOSTRING(__LINE__) \
74 " Assertion Error: " #cond "\n", _buflen); \
80 * The signature used by all testcases. If the test writes anything
81 * to buffer, then it will be considered a failure
82 * \param buffer Return storage for error messages
83 * \param buflen The space available in the buffer
85 typedef void testcase_t(char *buffer, size_t buflen);
88 * Signature used by setup functions
89 * \return nonzero on error
91 typedef int setup_t(void);
93 typedef void teardown_t(void);
95 /** A simple test fixture comprising setup, teardown, and test */
97 /** Will be run before the test to allocate and initialize variables */
100 /** Will be run if setup succeeds */
103 /** Cleans up test data whether or not the setup succeeded */
104 teardown_t *teardown;
107 typedef struct test_fixture test_fixture_t;
109 static int xnb_get1pkt(struct xnb_pkt *pkt, size_t size, uint16_t flags);
110 static int xnb_unit_test_runner(test_fixture_t const tests[], int ntests,
111 char *buffer, size_t buflen);
114 null_setup(void) { return 0; }
117 null_teardown(void) { }
119 static setup_t setup_pvt_data;
120 static teardown_t teardown_pvt_data;
121 static testcase_t xnb_ring2pkt_emptyring;
122 static testcase_t xnb_ring2pkt_1req;
123 static testcase_t xnb_ring2pkt_2req;
124 static testcase_t xnb_ring2pkt_3req;
125 static testcase_t xnb_ring2pkt_extra;
126 static testcase_t xnb_ring2pkt_partial;
127 static testcase_t xnb_ring2pkt_wraps;
128 static testcase_t xnb_txpkt2rsp_emptypkt;
129 static testcase_t xnb_txpkt2rsp_1req;
130 static testcase_t xnb_txpkt2rsp_extra;
131 static testcase_t xnb_txpkt2rsp_long;
132 static testcase_t xnb_txpkt2rsp_invalid;
133 static testcase_t xnb_txpkt2rsp_error;
134 static testcase_t xnb_txpkt2rsp_wraps;
135 static testcase_t xnb_pkt2mbufc_empty;
136 static testcase_t xnb_pkt2mbufc_short;
137 static testcase_t xnb_pkt2mbufc_csum;
138 static testcase_t xnb_pkt2mbufc_1cluster;
139 static testcase_t xnb_pkt2mbufc_largecluster;
140 static testcase_t xnb_pkt2mbufc_2cluster;
141 static testcase_t xnb_txpkt2gnttab_empty;
142 static testcase_t xnb_txpkt2gnttab_short;
143 static testcase_t xnb_txpkt2gnttab_2req;
144 static testcase_t xnb_txpkt2gnttab_2cluster;
145 static testcase_t xnb_update_mbufc_short;
146 static testcase_t xnb_update_mbufc_2req;
147 static testcase_t xnb_update_mbufc_2cluster;
148 static testcase_t xnb_mbufc2pkt_empty;
149 static testcase_t xnb_mbufc2pkt_short;
150 static testcase_t xnb_mbufc2pkt_1cluster;
151 static testcase_t xnb_mbufc2pkt_2short;
152 static testcase_t xnb_mbufc2pkt_long;
153 static testcase_t xnb_mbufc2pkt_extra;
154 static testcase_t xnb_mbufc2pkt_nospace;
155 static testcase_t xnb_rxpkt2gnttab_empty;
156 static testcase_t xnb_rxpkt2gnttab_short;
157 static testcase_t xnb_rxpkt2gnttab_2req;
158 static testcase_t xnb_rxpkt2rsp_empty;
159 static testcase_t xnb_rxpkt2rsp_short;
160 static testcase_t xnb_rxpkt2rsp_extra;
161 static testcase_t xnb_rxpkt2rsp_2short;
162 static testcase_t xnb_rxpkt2rsp_2slots;
163 static testcase_t xnb_rxpkt2rsp_copyerror;
164 static testcase_t xnb_sscanf_llu;
165 static testcase_t xnb_sscanf_lld;
166 static testcase_t xnb_sscanf_hhu;
167 static testcase_t xnb_sscanf_hhd;
168 static testcase_t xnb_sscanf_hhn;
170 #if defined(INET) || defined(INET6)
171 /* TODO: add test cases for xnb_add_mbuf_cksum for IPV6 tcp and udp */
172 static testcase_t xnb_add_mbuf_cksum_arp;
173 static testcase_t xnb_add_mbuf_cksum_tcp;
174 static testcase_t xnb_add_mbuf_cksum_udp;
175 static testcase_t xnb_add_mbuf_cksum_icmp;
176 static testcase_t xnb_add_mbuf_cksum_tcp_swcksum;
177 static void xnb_fill_eh_and_ip(struct mbuf *m, uint16_t ip_len,
178 uint16_t ip_id, uint16_t ip_p,
179 uint16_t ip_off, uint16_t ip_sum);
180 static void xnb_fill_tcp(struct mbuf *m);
181 #endif /* INET || INET6 */
183 /** Private data used by unit tests */
185 gnttab_copy_table gnttab;
186 netif_rx_back_ring_t rxb;
187 netif_rx_front_ring_t rxf;
188 netif_tx_back_ring_t txb;
189 netif_tx_front_ring_t txf;
191 netif_rx_sring_t* rxs;
192 netif_tx_sring_t* txs;
195 static inline void safe_m_freem(struct mbuf **ppMbuf) {
196 if (*ppMbuf != NULL) {
203 * The unit test runner. It will run every supplied test and return an
204 * output message as a string
205 * \param tests An array of tests. Every test will be attempted.
206 * \param ntests The length of tests
207 * \param buffer Return storage for the result string
208 * \param buflen The length of buffer
209 * \return The number of tests that failed
212 xnb_unit_test_runner(test_fixture_t const tests[], int ntests, char *buffer,
219 for (i = 0; i < ntests; i++) {
220 int error = tests[i].setup();
222 SNCATF(buffer, buflen,
223 "Setup failed for test idx %d\n", i);
228 tests[i].test(buffer, buflen);
229 new_chars = strnlen(buffer, buflen);
240 n_passes = ntests - n_failures;
242 SNCATF(buffer, buflen, "%d Tests Passed\n", n_passes);
244 if (n_failures > 0) {
245 SNCATF(buffer, buflen, "%d Tests FAILED\n", n_failures);
251 /** Number of unit tests. Must match the length of the tests array below */
252 #define TOTAL_TESTS (53)
254 * Max memory available for returning results. 400 chars/test should give
255 * enough space for a five line error message for every test
257 #define TOTAL_BUFLEN (400 * TOTAL_TESTS + 2)
260 * Called from userspace by a sysctl. Runs all internal unit tests, and
261 * returns the results to userspace as a string
263 * \param arg1 pointer to an xnb_softc for a specific xnb device
265 * \param req sysctl access structure
266 * \return a string via the special SYSCTL_OUT macro.
270 xnb_unit_test_main(SYSCTL_HANDLER_ARGS) {
271 test_fixture_t const tests[TOTAL_TESTS] = {
272 {setup_pvt_data, xnb_ring2pkt_emptyring, teardown_pvt_data},
273 {setup_pvt_data, xnb_ring2pkt_1req, teardown_pvt_data},
274 {setup_pvt_data, xnb_ring2pkt_2req, teardown_pvt_data},
275 {setup_pvt_data, xnb_ring2pkt_3req, teardown_pvt_data},
276 {setup_pvt_data, xnb_ring2pkt_extra, teardown_pvt_data},
277 {setup_pvt_data, xnb_ring2pkt_partial, teardown_pvt_data},
278 {setup_pvt_data, xnb_ring2pkt_wraps, teardown_pvt_data},
279 {setup_pvt_data, xnb_txpkt2rsp_emptypkt, teardown_pvt_data},
280 {setup_pvt_data, xnb_txpkt2rsp_1req, teardown_pvt_data},
281 {setup_pvt_data, xnb_txpkt2rsp_extra, teardown_pvt_data},
282 {setup_pvt_data, xnb_txpkt2rsp_long, teardown_pvt_data},
283 {setup_pvt_data, xnb_txpkt2rsp_invalid, teardown_pvt_data},
284 {setup_pvt_data, xnb_txpkt2rsp_error, teardown_pvt_data},
285 {setup_pvt_data, xnb_txpkt2rsp_wraps, teardown_pvt_data},
286 {setup_pvt_data, xnb_pkt2mbufc_empty, teardown_pvt_data},
287 {setup_pvt_data, xnb_pkt2mbufc_short, teardown_pvt_data},
288 {setup_pvt_data, xnb_pkt2mbufc_csum, teardown_pvt_data},
289 {setup_pvt_data, xnb_pkt2mbufc_1cluster, teardown_pvt_data},
290 {setup_pvt_data, xnb_pkt2mbufc_largecluster, teardown_pvt_data},
291 {setup_pvt_data, xnb_pkt2mbufc_2cluster, teardown_pvt_data},
292 {setup_pvt_data, xnb_txpkt2gnttab_empty, teardown_pvt_data},
293 {setup_pvt_data, xnb_txpkt2gnttab_short, teardown_pvt_data},
294 {setup_pvt_data, xnb_txpkt2gnttab_2req, teardown_pvt_data},
295 {setup_pvt_data, xnb_txpkt2gnttab_2cluster, teardown_pvt_data},
296 {setup_pvt_data, xnb_update_mbufc_short, teardown_pvt_data},
297 {setup_pvt_data, xnb_update_mbufc_2req, teardown_pvt_data},
298 {setup_pvt_data, xnb_update_mbufc_2cluster, teardown_pvt_data},
299 {setup_pvt_data, xnb_mbufc2pkt_empty, teardown_pvt_data},
300 {setup_pvt_data, xnb_mbufc2pkt_short, teardown_pvt_data},
301 {setup_pvt_data, xnb_mbufc2pkt_1cluster, teardown_pvt_data},
302 {setup_pvt_data, xnb_mbufc2pkt_2short, teardown_pvt_data},
303 {setup_pvt_data, xnb_mbufc2pkt_long, teardown_pvt_data},
304 {setup_pvt_data, xnb_mbufc2pkt_extra, teardown_pvt_data},
305 {setup_pvt_data, xnb_mbufc2pkt_nospace, teardown_pvt_data},
306 {setup_pvt_data, xnb_rxpkt2gnttab_empty, teardown_pvt_data},
307 {setup_pvt_data, xnb_rxpkt2gnttab_short, teardown_pvt_data},
308 {setup_pvt_data, xnb_rxpkt2gnttab_2req, teardown_pvt_data},
309 {setup_pvt_data, xnb_rxpkt2rsp_empty, teardown_pvt_data},
310 {setup_pvt_data, xnb_rxpkt2rsp_short, teardown_pvt_data},
311 {setup_pvt_data, xnb_rxpkt2rsp_extra, teardown_pvt_data},
312 {setup_pvt_data, xnb_rxpkt2rsp_2short, teardown_pvt_data},
313 {setup_pvt_data, xnb_rxpkt2rsp_2slots, teardown_pvt_data},
314 {setup_pvt_data, xnb_rxpkt2rsp_copyerror, teardown_pvt_data},
315 #if defined(INET) || defined(INET6)
316 {null_setup, xnb_add_mbuf_cksum_arp, null_teardown},
317 {null_setup, xnb_add_mbuf_cksum_icmp, null_teardown},
318 {null_setup, xnb_add_mbuf_cksum_tcp, null_teardown},
319 {null_setup, xnb_add_mbuf_cksum_tcp_swcksum, null_teardown},
320 {null_setup, xnb_add_mbuf_cksum_udp, null_teardown},
322 {null_setup, xnb_sscanf_hhd, null_teardown},
323 {null_setup, xnb_sscanf_hhu, null_teardown},
324 {null_setup, xnb_sscanf_lld, null_teardown},
325 {null_setup, xnb_sscanf_llu, null_teardown},
326 {null_setup, xnb_sscanf_hhn, null_teardown},
329 * results is static so that the data will persist after this function
330 * returns. The sysctl code expects us to return a constant string.
331 * \todo: the static variable is not thread safe. Put a mutex around
334 static char results[TOTAL_BUFLEN];
336 /* empty the result strings */
338 xnb_unit_test_runner(tests, TOTAL_TESTS, results, TOTAL_BUFLEN);
340 return (SYSCTL_OUT(req, results, strnlen(results, TOTAL_BUFLEN)));
348 bzero(xnb_unit_pvt.gnttab, sizeof(xnb_unit_pvt.gnttab));
350 xnb_unit_pvt.txs = malloc(PAGE_SIZE, M_XENNETBACK, M_WAITOK|M_ZERO);
351 if (xnb_unit_pvt.txs != NULL) {
352 SHARED_RING_INIT(xnb_unit_pvt.txs);
353 BACK_RING_INIT(&xnb_unit_pvt.txb, xnb_unit_pvt.txs, PAGE_SIZE);
354 FRONT_RING_INIT(&xnb_unit_pvt.txf, xnb_unit_pvt.txs, PAGE_SIZE);
359 xnb_unit_pvt.ifp = if_alloc(IFT_ETHER);
360 if (xnb_unit_pvt.ifp == NULL) {
364 xnb_unit_pvt.rxs = malloc(PAGE_SIZE, M_XENNETBACK, M_WAITOK|M_ZERO);
365 if (xnb_unit_pvt.rxs != NULL) {
366 SHARED_RING_INIT(xnb_unit_pvt.rxs);
367 BACK_RING_INIT(&xnb_unit_pvt.rxb, xnb_unit_pvt.rxs, PAGE_SIZE);
368 FRONT_RING_INIT(&xnb_unit_pvt.rxf, xnb_unit_pvt.rxs, PAGE_SIZE);
377 teardown_pvt_data(void)
379 if (xnb_unit_pvt.txs != NULL) {
380 free(xnb_unit_pvt.txs, M_XENNETBACK);
382 if (xnb_unit_pvt.rxs != NULL) {
383 free(xnb_unit_pvt.rxs, M_XENNETBACK);
385 if (xnb_unit_pvt.ifp != NULL) {
386 if_free(xnb_unit_pvt.ifp);
391 * Verify that xnb_ring2pkt will not consume any requests from an empty ring
394 xnb_ring2pkt_emptyring(char *buffer, size_t buflen)
399 num_consumed = xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb,
400 xnb_unit_pvt.txb.req_cons);
401 XNB_ASSERT(num_consumed == 0);
405 * Verify that xnb_ring2pkt can convert a single request packet correctly
408 xnb_ring2pkt_1req(char *buffer, size_t buflen)
412 struct netif_tx_request *req;
414 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
415 xnb_unit_pvt.txf.req_prod_pvt);
418 req->size = 69; /* arbitrary number for test */
419 xnb_unit_pvt.txf.req_prod_pvt++;
421 RING_PUSH_REQUESTS(&xnb_unit_pvt.txf);
423 num_consumed = xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb,
424 xnb_unit_pvt.txb.req_cons);
425 XNB_ASSERT(num_consumed == 1);
426 XNB_ASSERT(pkt.size == 69);
427 XNB_ASSERT(pkt.car_size == 69);
428 XNB_ASSERT(pkt.flags == 0);
429 XNB_ASSERT(xnb_pkt_is_valid(&pkt));
430 XNB_ASSERT(pkt.list_len == 1);
431 XNB_ASSERT(pkt.car == 0);
435 * Verify that xnb_ring2pkt can convert a two request packet correctly.
436 * This tests handling of the MORE_DATA flag and cdr
439 xnb_ring2pkt_2req(char *buffer, size_t buflen)
443 struct netif_tx_request *req;
444 RING_IDX start_idx = xnb_unit_pvt.txf.req_prod_pvt;
446 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
447 xnb_unit_pvt.txf.req_prod_pvt);
448 req->flags = NETTXF_more_data;
450 xnb_unit_pvt.txf.req_prod_pvt++;
452 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
453 xnb_unit_pvt.txf.req_prod_pvt);
456 xnb_unit_pvt.txf.req_prod_pvt++;
458 RING_PUSH_REQUESTS(&xnb_unit_pvt.txf);
460 num_consumed = xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb,
461 xnb_unit_pvt.txb.req_cons);
462 XNB_ASSERT(num_consumed == 2);
463 XNB_ASSERT(pkt.size == 100);
464 XNB_ASSERT(pkt.car_size == 60);
465 XNB_ASSERT(pkt.flags == 0);
466 XNB_ASSERT(xnb_pkt_is_valid(&pkt));
467 XNB_ASSERT(pkt.list_len == 2);
468 XNB_ASSERT(pkt.car == start_idx);
469 XNB_ASSERT(pkt.cdr == start_idx + 1);
473 * Verify that xnb_ring2pkt can convert a three request packet correctly
476 xnb_ring2pkt_3req(char *buffer, size_t buflen)
480 struct netif_tx_request *req;
481 RING_IDX start_idx = xnb_unit_pvt.txf.req_prod_pvt;
483 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
484 xnb_unit_pvt.txf.req_prod_pvt);
485 req->flags = NETTXF_more_data;
487 xnb_unit_pvt.txf.req_prod_pvt++;
489 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
490 xnb_unit_pvt.txf.req_prod_pvt);
491 req->flags = NETTXF_more_data;
493 xnb_unit_pvt.txf.req_prod_pvt++;
495 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
496 xnb_unit_pvt.txf.req_prod_pvt);
499 xnb_unit_pvt.txf.req_prod_pvt++;
501 RING_PUSH_REQUESTS(&xnb_unit_pvt.txf);
503 num_consumed = xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb,
504 xnb_unit_pvt.txb.req_cons);
505 XNB_ASSERT(num_consumed == 3);
506 XNB_ASSERT(pkt.size == 200);
507 XNB_ASSERT(pkt.car_size == 110);
508 XNB_ASSERT(pkt.flags == 0);
509 XNB_ASSERT(xnb_pkt_is_valid(&pkt));
510 XNB_ASSERT(pkt.list_len == 3);
511 XNB_ASSERT(pkt.car == start_idx);
512 XNB_ASSERT(pkt.cdr == start_idx + 1);
513 XNB_ASSERT(RING_GET_REQUEST(&xnb_unit_pvt.txb, pkt.cdr + 1) == req);
517 * Verify that xnb_ring2pkt can read extra inf
520 xnb_ring2pkt_extra(char *buffer, size_t buflen)
524 struct netif_tx_request *req;
525 struct netif_extra_info *ext;
526 RING_IDX start_idx = xnb_unit_pvt.txf.req_prod_pvt;
528 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
529 xnb_unit_pvt.txf.req_prod_pvt);
530 req->flags = NETTXF_extra_info | NETTXF_more_data;
532 xnb_unit_pvt.txf.req_prod_pvt++;
534 ext = (struct netif_extra_info*) RING_GET_REQUEST(&xnb_unit_pvt.txf,
535 xnb_unit_pvt.txf.req_prod_pvt);
537 ext->type = XEN_NETIF_EXTRA_TYPE_GSO;
538 ext->u.gso.size = 250;
539 ext->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
540 ext->u.gso.features = 0;
541 xnb_unit_pvt.txf.req_prod_pvt++;
543 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
544 xnb_unit_pvt.txf.req_prod_pvt);
547 xnb_unit_pvt.txf.req_prod_pvt++;
549 RING_PUSH_REQUESTS(&xnb_unit_pvt.txf);
551 num_consumed = xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb,
552 xnb_unit_pvt.txb.req_cons);
553 XNB_ASSERT(num_consumed == 3);
554 XNB_ASSERT(pkt.extra.flags == 0);
555 XNB_ASSERT(pkt.extra.type == XEN_NETIF_EXTRA_TYPE_GSO);
556 XNB_ASSERT(pkt.extra.u.gso.size == 250);
557 XNB_ASSERT(pkt.extra.u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4);
558 XNB_ASSERT(pkt.size == 150);
559 XNB_ASSERT(pkt.car_size == 100);
560 XNB_ASSERT(pkt.flags == NETTXF_extra_info);
561 XNB_ASSERT(xnb_pkt_is_valid(&pkt));
562 XNB_ASSERT(pkt.list_len == 2);
563 XNB_ASSERT(pkt.car == start_idx);
564 XNB_ASSERT(pkt.cdr == start_idx + 2);
565 XNB_ASSERT(RING_GET_REQUEST(&xnb_unit_pvt.txb, pkt.cdr) == req);
569 * Verify that xnb_ring2pkt will consume no requests if the entire packet is
570 * not yet in the ring
573 xnb_ring2pkt_partial(char *buffer, size_t buflen)
577 struct netif_tx_request *req;
579 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
580 xnb_unit_pvt.txf.req_prod_pvt);
581 req->flags = NETTXF_more_data;
583 xnb_unit_pvt.txf.req_prod_pvt++;
585 RING_PUSH_REQUESTS(&xnb_unit_pvt.txf);
587 num_consumed = xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb,
588 xnb_unit_pvt.txb.req_cons);
589 XNB_ASSERT(num_consumed == 0);
590 XNB_ASSERT(! xnb_pkt_is_valid(&pkt));
594 * Verity that xnb_ring2pkt can read a packet whose requests wrap around
595 * the end of the ring
598 xnb_ring2pkt_wraps(char *buffer, size_t buflen)
602 struct netif_tx_request *req;
606 * Manually tweak the ring indices to create a ring with no responses
607 * and the next request slot at position 2 from the end
609 rsize = RING_SIZE(&xnb_unit_pvt.txf);
610 xnb_unit_pvt.txf.req_prod_pvt = rsize - 2;
611 xnb_unit_pvt.txf.rsp_cons = rsize - 2;
612 xnb_unit_pvt.txs->req_prod = rsize - 2;
613 xnb_unit_pvt.txs->req_event = rsize - 1;
614 xnb_unit_pvt.txs->rsp_prod = rsize - 2;
615 xnb_unit_pvt.txs->rsp_event = rsize - 1;
616 xnb_unit_pvt.txb.rsp_prod_pvt = rsize - 2;
617 xnb_unit_pvt.txb.req_cons = rsize - 2;
619 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
620 xnb_unit_pvt.txf.req_prod_pvt);
621 req->flags = NETTXF_more_data;
623 xnb_unit_pvt.txf.req_prod_pvt++;
625 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
626 xnb_unit_pvt.txf.req_prod_pvt);
627 req->flags = NETTXF_more_data;
629 xnb_unit_pvt.txf.req_prod_pvt++;
631 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
632 xnb_unit_pvt.txf.req_prod_pvt);
635 xnb_unit_pvt.txf.req_prod_pvt++;
637 RING_PUSH_REQUESTS(&xnb_unit_pvt.txf);
639 num_consumed = xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb,
640 xnb_unit_pvt.txb.req_cons);
641 XNB_ASSERT(num_consumed == 3);
642 XNB_ASSERT(xnb_pkt_is_valid(&pkt));
643 XNB_ASSERT(pkt.list_len == 3);
644 XNB_ASSERT(RING_GET_REQUEST(&xnb_unit_pvt.txb, pkt.cdr + 1) == req);
649 * xnb_txpkt2rsp should do nothing for an empty packet
652 xnb_txpkt2rsp_emptypkt(char *buffer, size_t buflen)
656 netif_tx_back_ring_t txb_backup = xnb_unit_pvt.txb;
657 netif_tx_sring_t txs_backup = *xnb_unit_pvt.txs;
660 /* must call xnb_ring2pkt just to intialize pkt */
661 num_consumed = xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb,
662 xnb_unit_pvt.txb.req_cons);
663 xnb_txpkt2rsp(&pkt, &xnb_unit_pvt.txb, 0);
665 memcmp(&txb_backup, &xnb_unit_pvt.txb, sizeof(txb_backup)) == 0);
667 memcmp(&txs_backup, xnb_unit_pvt.txs, sizeof(txs_backup)) == 0);
671 * xnb_txpkt2rsp responding to one request
674 xnb_txpkt2rsp_1req(char *buffer, size_t buflen)
676 uint16_t num_consumed;
678 struct netif_tx_request *req;
679 struct netif_tx_response *rsp;
681 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
682 xnb_unit_pvt.txf.req_prod_pvt);
685 xnb_unit_pvt.txf.req_prod_pvt++;
687 RING_PUSH_REQUESTS(&xnb_unit_pvt.txf);
689 num_consumed = xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb,
690 xnb_unit_pvt.txb.req_cons);
691 xnb_unit_pvt.txb.req_cons += num_consumed;
693 xnb_txpkt2rsp(&pkt, &xnb_unit_pvt.txb, 0);
694 rsp = RING_GET_RESPONSE(&xnb_unit_pvt.txb, xnb_unit_pvt.txf.rsp_cons);
697 xnb_unit_pvt.txb.rsp_prod_pvt == xnb_unit_pvt.txs->req_prod);
698 XNB_ASSERT(rsp->id == req->id);
699 XNB_ASSERT(rsp->status == NETIF_RSP_OKAY);
703 * xnb_txpkt2rsp responding to 1 data request and 1 extra info
706 xnb_txpkt2rsp_extra(char *buffer, size_t buflen)
708 uint16_t num_consumed;
710 struct netif_tx_request *req;
711 netif_extra_info_t *ext;
712 struct netif_tx_response *rsp;
714 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
715 xnb_unit_pvt.txf.req_prod_pvt);
717 req->flags = NETTXF_extra_info;
719 xnb_unit_pvt.txf.req_prod_pvt++;
721 ext = (netif_extra_info_t*) RING_GET_REQUEST(&xnb_unit_pvt.txf,
722 xnb_unit_pvt.txf.req_prod_pvt);
723 ext->type = XEN_NETIF_EXTRA_TYPE_GSO;
725 xnb_unit_pvt.txf.req_prod_pvt++;
727 RING_PUSH_REQUESTS(&xnb_unit_pvt.txf);
729 num_consumed = xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb,
730 xnb_unit_pvt.txb.req_cons);
731 xnb_unit_pvt.txb.req_cons += num_consumed;
733 xnb_txpkt2rsp(&pkt, &xnb_unit_pvt.txb, 0);
736 xnb_unit_pvt.txb.rsp_prod_pvt == xnb_unit_pvt.txs->req_prod);
738 rsp = RING_GET_RESPONSE(&xnb_unit_pvt.txb, xnb_unit_pvt.txf.rsp_cons);
739 XNB_ASSERT(rsp->id == req->id);
740 XNB_ASSERT(rsp->status == NETIF_RSP_OKAY);
742 rsp = RING_GET_RESPONSE(&xnb_unit_pvt.txb,
743 xnb_unit_pvt.txf.rsp_cons + 1);
744 XNB_ASSERT(rsp->status == NETIF_RSP_NULL);
748 * xnb_pkg2rsp responding to 3 data requests and 1 extra info
751 xnb_txpkt2rsp_long(char *buffer, size_t buflen)
753 uint16_t num_consumed;
755 struct netif_tx_request *req;
756 netif_extra_info_t *ext;
757 struct netif_tx_response *rsp;
759 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
760 xnb_unit_pvt.txf.req_prod_pvt);
762 req->flags = NETTXF_extra_info | NETTXF_more_data;
764 xnb_unit_pvt.txf.req_prod_pvt++;
766 ext = (netif_extra_info_t*) RING_GET_REQUEST(&xnb_unit_pvt.txf,
767 xnb_unit_pvt.txf.req_prod_pvt);
768 ext->type = XEN_NETIF_EXTRA_TYPE_GSO;
770 xnb_unit_pvt.txf.req_prod_pvt++;
772 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
773 xnb_unit_pvt.txf.req_prod_pvt);
775 req->flags = NETTXF_more_data;
777 xnb_unit_pvt.txf.req_prod_pvt++;
779 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
780 xnb_unit_pvt.txf.req_prod_pvt);
784 xnb_unit_pvt.txf.req_prod_pvt++;
786 RING_PUSH_REQUESTS(&xnb_unit_pvt.txf);
788 num_consumed = xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb,
789 xnb_unit_pvt.txb.req_cons);
790 xnb_unit_pvt.txb.req_cons += num_consumed;
792 xnb_txpkt2rsp(&pkt, &xnb_unit_pvt.txb, 0);
795 xnb_unit_pvt.txb.rsp_prod_pvt == xnb_unit_pvt.txs->req_prod);
797 rsp = RING_GET_RESPONSE(&xnb_unit_pvt.txb, xnb_unit_pvt.txf.rsp_cons);
798 XNB_ASSERT(rsp->id ==
799 RING_GET_REQUEST(&xnb_unit_pvt.txf, 0)->id);
800 XNB_ASSERT(rsp->status == NETIF_RSP_OKAY);
802 rsp = RING_GET_RESPONSE(&xnb_unit_pvt.txb,
803 xnb_unit_pvt.txf.rsp_cons + 1);
804 XNB_ASSERT(rsp->status == NETIF_RSP_NULL);
806 rsp = RING_GET_RESPONSE(&xnb_unit_pvt.txb,
807 xnb_unit_pvt.txf.rsp_cons + 2);
808 XNB_ASSERT(rsp->id ==
809 RING_GET_REQUEST(&xnb_unit_pvt.txf, 2)->id);
810 XNB_ASSERT(rsp->status == NETIF_RSP_OKAY);
812 rsp = RING_GET_RESPONSE(&xnb_unit_pvt.txb,
813 xnb_unit_pvt.txf.rsp_cons + 3);
814 XNB_ASSERT(rsp->id ==
815 RING_GET_REQUEST(&xnb_unit_pvt.txf, 3)->id);
816 XNB_ASSERT(rsp->status == NETIF_RSP_OKAY);
820 * xnb_txpkt2rsp responding to an invalid packet.
821 * Note: this test will result in an error message being printed to the console
823 * xnb(xnb_ring2pkt:1306): Unknown extra info type 255. Discarding packet
826 xnb_txpkt2rsp_invalid(char *buffer, size_t buflen)
828 uint16_t num_consumed;
830 struct netif_tx_request *req;
831 netif_extra_info_t *ext;
832 struct netif_tx_response *rsp;
834 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
835 xnb_unit_pvt.txf.req_prod_pvt);
837 req->flags = NETTXF_extra_info;
839 xnb_unit_pvt.txf.req_prod_pvt++;
841 ext = (netif_extra_info_t*) RING_GET_REQUEST(&xnb_unit_pvt.txf,
842 xnb_unit_pvt.txf.req_prod_pvt);
843 ext->type = 0xFF; /* Invalid extra type */
845 xnb_unit_pvt.txf.req_prod_pvt++;
847 RING_PUSH_REQUESTS(&xnb_unit_pvt.txf);
849 num_consumed = xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb,
850 xnb_unit_pvt.txb.req_cons);
851 xnb_unit_pvt.txb.req_cons += num_consumed;
852 XNB_ASSERT(! xnb_pkt_is_valid(&pkt));
854 xnb_txpkt2rsp(&pkt, &xnb_unit_pvt.txb, 0);
857 xnb_unit_pvt.txb.rsp_prod_pvt == xnb_unit_pvt.txs->req_prod);
859 rsp = RING_GET_RESPONSE(&xnb_unit_pvt.txb, xnb_unit_pvt.txf.rsp_cons);
860 XNB_ASSERT(rsp->id == req->id);
861 XNB_ASSERT(rsp->status == NETIF_RSP_ERROR);
863 rsp = RING_GET_RESPONSE(&xnb_unit_pvt.txb,
864 xnb_unit_pvt.txf.rsp_cons + 1);
865 XNB_ASSERT(rsp->status == NETIF_RSP_NULL);
869 * xnb_txpkt2rsp responding to one request which caused an error
872 xnb_txpkt2rsp_error(char *buffer, size_t buflen)
874 uint16_t num_consumed;
876 struct netif_tx_request *req;
877 struct netif_tx_response *rsp;
879 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
880 xnb_unit_pvt.txf.req_prod_pvt);
883 xnb_unit_pvt.txf.req_prod_pvt++;
885 RING_PUSH_REQUESTS(&xnb_unit_pvt.txf);
887 num_consumed = xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb,
888 xnb_unit_pvt.txb.req_cons);
889 xnb_unit_pvt.txb.req_cons += num_consumed;
891 xnb_txpkt2rsp(&pkt, &xnb_unit_pvt.txb, 1);
892 rsp = RING_GET_RESPONSE(&xnb_unit_pvt.txb, xnb_unit_pvt.txf.rsp_cons);
895 xnb_unit_pvt.txb.rsp_prod_pvt == xnb_unit_pvt.txs->req_prod);
896 XNB_ASSERT(rsp->id == req->id);
897 XNB_ASSERT(rsp->status == NETIF_RSP_ERROR);
901 * xnb_txpkt2rsp's responses wrap around the end of the ring
904 xnb_txpkt2rsp_wraps(char *buffer, size_t buflen)
908 struct netif_tx_request *req;
909 struct netif_tx_response *rsp;
913 * Manually tweak the ring indices to create a ring with no responses
914 * and the next request slot at position 2 from the end
916 rsize = RING_SIZE(&xnb_unit_pvt.txf);
917 xnb_unit_pvt.txf.req_prod_pvt = rsize - 2;
918 xnb_unit_pvt.txf.rsp_cons = rsize - 2;
919 xnb_unit_pvt.txs->req_prod = rsize - 2;
920 xnb_unit_pvt.txs->req_event = rsize - 1;
921 xnb_unit_pvt.txs->rsp_prod = rsize - 2;
922 xnb_unit_pvt.txs->rsp_event = rsize - 1;
923 xnb_unit_pvt.txb.rsp_prod_pvt = rsize - 2;
924 xnb_unit_pvt.txb.req_cons = rsize - 2;
926 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
927 xnb_unit_pvt.txf.req_prod_pvt);
928 req->flags = NETTXF_more_data;
931 xnb_unit_pvt.txf.req_prod_pvt++;
933 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
934 xnb_unit_pvt.txf.req_prod_pvt);
935 req->flags = NETTXF_more_data;
938 xnb_unit_pvt.txf.req_prod_pvt++;
940 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
941 xnb_unit_pvt.txf.req_prod_pvt);
945 xnb_unit_pvt.txf.req_prod_pvt++;
947 RING_PUSH_REQUESTS(&xnb_unit_pvt.txf);
949 num_consumed = xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb,
950 xnb_unit_pvt.txb.req_cons);
952 xnb_txpkt2rsp(&pkt, &xnb_unit_pvt.txb, 0);
955 xnb_unit_pvt.txb.rsp_prod_pvt == xnb_unit_pvt.txs->req_prod);
956 rsp = RING_GET_RESPONSE(&xnb_unit_pvt.txb,
957 xnb_unit_pvt.txf.rsp_cons + 2);
958 XNB_ASSERT(rsp->id == req->id);
959 XNB_ASSERT(rsp->status == NETIF_RSP_OKAY);
964 * Helper function used to setup pkt2mbufc tests
965 * \param size size in bytes of the single request to push to the ring
966 * \param flags optional flags to put in the netif request
967 * \param[out] pkt the returned packet object
968 * \return number of requests consumed from the ring
971 xnb_get1pkt(struct xnb_pkt *pkt, size_t size, uint16_t flags)
973 struct netif_tx_request *req;
975 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
976 xnb_unit_pvt.txf.req_prod_pvt);
979 xnb_unit_pvt.txf.req_prod_pvt++;
981 RING_PUSH_REQUESTS(&xnb_unit_pvt.txf);
983 return xnb_ring2pkt(pkt, &xnb_unit_pvt.txb,
984 xnb_unit_pvt.txb.req_cons);
988 * xnb_pkt2mbufc on an empty packet
991 xnb_pkt2mbufc_empty(char *buffer, size_t buflen)
998 /* must call xnb_ring2pkt just to intialize pkt */
999 num_consumed = xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb,
1000 xnb_unit_pvt.txb.req_cons);
1002 pMbuf = xnb_pkt2mbufc(&pkt, xnb_unit_pvt.ifp);
1003 safe_m_freem(&pMbuf);
1007 * xnb_pkt2mbufc on short packet that can fit in an mbuf internal buffer
1010 xnb_pkt2mbufc_short(char *buffer, size_t buflen)
1012 const size_t size = MINCLSIZE - 1;
1016 xnb_get1pkt(&pkt, size, 0);
1018 pMbuf = xnb_pkt2mbufc(&pkt, xnb_unit_pvt.ifp);
1019 XNB_ASSERT(M_TRAILINGSPACE(pMbuf) >= size);
1020 safe_m_freem(&pMbuf);
1024 * xnb_pkt2mbufc on short packet whose checksum was validated by the netfron
1027 xnb_pkt2mbufc_csum(char *buffer, size_t buflen)
1029 const size_t size = MINCLSIZE - 1;
1033 xnb_get1pkt(&pkt, size, NETTXF_data_validated);
1035 pMbuf = xnb_pkt2mbufc(&pkt, xnb_unit_pvt.ifp);
1036 XNB_ASSERT(M_TRAILINGSPACE(pMbuf) >= size);
1037 XNB_ASSERT(pMbuf->m_pkthdr.csum_flags & CSUM_IP_CHECKED);
1038 XNB_ASSERT(pMbuf->m_pkthdr.csum_flags & CSUM_IP_VALID);
1039 XNB_ASSERT(pMbuf->m_pkthdr.csum_flags & CSUM_DATA_VALID);
1040 XNB_ASSERT(pMbuf->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR);
1041 safe_m_freem(&pMbuf);
1045 * xnb_pkt2mbufc on packet that can fit in one cluster
1048 xnb_pkt2mbufc_1cluster(char *buffer, size_t buflen)
1050 const size_t size = MINCLSIZE;
1054 xnb_get1pkt(&pkt, size, 0);
1056 pMbuf = xnb_pkt2mbufc(&pkt, xnb_unit_pvt.ifp);
1057 XNB_ASSERT(M_TRAILINGSPACE(pMbuf) >= size);
1058 safe_m_freem(&pMbuf);
1062 * xnb_pkt2mbufc on packet that cannot fit in one regular cluster
1065 xnb_pkt2mbufc_largecluster(char *buffer, size_t buflen)
1067 const size_t size = MCLBYTES + 1;
1071 xnb_get1pkt(&pkt, size, 0);
1073 pMbuf = xnb_pkt2mbufc(&pkt, xnb_unit_pvt.ifp);
1074 XNB_ASSERT(M_TRAILINGSPACE(pMbuf) >= size);
1075 safe_m_freem(&pMbuf);
1079 * xnb_pkt2mbufc on packet that cannot fit in one clusters
1082 xnb_pkt2mbufc_2cluster(char *buffer, size_t buflen)
1084 const size_t size = 2 * MCLBYTES + 1;
1090 xnb_get1pkt(&pkt, size, 0);
1092 pMbuf = xnb_pkt2mbufc(&pkt, xnb_unit_pvt.ifp);
1094 for (m = pMbuf; m != NULL; m = m->m_next) {
1095 space += M_TRAILINGSPACE(m);
1097 XNB_ASSERT(space >= size);
1098 safe_m_freem(&pMbuf);
1102 * xnb_txpkt2gnttab on an empty packet. Should return empty gnttab
1105 xnb_txpkt2gnttab_empty(char *buffer, size_t buflen)
1112 /* must call xnb_ring2pkt just to intialize pkt */
1113 xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb, xnb_unit_pvt.txb.req_cons);
1115 pMbuf = xnb_pkt2mbufc(&pkt, xnb_unit_pvt.ifp);
1116 n_entries = xnb_txpkt2gnttab(&pkt, pMbuf, xnb_unit_pvt.gnttab,
1117 &xnb_unit_pvt.txb, DOMID_FIRST_RESERVED);
1118 XNB_ASSERT(n_entries == 0);
1119 safe_m_freem(&pMbuf);
1123 * xnb_txpkt2gnttab on a short packet, that can fit in one mbuf internal buffer
1124 * and has one request
1127 xnb_txpkt2gnttab_short(char *buffer, size_t buflen)
1129 const size_t size = MINCLSIZE - 1;
1134 struct netif_tx_request *req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
1135 xnb_unit_pvt.txf.req_prod_pvt);
1140 xnb_unit_pvt.txf.req_prod_pvt++;
1142 RING_PUSH_REQUESTS(&xnb_unit_pvt.txf);
1144 xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb, xnb_unit_pvt.txb.req_cons);
1146 pMbuf = xnb_pkt2mbufc(&pkt, xnb_unit_pvt.ifp);
1147 n_entries = xnb_txpkt2gnttab(&pkt, pMbuf, xnb_unit_pvt.gnttab,
1148 &xnb_unit_pvt.txb, DOMID_FIRST_RESERVED);
1149 XNB_ASSERT(n_entries == 1);
1150 XNB_ASSERT(xnb_unit_pvt.gnttab[0].len == size);
1151 /* flags should indicate gref's for source */
1152 XNB_ASSERT(xnb_unit_pvt.gnttab[0].flags & GNTCOPY_source_gref);
1153 XNB_ASSERT(xnb_unit_pvt.gnttab[0].source.offset == req->offset);
1154 XNB_ASSERT(xnb_unit_pvt.gnttab[0].source.domid == DOMID_SELF);
1155 XNB_ASSERT(xnb_unit_pvt.gnttab[0].dest.offset == virt_to_offset(
1156 mtod(pMbuf, vm_offset_t)));
1157 XNB_ASSERT(xnb_unit_pvt.gnttab[0].dest.u.gmfn ==
1158 virt_to_mfn(mtod(pMbuf, vm_offset_t)));
1159 XNB_ASSERT(xnb_unit_pvt.gnttab[0].dest.domid == DOMID_FIRST_RESERVED);
1160 safe_m_freem(&pMbuf);
1164 * xnb_txpkt2gnttab on a packet with two requests, that can fit into a single
1168 xnb_txpkt2gnttab_2req(char *buffer, size_t buflen)
1174 struct netif_tx_request *req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
1175 xnb_unit_pvt.txf.req_prod_pvt);
1176 req->flags = NETTXF_more_data;
1180 xnb_unit_pvt.txf.req_prod_pvt++;
1182 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
1183 xnb_unit_pvt.txf.req_prod_pvt);
1188 xnb_unit_pvt.txf.req_prod_pvt++;
1190 RING_PUSH_REQUESTS(&xnb_unit_pvt.txf);
1192 xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb, xnb_unit_pvt.txb.req_cons);
1194 pMbuf = xnb_pkt2mbufc(&pkt, xnb_unit_pvt.ifp);
1195 n_entries = xnb_txpkt2gnttab(&pkt, pMbuf, xnb_unit_pvt.gnttab,
1196 &xnb_unit_pvt.txb, DOMID_FIRST_RESERVED);
1198 XNB_ASSERT(n_entries == 2);
1199 XNB_ASSERT(xnb_unit_pvt.gnttab[0].len == 1400);
1200 XNB_ASSERT(xnb_unit_pvt.gnttab[0].dest.offset == virt_to_offset(
1201 mtod(pMbuf, vm_offset_t)));
1203 XNB_ASSERT(xnb_unit_pvt.gnttab[1].len == 500);
1204 XNB_ASSERT(xnb_unit_pvt.gnttab[1].dest.offset == virt_to_offset(
1205 mtod(pMbuf, vm_offset_t) + 1400));
1206 safe_m_freem(&pMbuf);
1210 * xnb_txpkt2gnttab on a single request that spans two mbuf clusters
1213 xnb_txpkt2gnttab_2cluster(char *buffer, size_t buflen)
1218 const uint16_t data_this_transaction = (MCLBYTES*2) + 1;
1220 struct netif_tx_request *req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
1221 xnb_unit_pvt.txf.req_prod_pvt);
1223 req->size = data_this_transaction;
1226 xnb_unit_pvt.txf.req_prod_pvt++;
1228 RING_PUSH_REQUESTS(&xnb_unit_pvt.txf);
1229 xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb, xnb_unit_pvt.txb.req_cons);
1231 pMbuf = xnb_pkt2mbufc(&pkt, xnb_unit_pvt.ifp);
1232 XNB_ASSERT(pMbuf != NULL);
1236 n_entries = xnb_txpkt2gnttab(&pkt, pMbuf, xnb_unit_pvt.gnttab,
1237 &xnb_unit_pvt.txb, DOMID_FIRST_RESERVED);
1239 if (M_TRAILINGSPACE(pMbuf) == MCLBYTES) {
1240 /* there should be three mbufs and three gnttab entries */
1241 XNB_ASSERT(n_entries == 3);
1242 XNB_ASSERT(xnb_unit_pvt.gnttab[0].len == MCLBYTES);
1244 xnb_unit_pvt.gnttab[0].dest.offset == virt_to_offset(
1245 mtod(pMbuf, vm_offset_t)));
1246 XNB_ASSERT(xnb_unit_pvt.gnttab[0].source.offset == 0);
1248 XNB_ASSERT(xnb_unit_pvt.gnttab[1].len == MCLBYTES);
1250 xnb_unit_pvt.gnttab[1].dest.offset == virt_to_offset(
1251 mtod(pMbuf->m_next, vm_offset_t)));
1252 XNB_ASSERT(xnb_unit_pvt.gnttab[1].source.offset == MCLBYTES);
1254 XNB_ASSERT(xnb_unit_pvt.gnttab[2].len == 1);
1256 xnb_unit_pvt.gnttab[2].dest.offset == virt_to_offset(
1257 mtod(pMbuf->m_next, vm_offset_t)));
1258 XNB_ASSERT(xnb_unit_pvt.gnttab[2].source.offset == 2 *
1260 } else if (M_TRAILINGSPACE(pMbuf) == 2 * MCLBYTES) {
1261 /* there should be two mbufs and two gnttab entries */
1262 XNB_ASSERT(n_entries == 2);
1263 XNB_ASSERT(xnb_unit_pvt.gnttab[0].len == 2 * MCLBYTES);
1265 xnb_unit_pvt.gnttab[0].dest.offset == virt_to_offset(
1266 mtod(pMbuf, vm_offset_t)));
1267 XNB_ASSERT(xnb_unit_pvt.gnttab[0].source.offset == 0);
1269 XNB_ASSERT(xnb_unit_pvt.gnttab[1].len == 1);
1271 xnb_unit_pvt.gnttab[1].dest.offset == virt_to_offset(
1272 mtod(pMbuf->m_next, vm_offset_t)));
1274 xnb_unit_pvt.gnttab[1].source.offset == 2 * MCLBYTES);
1277 /* should never get here */
1285 * xnb_update_mbufc on a short packet that only has one gnttab entry
1288 xnb_update_mbufc_short(char *buffer, size_t buflen)
1290 const size_t size = MINCLSIZE - 1;
1295 struct netif_tx_request *req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
1296 xnb_unit_pvt.txf.req_prod_pvt);
1301 xnb_unit_pvt.txf.req_prod_pvt++;
1303 RING_PUSH_REQUESTS(&xnb_unit_pvt.txf);
1305 xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb, xnb_unit_pvt.txb.req_cons);
1307 pMbuf = xnb_pkt2mbufc(&pkt, xnb_unit_pvt.ifp);
1308 n_entries = xnb_txpkt2gnttab(&pkt, pMbuf, xnb_unit_pvt.gnttab,
1309 &xnb_unit_pvt.txb, DOMID_FIRST_RESERVED);
1311 /* Update grant table's status fields as the hypervisor call would */
1312 xnb_unit_pvt.gnttab[0].status = GNTST_okay;
1314 xnb_update_mbufc(pMbuf, xnb_unit_pvt.gnttab, n_entries);
1315 XNB_ASSERT(pMbuf->m_len == size);
1316 XNB_ASSERT(pMbuf->m_pkthdr.len == size);
1317 safe_m_freem(&pMbuf);
1321 * xnb_update_mbufc on a packet with two requests, that can fit into a single
1325 xnb_update_mbufc_2req(char *buffer, size_t buflen)
1331 struct netif_tx_request *req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
1332 xnb_unit_pvt.txf.req_prod_pvt);
1333 req->flags = NETTXF_more_data;
1337 xnb_unit_pvt.txf.req_prod_pvt++;
1339 req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
1340 xnb_unit_pvt.txf.req_prod_pvt);
1345 xnb_unit_pvt.txf.req_prod_pvt++;
1347 RING_PUSH_REQUESTS(&xnb_unit_pvt.txf);
1349 xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb, xnb_unit_pvt.txb.req_cons);
1351 pMbuf = xnb_pkt2mbufc(&pkt, xnb_unit_pvt.ifp);
1352 n_entries = xnb_txpkt2gnttab(&pkt, pMbuf, xnb_unit_pvt.gnttab,
1353 &xnb_unit_pvt.txb, DOMID_FIRST_RESERVED);
1355 /* Update grant table's status fields as the hypervisor call would */
1356 xnb_unit_pvt.gnttab[0].status = GNTST_okay;
1357 xnb_unit_pvt.gnttab[1].status = GNTST_okay;
1359 xnb_update_mbufc(pMbuf, xnb_unit_pvt.gnttab, n_entries);
1360 XNB_ASSERT(n_entries == 2);
1361 XNB_ASSERT(pMbuf->m_pkthdr.len == 1900);
1362 XNB_ASSERT(pMbuf->m_len == 1900);
1364 safe_m_freem(&pMbuf);
1368 * xnb_update_mbufc on a single request that spans two mbuf clusters
1371 xnb_update_mbufc_2cluster(char *buffer, size_t buflen)
1377 const uint16_t data_this_transaction = (MCLBYTES*2) + 1;
1379 struct netif_tx_request *req = RING_GET_REQUEST(&xnb_unit_pvt.txf,
1380 xnb_unit_pvt.txf.req_prod_pvt);
1382 req->size = data_this_transaction;
1385 xnb_unit_pvt.txf.req_prod_pvt++;
1387 RING_PUSH_REQUESTS(&xnb_unit_pvt.txf);
1388 xnb_ring2pkt(&pkt, &xnb_unit_pvt.txb, xnb_unit_pvt.txb.req_cons);
1390 pMbuf = xnb_pkt2mbufc(&pkt, xnb_unit_pvt.ifp);
1391 n_entries = xnb_txpkt2gnttab(&pkt, pMbuf, xnb_unit_pvt.gnttab,
1392 &xnb_unit_pvt.txb, DOMID_FIRST_RESERVED);
1394 /* Update grant table's status fields */
1395 for (i = 0; i < n_entries; i++) {
1396 xnb_unit_pvt.gnttab[0].status = GNTST_okay;
1398 xnb_update_mbufc(pMbuf, xnb_unit_pvt.gnttab, n_entries);
1400 if (n_entries == 3) {
1401 /* there should be three mbufs and three gnttab entries */
1402 XNB_ASSERT(pMbuf->m_pkthdr.len == data_this_transaction);
1403 XNB_ASSERT(pMbuf->m_len == MCLBYTES);
1404 XNB_ASSERT(pMbuf->m_next->m_len == MCLBYTES);
1405 XNB_ASSERT(pMbuf->m_next->m_next->m_len == 1);
1406 } else if (n_entries == 2) {
1407 /* there should be two mbufs and two gnttab entries */
1408 XNB_ASSERT(n_entries == 2);
1409 XNB_ASSERT(pMbuf->m_pkthdr.len == data_this_transaction);
1410 XNB_ASSERT(pMbuf->m_len == 2 * MCLBYTES);
1411 XNB_ASSERT(pMbuf->m_next->m_len == 1);
1413 /* should never get here */
1416 safe_m_freem(&pMbuf);
1419 /** xnb_mbufc2pkt on an empty mbufc */
1421 xnb_mbufc2pkt_empty(char *buffer, size_t buflen) {
1423 int free_slots = 64;
1426 mbuf = m_get(M_WAITOK, MT_DATA);
1428 * note: it is illegal to set M_PKTHDR on a mbuf with no data. Doing so
1429 * will cause m_freem to segfault
1431 XNB_ASSERT(mbuf->m_len == 0);
1433 xnb_mbufc2pkt(mbuf, &pkt, 0, free_slots);
1434 XNB_ASSERT(! xnb_pkt_is_valid(&pkt));
1436 safe_m_freem(&mbuf);
1439 /** xnb_mbufc2pkt on a short mbufc */
1441 xnb_mbufc2pkt_short(char *buffer, size_t buflen) {
1444 int free_slots = 64;
1448 mbuf = m_getm(NULL, size, M_WAITOK, MT_DATA);
1449 mbuf->m_flags |= M_PKTHDR;
1450 mbuf->m_pkthdr.len = size;
1453 xnb_mbufc2pkt(mbuf, &pkt, start, free_slots);
1454 XNB_ASSERT(xnb_pkt_is_valid(&pkt));
1455 XNB_ASSERT(pkt.size == size);
1456 XNB_ASSERT(pkt.car_size == size);
1457 XNB_ASSERT(! (pkt.flags &
1458 (NETRXF_more_data | NETRXF_extra_info)));
1459 XNB_ASSERT(pkt.list_len == 1);
1460 XNB_ASSERT(pkt.car == start);
1462 safe_m_freem(&mbuf);
1465 /** xnb_mbufc2pkt on a single mbuf with an mbuf cluster */
1467 xnb_mbufc2pkt_1cluster(char *buffer, size_t buflen) {
1469 size_t size = MCLBYTES;
1470 int free_slots = 32;
1471 RING_IDX start = 12;
1474 mbuf = m_getm(NULL, size, M_WAITOK, MT_DATA);
1475 mbuf->m_flags |= M_PKTHDR;
1476 mbuf->m_pkthdr.len = size;
1479 xnb_mbufc2pkt(mbuf, &pkt, start, free_slots);
1480 XNB_ASSERT(xnb_pkt_is_valid(&pkt));
1481 XNB_ASSERT(pkt.size == size);
1482 XNB_ASSERT(pkt.car_size == size);
1483 XNB_ASSERT(! (pkt.flags &
1484 (NETRXF_more_data | NETRXF_extra_info)));
1485 XNB_ASSERT(pkt.list_len == 1);
1486 XNB_ASSERT(pkt.car == start);
1488 safe_m_freem(&mbuf);
1491 /** xnb_mbufc2pkt on a two-mbuf chain with short data regions */
1493 xnb_mbufc2pkt_2short(char *buffer, size_t buflen) {
1495 size_t size1 = MHLEN - 5;
1496 size_t size2 = MHLEN - 15;
1497 int free_slots = 32;
1498 RING_IDX start = 14;
1499 struct mbuf *mbufc, *mbufc2;
1501 mbufc = m_getm(NULL, size1, M_WAITOK, MT_DATA);
1502 XNB_ASSERT(mbufc != NULL);
1505 mbufc->m_flags |= M_PKTHDR;
1507 mbufc2 = m_getm(mbufc, size2, M_WAITOK, MT_DATA);
1508 XNB_ASSERT(mbufc2 != NULL);
1509 if (mbufc2 == NULL) {
1510 safe_m_freem(&mbufc);
1513 mbufc2->m_pkthdr.len = size1 + size2;
1514 mbufc2->m_len = size1;
1516 xnb_mbufc2pkt(mbufc2, &pkt, start, free_slots);
1517 XNB_ASSERT(xnb_pkt_is_valid(&pkt));
1518 XNB_ASSERT(pkt.size == size1 + size2);
1519 XNB_ASSERT(pkt.car == start);
1521 * The second m_getm may allocate a new mbuf and append
1522 * it to the chain, or it may simply extend the first mbuf.
1524 if (mbufc2->m_next != NULL) {
1525 XNB_ASSERT(pkt.car_size == size1);
1526 XNB_ASSERT(pkt.list_len == 1);
1527 XNB_ASSERT(pkt.cdr == start + 1);
1530 safe_m_freem(&mbufc2);
1533 /** xnb_mbufc2pkt on a mbuf chain with >1 mbuf cluster */
1535 xnb_mbufc2pkt_long(char *buffer, size_t buflen) {
1537 size_t size = 14 * MCLBYTES / 3;
1538 size_t size_remaining;
1539 int free_slots = 15;
1541 struct mbuf *mbufc, *m;
1543 mbufc = m_getm(NULL, size, M_WAITOK, MT_DATA);
1544 XNB_ASSERT(mbufc != NULL);
1547 mbufc->m_flags |= M_PKTHDR;
1549 mbufc->m_pkthdr.len = size;
1550 size_remaining = size;
1551 for (m = mbufc; m != NULL; m = m->m_next) {
1552 m->m_len = MAX(M_TRAILINGSPACE(m), size_remaining);
1553 size_remaining -= m->m_len;
1556 xnb_mbufc2pkt(mbufc, &pkt, start, free_slots);
1557 XNB_ASSERT(xnb_pkt_is_valid(&pkt));
1558 XNB_ASSERT(pkt.size == size);
1559 XNB_ASSERT(pkt.car == start);
1560 XNB_ASSERT(pkt.car_size = mbufc->m_len);
1562 * There should be >1 response in the packet, and there is no
1565 XNB_ASSERT(! (pkt.flags & NETRXF_extra_info));
1566 XNB_ASSERT(pkt.cdr == pkt.car + 1);
1568 safe_m_freem(&mbufc);
1571 /** xnb_mbufc2pkt on a mbuf chain with >1 mbuf cluster and extra info */
1573 xnb_mbufc2pkt_extra(char *buffer, size_t buflen) {
1575 size_t size = 14 * MCLBYTES / 3;
1576 size_t size_remaining;
1577 int free_slots = 15;
1579 struct mbuf *mbufc, *m;
1581 mbufc = m_getm(NULL, size, M_WAITOK, MT_DATA);
1582 XNB_ASSERT(mbufc != NULL);
1586 mbufc->m_flags |= M_PKTHDR;
1587 mbufc->m_pkthdr.len = size;
1588 mbufc->m_pkthdr.csum_flags |= CSUM_TSO;
1589 mbufc->m_pkthdr.tso_segsz = TCP_MSS - 40;
1590 size_remaining = size;
1591 for (m = mbufc; m != NULL; m = m->m_next) {
1592 m->m_len = MAX(M_TRAILINGSPACE(m), size_remaining);
1593 size_remaining -= m->m_len;
1596 xnb_mbufc2pkt(mbufc, &pkt, start, free_slots);
1597 XNB_ASSERT(xnb_pkt_is_valid(&pkt));
1598 XNB_ASSERT(pkt.size == size);
1599 XNB_ASSERT(pkt.car == start);
1600 XNB_ASSERT(pkt.car_size = mbufc->m_len);
1601 /* There should be >1 response in the packet, there is extra info */
1602 XNB_ASSERT(pkt.flags & NETRXF_extra_info);
1603 XNB_ASSERT(pkt.flags & NETRXF_data_validated);
1604 XNB_ASSERT(pkt.cdr == pkt.car + 2);
1605 XNB_ASSERT(pkt.extra.u.gso.size = mbufc->m_pkthdr.tso_segsz);
1606 XNB_ASSERT(pkt.extra.type == XEN_NETIF_EXTRA_TYPE_GSO);
1607 XNB_ASSERT(! (pkt.extra.flags & XEN_NETIF_EXTRA_FLAG_MORE));
1609 safe_m_freem(&mbufc);
1612 /** xnb_mbufc2pkt with insufficient space in the ring */
1614 xnb_mbufc2pkt_nospace(char *buffer, size_t buflen) {
1616 size_t size = 14 * MCLBYTES / 3;
1617 size_t size_remaining;
1620 struct mbuf *mbufc, *m;
1623 mbufc = m_getm(NULL, size, M_WAITOK, MT_DATA);
1624 XNB_ASSERT(mbufc != NULL);
1627 mbufc->m_flags |= M_PKTHDR;
1629 mbufc->m_pkthdr.len = size;
1630 size_remaining = size;
1631 for (m = mbufc; m != NULL; m = m->m_next) {
1632 m->m_len = MAX(M_TRAILINGSPACE(m), size_remaining);
1633 size_remaining -= m->m_len;
1636 error = xnb_mbufc2pkt(mbufc, &pkt, start, free_slots);
1637 XNB_ASSERT(error == EAGAIN);
1638 XNB_ASSERT(! xnb_pkt_is_valid(&pkt));
1640 safe_m_freem(&mbufc);
1644 * xnb_rxpkt2gnttab on an empty packet. Should return empty gnttab
1647 xnb_rxpkt2gnttab_empty(char *buffer, size_t buflen)
1651 int free_slots = 60;
1654 mbuf = m_get(M_WAITOK, MT_DATA);
1656 xnb_mbufc2pkt(mbuf, &pkt, 0, free_slots);
1657 nr_entries = xnb_rxpkt2gnttab(&pkt, mbuf, xnb_unit_pvt.gnttab,
1658 &xnb_unit_pvt.rxb, DOMID_FIRST_RESERVED);
1660 XNB_ASSERT(nr_entries == 0);
1662 safe_m_freem(&mbuf);
1665 /** xnb_rxpkt2gnttab on a short packet without extra data */
1667 xnb_rxpkt2gnttab_short(char *buffer, size_t buflen) {
1671 int free_slots = 60;
1673 struct netif_rx_request *req;
1676 mbuf = m_getm(NULL, size, M_WAITOK, MT_DATA);
1677 mbuf->m_flags |= M_PKTHDR;
1678 mbuf->m_pkthdr.len = size;
1681 xnb_mbufc2pkt(mbuf, &pkt, start, free_slots);
1682 req = RING_GET_REQUEST(&xnb_unit_pvt.rxf,
1683 xnb_unit_pvt.txf.req_prod_pvt);
1686 nr_entries = xnb_rxpkt2gnttab(&pkt, mbuf, xnb_unit_pvt.gnttab,
1687 &xnb_unit_pvt.rxb, DOMID_FIRST_RESERVED);
1689 XNB_ASSERT(nr_entries == 1);
1690 XNB_ASSERT(xnb_unit_pvt.gnttab[0].len == size);
1691 /* flags should indicate gref's for dest */
1692 XNB_ASSERT(xnb_unit_pvt.gnttab[0].flags & GNTCOPY_dest_gref);
1693 XNB_ASSERT(xnb_unit_pvt.gnttab[0].dest.offset == 0);
1694 XNB_ASSERT(xnb_unit_pvt.gnttab[0].source.domid == DOMID_SELF);
1695 XNB_ASSERT(xnb_unit_pvt.gnttab[0].source.offset == virt_to_offset(
1696 mtod(mbuf, vm_offset_t)));
1697 XNB_ASSERT(xnb_unit_pvt.gnttab[0].source.u.gmfn ==
1698 virt_to_mfn(mtod(mbuf, vm_offset_t)));
1699 XNB_ASSERT(xnb_unit_pvt.gnttab[0].dest.domid == DOMID_FIRST_RESERVED);
1701 safe_m_freem(&mbuf);
1705 * xnb_rxpkt2gnttab on a packet with two different mbufs in a single chai
1708 xnb_rxpkt2gnttab_2req(char *buffer, size_t buflen)
1713 size_t total_granted_size = 0;
1714 size_t size = MJUMPAGESIZE + 1;
1715 int free_slots = 60;
1716 RING_IDX start = 11;
1717 struct netif_rx_request *req;
1718 struct mbuf *mbuf, *m;
1720 mbuf = m_getm(NULL, size, M_WAITOK, MT_DATA);
1721 mbuf->m_flags |= M_PKTHDR;
1722 mbuf->m_pkthdr.len = size;
1725 xnb_mbufc2pkt(mbuf, &pkt, start, free_slots);
1727 for (i = 0, m=mbuf; m != NULL; i++, m = m->m_next) {
1728 req = RING_GET_REQUEST(&xnb_unit_pvt.rxf,
1729 xnb_unit_pvt.txf.req_prod_pvt);
1735 nr_entries = xnb_rxpkt2gnttab(&pkt, mbuf, xnb_unit_pvt.gnttab,
1736 &xnb_unit_pvt.rxb, DOMID_FIRST_RESERVED);
1738 XNB_ASSERT(nr_entries >= num_mbufs);
1739 for (i = 0; i < nr_entries; i++) {
1740 int end_offset = xnb_unit_pvt.gnttab[i].len +
1741 xnb_unit_pvt.gnttab[i].dest.offset;
1742 XNB_ASSERT(end_offset <= PAGE_SIZE);
1743 total_granted_size += xnb_unit_pvt.gnttab[i].len;
1745 XNB_ASSERT(total_granted_size == size);
1749 * xnb_rxpkt2rsp on an empty packet. Shouldn't make any response
1752 xnb_rxpkt2rsp_empty(char *buffer, size_t buflen)
1757 int free_slots = 60;
1758 netif_rx_back_ring_t rxb_backup = xnb_unit_pvt.rxb;
1759 netif_rx_sring_t rxs_backup = *xnb_unit_pvt.rxs;
1762 mbuf = m_get(M_WAITOK, MT_DATA);
1764 xnb_mbufc2pkt(mbuf, &pkt, 0, free_slots);
1765 nr_entries = xnb_rxpkt2gnttab(&pkt, mbuf, xnb_unit_pvt.gnttab,
1766 &xnb_unit_pvt.rxb, DOMID_FIRST_RESERVED);
1768 nr_reqs = xnb_rxpkt2rsp(&pkt, xnb_unit_pvt.gnttab, nr_entries,
1770 XNB_ASSERT(nr_reqs == 0);
1772 memcmp(&rxb_backup, &xnb_unit_pvt.rxb, sizeof(rxb_backup)) == 0);
1774 memcmp(&rxs_backup, xnb_unit_pvt.rxs, sizeof(rxs_backup)) == 0);
1776 safe_m_freem(&mbuf);
1780 * xnb_rxpkt2rsp on a short packet with no extras
1783 xnb_rxpkt2rsp_short(char *buffer, size_t buflen)
1786 int nr_entries, nr_reqs;
1788 int free_slots = 60;
1790 struct netif_rx_request *req;
1791 struct netif_rx_response *rsp;
1794 mbuf = m_getm(NULL, size, M_WAITOK, MT_DATA);
1795 mbuf->m_flags |= M_PKTHDR;
1796 mbuf->m_pkthdr.len = size;
1799 xnb_mbufc2pkt(mbuf, &pkt, start, free_slots);
1800 req = RING_GET_REQUEST(&xnb_unit_pvt.rxf, start);
1802 xnb_unit_pvt.rxb.req_cons = start;
1803 xnb_unit_pvt.rxb.rsp_prod_pvt = start;
1804 xnb_unit_pvt.rxs->req_prod = start + 1;
1805 xnb_unit_pvt.rxs->rsp_prod = start;
1807 nr_entries = xnb_rxpkt2gnttab(&pkt, mbuf, xnb_unit_pvt.gnttab,
1808 &xnb_unit_pvt.rxb, DOMID_FIRST_RESERVED);
1810 nr_reqs = xnb_rxpkt2rsp(&pkt, xnb_unit_pvt.gnttab, nr_entries,
1813 XNB_ASSERT(nr_reqs == 1);
1814 XNB_ASSERT(xnb_unit_pvt.rxb.rsp_prod_pvt == start + 1);
1815 rsp = RING_GET_RESPONSE(&xnb_unit_pvt.rxb, start);
1816 XNB_ASSERT(rsp->id == req->id);
1817 XNB_ASSERT(rsp->offset == 0);
1818 XNB_ASSERT((rsp->flags & (NETRXF_more_data | NETRXF_extra_info)) == 0);
1819 XNB_ASSERT(rsp->status == size);
1821 safe_m_freem(&mbuf);
1825 * xnb_rxpkt2rsp with extra data
1828 xnb_rxpkt2rsp_extra(char *buffer, size_t buflen)
1831 int nr_entries, nr_reqs;
1833 int free_slots = 15;
1837 uint16_t mss = TCP_MSS - 40;
1839 struct netif_rx_request *req;
1840 struct netif_rx_response *rsp;
1841 struct netif_extra_info *ext;
1843 mbufc = m_getm(NULL, size, M_WAITOK, MT_DATA);
1844 XNB_ASSERT(mbufc != NULL);
1848 mbufc->m_flags |= M_PKTHDR;
1849 mbufc->m_pkthdr.len = size;
1850 mbufc->m_pkthdr.csum_flags |= CSUM_TSO;
1851 mbufc->m_pkthdr.tso_segsz = mss;
1852 mbufc->m_len = size;
1854 xnb_mbufc2pkt(mbufc, &pkt, start, free_slots);
1855 req = RING_GET_REQUEST(&xnb_unit_pvt.rxf, start);
1858 req = RING_GET_REQUEST(&xnb_unit_pvt.rxf, start + 1);
1860 req->gref = gref + 1;
1861 xnb_unit_pvt.rxb.req_cons = start;
1862 xnb_unit_pvt.rxb.rsp_prod_pvt = start;
1863 xnb_unit_pvt.rxs->req_prod = start + 2;
1864 xnb_unit_pvt.rxs->rsp_prod = start;
1866 nr_entries = xnb_rxpkt2gnttab(&pkt, mbufc, xnb_unit_pvt.gnttab,
1867 &xnb_unit_pvt.rxb, DOMID_FIRST_RESERVED);
1869 nr_reqs = xnb_rxpkt2rsp(&pkt, xnb_unit_pvt.gnttab, nr_entries,
1872 XNB_ASSERT(nr_reqs == 2);
1873 XNB_ASSERT(xnb_unit_pvt.rxb.rsp_prod_pvt == start + 2);
1874 rsp = RING_GET_RESPONSE(&xnb_unit_pvt.rxb, start);
1875 XNB_ASSERT(rsp->id == id);
1876 XNB_ASSERT((rsp->flags & NETRXF_more_data) == 0);
1877 XNB_ASSERT((rsp->flags & NETRXF_extra_info));
1878 XNB_ASSERT((rsp->flags & NETRXF_data_validated));
1879 XNB_ASSERT((rsp->flags & NETRXF_csum_blank));
1880 XNB_ASSERT(rsp->status == size);
1882 ext = (struct netif_extra_info*)
1883 RING_GET_RESPONSE(&xnb_unit_pvt.rxb, start + 1);
1884 XNB_ASSERT(ext->type == XEN_NETIF_EXTRA_TYPE_GSO);
1885 XNB_ASSERT(! (ext->flags & XEN_NETIF_EXTRA_FLAG_MORE));
1886 XNB_ASSERT(ext->u.gso.size == mss);
1887 XNB_ASSERT(ext->u.gso.type == XEN_NETIF_EXTRA_TYPE_GSO);
1889 safe_m_freem(&mbufc);
1893 * xnb_rxpkt2rsp on a packet with more than a pages's worth of data. It should
1894 * generate two response slot
1897 xnb_rxpkt2rsp_2slots(char *buffer, size_t buflen)
1900 int nr_entries, nr_reqs;
1901 size_t size = PAGE_SIZE + 100;
1905 uint16_t gref1 = 24;
1906 uint16_t gref2 = 34;
1907 RING_IDX start = 15;
1908 struct netif_rx_request *req;
1909 struct netif_rx_response *rsp;
1912 mbuf = m_getm(NULL, size, M_WAITOK, MT_DATA);
1913 mbuf->m_flags |= M_PKTHDR;
1914 mbuf->m_pkthdr.len = size;
1915 if (mbuf->m_next != NULL) {
1916 size_t first_len = MIN(M_TRAILINGSPACE(mbuf), size);
1917 mbuf->m_len = first_len;
1918 mbuf->m_next->m_len = size - first_len;
1924 xnb_mbufc2pkt(mbuf, &pkt, start, free_slots);
1925 req = RING_GET_REQUEST(&xnb_unit_pvt.rxf, start);
1928 req = RING_GET_REQUEST(&xnb_unit_pvt.rxf, start + 1);
1931 xnb_unit_pvt.rxb.req_cons = start;
1932 xnb_unit_pvt.rxb.rsp_prod_pvt = start;
1933 xnb_unit_pvt.rxs->req_prod = start + 2;
1934 xnb_unit_pvt.rxs->rsp_prod = start;
1936 nr_entries = xnb_rxpkt2gnttab(&pkt, mbuf, xnb_unit_pvt.gnttab,
1937 &xnb_unit_pvt.rxb, DOMID_FIRST_RESERVED);
1939 nr_reqs = xnb_rxpkt2rsp(&pkt, xnb_unit_pvt.gnttab, nr_entries,
1942 XNB_ASSERT(nr_reqs == 2);
1943 XNB_ASSERT(xnb_unit_pvt.rxb.rsp_prod_pvt == start + 2);
1944 rsp = RING_GET_RESPONSE(&xnb_unit_pvt.rxb, start);
1945 XNB_ASSERT(rsp->id == id1);
1946 XNB_ASSERT(rsp->offset == 0);
1947 XNB_ASSERT((rsp->flags & NETRXF_extra_info) == 0);
1948 XNB_ASSERT(rsp->flags & NETRXF_more_data);
1949 XNB_ASSERT(rsp->status == PAGE_SIZE);
1951 rsp = RING_GET_RESPONSE(&xnb_unit_pvt.rxb, start + 1);
1952 XNB_ASSERT(rsp->id == id2);
1953 XNB_ASSERT(rsp->offset == 0);
1954 XNB_ASSERT((rsp->flags & NETRXF_extra_info) == 0);
1955 XNB_ASSERT(! (rsp->flags & NETRXF_more_data));
1956 XNB_ASSERT(rsp->status == size - PAGE_SIZE);
1958 safe_m_freem(&mbuf);
1961 /** xnb_rxpkt2rsp on a grant table with two sub-page entries */
1963 xnb_rxpkt2rsp_2short(char *buffer, size_t buflen) {
1965 int nr_reqs, nr_entries;
1966 size_t size1 = MHLEN - 5;
1967 size_t size2 = MHLEN - 15;
1968 int free_slots = 32;
1969 RING_IDX start = 14;
1972 struct netif_rx_request *req;
1973 struct netif_rx_response *rsp;
1976 mbufc = m_getm(NULL, size1, M_WAITOK, MT_DATA);
1977 XNB_ASSERT(mbufc != NULL);
1980 mbufc->m_flags |= M_PKTHDR;
1982 m_getm(mbufc, size2, M_WAITOK, MT_DATA);
1983 XNB_ASSERT(mbufc->m_next != NULL);
1984 mbufc->m_pkthdr.len = size1 + size2;
1985 mbufc->m_len = size1;
1986 mbufc->m_next->m_len = size2;
1988 xnb_mbufc2pkt(mbufc, &pkt, start, free_slots);
1990 req = RING_GET_REQUEST(&xnb_unit_pvt.rxf, start);
1993 xnb_unit_pvt.rxb.req_cons = start;
1994 xnb_unit_pvt.rxb.rsp_prod_pvt = start;
1995 xnb_unit_pvt.rxs->req_prod = start + 1;
1996 xnb_unit_pvt.rxs->rsp_prod = start;
1998 nr_entries = xnb_rxpkt2gnttab(&pkt, mbufc, xnb_unit_pvt.gnttab,
1999 &xnb_unit_pvt.rxb, DOMID_FIRST_RESERVED);
2001 nr_reqs = xnb_rxpkt2rsp(&pkt, xnb_unit_pvt.gnttab, nr_entries,
2004 XNB_ASSERT(nr_entries == 2);
2005 XNB_ASSERT(nr_reqs == 1);
2006 rsp = RING_GET_RESPONSE(&xnb_unit_pvt.rxb, start);
2007 XNB_ASSERT(rsp->id == id);
2008 XNB_ASSERT(rsp->status == size1 + size2);
2009 XNB_ASSERT(rsp->offset == 0);
2010 XNB_ASSERT(! (rsp->flags & (NETRXF_more_data | NETRXF_extra_info)));
2012 safe_m_freem(&mbufc);
2016 * xnb_rxpkt2rsp on a long packet with a hypervisor gnttab_copy error
2017 * Note: this test will result in an error message being printed to the console
2019 * xnb(xnb_rxpkt2rsp:1720): Got error -1 for hypervisor gnttab_copy status
2022 xnb_rxpkt2rsp_copyerror(char *buffer, size_t buflen)
2025 int nr_entries, nr_reqs;
2028 uint16_t canary = 6859;
2029 size_t size = 7 * MCLBYTES;
2032 struct netif_rx_request *req;
2033 struct netif_rx_response *rsp;
2036 mbuf = m_getm(NULL, size, M_WAITOK, MT_DATA);
2037 mbuf->m_flags |= M_PKTHDR;
2038 mbuf->m_pkthdr.len = size;
2041 xnb_mbufc2pkt(mbuf, &pkt, start, free_slots);
2042 req = RING_GET_REQUEST(&xnb_unit_pvt.rxf, start);
2045 xnb_unit_pvt.rxb.req_cons = start;
2046 xnb_unit_pvt.rxb.rsp_prod_pvt = start;
2047 xnb_unit_pvt.rxs->req_prod = start + 1;
2048 xnb_unit_pvt.rxs->rsp_prod = start;
2049 req = RING_GET_REQUEST(&xnb_unit_pvt.rxf, start + 1);
2053 nr_entries = xnb_rxpkt2gnttab(&pkt, mbuf, xnb_unit_pvt.gnttab,
2054 &xnb_unit_pvt.rxb, DOMID_FIRST_RESERVED);
2055 /* Inject the error*/
2056 xnb_unit_pvt.gnttab[2].status = GNTST_general_error;
2058 nr_reqs = xnb_rxpkt2rsp(&pkt, xnb_unit_pvt.gnttab, nr_entries,
2061 XNB_ASSERT(nr_reqs == 1);
2062 XNB_ASSERT(xnb_unit_pvt.rxb.rsp_prod_pvt == start + 1);
2063 rsp = RING_GET_RESPONSE(&xnb_unit_pvt.rxb, start);
2064 XNB_ASSERT(rsp->id == id);
2065 XNB_ASSERT(rsp->status == NETIF_RSP_ERROR);
2066 req = RING_GET_REQUEST(&xnb_unit_pvt.rxf, start + 1);
2067 XNB_ASSERT(req->gref == canary);
2068 XNB_ASSERT(req->id == canary);
2070 safe_m_freem(&mbuf);
2073 #if defined(INET) || defined(INET6)
2075 * xnb_add_mbuf_cksum on an ARP request packet
2078 xnb_add_mbuf_cksum_arp(char *buffer, size_t buflen)
2080 const size_t pkt_len = sizeof(struct ether_header) +
2081 sizeof(struct ether_arp);
2083 struct ether_header *eh;
2084 struct ether_arp *ep;
2085 unsigned char pkt_orig[pkt_len];
2087 mbufc = m_getm(NULL, pkt_len, M_WAITOK, MT_DATA);
2088 /* Fill in an example arp request */
2089 eh = mtod(mbufc, struct ether_header*);
2090 eh->ether_dhost[0] = 0xff;
2091 eh->ether_dhost[1] = 0xff;
2092 eh->ether_dhost[2] = 0xff;
2093 eh->ether_dhost[3] = 0xff;
2094 eh->ether_dhost[4] = 0xff;
2095 eh->ether_dhost[5] = 0xff;
2096 eh->ether_shost[0] = 0x00;
2097 eh->ether_shost[1] = 0x15;
2098 eh->ether_shost[2] = 0x17;
2099 eh->ether_shost[3] = 0xe9;
2100 eh->ether_shost[4] = 0x30;
2101 eh->ether_shost[5] = 0x68;
2102 eh->ether_type = htons(ETHERTYPE_ARP);
2103 ep = (struct ether_arp*)(eh + 1);
2104 ep->ea_hdr.ar_hrd = htons(ARPHRD_ETHER);
2105 ep->ea_hdr.ar_pro = htons(ETHERTYPE_IP);
2106 ep->ea_hdr.ar_hln = 6;
2107 ep->ea_hdr.ar_pln = 4;
2108 ep->ea_hdr.ar_op = htons(ARPOP_REQUEST);
2109 ep->arp_sha[0] = 0x00;
2110 ep->arp_sha[1] = 0x15;
2111 ep->arp_sha[2] = 0x17;
2112 ep->arp_sha[3] = 0xe9;
2113 ep->arp_sha[4] = 0x30;
2114 ep->arp_sha[5] = 0x68;
2115 ep->arp_spa[0] = 0xc0;
2116 ep->arp_spa[1] = 0xa8;
2117 ep->arp_spa[2] = 0x0a;
2118 ep->arp_spa[3] = 0x04;
2119 bzero(&(ep->arp_tha), ETHER_ADDR_LEN);
2120 ep->arp_tpa[0] = 0xc0;
2121 ep->arp_tpa[1] = 0xa8;
2122 ep->arp_tpa[2] = 0x0a;
2123 ep->arp_tpa[3] = 0x06;
2125 /* fill in the length field */
2126 mbufc->m_len = pkt_len;
2127 mbufc->m_pkthdr.len = pkt_len;
2128 /* indicate that the netfront uses hw-assisted checksums */
2129 mbufc->m_pkthdr.csum_flags = CSUM_IP_CHECKED | CSUM_IP_VALID |
2130 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2132 /* Make a backup copy of the packet */
2133 bcopy(mtod(mbufc, const void*), pkt_orig, pkt_len);
2135 /* Function under test */
2136 xnb_add_mbuf_cksum(mbufc);
2138 /* Verify that the packet's data did not change */
2139 XNB_ASSERT(bcmp(mtod(mbufc, const void*), pkt_orig, pkt_len) == 0);
2144 * Helper function that populates the ethernet header and IP header used by
2145 * some of the xnb_add_mbuf_cksum unit tests. m must already be allocated
2146 * and must be large enough
2149 xnb_fill_eh_and_ip(struct mbuf *m, uint16_t ip_len, uint16_t ip_id,
2150 uint16_t ip_p, uint16_t ip_off, uint16_t ip_sum)
2152 struct ether_header *eh;
2155 eh = mtod(m, struct ether_header*);
2156 eh->ether_dhost[0] = 0x00;
2157 eh->ether_dhost[1] = 0x16;
2158 eh->ether_dhost[2] = 0x3e;
2159 eh->ether_dhost[3] = 0x23;
2160 eh->ether_dhost[4] = 0x50;
2161 eh->ether_dhost[5] = 0x0b;
2162 eh->ether_shost[0] = 0x00;
2163 eh->ether_shost[1] = 0x16;
2164 eh->ether_shost[2] = 0x30;
2165 eh->ether_shost[3] = 0x00;
2166 eh->ether_shost[4] = 0x00;
2167 eh->ether_shost[5] = 0x00;
2168 eh->ether_type = htons(ETHERTYPE_IP);
2169 iph = (struct ip*)(eh + 1);
2170 iph->ip_hl = 0x5; /* 5 dwords == 20 bytes */
2171 iph->ip_v = 4; /* IP v4 */
2173 iph->ip_len = htons(ip_len);
2174 iph->ip_id = htons(ip_id);
2175 iph->ip_off = htons(ip_off);
2178 iph->ip_sum = htons(ip_sum);
2179 iph->ip_src.s_addr = htonl(0xc0a80a04);
2180 iph->ip_dst.s_addr = htonl(0xc0a80a05);
2184 * xnb_add_mbuf_cksum on an ICMP packet, based on a tcpdump of an actual
2188 xnb_add_mbuf_cksum_icmp(char *buffer, size_t buflen)
2190 const size_t icmp_len = 64; /* set by ping(1) */
2191 const size_t pkt_len = sizeof(struct ether_header) +
2192 sizeof(struct ip) + icmp_len;
2194 struct ether_header *eh;
2197 unsigned char pkt_orig[icmp_len];
2199 uint8_t *data_payload;
2201 const uint16_t ICMP_CSUM = 0xaed7;
2202 const uint16_t IP_CSUM = 0xe533;
2204 mbufc = m_getm(NULL, pkt_len, M_WAITOK, MT_DATA);
2205 /* Fill in an example ICMP ping request */
2206 eh = mtod(mbufc, struct ether_header*);
2207 xnb_fill_eh_and_ip(mbufc, 84, 28, IPPROTO_ICMP, 0, 0);
2208 iph = (struct ip*)(eh + 1);
2209 icmph = (struct icmp*)(iph + 1);
2210 icmph->icmp_type = ICMP_ECHO;
2211 icmph->icmp_code = 0;
2212 icmph->icmp_cksum = htons(ICMP_CSUM);
2213 icmph->icmp_id = htons(31492);
2214 icmph->icmp_seq = htons(0);
2216 * ping(1) uses bcopy to insert a native-endian timeval after icmp_seq.
2217 * For this test, we will set the bytes individually for portability.
2219 tv_field = (uint32_t*)(&(icmph->icmp_hun));
2220 tv_field[0] = 0x4f02cfac;
2221 tv_field[1] = 0x0007c46a;
2223 * Remainder of packet is an incrmenting 8 bit integer, starting with 8
2225 data_payload = (uint8_t*)(&tv_field[2]);
2226 for (i = 8; i < 37; i++) {
2227 *data_payload++ = i;
2230 /* fill in the length field */
2231 mbufc->m_len = pkt_len;
2232 mbufc->m_pkthdr.len = pkt_len;
2233 /* indicate that the netfront uses hw-assisted checksums */
2234 mbufc->m_pkthdr.csum_flags = CSUM_IP_CHECKED | CSUM_IP_VALID |
2235 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2237 bcopy(mtod(mbufc, const void*), pkt_orig, icmp_len);
2238 /* Function under test */
2239 xnb_add_mbuf_cksum(mbufc);
2241 /* Check the IP checksum */
2242 XNB_ASSERT(iph->ip_sum == htons(IP_CSUM));
2244 /* Check that the ICMP packet did not change */
2245 XNB_ASSERT(bcmp(icmph, pkt_orig, icmp_len));
2250 * xnb_add_mbuf_cksum on a UDP packet, based on a tcpdump of an actual
2254 xnb_add_mbuf_cksum_udp(char *buffer, size_t buflen)
2256 const size_t udp_len = 16;
2257 const size_t pkt_len = sizeof(struct ether_header) +
2258 sizeof(struct ip) + udp_len;
2260 struct ether_header *eh;
2263 uint8_t *data_payload;
2264 const uint16_t IP_CSUM = 0xe56b;
2265 const uint16_t UDP_CSUM = 0xdde2;
2267 mbufc = m_getm(NULL, pkt_len, M_WAITOK, MT_DATA);
2268 /* Fill in an example UDP packet made by 'uname | nc -u <host> 2222 */
2269 eh = mtod(mbufc, struct ether_header*);
2270 xnb_fill_eh_and_ip(mbufc, 36, 4, IPPROTO_UDP, 0, 0xbaad);
2271 iph = (struct ip*)(eh + 1);
2272 udp = (struct udphdr*)(iph + 1);
2273 udp->uh_sport = htons(0x51ae);
2274 udp->uh_dport = htons(0x08ae);
2275 udp->uh_ulen = htons(udp_len);
2276 udp->uh_sum = htons(0xbaad); /* xnb_add_mbuf_cksum will fill this in */
2277 data_payload = (uint8_t*)(udp + 1);
2278 data_payload[0] = 'F';
2279 data_payload[1] = 'r';
2280 data_payload[2] = 'e';
2281 data_payload[3] = 'e';
2282 data_payload[4] = 'B';
2283 data_payload[5] = 'S';
2284 data_payload[6] = 'D';
2285 data_payload[7] = '\n';
2287 /* fill in the length field */
2288 mbufc->m_len = pkt_len;
2289 mbufc->m_pkthdr.len = pkt_len;
2290 /* indicate that the netfront uses hw-assisted checksums */
2291 mbufc->m_pkthdr.csum_flags = CSUM_IP_CHECKED | CSUM_IP_VALID |
2292 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2294 /* Function under test */
2295 xnb_add_mbuf_cksum(mbufc);
2297 /* Check the checksums */
2298 XNB_ASSERT(iph->ip_sum == htons(IP_CSUM));
2299 XNB_ASSERT(udp->uh_sum == htons(UDP_CSUM));
2305 * Helper function that populates a TCP packet used by all of the
2306 * xnb_add_mbuf_cksum tcp unit tests. m must already be allocated and must be
2310 xnb_fill_tcp(struct mbuf *m)
2312 struct ether_header *eh;
2316 uint8_t *data_payload;
2318 /* Fill in an example TCP packet made by 'uname | nc <host> 2222' */
2319 eh = mtod(m, struct ether_header*);
2320 xnb_fill_eh_and_ip(m, 60, 8, IPPROTO_TCP, IP_DF, 0);
2321 iph = (struct ip*)(eh + 1);
2322 tcp = (struct tcphdr*)(iph + 1);
2323 tcp->th_sport = htons(0x9cd9);
2324 tcp->th_dport = htons(2222);
2325 tcp->th_seq = htonl(0x00f72b10);
2326 tcp->th_ack = htonl(0x7f37ba6c);
2329 tcp->th_flags = 0x18;
2330 tcp->th_win = htons(0x410);
2331 /* th_sum is incorrect; will be inserted by function under test */
2332 tcp->th_sum = htons(0xbaad);
2333 tcp->th_urp = htons(0);
2335 * The following 12 bytes of options encode:
2336 * [nop, nop, TS val 33247 ecr 3457687679]
2338 options = (uint32_t*)(tcp + 1);
2339 options[0] = htonl(0x0101080a);
2340 options[1] = htonl(0x000081df);
2341 options[2] = htonl(0xce18207f);
2342 data_payload = (uint8_t*)(&options[3]);
2343 data_payload[0] = 'F';
2344 data_payload[1] = 'r';
2345 data_payload[2] = 'e';
2346 data_payload[3] = 'e';
2347 data_payload[4] = 'B';
2348 data_payload[5] = 'S';
2349 data_payload[6] = 'D';
2350 data_payload[7] = '\n';
2354 * xnb_add_mbuf_cksum on a TCP packet, based on a tcpdump of an actual TCP
2358 xnb_add_mbuf_cksum_tcp(char *buffer, size_t buflen)
2360 const size_t payload_len = 8;
2361 const size_t tcp_options_len = 12;
2362 const size_t pkt_len = sizeof(struct ether_header) + sizeof(struct ip) +
2363 sizeof(struct tcphdr) + tcp_options_len + payload_len;
2365 struct ether_header *eh;
2368 const uint16_t IP_CSUM = 0xa55a;
2369 const uint16_t TCP_CSUM = 0x2f64;
2371 mbufc = m_getm(NULL, pkt_len, M_WAITOK, MT_DATA);
2372 /* Fill in an example TCP packet made by 'uname | nc <host> 2222' */
2373 xnb_fill_tcp(mbufc);
2374 eh = mtod(mbufc, struct ether_header*);
2375 iph = (struct ip*)(eh + 1);
2376 tcp = (struct tcphdr*)(iph + 1);
2378 /* fill in the length field */
2379 mbufc->m_len = pkt_len;
2380 mbufc->m_pkthdr.len = pkt_len;
2381 /* indicate that the netfront uses hw-assisted checksums */
2382 mbufc->m_pkthdr.csum_flags = CSUM_IP_CHECKED | CSUM_IP_VALID |
2383 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2385 /* Function under test */
2386 xnb_add_mbuf_cksum(mbufc);
2388 /* Check the checksums */
2389 XNB_ASSERT(iph->ip_sum == htons(IP_CSUM));
2390 XNB_ASSERT(tcp->th_sum == htons(TCP_CSUM));
2396 * xnb_add_mbuf_cksum on a TCP packet that does not use HW assisted checksums
2399 xnb_add_mbuf_cksum_tcp_swcksum(char *buffer, size_t buflen)
2401 const size_t payload_len = 8;
2402 const size_t tcp_options_len = 12;
2403 const size_t pkt_len = sizeof(struct ether_header) + sizeof(struct ip) +
2404 sizeof(struct tcphdr) + tcp_options_len + payload_len;
2406 struct ether_header *eh;
2409 /* Use deliberately bad checksums, and verify that they don't get */
2410 /* corrected by xnb_add_mbuf_cksum */
2411 const uint16_t IP_CSUM = 0xdead;
2412 const uint16_t TCP_CSUM = 0xbeef;
2414 mbufc = m_getm(NULL, pkt_len, M_WAITOK, MT_DATA);
2415 /* Fill in an example TCP packet made by 'uname | nc <host> 2222' */
2416 xnb_fill_tcp(mbufc);
2417 eh = mtod(mbufc, struct ether_header*);
2418 iph = (struct ip*)(eh + 1);
2419 iph->ip_sum = htons(IP_CSUM);
2420 tcp = (struct tcphdr*)(iph + 1);
2421 tcp->th_sum = htons(TCP_CSUM);
2423 /* fill in the length field */
2424 mbufc->m_len = pkt_len;
2425 mbufc->m_pkthdr.len = pkt_len;
2426 /* indicate that the netfront does not use hw-assisted checksums */
2427 mbufc->m_pkthdr.csum_flags = 0;
2429 /* Function under test */
2430 xnb_add_mbuf_cksum(mbufc);
2432 /* Check that the checksums didn't change */
2433 XNB_ASSERT(iph->ip_sum == htons(IP_CSUM));
2434 XNB_ASSERT(tcp->th_sum == htons(TCP_CSUM));
2438 #endif /* INET || INET6 */
2441 * sscanf on unsigned chars
2444 xnb_sscanf_hhu(char *buffer, size_t buflen)
2446 const char mystr[] = "137";
2450 for (i = 0; i < 12; i++)
2453 XNB_ASSERT(sscanf(mystr, "%hhu", &dest[4]) == 1);
2454 for (i = 0; i < 12; i++)
2455 XNB_ASSERT(dest[i] == (i == 4 ? 137 : 'X'));
2459 * sscanf on signed chars
2462 xnb_sscanf_hhd(char *buffer, size_t buflen)
2464 const char mystr[] = "-27";
2468 for (i = 0; i < 12; i++)
2471 XNB_ASSERT(sscanf(mystr, "%hhd", &dest[4]) == 1);
2472 for (i = 0; i < 12; i++)
2473 XNB_ASSERT(dest[i] == (i == 4 ? -27 : 'X'));
2477 * sscanf on signed long longs
2480 xnb_sscanf_lld(char *buffer, size_t buflen)
2482 const char mystr[] = "-123456789012345"; /* about -2**47 */
2486 for (i = 0; i < 3; i++)
2487 dest[i] = (long long)0xdeadbeefdeadbeef;
2489 XNB_ASSERT(sscanf(mystr, "%lld", &dest[1]) == 1);
2490 for (i = 0; i < 3; i++)
2491 XNB_ASSERT(dest[i] == (i != 1 ? (long long)0xdeadbeefdeadbeef :
2496 * sscanf on unsigned long longs
2499 xnb_sscanf_llu(char *buffer, size_t buflen)
2501 const char mystr[] = "12802747070103273189";
2502 unsigned long long dest[3];
2505 for (i = 0; i < 3; i++)
2506 dest[i] = (long long)0xdeadbeefdeadbeef;
2508 XNB_ASSERT(sscanf(mystr, "%llu", &dest[1]) == 1);
2509 for (i = 0; i < 3; i++)
2510 XNB_ASSERT(dest[i] == (i != 1 ? (long long)0xdeadbeefdeadbeef :
2511 12802747070103273189ull));
2515 * sscanf on unsigned short short n's
2518 xnb_sscanf_hhn(char *buffer, size_t buflen)
2520 const char mystr[] =
2521 "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
2522 "202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"
2523 "404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f";
2524 unsigned char dest[12];
2527 for (i = 0; i < 12; i++)
2528 dest[i] = (unsigned char)'X';
2530 XNB_ASSERT(sscanf(mystr,
2531 "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
2532 "202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"
2533 "404142434445464748494a4b4c4d4e4f%hhn", &dest[4]) == 0);
2534 for (i = 0; i < 12; i++)
2535 XNB_ASSERT(dest[i] == (i == 4 ? 160 : 'X'));