Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit 39d3335

Browse files
author
Paul Sokolovsky
committed
zephyr/modusocket: Update for net_pkt refactor.
1 parent c022c9a commit 39d3335

File tree

1 file changed

+54
-59
lines changed

1 file changed

+54
-59
lines changed

zephyr/modusocket.c

+54-59
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
// Zephyr's generated version header
3636
#include <version.h>
3737
#include <net/net_context.h>
38-
#include <net/nbuf.h>
38+
#include <net/net_pkt.h>
3939

4040
#define DEBUG 0
4141
#if DEBUG // print debugging info
@@ -51,7 +51,7 @@ typedef struct _socket_obj_t {
5151
struct k_fifo recv_q;
5252
struct k_fifo accept_q;
5353
};
54-
struct net_buf *cur_buf;
54+
struct net_pkt *cur_pkt;
5555

5656
#define STATE_NEW 0
5757
#define STATE_CONNECTING 1
@@ -115,12 +115,12 @@ STATIC void parse_inet_addr(socket_obj_t *socket, mp_obj_t addr_in, struct socka
115115
}
116116

117117
// Copy data from Zephyr net_buf chain into linear buffer.
118-
// We don't use net_nbuf_read(), because it's weird (e.g., we'd like to
119-
// free processed data fragment ASAP, while net_nbuf_read() holds onto
118+
// We don't use net_pkt_read(), because it's weird (e.g., we'd like to
119+
// free processed data fragment ASAP, while net_pkt_read() holds onto
120120
// the whole fragment chain to do its deeds, and that's minor comparing
121121
// to the fact that it copies data byte by byte).
122-
static char *net_buf_gather(struct net_buf *buf, char *to, unsigned max_len) {
123-
struct net_buf *tmp = buf->frags;
122+
static char *net_pkt_gather(struct net_pkt *pkt, char *to, unsigned max_len) {
123+
struct net_buf *tmp = pkt->frags;
124124

125125
while (tmp && max_len) {
126126
unsigned len = tmp->len;
@@ -130,48 +130,46 @@ static char *net_buf_gather(struct net_buf *buf, char *to, unsigned max_len) {
130130
memcpy(to, tmp->data, len);
131131
to += len;
132132
max_len -= len;
133-
tmp = net_buf_frag_del(buf, tmp);
133+
tmp = net_pkt_frag_del(pkt, NULL, tmp);
134134
}
135135

136136
return to;
137137
}
138138

139139
// Callback for incoming packets.
140-
static void sock_received_cb(struct net_context *context, struct net_buf *net_buf, int status, void *user_data) {
140+
static void sock_received_cb(struct net_context *context, struct net_pkt *pkt, int status, void *user_data) {
141141
socket_obj_t *socket = (socket_obj_t*)user_data;
142-
DEBUG_printf("recv cb: context: %p, status: %d, buf: %p", context, status, net_buf);
143-
if (net_buf) {
144-
DEBUG_printf(" (sz=%d, l=%d), token: %p", net_buf->size, net_buf->len, net_nbuf_token(net_buf));
142+
DEBUG_printf("recv cb: context: %p, status: %d, pkt: %p", context, status, pkt);
143+
if (pkt) {
144+
DEBUG_printf(" (appdatalen=%d), token: %p", pkt->appdatalen, net_pkt_token(pkt));
145145
}
146146
DEBUG_printf("\n");
147147
#if DEBUG > 1
148-
net_nbuf_print_frags(net_buf);
148+
net_pkt_print_frags(pkt);
149149
#endif
150150

151151
// if net_buf == NULL, EOF
152-
if (net_buf == NULL) {
153-
struct net_buf *last_buf = _k_fifo_peek_tail(&socket->recv_q);
154-
if (last_buf == NULL) {
152+
if (pkt == NULL) {
153+
struct net_pkt *last_pkt = _k_fifo_peek_tail(&socket->recv_q);
154+
if (last_pkt == NULL) {
155155
socket->state = STATE_PEER_CLOSED;
156156
DEBUG_printf("Marked socket %p as peer-closed\n", socket);
157157
} else {
158158
// We abuse "buf_sent" flag to store EOF flag
159-
net_nbuf_set_buf_sent(last_buf, true);
160-
DEBUG_printf("Set EOF flag on %p\n", last_buf);
159+
net_pkt_set_sent(last_pkt, true);
160+
DEBUG_printf("Set EOF flag on %p\n", last_pkt);
161161
}
162162
return;
163163
}
164164

165165
// Make sure that "EOF flag" is not set
166-
net_nbuf_set_buf_sent(net_buf, false);
166+
net_pkt_set_sent(pkt, false);
167167

168168
// We don't care about packet header, so get rid of it asap
169-
unsigned header_len = net_nbuf_appdata(net_buf) - net_buf->frags->data;
170-
net_buf_pull(net_buf->frags, header_len);
169+
unsigned header_len = net_pkt_appdata(pkt) - pkt->frags->data;
170+
net_buf_pull(pkt->frags, header_len);
171171

172-
// net_buf->frags will be overwritten by fifo, so save it
173-
net_nbuf_set_token(net_buf, net_buf->frags);
174-
k_fifo_put(&socket->recv_q, net_buf);
172+
k_fifo_put(&socket->recv_q, pkt);
175173
}
176174

177175
// Callback for incoming connections.
@@ -187,7 +185,7 @@ socket_obj_t *socket_new(void) {
187185
socket_obj_t *socket = m_new_obj_with_finaliser(socket_obj_t);
188186
socket->base.type = (mp_obj_t)&socket_type;
189187
k_fifo_init(&socket->recv_q);
190-
socket->cur_buf = NULL;
188+
socket->cur_pkt = NULL;
191189
socket->state = STATE_NEW;
192190
return socket;
193191
}
@@ -309,7 +307,7 @@ STATIC mp_uint_t sock_write(mp_obj_t self_in, const void *buf, mp_uint_t size, i
309307
return MP_STREAM_ERROR;
310308
}
311309

312-
struct net_buf *send_buf = net_nbuf_get_tx(socket->ctx, K_FOREVER);
310+
struct net_pkt *send_pkt = net_pkt_get_tx(socket->ctx, K_FOREVER);
313311

314312
unsigned len = net_if_get_mtu(net_context_get_iface(socket->ctx));
315313
// Arbitrary value to account for protocol headers
@@ -318,11 +316,11 @@ STATIC mp_uint_t sock_write(mp_obj_t self_in, const void *buf, mp_uint_t size, i
318316
len = size;
319317
}
320318

321-
if (!net_nbuf_append(send_buf, len, buf, K_FOREVER)) {
322-
len = net_buf_frags_len(send_buf);
319+
if (!net_pkt_append(send_pkt, len, buf, K_FOREVER)) {
320+
len = net_pkt_get_len(send_pkt);
323321
}
324322

325-
int err = net_context_send(send_buf, /*cb*/NULL, K_FOREVER, NULL, NULL);
323+
int err = net_context_send(send_pkt, /*cb*/NULL, K_FOREVER, NULL, NULL);
326324
if (err < 0) {
327325
*errcode = -err;
328326
return MP_STREAM_ERROR;
@@ -356,41 +354,37 @@ STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int *
356354

357355
if (sock_type == SOCK_DGRAM) {
358356

359-
struct net_buf *net_buf = k_fifo_get(&socket->recv_q, K_FOREVER);
360-
// Restore ->frags overwritten by fifo
361-
net_buf->frags = net_nbuf_token(net_buf);
357+
struct net_pkt *pkt = k_fifo_get(&socket->recv_q, K_FOREVER);
362358

363-
recv_len = net_nbuf_appdatalen(net_buf);
364-
DEBUG_printf("recv: net_buf=%p, appdatalen: %d\n", net_buf, recv_len);
359+
recv_len = net_pkt_appdatalen(pkt);
360+
DEBUG_printf("recv: pkt=%p, appdatalen: %d\n", pkt, recv_len);
365361

366362
if (recv_len > max_len) {
367363
recv_len = max_len;
368364
}
369365

370-
net_buf_gather(net_buf, buf, recv_len);
371-
net_nbuf_unref(net_buf);
366+
net_pkt_gather(pkt, buf, recv_len);
367+
net_pkt_unref(pkt);
372368

373369
} else if (sock_type == SOCK_STREAM) {
374370

375371
do {
376372

377-
if (socket->cur_buf == NULL) {
373+
if (socket->cur_pkt == NULL) {
378374
if (socket->state == STATE_PEER_CLOSED) {
379375
return 0;
380376
}
381377

382-
DEBUG_printf("TCP recv: no cur_buf, getting\n");
383-
struct net_buf *net_buf = k_fifo_get(&socket->recv_q, K_FOREVER);
384-
// Restore ->frags overwritten by fifo
385-
net_buf->frags = net_nbuf_token(net_buf);
378+
DEBUG_printf("TCP recv: no cur_pkt, getting\n");
379+
struct net_pkt *pkt = k_fifo_get(&socket->recv_q, K_FOREVER);
386380

387-
DEBUG_printf("TCP recv: new cur_buf: %p\n", net_buf);
388-
socket->cur_buf = net_buf;
381+
DEBUG_printf("TCP recv: new cur_pkt: %p\n", pkt);
382+
socket->cur_pkt = pkt;
389383
}
390384

391-
struct net_buf *frag = socket->cur_buf->frags;
385+
struct net_buf *frag = socket->cur_pkt->frags;
392386
if (frag == NULL) {
393-
printf("net_buf has empty fragments on start!\n");
387+
printf("net_pkt has empty fragments on start!\n");
394388
assert(0);
395389
}
396390

@@ -406,20 +400,20 @@ STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int *
406400
if (recv_len != frag_len) {
407401
net_buf_pull(frag, recv_len);
408402
} else {
409-
frag = net_buf_frag_del(socket->cur_buf, frag);
403+
frag = net_pkt_frag_del(socket->cur_pkt, NULL, frag);
410404
if (frag == NULL) {
411-
DEBUG_printf("Finished processing net_buf %p\n", socket->cur_buf);
412-
// If "buf_sent" flag was set, it's last packet and we reached EOF
413-
if (net_nbuf_buf_sent(socket->cur_buf)) {
405+
DEBUG_printf("Finished processing pkt %p\n", socket->cur_pkt);
406+
// If "sent" flag was set, it's last packet and we reached EOF
407+
if (net_pkt_sent(socket->cur_pkt)) {
414408
socket->state = STATE_PEER_CLOSED;
415409
}
416-
net_nbuf_unref(socket->cur_buf);
417-
socket->cur_buf = NULL;
410+
net_pkt_unref(socket->cur_pkt);
411+
socket->cur_pkt = NULL;
418412
}
419413
}
420414
// Keep repeating while we're getting empty fragments
421-
// Zephyr IP stack appears to feed empty net_buf's with empty
422-
// frags for various TCP control packets.
415+
// Zephyr IP stack appears to have fed empty net_buf's with empty
416+
// frags for various TCP control packets - in previous versions.
423417
} while (recv_len == 0);
424418
}
425419

@@ -507,17 +501,18 @@ STATIC const mp_obj_type_t socket_type = {
507501
.locals_dict = (mp_obj_t)&socket_locals_dict,
508502
};
509503

510-
STATIC mp_obj_t nbuf_get_info(void) {
511-
struct net_buf_pool *rx, *tx, *rx_data, *tx_data;
512-
net_nbuf_get_info(&rx, &tx, &rx_data, &tx_data);
504+
STATIC mp_obj_t pkt_get_info(void) {
505+
struct k_mem_slab *rx, *tx;
506+
struct net_buf_pool *rx_data, *tx_data;
507+
net_pkt_get_info(&rx, &tx, &rx_data, &tx_data);
513508
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(4, NULL));
514-
t->items[0] = MP_OBJ_NEW_SMALL_INT(rx->avail_count);
515-
t->items[1] = MP_OBJ_NEW_SMALL_INT(tx->avail_count);
509+
t->items[0] = MP_OBJ_NEW_SMALL_INT(k_mem_slab_num_free_get(rx));
510+
t->items[1] = MP_OBJ_NEW_SMALL_INT(k_mem_slab_num_free_get(tx));
516511
t->items[2] = MP_OBJ_NEW_SMALL_INT(rx_data->avail_count);
517512
t->items[3] = MP_OBJ_NEW_SMALL_INT(tx_data->avail_count);
518513
return MP_OBJ_FROM_PTR(t);
519514
}
520-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(nbuf_get_info_obj, nbuf_get_info);
515+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pkt_get_info_obj, pkt_get_info);
521516

522517
STATIC const mp_map_elem_t mp_module_usocket_globals_table[] = {
523518
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usocket) },
@@ -533,7 +528,7 @@ STATIC const mp_map_elem_t mp_module_usocket_globals_table[] = {
533528
{ MP_OBJ_NEW_QSTR(MP_QSTR_SOL_SOCKET), MP_OBJ_NEW_SMALL_INT(1) },
534529
{ MP_OBJ_NEW_QSTR(MP_QSTR_SO_REUSEADDR), MP_OBJ_NEW_SMALL_INT(2) },
535530

536-
{ MP_OBJ_NEW_QSTR(MP_QSTR_nbuf_get_info), (mp_obj_t)&nbuf_get_info_obj },
531+
{ MP_OBJ_NEW_QSTR(MP_QSTR_pkt_get_info), (mp_obj_t)&pkt_get_info_obj },
537532
};
538533

539534
STATIC MP_DEFINE_CONST_DICT(mp_module_usocket_globals, mp_module_usocket_globals_table);

0 commit comments

Comments
 (0)