[lib][minip] a few fixes

-Add C++ header guards around the public api.
-Fix a divide by zero bug in the test console routine.
-Add a new pktbuf api to assist with resetting a pktbuf back to default
values for reuse in an ethernet driver.
This commit is contained in:
Travis Geiselbrecht
2021-12-27 20:25:35 -08:00
parent 06105c5835
commit b5a9c2d8b2
4 changed files with 31 additions and 3 deletions

View File

@@ -8,6 +8,7 @@
*/
#pragma once
#include <lk/compiler.h>
#include <endian.h>
#include <lk/list.h>
#include <stdint.h>
@@ -16,6 +17,8 @@
#include <lib/pktbuf.h>
__BEGIN_CDECLS
#define IPV4(a,b,c,d) (((a)&0xFF)|(((b)&0xFF)<<8)|(((c)&0xFF)<<16)|(((d)&0xFF)<<24))
#define IPV4_SPLIT(a) (a & 0xFF), ((a >> 8) & 0xFF), ((a >> 16) & 0xFF), ((a >> 24) & 0xFF)
#define IPV4_PACK(a) (a[3] << 24 | a[2] << 16 | a[1] << 8 | a[0])
@@ -72,3 +75,5 @@ static inline status_t tcp_accept(tcp_socket_t *listen_socket, tcp_socket_t **ac
/* utilities */
void gen_random_mac_address(uint8_t *mac_addr);
__END_CDECLS

View File

@@ -9,6 +9,9 @@
#include <sys/types.h>
#include <lk/list.h>
#include <lk/compiler.h>
__BEGIN_CDECLS
/* PAGE_SIZE minus 16 bytes of metadata in pktbuf_buf */
#ifndef PKTBUF_POOL_SIZE
@@ -72,6 +75,7 @@ pktbuf_t *pktbuf_alloc_empty(void);
/* Add a buffer to an existing packet buffer */
void pktbuf_add_buffer(pktbuf_t *p, u8 *buf, u32 len, uint32_t header_sz,
uint32_t flags, pktbuf_free_callback cb, void *cb_args);
// return packet buffer to buffer pool
// returns number of threads woken up
int pktbuf_free(pktbuf_t *p, bool reschedule);
@@ -96,6 +100,12 @@ void *pktbuf_consume(pktbuf_t *p, size_t sz);
// remove sz bytes from the end of the pktbuf
void pktbuf_consume_tail(pktbuf_t *p, size_t sz);
// reset the header size to header_sz into the underlying buffer.
// p->dlen is also set to 0.
// pktbuf must have a buffer with a valid blen and header_sz must
// be within the buffer.
void pktbuf_reset(pktbuf_t *p, uint32_t header_sz);
// create a new packet buffer from raw memory and add
// it to the free pool
void pktbuf_create(void *ptr, size_t size);
@@ -104,3 +114,5 @@ void pktbuf_create(void *ptr, size_t size);
void pktbuf_create_bufs(void *ptr, size_t size);
void pktbuf_dump(pktbuf_t *p);
__END_CDECLS

View File

@@ -117,7 +117,7 @@ minip_usage:
memset(buf, 0x00, BUFSIZE);
printf("sending %u packet(s) to %u.%u.%u.%u:%u\n", count, IPV4_SPLIT(host), port);
lk_time_t t = current_time();
lk_bigtime_t t = current_time_hires();
uint32_t failures = 0;
for (uint32_t i = 0; i < count; i++) {
if (udp_send(buf, BUFSIZE, handle) != 0) {
@@ -125,11 +125,13 @@ minip_usage:
}
buf[128]++;
}
t = current_time() - t;
t = current_time_hires() - t;
if (t == 0)
t++;
printf("%d pkts failed\n", failures);
uint64_t total_count = (uint64_t)count * BUFSIZE;
printf("wrote %llu bytes in %u msecs (%llu bytes/sec)\n",
total_count, (uint32_t)t, total_count * 1000 / t);
total_count, (uint32_t)t, total_count * 1000000 / t);
free(buf);
udp_close(handle);

View File

@@ -117,6 +117,15 @@ pktbuf_t *pktbuf_alloc(void) {
return p;
}
void pktbuf_reset(pktbuf_t *p, uint32_t header_sz) {
DEBUG_ASSERT(p);
DEBUG_ASSERT(p->buffer);
DEBUG_ASSERT(header_sz < p->blen);
p->data = p->buffer + header_sz;
p->dlen = 0;
}
pktbuf_t *pktbuf_alloc_empty(void) {
pktbuf_t *p = (pktbuf_t *) get_pool_object();