wes3 commented on a change in pull request #2075: kernel/os: Add os_mbuf_pack_chains URL: https://github.com/apache/mynewt-core/pull/2075#discussion_r340383783
########## File path: kernel/os/selftest/src/testcases/os_mbuf_test_pack_chains.c ########## @@ -0,0 +1,371 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#include "os_test_priv.h" + +#if MBUF_TEST_POOL_BUF_SIZE != 256 + #error "Test pool buffer size must be 256!" +#endif + +/* This structure is used to create mbuf chains. It contains the length + of data that should be in each mbuf in the chain and the amount of + leading space in the mbuf */ +struct os_mbtpc_cd +{ + uint16_t mlen; + uint16_t leadingspace; +}; + +/* The random seed (chosen at random) */ +static unsigned long int rand_next = 1001; + +/* Used for data integrity testing */ +static uint8_t os_mbuf_test_pack_chains_test_data[2048]; + +/** + * Calculates the total number of mbufs needed for a chain + * presuming each mbuf is filled to capacity except the last. + * NOTE: the pkthdr_len must include the os_mbuf_pkthdr struct; + * it is not automatically accounted for. + */ +uint16_t +os_mbuf_test_pack_chains_calc_total_mbufs(uint16_t len, int pkthdr_len) +{ + uint16_t total; + uint16_t rem; + uint16_t dbuflen; + + rem = len; + dbuflen = os_mbuf_pool.omp_databuf_len; + + /* Only the first mbuf should have a packet header so this is subtracted + for only one mbuf (if present) */ + total = 1; + if (len > (dbuflen - pkthdr_len)) { + rem -= (dbuflen - pkthdr_len); + while (rem > 0) { + ++total; + if (rem <= dbuflen) { + break; + } + rem -= dbuflen; + } + } + + return total; +} + +struct os_mbuf * +os_mbuf_test_pack_chains_create_chain(int num_mbufs, struct os_mbtpc_cd *mdata, + uint16_t srcoff, int is_pkthdr, + uint8_t pkthdr_len) +{ + int i; + int rc; + struct os_mbuf *m; + struct os_mbuf *cur; + struct os_mbuf *tmp; + uint8_t *src; + uint16_t hdrlen; + + TEST_ASSERT_FATAL(mdata != NULL, "mdata NULL"); + TEST_ASSERT_FATAL(num_mbufs != 0, "mbufs cannot be zero"); + + if (is_pkthdr) { + m = os_mbuf_get_pkthdr(&os_mbuf_pool, pkthdr_len); + m->om_data += mdata[0].leadingspace; + hdrlen = sizeof(struct os_mbuf_pkthdr) + pkthdr_len; + } else { + m = os_mbuf_get(&os_mbuf_pool, mdata[0].leadingspace); + hdrlen = 0; + } + os_mbuf_test_misc_assert_sane(m, NULL, 0, 0, hdrlen); + TEST_ASSERT_FATAL(mdata[0].mlen != 0, "mlen cannot be zero"); + + src = os_mbuf_test_pack_chains_test_data + srcoff; + rc = os_mbuf_copyinto(m, 0, src, (int)mdata[0].mlen); + TEST_ASSERT_FATAL(rc == 0, "copyinto failed"); + src += mdata[0].mlen; + + cur = m; + for (i = 1; i < num_mbufs; ++i) { + tmp = os_mbuf_get(&os_mbuf_pool, mdata[i].leadingspace); + os_mbuf_test_misc_assert_sane(tmp, NULL, 0, 0, 0); + rc = os_mbuf_copyinto(tmp, 0, src, (int)mdata[i].mlen); + TEST_ASSERT_FATAL(rc == 0, "copyinto failed"); + if (is_pkthdr) { + OS_MBUF_PKTLEN(m) += mdata[i].mlen; + } + src += mdata[i].mlen; + SLIST_NEXT(cur, om_next) = tmp; + cur = tmp; + } + return m; +} + +/* + * This is here cause I dont feel like calling rand :-) + * + * Taken from the K&R C programming language book. Page 46. + * returns a pseudo-random integer of 0..32767. Note that + * this is compatible with the System V function rand(), not + * with the bsd function rand() that returns 0..(2**31)-1. + */ +unsigned int +os_mbuf_test_pack_chans_rand(void) Review comment: I have no good reason why I did not want to call rand() :-) I just wanted it all self-contained. I did have a reason why I used random data instead of, say, a pattern that increments by 1. I was worried that there might be some weird test patterns where the data would be the same but incorrect (if you see what I mean). I think with using random data we are pretty sure that wont be the case. Did not want to hash things or add a crc or checksum. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services