• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1From d9ef907e03f44c30e26190b0c5ad895de716ac5c Mon Sep 17 00:00:00 2001
2From: kircher <majun65@huawei.com>
3Date: Fri, 12 May 2023 20:54:51 +0800
4Subject: [PATCH] add udp multicast in support
5
6---
7 src/api/api_msg.c          |  5 +++++
8 src/api/sockets.c          | 21 ++++++++++++++++++++-
9 src/core/dir.mk            |  2 +-
10 src/core/udp.c             | 28 +++++++++++++++++++++++++---
11 src/include/dpdk_cksum.h   |  4 ++++
12 src/include/lwip/opt.h     |  5 +++--
13 src/include/lwip/pbuf.h    |  4 ++++
14 src/include/lwip/sockets.h | 15 +++++++++++++++
15 src/include/lwipopts.h     |  2 +-
16 9 files changed, 78 insertions(+), 8 deletions(-)
17
18diff --git a/src/api/api_msg.c b/src/api/api_msg.c
19index 1840c9d..0287c06 100644
20--- a/src/api/api_msg.c
21+++ b/src/api/api_msg.c
22@@ -282,8 +282,13 @@ recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
23 #if LWIP_SO_RCVBUF
24     SYS_ARCH_INC(conn->recv_avail, len);
25 #endif /* LWIP_SO_RCVBUF */
26+#if GAZELLE_ENABLE
27+    add_recv_list(conn->socket);
28+    LWIP_UNUSED_ARG(len);
29+#else
30     /* Register event with callback */
31     API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
32+#endif
33   }
34 }
35 #endif /* LWIP_UDP */
36diff --git a/src/api/sockets.c b/src/api/sockets.c
37index 7a5da26..a0f9d50 100644
38--- a/src/api/sockets.c
39+++ b/src/api/sockets.c
40@@ -54,6 +54,7 @@
41 #include "lwip/netif.h"
42 #include "lwip/priv/tcpip_priv.h"
43 #include "lwip/mld6.h"
44+#include "lwip/api.h"
45 #if LWIP_CHECKSUM_ON_COPY
46 #include "lwip/inet_chksum.h"
47 #endif
48@@ -1187,7 +1188,7 @@ lwip_recv_tcp_done:
49 #endif
50
51 /* Convert a netbuf's address data to struct sockaddr */
52-static int
53+int
54 lwip_sock_make_addr(struct netconn *conn, ip_addr_t *fromaddr, u16_t port,
55                     struct sockaddr *from, socklen_t *fromlen)
56 {
57@@ -1274,6 +1275,7 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16
58     apiflags = 0;
59   }
60
61+#if !GAZELLE_ENABLE
62   LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom_udp_raw[UDP/RAW]: top sock->lastdata=%p\n", (void *)sock->lastdata.netbuf));
63   /* Check if there is data left from the last recv operation. */
64   buf = sock->lastdata.netbuf;
65@@ -1361,6 +1363,18 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16
66     sock->lastdata.netbuf = NULL;
67     netbuf_delete(buf);
68   }
69+#else /* GAZELLE_ENABLE */
70+  LWIP_UNUSED_ARG(copylen);
71+  LWIP_UNUSED_ARG(buf);
72+  LWIP_UNUSED_ARG(err);
73+  LWIP_UNUSED_ARG(copied);
74+  LWIP_UNUSED_ARG(i);
75+  buflen = read_lwip_data(sock, flags, apiflags);
76+  if (buflen <= 0) {
77+    return ERR_BUF;
78+  }
79+
80+#endif /* GAZELLE_ENABLE */
81   if (datagram_len) {
82     *datagram_len = buflen;
83   }
84@@ -1409,6 +1423,7 @@ lwip_recvfrom(int s, void *mem, size_t len, int flags,
85       done_socket(sock);
86       return -1;
87     }
88+
89     ret = (ssize_t)LWIP_MIN(LWIP_MIN(len, datagram_len), SSIZE_MAX);
90     if (fromlen) {
91       *fromlen = msg.msg_namelen;
92@@ -3956,6 +3971,10 @@ lwip_ioctl(int s, long cmd, ...)
93   struct lwip_sock *sock = posix_api->get_socket(s);
94   u8_t val;
95
96+#if LWIP_SO_RCVBUF
97+  int recv_avail;
98+#endif /* LWIP_SO_RCVBUF */
99+
100   int ret = -1;
101   void *argp;
102   va_list ap;
103diff --git a/src/core/dir.mk b/src/core/dir.mk
104index ebc01a5..57a9670 100644
105--- a/src/core/dir.mk
106+++ b/src/core/dir.mk
107@@ -1,6 +1,6 @@
108 SRC = def.c inet_chksum.c init.c ip.c mem.c memp.c netif.c pbuf.c \
109 	  raw.c tcp.c tcp_in.c tcp_out.c timeouts.c udp.c stats.c\
110 	  ipv4/icmp.c ipv4/ip4_addr.c ipv4/ip4_frag.c ipv4/etharp.c \
111-	  ipv4/ip4.c
112+	  ipv4/ip4.c ipv4/igmp.c
113
114 $(eval $(call register_dir, core, $(SRC)))
115diff --git a/src/core/udp.c b/src/core/udp.c
116index a5f76b9..1398537 100644
117--- a/src/core/udp.c
118+++ b/src/core/udp.c
119@@ -65,6 +65,12 @@
120
121 #include <string.h>
122
123+#if GAZELLE_ENABLE
124+#include "lwipsock.h"
125+#include <rte_prefetch.h>
126+#include "dpdk_cksum.h"
127+#endif
128+
129 #ifndef UDP_LOCAL_PORT_RANGE_START
130 /* From http://www.iana.org/assignments/port-numbers:
131    "The Dynamic and/or Private Ports are those from 49152 through 65535" */
132@@ -210,7 +216,7 @@ udp_input(struct pbuf *p, struct netif *inp)
133 #if LWIP_RECORD_PERF
134   PERF_START(PERF_LAYER_UDP, PERF_POINT_UDP);
135 #else
136-  PERF_START;
137+  //PERF_START;
138 #endif
139
140   UDP_STATS_INC(udp.recv);
141@@ -435,7 +441,7 @@ end:
142 #if LWIP_RECORD_PERF
143   PERF_STOP_INCREASE_COUNT("udp_input", PERF_LAYER_UDP);
144 #else
145-  PERF_STOP("udp_input");
146+  //PERF_STOP("udp_input");
147 #endif
148
149   return;
150@@ -451,7 +457,7 @@ chkerr:
151 #if LWIP_RECORD_PERF
152   PERF_STOP_INCREASE_COUNT("udp_input", PERF_LAYER_UDP);
153 #else
154-  PERF_STOP("udp_input");
155+  //PERF_STOP("udp_input");
156 #endif
157
158 #endif /* CHECKSUM_CHECK_UDP */
159@@ -608,11 +614,26 @@ udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
160     UDP_STATS_INC(udp.rterr);
161     return ERR_RTE;
162   }
163+  uint8_t apiflags = 0;
164+
165+  struct pbuf *udp_pbuf = write_lwip_data((struct lwip_sock *)(p->payload), p->tot_len, &apiflags);
166+  write_lwip_over((struct lwip_sock *)(p->payload));
167+
168+  pbuf_free(p);
169+  p = udp_pbuf;
170+  if (p == NULL) {
171+    return ERR_MEM;
172+  }
173+
174+  if (p->port) {
175+    return udp_sendto_if(pcb, p, &(p->addr), p->port, netif);
176+  } else {
177 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
178   return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum);
179 #else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
180   return udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
181 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
182+  }
183 }
184
185 /**
186@@ -905,6 +926,7 @@ udp_sendto_if_src_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *d
187   LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,0x%02"X16_F",)\n", (u16_t)ip_proto));
188   /* output to IP */
189   NETIF_SET_HINTS(netif, &(pcb->netif_hints));
190+  udph_cksum_set(q, UDP_HLEN);
191   err = ip_output_if_src(q, src_ip, dst_ip, ttl, pcb->tos, ip_proto, netif);
192   NETIF_RESET_HINTS(netif);
193
194diff --git a/src/include/dpdk_cksum.h b/src/include/dpdk_cksum.h
195index df2e2a5..e41644b 100644
196--- a/src/include/dpdk_cksum.h
197+++ b/src/include/dpdk_cksum.h
198@@ -82,6 +82,10 @@ static inline void tcph_cksum_set(struct pbuf *p, u16_t len) {
199     p->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
200 }
201
202+static inline void udph_cksum_set(struct pbuf *p, u16_t len) {
203+    p->l4_len = len;
204+}
205+
206 static inline u16_t ip_chksum_pseudo_offload(u8_t proto, u16_t proto_len,
207                                              const ip_addr_t *src, const ip_addr_t *dst)
208 {
209diff --git a/src/include/lwip/opt.h b/src/include/lwip/opt.h
210index 0376f60..38c6e9b 100644
211--- a/src/include/lwip/opt.h
212+++ b/src/include/lwip/opt.h
213@@ -133,6 +133,7 @@
214  * MEMCPY: override this if you have a faster implementation at hand than the
215  * one included in your C library
216  */
217+//#include <rte_memcpy.h>
218 #if !defined MEMCPY || defined __DOXYGEN__
219 #define MEMCPY(dst,src,len)             memcpy(dst,src,len)
220 #endif
221@@ -1083,7 +1084,7 @@
222  * LWIP_IGMP==1: Turn on IGMP module.
223  */
224 #if !defined LWIP_IGMP || defined __DOXYGEN__
225-#define LWIP_IGMP                       0
226+#define LWIP_IGMP                       1
227 #endif
228 #if !LWIP_IPV4
229 #undef LWIP_IGMP
230@@ -2030,7 +2031,7 @@
231  * LWIP_SO_RCVBUF==1: Enable SO_RCVBUF processing.
232  */
233 #if !defined LWIP_SO_RCVBUF || defined __DOXYGEN__
234-#define LWIP_SO_RCVBUF                  0
235+#define LWIP_SO_RCVBUF                  1
236 #endif
237
238 /**
239diff --git a/src/include/lwip/pbuf.h b/src/include/lwip/pbuf.h
240index 9321afc..fb21134 100644
241--- a/src/include/lwip/pbuf.h
242+++ b/src/include/lwip/pbuf.h
243@@ -40,6 +40,8 @@
244
245 #include "lwip/opt.h"
246 #include "lwip/err.h"
247+#include "lwip/ip_addr.h"
248+#include "lwip/ip6_addr.h"
249
250 #ifdef __cplusplus
251 extern "C" {
252@@ -236,6 +238,8 @@ struct pbuf {
253   struct pbuf *last;
254   pthread_spinlock_t pbuf_lock;
255   struct tcp_pcb *pcb;
256+  ip_addr_t addr;
257+  u16_t port;
258 #endif /* GAZELLE_ENABLE CHECKSUM_OFFLOAD_SWITCH */
259
260   /** In case the user needs to store data custom data on a pbuf */
261diff --git a/src/include/lwip/sockets.h b/src/include/lwip/sockets.h
262index 58acf0f..36a47eb 100644
263--- a/src/include/lwip/sockets.h
264+++ b/src/include/lwip/sockets.h
265@@ -48,6 +48,7 @@
266 #include "lwip/err.h"
267 #include "lwip/inet.h"
268 #include "lwip/errno.h"
269+#include "lwip/api.h"
270
271 #include <string.h>
272
273@@ -323,20 +324,31 @@ struct linger {
274
275
276 #if LWIP_MULTICAST_TX_OPTIONS
277+#if GAZELLE_ENABLE
278+#define IP_MULTICAST_IF 32
279+#define IP_MULTICAST_TTL 33
280+#define IP_MULTICAST_LOOP 34
281+#else
282 /*
283  * Options and types for UDP multicast traffic handling
284  */
285 #define IP_MULTICAST_TTL   5
286 #define IP_MULTICAST_IF    6
287 #define IP_MULTICAST_LOOP  7
288+#endif /* GAZELLE_ENABLE */
289 #endif /* LWIP_MULTICAST_TX_OPTIONS */
290
291 #if LWIP_IGMP
292+#if GAZELLE_ENABLE
293+#define IP_ADD_MEMBERSHIP  35
294+#define IP_DROP_MEMBERSHIP 36
295+#else
296 /*
297  * Options and types related to multicast membership
298  */
299 #define IP_ADD_MEMBERSHIP  3
300 #define IP_DROP_MEMBERSHIP 4
301+#endif /* GAZELLE_ENABLE */
302
303 typedef struct ip_mreq {
304     struct in_addr imr_multiaddr; /* IP multicast address of group */
305@@ -656,6 +668,7 @@ ssize_t lwip_sendto(int s, const void *dataptr, size_t size, int flags,
306 int lwip_socket(int domain, int type, int protocol);
307 ssize_t lwip_write(int s, const void *dataptr, size_t size);
308 ssize_t lwip_writev(int s, const struct iovec *iov, int iovcnt);
309+
310 #if LWIP_SOCKET_SELECT
311 int lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
312                 struct timeval *timeout);
313@@ -667,6 +680,8 @@ int lwip_poll(struct pollfd *fds, nfds_t nfds, int timeout);
314 #if GAZELLE_ENABLE
315 int lwip_ioctl(int s, long cmd, ...);
316 int lwip_fcntl(int s, int cmd, int val);
317+int lwip_sock_make_addr(struct netconn *conn, ip_addr_t *fromaddr, u16_t port,
318+                    struct sockaddr *from, socklen_t *fromlen);
319 #else
320 int lwip_ioctl(int s, long cmd, void *argp);
321 int lwip_fcntl(int s, int cmd, int val);
322diff --git a/src/include/lwipopts.h b/src/include/lwipopts.h
323index 0d2a6d9..bcb0879 100644
324--- a/src/include/lwipopts.h
325+++ b/src/include/lwipopts.h
326@@ -175,7 +175,7 @@
327    ---------- UDP options ----------
328    ---------------------------------
329 */
330-#define LWIP_UDP 0
331+#define LWIP_UDP 1
332
333
334 /*
335--
3362.33.0
337
338