Skip to contentSkip to conversation

Simple ARP table

PATCH Superseded Fabien Chouteau 3 participants 10 messages Jul 29, 2011 – Aug 1, 2011 Kbuild

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 Simple ARP table

This patch adds a simple ARP table in Slirp and also adds handling of gratuitous ARP requests.

---
 Makefile.objs     |    2 +-
 slirp/arp_table.c |   50 ++++++++++++++++++++++++++++++++++++++++++
 slirp/bootp.c     |   23 ++++++++++++------
 slirp/slirp.c     |   63 +++++++++++++---------------------------------------
 slirp/slirp.h     |   50 +++++++++++++++++++++++++++++++++++++++--
 5 files changed, 129 insertions(+), 59 deletions(-)
 create mode 100644 slirp/arp_table.c
Makefile.objs +1 -1
index 6991a9f..0c10557 100644
@@ -151,7 +151,7 @@ common-obj-y += qemu-timer.o qemu-timer-common.o
 
 slirp-obj-y = cksum.o if.o ip_icmp.o ip_input.o ip_output.o
 slirp-obj-y += slirp.o mbuf.o misc.o sbuf.o socket.o tcp_input.o tcp_output.o
-slirp-obj-y += tcp_subr.o tcp_timer.o udp.o bootp.o tftp.o
+slirp-obj-y += tcp_subr.o tcp_timer.o udp.o bootp.o tftp.o arp_table.o
 common-obj-$(CONFIG_SLIRP) += $(addprefix slirp/, $(slirp-obj-y))
 
 # xen backend driver support
slirp/arp_table.c new file +50 -0

50 lines not on this page · raw message

slirp/bootp.c +15 -8
index 1eb2ed1..07cbb80 100644
@@ -149,6 +149,7 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
     struct in_addr preq_addr;
     int dhcp_msg_type, val;
     uint8_t *q;
+    uint8_t client_ethaddr[ETH_ALEN];
 
     /* extract exact DHCP msg type */
     dhcp_decode(bp, &dhcp_msg_type, &preq_addr);
@@ -164,8 +165,9 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
     if (dhcp_msg_type != DHCPDISCOVER &&
         dhcp_msg_type != DHCPREQUEST)
         return;
-    /* XXX: this is a hack to get the client mac address */
-    memcpy(slirp->client_ethaddr, bp->bp_hwaddr, 6);
+
+    /* Get client's hardware address from bootp request */
+    memcpy(client_ethaddr, bp->bp_hwaddr, ETH_ALEN);
 
     m = m_get(slirp);
     if (!m) {
@@ -178,25 +180,25 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
 
     if (dhcp_msg_type == DHCPDISCOVER) {
         if (preq_addr.s_addr != htonl(0L)) {
-            bc = request_addr(slirp, &preq_addr, slirp->client_ethaddr);
+            bc = request_addr(slirp, &preq_addr, client_ethaddr);
             if (bc) {
                 daddr.sin_addr = preq_addr;
             }
         }
         if (!bc) {
          new_addr:
-            bc = get_new_addr(slirp, &daddr.sin_addr, slirp->client_ethaddr);
+            bc = get_new_addr(slirp, &daddr.sin_addr, client_ethaddr);
             if (!bc) {
                 DPRINTF("no address left\n");
                 return;
             }
         }
-        memcpy(bc->macaddr, slirp->client_ethaddr, 6);
+        memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
     } else if (preq_addr.s_addr != htonl(0L)) {
-        bc = request_addr(slirp, &preq_addr, slirp->client_ethaddr);
+        bc = request_addr(slirp, &preq_addr, client_ethaddr);
         if (bc) {
             daddr.sin_addr = preq_addr;
-            memcpy(bc->macaddr, slirp->client_ethaddr, 6);
+            memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
         } else {
             daddr.sin_addr.s_addr = 0;
         }
@@ -209,6 +211,11 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
         }
     }
 
+    if (daddr.sin_addr.s_addr != 0) {
+        /* Update ARP table for this IP address */
+        arp_table_add(&slirp->arp_table, daddr.sin_addr.s_addr, client_ethaddr);
+    }
+
     saddr.sin_addr = slirp->vhost_addr;
     saddr.sin_port = htons(BOOTP_SERVER);
 
@@ -218,7 +225,7 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
     rbp->bp_xid = bp->bp_xid;
     rbp->bp_htype = 1;
     rbp->bp_hlen = 6;
-    memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
+    memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, ETH_ALEN);
 
     rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
     rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
slirp/slirp.c +16 -47

116 lines not on this page · raw message

slirp/slirp.h +47 -3

68 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#1 lore raw

PATCH v2 2/2 Delayed IP packets

In the current implementation, if Slirp tries to send an IP packet to a client with an unknown hardware address, the packet is simply dropped and an ARP request is sent (if_encap in slirp/slirp.c).

With this patch, Slirp will send the ARP request, re-queue the packet and try to send it later. The packet is dropped after one second if the ARP reply is not received.

---
 slirp/if.c    |   28 ++++++++++++++++++++---
 slirp/main.h  |    2 +-
 slirp/mbuf.c  |    2 +
 slirp/mbuf.h  |    2 +
 slirp/slirp.c |   67 ++++++++++++++++++++++++++++++--------------------------
 slirp/slirp.h |   15 ++++++++++++
 6 files changed, 80 insertions(+), 36 deletions(-)
slirp/if.c +24 -4
index 0f04e13..80a5c4e 100644
@@ -6,6 +6,7 @@
  */
 
 #include <slirp.h>
+#include "qemu-timer.h"
 
 #define ifs_init(ifm) ((ifm)->ifs_next = (ifm)->ifs_prev = (ifm))
 
@@ -105,6 +106,9 @@ if_output(struct socket *so, struct mbuf *ifm)
 	ifs_init(ifm);
 	insque(ifm, ifq);
 
+        /* Expiration date = Now + 1 second */
+        ifm->expiration_date = qemu_get_clock_ns(vm_clock) + 1000000000ULL;
+
 diddit:
 	slirp->if_queued++;
 
@@ -153,6 +157,9 @@ diddit:
 void
 if_start(Slirp *slirp)
 {
+    int requeued = 0;
+    uint64_t now;
+
 	struct mbuf *ifm, *ifqt;
 
 	DEBUG_CALL("if_start");
@@ -165,6 +172,8 @@ if_start(Slirp *slirp)
         if (!slirp_can_output(slirp->opaque))
             return;
 
+        now = qemu_get_clock_ns(vm_clock);
+
 	/*
 	 * See which queue to get next packet from
 	 * If there's something in the fastq, select it immediately
@@ -199,11 +208,22 @@ if_start(Slirp *slirp)
 		   ifm->ifq_so->so_nqueued = 0;
 	}
 
-	/* Encapsulate the packet for sending */
-        if_encap(slirp, (uint8_t *)ifm->m_data, ifm->m_len);
-
-        m_free(ifm);
+        if (ifm->expiration_date < now) {
+            /* Expired */
+            m_free(ifm);
+        } else {
+            /* Encapsulate the packet for sending */
+            if (if_encap(slirp, ifm)) {
+                m_free(ifm);
+            } else {
+                /* re-queue */
+                insque(ifm, ifqt);
+                requeued++;
+            }
+        }
 
 	if (slirp->if_queued)
 	   goto again;
+
+        slirp->if_queued = requeued;
 }
slirp/main.h +1 -1
index 0dd8d81..028df4b 100644
@@ -42,5 +42,5 @@ extern int tcp_keepintvl;
 #define PROTO_PPP 0x2
 #endif
 
-void if_encap(Slirp *slirp, const uint8_t *ip_data, int ip_data_len);
+int if_encap(Slirp *slirp, struct mbuf *ifm);
 ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags);
slirp/mbuf.c +2 -0
index ce2eb84..c699c75 100644
@@ -70,6 +70,8 @@ m_get(Slirp *slirp)
 	m->m_len = 0;
         m->m_nextpkt = NULL;
         m->m_prevpkt = NULL;
+        m->arp_requested = false;
+        m->expiration_date = (uint64_t)-1;
 end_error:
 	DEBUG_ARG("m = %lx", (long )m);
 	return m;
slirp/mbuf.h +2 -0
index b74544b..55170e5 100644
@@ -86,6 +86,8 @@ struct mbuf {
 		char	m_dat_[1]; /* ANSI don't like 0 sized arrays */
 		char	*m_ext_;
 	} M_dat;
+    bool     arp_requested;
+    uint64_t expiration_date;
 };
 
 #define m_next		m_hdr.mh_next
slirp/slirp.c +36 -31

96 lines not on this page · raw message

slirp/slirp.h +15 -0
index 103b39d..07d7d29 100644
@@ -216,6 +216,15 @@ bool arp_table_search(ArpTable *arptbl,
                       int       in_ip_addr,
                       uint8_t out_ethaddr[ETH_ALEN]);
 
+struct DelayedIpPacket;
+typedef struct DelayedIpPacket DelayedIpPacket;
+
+struct DelayedIpPacket {
+    uint8_t         *data;
+    int              len;
+    DelayedIpPacket *next;
+};
+
 struct Slirp {
     QTAILQ_ENTRY(Slirp) entry;
 
@@ -271,6 +280,12 @@ struct Slirp {
 
     ArpTable arp_table;
 
+    /* List of delayed IP packets.
+     * These packets are delayed because the target hw address is unknown. Slirp
+     * will try to send them each time the arp table is updated.
+     */
+    DelayedIpPacket *delayed;
+
     void *opaque;
 };
 
Signature · 1 line
1.7.4.1
#2 lore raw

Re: [Qemu-devel] [PATCH V2 2/2] [SLIRP] Delayed IP packets

Quoted 31 lines · Fabien Chouteau

In the current implementation, if Slirp tries to send an IP packet to a client with an unknown hardware address, the packet is simply dropped and an ARP request is sent (if_encap in slirp/slirp.c).

With this patch, Slirp will send the ARP request, re-queue the packet and try to send it later. The packet is dropped after one second if the ARP reply is not received.

---
 slirp/if.c    |   28 ++++++++++++++++++++---
 slirp/main.h  |    2 +-
 slirp/mbuf.c  |    2 +
 slirp/mbuf.h  |    2 +
 slirp/slirp.c |   67 ++++++++++++++++++++++++++++++--------------------------
 slirp/slirp.h |   15 ++++++++++++
 6 files changed, 80 insertions(+), 36 deletions(-)
slirp/if.c +1 -0
index 0f04e13..80a5c4e 100644
@@ -6,6 +6,7 @@
  */
 
 #include <slirp.h>
+#include "qemu-timer.h"
 
 #define ifs_init(ifm) ((ifm)->ifs_next = (ifm)->ifs_prev = (ifm))
 

slirp/if.c if_output(struct socket *so, struct mbuf *ifm) · line 110

Quoted 5 lines
slirp/if.c +2 -0
@@ -105,6 +106,9 @@ if_output(struct socket *so, struct mbuf *ifm)
 	ifs_init(ifm);
 	insque(ifm, ifq);
 
+        /* Expiration date = Now + 1 second */
+        ifm->expiration_date = qemu_get_clock_ns(vm_clock) + 1000000000ULL;

Slirp uses rt_clock for expiry, let's stick with that clock.

Quoted 107 lines
slirp/if.c +21 -4
+
 diddit:
 	slirp->if_queued++;
 
@@ -153,6 +157,9 @@ diddit:
 void
 if_start(Slirp *slirp)
 {
+    int requeued = 0;
+    uint64_t now;
+
 	struct mbuf *ifm, *ifqt;
 
 	DEBUG_CALL("if_start");
@@ -165,6 +172,8 @@ if_start(Slirp *slirp)
         if (!slirp_can_output(slirp->opaque))
             return;
 
+        now = qemu_get_clock_ns(vm_clock);
+
 	/*
 	 * See which queue to get next packet from
 	 * If there's something in the fastq, select it immediately
@@ -199,11 +208,22 @@ if_start(Slirp *slirp)
 		   ifm->ifq_so->so_nqueued = 0;
 	}
 
-	/* Encapsulate the packet for sending */
-        if_encap(slirp, (uint8_t *)ifm->m_data, ifm->m_len);
-
-        m_free(ifm);
+        if (ifm->expiration_date < now) {
+            /* Expired */
+            m_free(ifm);
+        } else {
+            /* Encapsulate the packet for sending */
+            if (if_encap(slirp, ifm)) {
+                m_free(ifm);
+            } else {
+                /* re-queue */
+                insque(ifm, ifqt);
+                requeued++;
+            }
+        }
 
 	if (slirp->if_queued)
 	   goto again;
+
+        slirp->if_queued = requeued;
 }
slirp/main.h +1 -1
index 0dd8d81..028df4b 100644
@@ -42,5 +42,5 @@ extern int tcp_keepintvl;
 #define PROTO_PPP 0x2
 #endif
 
-void if_encap(Slirp *slirp, const uint8_t *ip_data, int ip_data_len);
+int if_encap(Slirp *slirp, struct mbuf *ifm);
 ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags);
slirp/mbuf.c +2 -0
index ce2eb84..c699c75 100644
@@ -70,6 +70,8 @@ m_get(Slirp *slirp)
 	m->m_len = 0;
         m->m_nextpkt = NULL;
         m->m_prevpkt = NULL;
+        m->arp_requested = false;
+        m->expiration_date = (uint64_t)-1;
 end_error:
 	DEBUG_ARG("m = %lx", (long )m);
 	return m;
slirp/mbuf.h +2 -0
index b74544b..55170e5 100644
@@ -86,6 +86,8 @@ struct mbuf {
 		char	m_dat_[1]; /* ANSI don't like 0 sized arrays */
 		char	*m_ext_;
 	} M_dat;
+    bool     arp_requested;
+    uint64_t expiration_date;
 };
 
 #define m_next		m_hdr.mh_next
slirp/slirp.c +3 -1
index ba76757..b006620 100644
@@ -237,6 +237,8 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
 
     QTAILQ_INSERT_TAIL(&slirp_instances, slirp, entry);
 
+    slirp->delayed = NULL;
+
     return slirp;
 }
 
@@ -693,54 +695,57 @@ void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
 }
 
 /* output the IP packet to the ethernet device */
-void if_encap(Slirp *slirp, const uint8_t *ip_data, int ip_data_len)
+int if_encap(Slirp *slirp, struct mbuf *ifm)
 {

slirp/slirp.c void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) · line 706

Quoted 10 lines
slirp/slirp.c +3 -3
     uint8_t buf[1600];
     struct ethhdr *eh = (struct ethhdr *)buf;
     uint8_t ethaddr[ETH_ALEN];
-    const struct ip *iph = (const struct ip *)ip_data;
+    const struct ip *iph = (const struct ip *)ifm->m_data;
 
-    if (ip_data_len + ETH_HLEN > sizeof(buf))
-        return;
+    if (ifm->m_len + ETH_HLEN > sizeof(buf))
+        return 1;

Even if the old cold lacked them as well: { }

Quoted 23 lines
slirp/slirp.c +0 -17
     if (!arp_table_search(&slirp->arp_table, iph->ip_dst.s_addr, ethaddr)) {
         uint8_t arp_req[ETH_HLEN + sizeof(struct arphdr)];
         struct ethhdr *reh = (struct ethhdr *)arp_req;
         struct arphdr *rah = (struct arphdr *)(arp_req + ETH_HLEN);
 
-        /* If the client addr is not known, there is no point in
-           sending the packet to it. Normally the sender should have
-           done an ARP request to get its MAC address. Here we do it
-           in place of sending the packet and we hope that the sender
-           will retry sending its packet. */
-        memset(reh->h_dest, 0xff, ETH_ALEN);
-        memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
-        memcpy(&reh->h_source[2], &slirp->vhost_addr, 4);
-        reh->h_proto = htons(ETH_P_ARP);
-        rah->ar_hrd = htons(1);
-        rah->ar_pro = htons(ETH_P_IP);
-        rah->ar_hln = ETH_ALEN;
-        rah->ar_pln = 4;
-        rah->ar_op = htons(ARPOP_REQUEST);
-        /* source hw addr */
-        memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 4);
-        memcpy(&rah->ar_sha[2], &slirp->vhost_addr, 4);

slirp/slirp.c void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) · line 714

Quoted 10 lines
slirp/slirp.c +2 -8
-        /* source IP */
-        rah->ar_sip = slirp->vhost_addr.s_addr;
-        /* target hw addr (none) */
-        memset(rah->ar_tha, 0, ETH_ALEN);
-        /* target IP */
-        rah->ar_tip = iph->ip_dst.s_addr;
-        slirp->client_ipaddr = iph->ip_dst;
-        slirp_output(slirp->opaque, arp_req, sizeof(arp_req));
+        if (!ifm->arp_requested) {
+

This newline is superfluous...

slirp/slirp.c void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) · line 724

Quoted 10 lines
slirp/slirp.c +10 -0
+            /* If the client addr is not known, send an ARP request */
+            memset(reh->h_dest, 0xff, ETH_ALEN);
+            memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
+            memcpy(&reh->h_source[2], &slirp->vhost_addr, 4);
+            reh->h_proto = htons(ETH_P_ARP);
+            rah->ar_hrd = htons(1);
+            rah->ar_pro = htons(ETH_P_IP);
+            rah->ar_hln = ETH_ALEN;
+            rah->ar_pln = 4;
+            rah->ar_op = htons(ARPOP_REQUEST);

...while it would improve readability here (and below).

Quoted 6 lines
slirp/slirp.c +6 -0
+            /* source hw addr */
+            memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 4);
+            memcpy(&rah->ar_sha[2], &slirp->vhost_addr, 4);
+            /* source IP */
+            rah->ar_sip = slirp->vhost_addr.s_addr;
+            /* target hw addr (none) */

slirp/slirp.c void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) · line 740

Quoted 10 lines
slirp/slirp.c +9 -0
+            memset(rah->ar_tha, 0, ETH_ALEN);
+            /* target IP */
+            rah->ar_tip = iph->ip_dst.s_addr;
+            slirp->client_ipaddr = iph->ip_dst;
+            slirp_output(slirp->opaque, arp_req, sizeof(arp_req));
+            ifm->arp_requested = true;
+        }
+        return 0;
     } else {
+        ifm->arp_requested = false;

Why? If that is required, you would have to make expiry checks depend on arp_requested as well - or reset the expiry date here.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#3 lore raw

Re: [Qemu-devel] [PATCH V2 2/2] [SLIRP] Delayed IP packets

Quoted 31 lines · Jan Kiszka
Quoted 30 lines · Fabien Chouteau

In the current implementation, if Slirp tries to send an IP packet to a client with an unknown hardware address, the packet is simply dropped and an ARP request is sent (if_encap in slirp/slirp.c).

With this patch, Slirp will send the ARP request, re-queue the packet and try to send it later. The packet is dropped after one second if the ARP reply is not received.

---
 slirp/if.c    |   28 ++++++++++++++++++++---
 slirp/main.h  |    2 +-
 slirp/mbuf.c  |    2 +
 slirp/mbuf.h  |    2 +
 slirp/slirp.c |   67 ++++++++++++++++++++++++++++++--------------------------
 slirp/slirp.h |   15 ++++++++++++
 6 files changed, 80 insertions(+), 36 deletions(-)
slirp/if.c +1 -0
index 0f04e13..80a5c4e 100644
@@ -6,6 +6,7 @@
  */
 
 #include <slirp.h>
+#include "qemu-timer.h"
 
 #define ifs_init(ifm) ((ifm)->ifs_next = (ifm)->ifs_prev = (ifm))
 
Quoted 8 lines

slirp/if.c if_output(struct socket *so, struct mbuf *ifm) · line 110

Quoted 6 lines
slirp/if.c +2 -0
@@ -105,6 +106,9 @@ if_output(struct socket *so, struct mbuf *ifm)
 	ifs_init(ifm);
 	insque(ifm, ifq);
 
+        /* Expiration date = Now + 1 second */
+        ifm->expiration_date = qemu_get_clock_ns(vm_clock) + 1000000000ULL;

Slirp uses rt_clock for expiry, let's stick with that clock.

OK, fixed.

Quoted 13 lines
Quoted 13 lines
slirp/if.c +2 -2
@@ -693,54 +695,57 @@ void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
 }
 
 /* output the IP packet to the ethernet device */
-void if_encap(Slirp *slirp, const uint8_t *ip_data, int ip_data_len)
+int if_encap(Slirp *slirp, struct mbuf *ifm)
 {
     uint8_t buf[1600];
     struct ethhdr *eh = (struct ethhdr *)buf;
     uint8_t ethaddr[ETH_ALEN];
-    const struct ip *iph = (const struct ip *)ip_data;
+    const struct ip *iph = (const struct ip *)ifm->m_data;
 
Quoted 6 lines

slirp/if.c void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) · line 706

Quoted 4 lines
slirp/if.c +2 -2
-    if (ip_data_len + ETH_HLEN > sizeof(buf))
-        return;
+    if (ifm->m_len + ETH_HLEN > sizeof(buf))
+        return 1;

Even if the old cold lacked them as well: { }

I forgot to use checkpatch.pl...

Quoted 11 lines

slirp/if.c void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) · line 716

Quoted 10 lines
slirp/if.c +9 -0
+            memset(rah->ar_tha, 0, ETH_ALEN);
+            /* target IP */
+            rah->ar_tip = iph->ip_dst.s_addr;
+            slirp->client_ipaddr = iph->ip_dst;
+            slirp_output(slirp->opaque, arp_req, sizeof(arp_req));
+            ifm->arp_requested = true;
+        }
+        return 0;
     } else {
+        ifm->arp_requested = false;
Quoted 3 lines

Why? If that is required, you would have to make expiry checks depend on arp_requested as well - or reset the expiry date here.

That's right, the packet is sent so there's no need to reset this value.

Thanks for your review.

Signature · 1 line
Fabien Chouteau
#4 lore raw
In reply to Fabien Chouteau
Quoted 32 lines · Fabien Chouteau

This patch adds a simple ARP table in Slirp and also adds handling of gratuitous ARP requests.

---
 Makefile.objs     |    2 +-
 slirp/arp_table.c |   50 ++++++++++++++++++++++++++++++++++++++++++
 slirp/bootp.c     |   23 ++++++++++++------
 slirp/slirp.c     |   63 +++++++++++++---------------------------------------
 slirp/slirp.h     |   50 +++++++++++++++++++++++++++++++++++++++--
 5 files changed, 129 insertions(+), 59 deletions(-)
 create mode 100644 slirp/arp_table.c
Makefile.objs +1 -1
index 6991a9f..0c10557 100644
@@ -151,7 +151,7 @@ common-obj-y += qemu-timer.o qemu-timer-common.o
 
 slirp-obj-y = cksum.o if.o ip_icmp.o ip_input.o ip_output.o
 slirp-obj-y += slirp.o mbuf.o misc.o sbuf.o socket.o tcp_input.o tcp_output.o
-slirp-obj-y += tcp_subr.o tcp_timer.o udp.o bootp.o tftp.o
+slirp-obj-y += tcp_subr.o tcp_timer.o udp.o bootp.o tftp.o arp_table.o
 common-obj-$(CONFIG_SLIRP) += $(addprefix slirp/, $(slirp-obj-y))
 
 # xen backend driver support
slirp/arp_table.c new file +0 -0
new file mode 100644
index 0000000..5d7404b

slirp/arp_table.c line 5

Quoted 5 lines
slirp/arp_table.c new file +5 -0
@@ -0,0 +1,50 @@
+#include "slirp.h"
+
+void arp_table_add(ArpTable *arptbl,
+                   int       ip_addr,
+                   uint8_t   ethaddr[ETH_ALEN])

I still prefer the condensed formatting, but that's a minor nit.

slirp/arp_table.c line 16

Quoted 11 lines
slirp/arp_table.c new file +11 -0
+{
+    int i;
+
+    DEBUG_CALL("arp_table_add");
+    DEBUG_ARG("ip = 0x%x", ip_addr);
+    DEBUG_ARGS((dfd, " hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
+                ethaddr[0], ethaddr[1], ethaddr[2],
+                ethaddr[3], ethaddr[4], ethaddr[5]));
+
+    /* Search for an entry */
+    for (i = 0; i < ARP_TABLE_SIZE && arptbl->table[i].ar_sip != 0; i++) {

Missed that on round #1: Why treating 0.0.0.0 special? If ip_addr can be zero, the current logic will append every "update" of that address as a new entry.

Quoted 90 lines
slirp/arp_table.c new file +34 -0
+        if (arptbl->table[i].ar_sip == ip_addr) {
+            /* Update the entry */
+            memcpy(arptbl->table[i].ar_sha, ethaddr, ETH_ALEN);
+            return;
+        }
+    }
+
+    /* No entry found, create a new one */
+    arptbl->table[arptbl->next_victim].ar_sip = ip_addr;
+    memcpy(arptbl->table[arptbl->next_victim].ar_sha,  ethaddr, ETH_ALEN);
+    arptbl->next_victim = (arptbl->next_victim + 1) % ARP_TABLE_SIZE;
+}
+
+bool arp_table_search(ArpTable *arptbl,
+                      int       in_ip_addr,
+                      uint8_t   out_ethaddr[ETH_ALEN])
+{
+    int i;
+
+    DEBUG_CALL("arp_table_search");
+    DEBUG_ARG("ip = 0x%x", in_ip_addr);
+
+    for (i = 0; i < ARP_TABLE_SIZE; i++) {
+        if (arptbl->table[i].ar_sip == in_ip_addr) {
+            memcpy(out_ethaddr, arptbl->table[i].ar_sha,  ETH_ALEN);
+            DEBUG_ARGS((dfd, " found hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
+                        out_ethaddr[0], out_ethaddr[1], out_ethaddr[2],
+                        out_ethaddr[3], out_ethaddr[4], out_ethaddr[5]));
+            return 1;
+        }
+    }
+
+    return 0;
+}
slirp/bootp.c +9 -7
index 1eb2ed1..07cbb80 100644
@@ -149,6 +149,7 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
     struct in_addr preq_addr;
     int dhcp_msg_type, val;
     uint8_t *q;
+    uint8_t client_ethaddr[ETH_ALEN];
 
     /* extract exact DHCP msg type */
     dhcp_decode(bp, &dhcp_msg_type, &preq_addr);
@@ -164,8 +165,9 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
     if (dhcp_msg_type != DHCPDISCOVER &&
         dhcp_msg_type != DHCPREQUEST)
         return;
-    /* XXX: this is a hack to get the client mac address */
-    memcpy(slirp->client_ethaddr, bp->bp_hwaddr, 6);
+
+    /* Get client's hardware address from bootp request */
+    memcpy(client_ethaddr, bp->bp_hwaddr, ETH_ALEN);
 
     m = m_get(slirp);
     if (!m) {
@@ -178,25 +180,25 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
 
     if (dhcp_msg_type == DHCPDISCOVER) {
         if (preq_addr.s_addr != htonl(0L)) {
-            bc = request_addr(slirp, &preq_addr, slirp->client_ethaddr);
+            bc = request_addr(slirp, &preq_addr, client_ethaddr);
             if (bc) {
                 daddr.sin_addr = preq_addr;
             }
         }
         if (!bc) {
          new_addr:
-            bc = get_new_addr(slirp, &daddr.sin_addr, slirp->client_ethaddr);
+            bc = get_new_addr(slirp, &daddr.sin_addr, client_ethaddr);
             if (!bc) {
                 DPRINTF("no address left\n");
                 return;
             }
         }
-        memcpy(bc->macaddr, slirp->client_ethaddr, 6);
+        memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
     } else if (preq_addr.s_addr != htonl(0L)) {
-        bc = request_addr(slirp, &preq_addr, slirp->client_ethaddr);
+        bc = request_addr(slirp, &preq_addr, client_ethaddr);
         if (bc) {
             daddr.sin_addr = preq_addr;
-            memcpy(bc->macaddr, slirp->client_ethaddr, 6);
+            memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
         } else {
             daddr.sin_addr.s_addr = 0;
         }

slirp/bootp.c static void bootp_reply(Slirp *slirp, const struct bootp_t *bp) · line 216

Quoted 6 lines
slirp/bootp.c +3 -0
@@ -209,6 +211,11 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
         }
     }
 
+    if (daddr.sin_addr.s_addr != 0) {
+        /* Update ARP table for this IP address */
+        arp_table_add(&slirp->arp_table, daddr.sin_addr.s_addr, client_ethaddr);

Here you prevent that arp_table_add is called with a zero IP, but not in arp_input below. Likely harmless, but at least inconsistent.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#5 lore raw
Quoted 150 lines · Jan Kiszka
Quoted 32 lines · Fabien Chouteau

This patch adds a simple ARP table in Slirp and also adds handling of gratuitous ARP requests.

---
 Makefile.objs     |    2 +-
 slirp/arp_table.c |   50 ++++++++++++++++++++++++++++++++++++++++++
 slirp/bootp.c     |   23 ++++++++++++------
 slirp/slirp.c     |   63 +++++++++++++---------------------------------------
 slirp/slirp.h     |   50 +++++++++++++++++++++++++++++++++++++++--
 5 files changed, 129 insertions(+), 59 deletions(-)
 create mode 100644 slirp/arp_table.c
Makefile.objs +1 -1
index 6991a9f..0c10557 100644
@@ -151,7 +151,7 @@ common-obj-y += qemu-timer.o qemu-timer-common.o
 
 slirp-obj-y = cksum.o if.o ip_icmp.o ip_input.o ip_output.o
 slirp-obj-y += slirp.o mbuf.o misc.o sbuf.o socket.o tcp_input.o tcp_output.o
-slirp-obj-y += tcp_subr.o tcp_timer.o udp.o bootp.o tftp.o
+slirp-obj-y += tcp_subr.o tcp_timer.o udp.o bootp.o tftp.o arp_table.o
 common-obj-$(CONFIG_SLIRP) += $(addprefix slirp/, $(slirp-obj-y))
 
 # xen backend driver support
slirp/arp_table.c new file +0 -0
new file mode 100644
index 0000000..5d7404b

slirp/arp_table.c line 5

Quoted 5 lines
slirp/arp_table.c new file +5 -0
@@ -0,0 +1,50 @@
+#include "slirp.h"
+
+void arp_table_add(ArpTable *arptbl,
+                   int       ip_addr,
+                   uint8_t   ethaddr[ETH_ALEN])

I still prefer the condensed formatting, but that's a minor nit.

slirp/arp_table.c line 16

Quoted 11 lines
slirp/arp_table.c new file +11 -0
+{
+    int i;
+
+    DEBUG_CALL("arp_table_add");
+    DEBUG_ARG("ip = 0x%x", ip_addr);
+    DEBUG_ARGS((dfd, " hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
+                ethaddr[0], ethaddr[1], ethaddr[2],
+                ethaddr[3], ethaddr[4], ethaddr[5]));
+
+    /* Search for an entry */
+    for (i = 0; i < ARP_TABLE_SIZE && arptbl->table[i].ar_sip != 0; i++) {

Missed that on round #1: Why treating 0.0.0.0 special? If ip_addr can be zero, the current logic will append every "update" of that address as a new entry.

Quoted 93 lines
slirp/arp_table.c new file +34 -0
+        if (arptbl->table[i].ar_sip == ip_addr) {
+            /* Update the entry */
+            memcpy(arptbl->table[i].ar_sha, ethaddr, ETH_ALEN);
+            return;
+        }
+    }
+
+    /* No entry found, create a new one */
+    arptbl->table[arptbl->next_victim].ar_sip = ip_addr;
+    memcpy(arptbl->table[arptbl->next_victim].ar_sha,  ethaddr, ETH_ALEN);
+    arptbl->next_victim = (arptbl->next_victim + 1) % ARP_TABLE_SIZE;
+}
+
+bool arp_table_search(ArpTable *arptbl,
+                      int       in_ip_addr,
+                      uint8_t   out_ethaddr[ETH_ALEN])
+{
+    int i;
+
+    DEBUG_CALL("arp_table_search");
+    DEBUG_ARG("ip = 0x%x", in_ip_addr);
+
+    for (i = 0; i < ARP_TABLE_SIZE; i++) {
+        if (arptbl->table[i].ar_sip == in_ip_addr) {
+            memcpy(out_ethaddr, arptbl->table[i].ar_sha,  ETH_ALEN);
+            DEBUG_ARGS((dfd, " found hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
+                        out_ethaddr[0], out_ethaddr[1], out_ethaddr[2],
+                        out_ethaddr[3], out_ethaddr[4], out_ethaddr[5]));
+            return 1;
+        }
+    }
+
+    return 0;
+}
slirp/bootp.c +9 -7
index 1eb2ed1..07cbb80 100644
@@ -149,6 +149,7 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
     struct in_addr preq_addr;
     int dhcp_msg_type, val;
     uint8_t *q;
+    uint8_t client_ethaddr[ETH_ALEN];
 
     /* extract exact DHCP msg type */
     dhcp_decode(bp, &dhcp_msg_type, &preq_addr);
@@ -164,8 +165,9 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
     if (dhcp_msg_type != DHCPDISCOVER &&
         dhcp_msg_type != DHCPREQUEST)
         return;
-    /* XXX: this is a hack to get the client mac address */
-    memcpy(slirp->client_ethaddr, bp->bp_hwaddr, 6);
+
+    /* Get client's hardware address from bootp request */
+    memcpy(client_ethaddr, bp->bp_hwaddr, ETH_ALEN);
 
     m = m_get(slirp);
     if (!m) {
@@ -178,25 +180,25 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
 
     if (dhcp_msg_type == DHCPDISCOVER) {
         if (preq_addr.s_addr != htonl(0L)) {
-            bc = request_addr(slirp, &preq_addr, slirp->client_ethaddr);
+            bc = request_addr(slirp, &preq_addr, client_ethaddr);
             if (bc) {
                 daddr.sin_addr = preq_addr;
             }
         }
         if (!bc) {
          new_addr:
-            bc = get_new_addr(slirp, &daddr.sin_addr, slirp->client_ethaddr);
+            bc = get_new_addr(slirp, &daddr.sin_addr, client_ethaddr);
             if (!bc) {
                 DPRINTF("no address left\n");
                 return;
             }
         }
-        memcpy(bc->macaddr, slirp->client_ethaddr, 6);
+        memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
     } else if (preq_addr.s_addr != htonl(0L)) {
-        bc = request_addr(slirp, &preq_addr, slirp->client_ethaddr);
+        bc = request_addr(slirp, &preq_addr, client_ethaddr);
         if (bc) {
             daddr.sin_addr = preq_addr;
-            memcpy(bc->macaddr, slirp->client_ethaddr, 6);
+            memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
         } else {
             daddr.sin_addr.s_addr = 0;
         }
@@ -209,6 +211,11 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
         }
     }
 
Quoted 7 lines

slirp/bootp.c static void bootp_reply(Slirp *slirp, const struct bootp_t *bp) · line 216

Quoted 3 lines
slirp/bootp.c +3 -0
+    if (daddr.sin_addr.s_addr != 0) {
+        /* Update ARP table for this IP address */
+        arp_table_add(&slirp->arp_table, daddr.sin_addr.s_addr, client_ethaddr);

Here you prevent that arp_table_add is called with a zero IP, but not in arp_input below. Likely harmless, but at least inconsistent.

Unfortunately, this patch also has a more sever issues: it breaks DHCP e.g.

Slirp's bootp sends out all replies, also acks and offers, as broadcasts. Normal servers already use the clients IP address as destination here.

Even if bootp is fixed, you still lack logic to deal with special addresses in your arp table lookup. At least broadcasts need to be handled, I think other multicasts aren't supported by slirp anyway.

Jan

#6 lore raw
Quoted 159 lines · Jan Kiszka
Quoted 150 lines · Jan Kiszka
Quoted 32 lines · Fabien Chouteau

This patch adds a simple ARP table in Slirp and also adds handling of gratuitous ARP requests.

---
 Makefile.objs     |    2 +-
 slirp/arp_table.c |   50 ++++++++++++++++++++++++++++++++++++++++++
 slirp/bootp.c     |   23 ++++++++++++------
 slirp/slirp.c     |   63 +++++++++++++---------------------------------------
 slirp/slirp.h     |   50 +++++++++++++++++++++++++++++++++++++++--
 5 files changed, 129 insertions(+), 59 deletions(-)
 create mode 100644 slirp/arp_table.c
Makefile.objs +1 -1
index 6991a9f..0c10557 100644
@@ -151,7 +151,7 @@ common-obj-y += qemu-timer.o qemu-timer-common.o
 
 slirp-obj-y = cksum.o if.o ip_icmp.o ip_input.o ip_output.o
 slirp-obj-y += slirp.o mbuf.o misc.o sbuf.o socket.o tcp_input.o tcp_output.o
-slirp-obj-y += tcp_subr.o tcp_timer.o udp.o bootp.o tftp.o
+slirp-obj-y += tcp_subr.o tcp_timer.o udp.o bootp.o tftp.o arp_table.o
 common-obj-$(CONFIG_SLIRP) += $(addprefix slirp/, $(slirp-obj-y))
 
 # xen backend driver support
slirp/arp_table.c new file +0 -0
new file mode 100644
index 0000000..5d7404b

slirp/arp_table.c line 5

Quoted 5 lines
slirp/arp_table.c new file +5 -0
@@ -0,0 +1,50 @@
+#include "slirp.h"
+
+void arp_table_add(ArpTable *arptbl,
+                   int       ip_addr,
+                   uint8_t   ethaddr[ETH_ALEN])

I still prefer the condensed formatting, but that's a minor nit.

slirp/arp_table.c line 16

Quoted 11 lines
slirp/arp_table.c new file +11 -0
+{
+    int i;
+
+    DEBUG_CALL("arp_table_add");
+    DEBUG_ARG("ip = 0x%x", ip_addr);
+    DEBUG_ARGS((dfd, " hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
+                ethaddr[0], ethaddr[1], ethaddr[2],
+                ethaddr[3], ethaddr[4], ethaddr[5]));
+
+    /* Search for an entry */
+    for (i = 0; i < ARP_TABLE_SIZE && arptbl->table[i].ar_sip != 0; i++) {

Missed that on round #1: Why treating 0.0.0.0 special? If ip_addr can be zero, the current logic will append every "update" of that address as a new entry.

Quoted 93 lines
slirp/arp_table.c new file +34 -0
+        if (arptbl->table[i].ar_sip == ip_addr) {
+            /* Update the entry */
+            memcpy(arptbl->table[i].ar_sha, ethaddr, ETH_ALEN);
+            return;
+        }
+    }
+
+    /* No entry found, create a new one */
+    arptbl->table[arptbl->next_victim].ar_sip = ip_addr;
+    memcpy(arptbl->table[arptbl->next_victim].ar_sha,  ethaddr, ETH_ALEN);
+    arptbl->next_victim = (arptbl->next_victim + 1) % ARP_TABLE_SIZE;
+}
+
+bool arp_table_search(ArpTable *arptbl,
+                      int       in_ip_addr,
+                      uint8_t   out_ethaddr[ETH_ALEN])
+{
+    int i;
+
+    DEBUG_CALL("arp_table_search");
+    DEBUG_ARG("ip = 0x%x", in_ip_addr);
+
+    for (i = 0; i < ARP_TABLE_SIZE; i++) {
+        if (arptbl->table[i].ar_sip == in_ip_addr) {
+            memcpy(out_ethaddr, arptbl->table[i].ar_sha,  ETH_ALEN);
+            DEBUG_ARGS((dfd, " found hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
+                        out_ethaddr[0], out_ethaddr[1], out_ethaddr[2],
+                        out_ethaddr[3], out_ethaddr[4], out_ethaddr[5]));
+            return 1;
+        }
+    }
+
+    return 0;
+}
slirp/bootp.c +9 -7
index 1eb2ed1..07cbb80 100644
@@ -149,6 +149,7 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
     struct in_addr preq_addr;
     int dhcp_msg_type, val;
     uint8_t *q;
+    uint8_t client_ethaddr[ETH_ALEN];
 
     /* extract exact DHCP msg type */
     dhcp_decode(bp, &dhcp_msg_type, &preq_addr);
@@ -164,8 +165,9 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
     if (dhcp_msg_type != DHCPDISCOVER &&
         dhcp_msg_type != DHCPREQUEST)
         return;
-    /* XXX: this is a hack to get the client mac address */
-    memcpy(slirp->client_ethaddr, bp->bp_hwaddr, 6);
+
+    /* Get client's hardware address from bootp request */
+    memcpy(client_ethaddr, bp->bp_hwaddr, ETH_ALEN);
 
     m = m_get(slirp);
     if (!m) {
@@ -178,25 +180,25 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
 
     if (dhcp_msg_type == DHCPDISCOVER) {
         if (preq_addr.s_addr != htonl(0L)) {
-            bc = request_addr(slirp, &preq_addr, slirp->client_ethaddr);
+            bc = request_addr(slirp, &preq_addr, client_ethaddr);
             if (bc) {
                 daddr.sin_addr = preq_addr;
             }
         }
         if (!bc) {
          new_addr:
-            bc = get_new_addr(slirp, &daddr.sin_addr, slirp->client_ethaddr);
+            bc = get_new_addr(slirp, &daddr.sin_addr, client_ethaddr);
             if (!bc) {
                 DPRINTF("no address left\n");
                 return;
             }
         }
-        memcpy(bc->macaddr, slirp->client_ethaddr, 6);
+        memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
     } else if (preq_addr.s_addr != htonl(0L)) {
-        bc = request_addr(slirp, &preq_addr, slirp->client_ethaddr);
+        bc = request_addr(slirp, &preq_addr, client_ethaddr);
         if (bc) {
             daddr.sin_addr = preq_addr;
-            memcpy(bc->macaddr, slirp->client_ethaddr, 6);
+            memcpy(bc->macaddr, client_ethaddr, ETH_ALEN);
         } else {
             daddr.sin_addr.s_addr = 0;
         }
@@ -209,6 +211,11 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
         }
     }
 
Quoted 7 lines

slirp/bootp.c static void bootp_reply(Slirp *slirp, const struct bootp_t *bp) · line 216

Quoted 3 lines
slirp/bootp.c +3 -0
+    if (daddr.sin_addr.s_addr != 0) {
+        /* Update ARP table for this IP address */
+        arp_table_add(&slirp->arp_table, daddr.sin_addr.s_addr, client_ethaddr);

Here you prevent that arp_table_add is called with a zero IP, but not in arp_input below. Likely harmless, but at least inconsistent.

Quoted 5 lines

Unfortunately, this patch also has a more sever issues: it breaks DHCP e.g.

Slirp's bootp sends out all replies, also acks and offers, as broadcasts. Normal servers already use the clients IP address as destination here.

Obviously, using a broadcast address on return is valid as well. So this is no slirp bug, it's purely an ARP table lookup issue introduced by this patch.

Quoted 5 lines

Even if bootp is fixed, you still lack logic to deal with special addresses in your arp table lookup. At least broadcasts need to be handled, I think other multicasts aren't supported by slirp anyway.

Jan

#7 lore raw
Quoted 28 lines · Jan Kiszka
Quoted 27 lines · Jan Kiszka
Quoted 26 lines · Jan Kiszka
Quoted 25 lines · Fabien Chouteau

This patch adds a simple ARP table in Slirp and also adds handling of gratuitous ARP requests.

---
 Makefile.objs     |    2 +-
 slirp/arp_table.c |   50 ++++++++++++++++++++++++++++++++++++++++++
 slirp/bootp.c     |   23 ++++++++++++------
 slirp/slirp.c     |   63 +++++++++++++---------------------------------------
 slirp/slirp.h     |   50 +++++++++++++++++++++++++++++++++++++++--
 5 files changed, 129 insertions(+), 59 deletions(-)
 create mode 100644 slirp/arp_table.c
Makefile.objs +1 -1
index 6991a9f..0c10557 100644
@@ -151,7 +151,7 @@ common-obj-y += qemu-timer.o qemu-timer-common.o
 
 slirp-obj-y = cksum.o if.o ip_icmp.o ip_input.o ip_output.o
 slirp-obj-y += slirp.o mbuf.o misc.o sbuf.o socket.o tcp_input.o tcp_output.o
-slirp-obj-y += tcp_subr.o tcp_timer.o udp.o bootp.o tftp.o
+slirp-obj-y += tcp_subr.o tcp_timer.o udp.o bootp.o tftp.o arp_table.o
 common-obj-$(CONFIG_SLIRP) += $(addprefix slirp/, $(slirp-obj-y))
 
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Makefile.objs +0 -0
 # xen backend driver support
slirp/arp_table.c new file +0 -0
new file mode 100644
index 0000000..5d7404b
Quoted 8 lines
Quoted 8 lines

slirp/arp_table.c line 5

Quoted 6 lines
slirp/arp_table.c new file +5 -0
@@ -0,0 +1,50 @@
+#include "slirp.h"
+
+void arp_table_add(ArpTable *arptbl,
+                   int       ip_addr,
+                   uint8_t   ethaddr[ETH_ALEN])

I still prefer the condensed formatting, but that's a minor nit.

OK I'll change it to keep consistent style.

Unfortunately, there's nothing on this subject in the CODING_STYLE...

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines

slirp/arp_table.c line 16

Quoted 12 lines
slirp/arp_table.c new file +11 -0
+{
+    int i;
+
+    DEBUG_CALL("arp_table_add");
+    DEBUG_ARG("ip = 0x%x", ip_addr);
+    DEBUG_ARGS((dfd, " hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
+                ethaddr[0], ethaddr[1], ethaddr[2],
+                ethaddr[3], ethaddr[4], ethaddr[5]));
+
+    /* Search for an entry */
+    for (i = 0; i < ARP_TABLE_SIZE && arptbl->table[i].ar_sip != 0; i++) {

Missed that on round #1: Why treating 0.0.0.0 special? If ip_addr can be zero, the current logic will append every "update" of that address as a new entry.

Isn't 0.0.0.0 a reserved address? I think it's safe to use it here.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
slirp/bootp.c +0 -0
index 1eb2ed1..07cbb80 100644
@@ -209,6 +211,11 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
         }
     }
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

slirp/bootp.c static void bootp_reply(Slirp *slirp, const struct bootp_t *bp) · line 216

Quoted 3 lines
slirp/bootp.c +3 -0
+    if (daddr.sin_addr.s_addr != 0) {
+        /* Update ARP table for this IP address */
+        arp_table_add(&slirp->arp_table, daddr.sin_addr.s_addr, client_ethaddr);

Here you prevent that arp_table_add is called with a zero IP, but not in arp_input below. Likely harmless, but at least inconsistent.

I will handle this case directly in arp_table_add to get a consistent behavior.

Quoted 17 lines
Quoted 6 lines

Unfortunately, this patch also has a more sever issues: it breaks DHCP e.g.

Slirp's bootp sends out all replies, also acks and offers, as broadcasts. Normal servers already use the clients IP address as destination here.

Obviously, using a broadcast address on return is valid as well. So this is no slirp bug, it's purely an ARP table lookup issue introduced by this patch.

Quoted 5 lines

Even if bootp is fixed, you still lack logic to deal with special addresses in your arp table lookup. At least broadcasts need to be handled, I think other multicasts aren't supported by slirp anyway.

That's right, I will add broadcast handling in the next version.

Thanks for your review.

Signature · 1 line
Fabien Chouteau
#8 lore raw
Quoted 44 lines · Fabien Chouteau
Quoted 28 lines · Jan Kiszka
Quoted 27 lines · Jan Kiszka
Quoted 26 lines · Jan Kiszka
Quoted 25 lines · Fabien Chouteau

This patch adds a simple ARP table in Slirp and also adds handling of gratuitous ARP requests.

---
 Makefile.objs     |    2 +-
 slirp/arp_table.c |   50 ++++++++++++++++++++++++++++++++++++++++++
 slirp/bootp.c     |   23 ++++++++++++------
 slirp/slirp.c     |   63 +++++++++++++---------------------------------------
 slirp/slirp.h     |   50 +++++++++++++++++++++++++++++++++++++++--
 5 files changed, 129 insertions(+), 59 deletions(-)
 create mode 100644 slirp/arp_table.c
Makefile.objs +1 -1
index 6991a9f..0c10557 100644
@@ -151,7 +151,7 @@ common-obj-y += qemu-timer.o qemu-timer-common.o
 
 slirp-obj-y = cksum.o if.o ip_icmp.o ip_input.o ip_output.o
 slirp-obj-y += slirp.o mbuf.o misc.o sbuf.o socket.o tcp_input.o tcp_output.o
-slirp-obj-y += tcp_subr.o tcp_timer.o udp.o bootp.o tftp.o
+slirp-obj-y += tcp_subr.o tcp_timer.o udp.o bootp.o tftp.o arp_table.o
 common-obj-$(CONFIG_SLIRP) += $(addprefix slirp/, $(slirp-obj-y))
 
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Makefile.objs +0 -0
 # xen backend driver support
slirp/arp_table.c new file +0 -0
new file mode 100644
index 0000000..5d7404b
Quoted 8 lines
Quoted 8 lines

slirp/arp_table.c line 5

Quoted 6 lines
slirp/arp_table.c new file +5 -0
@@ -0,0 +1,50 @@
+#include "slirp.h"
+
+void arp_table_add(ArpTable *arptbl,
+                   int       ip_addr,
+                   uint8_t   ethaddr[ETH_ALEN])

I still prefer the condensed formatting, but that's a minor nit.

Quoted 3 lines

OK I'll change it to keep consistent style.

Unfortunately, there's nothing on this subject in the CODING_STYLE...

We should add a section on consistency - but I guess that will always remain a subjective matter. :)

Quoted 18 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines

slirp/arp_table.c line 16

Quoted 13 lines
slirp/arp_table.c new file +11 -0
+{
+    int i;
+
+    DEBUG_CALL("arp_table_add");
+    DEBUG_ARG("ip = 0x%x", ip_addr);
+    DEBUG_ARGS((dfd, " hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
+                ethaddr[0], ethaddr[1], ethaddr[2],
+                ethaddr[3], ethaddr[4], ethaddr[5]));
+
+    /* Search for an entry */
+    for (i = 0; i < ARP_TABLE_SIZE && arptbl->table[i].ar_sip != 0; i++) {

Missed that on round #1: Why treating 0.0.0.0 special? If ip_addr can be zero, the current logic will append every "update" of that address as a new entry.

Quoted 1 line

Isn't 0.0.0.0 a reserved address? I think it's safe to use it here.

Actually, the whole 0.0.0.0/8 is source-only, ie. it should never show up in the ARP table.

Jan

#9 lore raw
Quoted 45 lines · Jan Kiszka
Quoted 44 lines · Fabien Chouteau
Quoted 42 lines · Jan Kiszka
Quoted 41 lines · Jan Kiszka
Quoted 40 lines · Jan Kiszka
Quoted 32 lines · Fabien Chouteau

This patch adds a simple ARP table in Slirp and also adds handling of gratuitous ARP requests.

---
 Makefile.objs     |    2 +-
 slirp/arp_table.c |   50 ++++++++++++++++++++++++++++++++++++++++++
 slirp/bootp.c     |   23 ++++++++++++------
 slirp/slirp.c     |   63 +++++++++++++---------------------------------------
 slirp/slirp.h     |   50 +++++++++++++++++++++++++++++++++++++++--
 5 files changed, 129 insertions(+), 59 deletions(-)
 create mode 100644 slirp/arp_table.c
Makefile.objs +1 -1
index 6991a9f..0c10557 100644
@@ -151,7 +151,7 @@ common-obj-y += qemu-timer.o qemu-timer-common.o
 
 slirp-obj-y = cksum.o if.o ip_icmp.o ip_input.o ip_output.o
 slirp-obj-y += slirp.o mbuf.o misc.o sbuf.o socket.o tcp_input.o tcp_output.o
-slirp-obj-y += tcp_subr.o tcp_timer.o udp.o bootp.o tftp.o
+slirp-obj-y += tcp_subr.o tcp_timer.o udp.o bootp.o tftp.o arp_table.o
 common-obj-$(CONFIG_SLIRP) += $(addprefix slirp/, $(slirp-obj-y))
 
 # xen backend driver support
slirp/arp_table.c new file +0 -0
new file mode 100644
index 0000000..5d7404b

slirp/arp_table.c line 5

Quoted 5 lines
slirp/arp_table.c new file +5 -0
@@ -0,0 +1,50 @@
+#include "slirp.h"
+
+void arp_table_add(ArpTable *arptbl,
+                   int       ip_addr,
+                   uint8_t   ethaddr[ETH_ALEN])

I still prefer the condensed formatting, but that's a minor nit.

Quoted 6 lines
Quoted 3 lines

OK I'll change it to keep consistent style.

Unfortunately, there's nothing on this subject in the CODING_STYLE...

We should add a section on consistency - but I guess that will always remain a subjective matter. :)

Indeed, I bet we can find in Qemu an example of every C coding style...

Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 4 lines
slirp/arp_table.c new file +1 -0
+{

slirp/arp_table.c line 16

Quoted 10 lines
slirp/arp_table.c new file +10 -0
+    int i;
+
+    DEBUG_CALL("arp_table_add");
+    DEBUG_ARG("ip = 0x%x", ip_addr);
+    DEBUG_ARGS((dfd, " hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
+                ethaddr[0], ethaddr[1], ethaddr[2],
+                ethaddr[3], ethaddr[4], ethaddr[5]));
+
+    /* Search for an entry */
+    for (i = 0; i < ARP_TABLE_SIZE && arptbl->table[i].ar_sip != 0; i++) {

Missed that on round #1: Why treating 0.0.0.0 special? If ip_addr can be zero, the current logic will append every "update" of that address as a new entry.

Quoted 5 lines
Quoted 1 line

Isn't 0.0.0.0 a reserved address? I think it's safe to use it here.

Actually, the whole 0.0.0.0/8 is source-only, ie. it should never show up in the ARP table.

Great, so I will just prevent insertion and search of 0.0.0.0/8 addresses.

Regards,

Signature · 1 line
Fabien Chouteau