]> git.corax.cc Git - corax/commitdiff
Add mbuf type and functions to allocate and free them
authorMatthias Kruk <m@m10k.eu>
Fri, 25 Oct 2019 06:45:33 +0000 (15:45 +0900)
committerMatthias Kruk <m@m10k.eu>
Fri, 25 Oct 2019 06:45:33 +0000 (15:45 +0900)
config.h
include/sys/mbuf.h [new file with mode: 0644]
kernel/core/Makefile
kernel/core/mbuf.c [new file with mode: 0644]

index 502a336f359e10f3a39864ec11fb3d4a5f2d2c80..04b15c52fe89d4827b85e3d9e8558e1b3b4552c9 100644 (file)
--- a/config.h
+++ b/config.h
@@ -41,7 +41,8 @@
 #define CONFIG_DEBUG_SCHED 0
 #define CONFIG_DEBUG_NET   1
 
-#define CONFIG_SOCKET_MAX  1024
+#define CONFIG_SOCKET_MAX    1024
+#define CONFIG_NET_MBUF_SIZE 2048
 
 /* sanity checks */
 
diff --git a/include/sys/mbuf.h b/include/sys/mbuf.h
new file mode 100644 (file)
index 0000000..1472e32
--- /dev/null
@@ -0,0 +1,42 @@
+#ifndef __CORAX_MBUF_H__
+#define __CORAX_MBUF_H__
+
+#include <config.h>
+
+struct mbuf {
+       struct mbuf *m_next;
+       struct mbuf *m_nextpkt;
+       void        *m_data;
+       u16_t       m_len;
+       u8_t        m_type;
+       u8_t        m_flags;
+
+       union {
+               struct {
+                       void *MH_rcvif;
+                       u16_t MH_len;
+                       u32_t MH_csum_flags;
+                       u32_t MH_csum_data;
+               } M_hdr;
+               u8_t        M_databuf[CONFIG_NET_MBUF_SIZE];
+       } M_dat;
+} __attribute__((packed));
+
+#define m_rcvif     M_dat.M_hdr.MH_rcvif
+#define m_pktlen    M_dat.M_hdr.MH_len
+#define m_csumflags M_dat.M_hdr.MH_csum_flags
+#define m_csumdata  M_dat.M_hdr.MH_csum_data
+
+#define MT_NOTMBUF    0
+#define MT_DATA       1
+#define MT_HEADER     2
+#define MT_SONAME     3
+#define MT_CONTROL    4
+#define MT_EXTCONTROL 5
+#define MT_OOBDATA    6
+
+
+struct mbuf* mbuf_alloc(void);
+void mbuf_free(struct mbuf*);
+
+#endif /* __CORAX_MBUF_H__ */
index b9a74e034bbc7b45dbf8f330c2b7c34bb4e0a8df..ef8b7c5e9fe19f6a455349c1a14514506ef17a4c 100644 (file)
@@ -1,4 +1,4 @@
-OBJECTS = main.o process.o sched.o cxnet.o socket.o
+OBJECTS = main.o process.o sched.o cxnet.o socket.o mbuf.o
 OUTPUT = core.o
 INCLUDES = -I../include -I../../include -I../..
 CFLAGS += $(INCLUDES)
diff --git a/kernel/core/mbuf.c b/kernel/core/mbuf.c
new file mode 100644 (file)
index 0000000..c479beb
--- /dev/null
@@ -0,0 +1,61 @@
+#include <config.h>
+#include <corax/types.h>
+#include <corax/errno.h>
+#include <sys/mbuf.h>
+#include <debug.h>
+#define ALLOC_SIZE 128
+
+static struct mbuf *_unused;
+
+static struct mbuf* _alloc_mbufs(void)
+{
+       struct mbuf *m;
+
+       m = kmalloc(sizeof(*m) * ALLOC_SIZE);
+
+       if(m) {
+               int n;
+
+               /* skip the last mbuf so its m_next member remains NULL */
+               for(n = 0; n < (ALLOC_SIZE - 1); n++) {
+                       m[n].m_next = m + 1;
+               }
+       }
+
+       return(m);
+}
+
+struct mbuf* mbuf_alloc(void)
+{
+       struct mbuf *m;
+
+       if(!_unused) {
+               /* need to allocate more mbufs */
+               _unused = _alloc_mbufs();
+       }
+
+       m = _unused;
+
+       if(m) {
+               _unused = m->m_next;
+
+               /*
+                * Clear the mbuf's m_next member so the caller can't access more
+                * mbufs than they're supposed to
+                */
+               m->m_next = NULL;
+       }
+
+       return(m);
+}
+
+void mbuf_free(struct mbuf *m)
+{
+       /* clear the mbuf and prepend it to the list of unused mbufs */
+       memset(m, 0, sizeof(*m));
+
+       m->m_next = _unused;
+       _unused = m;
+
+       return;
+}