#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 */
--- /dev/null
+#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__ */
-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)
--- /dev/null
+#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;
+}