Skip to contentSkip to conversation

mmaped netlink

PATCH In review Ken-ichirou MATSUZAWA 1 participant 2 messages Dec 7, 2013 – Jan 1, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover mmaped netlink

Hello Pablo, list

# First of all, you will have to excuse my poor English, sorry

I started implementing mmaped netlink socket in libmnl. Would you review the code? differ from original/nl-mmap, I added new struct mnl_ring_socket wrapping mnl_socket and new file mmap.c. Patches are divided into 4 parts.

part1: [2, 3] resemble to original/nl-mmap
part2: [4, 5] add helper
part3: [6, 7] add more helper
part4: [8, 9] add new batch function for mmap

Theses can be seen at https://github.com/chamaken/libmnl/tree/mmap-sbs too.

I have no experience using libmnl, netlink then do not know what is suitable for minimalistic. And I doubt part2 - 4 is suitable only for examples. Any advice is welcome.

thanks,

#1 lore raw

libmnl nl-mmap patch review request

Hello Pablo, all.
# May you all have a wonderful new year

I found original nl-mmap branch is probably enough and follow them. Would you revew my patch?

Two things I've changed to nl-mmap. One is holding frame params for each of TX and RX ring. Another is adding two functions instead of mnl_socket_set_ring() - mnl_socket_set_ringopt() and mnl_socket_map_ring(). I think it's difficult to notify what's wrong on error in one function.

Any advice is welcome, thanks.

---

 include/libmnl/libmnl.h |  13 ++++
 include/linux/netlink.h |  40 +++++++++-
 src/libmnl.map          |   6 +-
 src/nlmsg.c             |  29 ++++++++
 src/socket.c            | 193 ++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 276 insertions(+), 5 deletions(-)
include/libmnl/libmnl.h +13 -0
index 223709c..348cd3c 100644
@@ -21,8 +21,20 @@ extern "C" {
 
 struct mnl_socket;
 
+enum mnl_ring_types {
+	MNL_RING_RX,
+	MNL_RING_TX,
+};
+
+#define MNL_RING_MSGHDR(hdr) ((void *)(hdr) + NL_MMAP_HDRLEN)
+
 extern struct mnl_socket *mnl_socket_open(int type);
 extern int mnl_socket_bind(struct mnl_socket *nl, unsigned int groups, pid_t pid);
+extern int mnl_socket_set_ringopt(struct mnl_socket *nl, struct nl_mmap_req *req,
+				  enum mnl_ring_types type);
+extern int mnl_socket_map_ring(struct mnl_socket *nl);
+extern struct nl_mmap_hdr *mnl_socket_get_frame(const struct mnl_socket *nl, enum mnl_ring_types type);
+extern int mnl_socket_advance_ring(const struct mnl_socket *nl, enum mnl_ring_types type);
 extern int mnl_socket_close(struct mnl_socket *nl);
 extern int mnl_socket_get_fd(const struct mnl_socket *nl);
 extern unsigned int mnl_socket_get_portid(const struct mnl_socket *nl);
@@ -74,6 +86,7 @@ extern void mnl_nlmsg_batch_reset(struct mnl_nlmsg_batch *b);
 extern void *mnl_nlmsg_batch_head(struct mnl_nlmsg_batch *b);
 extern void *mnl_nlmsg_batch_current(struct mnl_nlmsg_batch *b);
 extern bool mnl_nlmsg_batch_is_empty(struct mnl_nlmsg_batch *b);
+extern void mnl_nlmsg_batch_reset_buffer(struct mnl_nlmsg_batch *b, void *buf, size_t limit);
 
 /*
  * Netlink attributes API
include/linux/netlink.h +36 -4
index ced0e1a..1a85940 100644
@@ -1,6 +1,7 @@
-#ifndef __LINUX_NETLINK_H
-#define __LINUX_NETLINK_H
+#ifndef _UAPI__LINUX_NETLINK_H
+#define _UAPI__LINUX_NETLINK_H
 
+#include <linux/kernel.h>
 #include <linux/socket.h> /* for __kernel_sa_family_t */
 #include <linux/types.h>
 
@@ -78,7 +79,7 @@ struct nlmsghdr {
 #define NLMSG_ALIGNTO	4U
 #define NLMSG_ALIGN(len) ( ((len)+NLMSG_ALIGNTO-1) & ~(NLMSG_ALIGNTO-1) )
 #define NLMSG_HDRLEN	 ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr)))
-#define NLMSG_LENGTH(len) ((len)+NLMSG_ALIGN(NLMSG_HDRLEN))
+#define NLMSG_LENGTH(len) ((len) + NLMSG_HDRLEN)
 #define NLMSG_SPACE(len) NLMSG_ALIGN(NLMSG_LENGTH(len))
 #define NLMSG_DATA(nlh)  ((void*)(((char*)nlh) + NLMSG_LENGTH(0)))
 #define NLMSG_NEXT(nlh,len)	 ((len) -= NLMSG_ALIGN((nlh)->nlmsg_len), \
@@ -105,11 +106,42 @@ struct nlmsgerr {
 #define NETLINK_PKTINFO		3
 #define NETLINK_BROADCAST_ERROR	4
 #define NETLINK_NO_ENOBUFS	5
+#define NETLINK_RX_RING		6
+#define NETLINK_TX_RING		7
 
 struct nl_pktinfo {
 	__u32	group;
 };
 
+struct nl_mmap_req {
+	unsigned int	nm_block_size;
+	unsigned int	nm_block_nr;
+	unsigned int	nm_frame_size;
+	unsigned int	nm_frame_nr;
+};
+
+struct nl_mmap_hdr {
+	unsigned int	nm_status;
+	unsigned int	nm_len;
+	__u32		nm_group;
+	/* credentials */
+	__u32		nm_pid;
+	__u32		nm_uid;
+	__u32		nm_gid;
+};
+
+enum nl_mmap_status {
+	NL_MMAP_STATUS_UNUSED,
+	NL_MMAP_STATUS_RESERVED,
+	NL_MMAP_STATUS_VALID,
+	NL_MMAP_STATUS_COPY,
+	NL_MMAP_STATUS_SKIP,
+};
+
+#define NL_MMAP_MSG_ALIGNMENT		NLMSG_ALIGNTO
+#define NL_MMAP_MSG_ALIGN(sz)		__ALIGN_KERNEL(sz, NL_MMAP_MSG_ALIGNMENT)
+#define NL_MMAP_HDRLEN			NL_MMAP_MSG_ALIGN(sizeof(struct nl_mmap_hdr))
+
 #define NET_MAJOR 36		/* Major 36 is reserved for networking 						*/
 
 enum {
@@ -150,4 +182,4 @@ struct nlattr {
 #define NLA_HDRLEN		((int) NLA_ALIGN(sizeof(struct nlattr)))
 
 
-#endif /* __LINUX_NETLINK_H */
+#endif /* _UAPI__LINUX_NETLINK_H */
src/libmnl.map +5 -1
index dbc332e..26269c2 100644
@@ -65,10 +65,14 @@ global:
   mnl_socket_recvfrom;
   mnl_socket_sendto;
   mnl_socket_setsockopt;
-
 local: *;
 };
 
 LIBMNL_1.1 {
   mnl_attr_parse_payload;
+  mnl_socket_set_ringopt;
+  mnl_socket_map_ring;
+  mnl_socket_get_frame;
+  mnl_socket_advance_ring;
+  mnl_nlmsg_batch_reset_buffer;
 } LIBMNL_1.0;
src/nlmsg.c +29 -0
index fdb7af8..5dbe1ba 100644
@@ -569,5 +569,34 @@ bool mnl_nlmsg_batch_is_empty(struct mnl_nlmsg_batch *b)
 EXPORT_SYMBOL(mnl_nlmsg_batch_is_empty);
 
 /**
+ * mnl_nlmsg_batch_reset_buffer - reset to the new buffer
+ * \param buf pointer to the new (mmaped tx frame) buffer that will store this
+ *        batch
+ * \param limit maximum size of the batch (should be half of
+ *        nl_mmap_req.frame_size)
+ *
+ * This function is for mmap tx frame, allows to set new buffer (frame) and
+ * reset a batch, so you can reuse it to create a new one. This function moves
+ * the last message which does not fit the batch to the head of the new buffer,
+ * if any.
+ */
+void mnl_nlmsg_batch_reset_buffer(struct mnl_nlmsg_batch *b, void *buf, size_t limit)
+{
+	if (b->overflow) {
+		struct nlmsghdr *nlh = b->cur;
+		memcpy(buf, b->cur, nlh->nlmsg_len);
+		b->buflen = nlh->nlmsg_len;
+		b->cur = buf + b->buflen;
+		b->overflow = false;
+	} else {
+		b->buflen = 0;
+		b->cur = buf;
+	}
+	b->buf = buf;
+	b->limit = limit;
+}
+EXPORT_SYMBOL(mnl_nlmsg_batch_reset_buffer);
+
+/**
  * @}
  */
src/socket.c +193 -0

220 lines not on this page · raw message