[U-Boot] [PATCH 17/28] net: Refactor to separate the UDP handler from the ARP handler

Joe Hershberger joe.hershberger at ni.com
Fri Jan 20 01:53:20 CET 2012


Only call the handlers if one was registered... don't require
   protocols to register a handler (eliminating dummies)
NetConsole now uses the ARP handler when waiting on arp (instead of
   needing a #define hack in arp.c)
Clear handlers at the end of net loop

Signed-off-by: Joe Hershberger <joe.hershberger at ni.com>
Cc: Joe Hershberger <joe.hershberger at gmail.com>
Cc: Wolfgang Denk <wd at denx.de>
---
 drivers/net/netconsole.c |    4 +-
 include/net.h            |    9 ++++--
 net/arp.c                |    7 ++--
 net/bootp.c              |    4 +-
 net/cdp.c                |    8 -----
 net/dns.c                |    2 +-
 net/net.c                |   70 +++++++++++++++++++++++++++++-----------------
 net/nfs.c                |    2 +-
 net/sntp.c               |    2 +-
 net/tftp.c               |    4 +-
 10 files changed, 63 insertions(+), 49 deletions(-)

diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c
index 89b5956..1e31159 100644
--- a/drivers/net/netconsole.c
+++ b/drivers/net/netconsole.c
@@ -63,12 +63,12 @@ void NcStart(void)
 {
 	if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) {
 		/* going to check for input packet */
-		NetSetHandler(nc_handler);
+		NetSetUDPHandler(nc_handler);
 		NetSetTimeout(net_timeout, nc_timeout);
 	} else {
 		/* send arp request */
 		uchar *pkt;
-		NetSetHandler(nc_wait_arp_handler);
+		NetSetARPHandler(nc_wait_arp_handler);
 		pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
 		memcpy(pkt, output_packet, output_packet_len);
 		NetSendUDPPacket(nc_ether, nc_ip, nc_port, nc_port,
diff --git a/include/net.h b/include/net.h
index fe49ff7..dca8e1c 100644
--- a/include/net.h
+++ b/include/net.h
@@ -424,8 +424,10 @@ extern int	NetCksumOk(uchar *, int);	/* Return true if cksum OK */
 extern uint	NetCksum(uchar *, int);		/* Calculate the checksum */
 
 /* Callbacks */
-extern rxhand_f *NetGetHandler(void);		/* Get RX packet handler */
-extern void	NetSetHandler(rxhand_f *);	/* Set RX packet handler */
+extern rxhand_f *NetGetUDPHandler(void);	/* Get UDP RX packet handler */
+extern void	NetSetUDPHandler(rxhand_f *);	/* Set UDP RX packet handler */
+extern rxhand_f *NetGetARPHandler(void);	/* Get ARP RX packet handler */
+extern void	NetSetARPHandler(rxhand_f *);	/* Set ARP RX packet handler */
 extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */
 extern void	NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
 
@@ -439,7 +441,8 @@ extern void	NetSetState(int state);
 /* Transmit "NetTxPacket" */
 extern void NetSendPacket(uchar *, int);
 
-/* Transmit UDP packet, performing ARP request if needed */
+/* Transmit UDP packet, performing ARP request if needed
+   (ether will be populated) */
 extern int	NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport,
 			int sport, int payload_len);
 
diff --git a/net/arp.c b/net/arp.c
index d9113dc..5b6c787 100644
--- a/net/arp.c
+++ b/net/arp.c
@@ -192,9 +192,10 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len)
 			memcpy(NetArpWaitPacketMAC,
 				&arp->ar_sha, ARP_HLEN);
 
-#ifdef CONFIG_NETCONSOLE
-			NetGetHandler()(0, 0, 0, 0, 0);
-#endif
+			if (NetGetARPHandler() != NULL)
+				NetGetARPHandler()((uchar *)arp, 0,
+					reply_ip_addr, 0, len);
+
 			/* modify header, and transmit it */
 			memcpy(((struct Ethernet_t *)NetArpWaitTxPacket)->
 				et_dest, NetArpWaitPacketMAC, ARP_HLEN);
diff --git a/net/bootp.c b/net/bootp.c
index 0d5f4cf..9d709f1 100644
--- a/net/bootp.c
+++ b/net/bootp.c
@@ -669,9 +669,9 @@ BootpRequest(void)
 
 #if defined(CONFIG_CMD_DHCP)
 	dhcp_state = SELECTING;
-	NetSetHandler(DhcpHandler);
+	NetSetUDPHandler(DhcpHandler);
 #else
-	NetSetHandler(BootpHandler);
+	NetSetUDPHandler(BootpHandler);
 #endif
 	NetSendPacket(NetTxPacket, pktlen);
 }
diff --git a/net/cdp.c b/net/cdp.c
index 31f9ce7..120d410 100644
--- a/net/cdp.c
+++ b/net/cdp.c
@@ -237,13 +237,6 @@ CDPTimeout(void)
 		NetSetState(NETLOOP_SUCCESS);
 }
 
-static void
-CDPDummyHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
-		unsigned len)
-{
-	/* nothing */
-}
-
 void
 CDPReceive(const uchar *pkt, unsigned len)
 {
@@ -368,7 +361,6 @@ CDPStart(void)
 	CDPApplianceVLAN = htons(-1);
 
 	NetSetTimeout(CDP_TIMEOUT, CDPTimeout);
-	NetSetHandler(CDPDummyHandler);
 
 	CDPSendTrigger();
 }
diff --git a/net/dns.c b/net/dns.c
index 0b655cc..9da3cac 100644
--- a/net/dns.c
+++ b/net/dns.c
@@ -200,7 +200,7 @@ DnsStart(void)
 	debug("%s\n", __func__);
 
 	NetSetTimeout(DNS_TIMEOUT, DnsTimeout);
-	NetSetHandler(DnsHandler);
+	NetSetUDPHandler(DnsHandler);
 
 	DnsSend();
 }
diff --git a/net/net.c b/net/net.c
index a92df90..2dc49f5 100644
--- a/net/net.c
+++ b/net/net.c
@@ -183,10 +183,13 @@ uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
 /* Receive packet */
 uchar *NetRxPackets[PKTBUFSRX];
 
-/* Current RX packet handler */
-static rxhand_f *packetHandler;
+/* Current UDP RX packet handler */
+static rxhand_f *udpPacketHandler;
+/* Current ARP RX packet handler */
+static rxhand_f *arpPacketHandler;
 #ifdef CONFIG_CMD_TFTPPUT
-static rxhand_icmp_f *packet_icmp_handler;	/* Current ICMP rx handler */
+/* Current ICMP rx handler */
+static rxhand_icmp_f *packet_icmp_handler;
 #endif
 /* Current timeout handler */
 static thand_f *timeHandler;
@@ -257,6 +260,15 @@ static void NetInitLoop(enum proto_t protocol)
 	return;
 }
 
+static void
+NetCleanupLoop(void)
+{
+	/* Clear the handlers */
+	NetSetUDPHandler(NULL);
+	NetSetARPHandler(NULL);
+	NetSetTimeout(0, NULL);
+}
+
 /**********************************************************************/
 /*
  *	Main network processing loop.
@@ -264,6 +276,7 @@ static void NetInitLoop(enum proto_t protocol)
 
 int NetLoop(enum proto_t protocol)
 {
+	int	i;
 	bd_t *bd = gd->bd;
 	int ret = -1;
 
@@ -275,16 +288,13 @@ int NetLoop(enum proto_t protocol)
 
 	ArpInit();
 
-	if (!NetTxPacket) {
-		int	i;
-		/*
-		 *	Setup packet buffers, aligned correctly.
-		 */
-		NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
-		NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
-		for (i = 0; i < PKTBUFSRX; i++)
-			NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
-	}
+	/*
+	 *	Setup packet buffers, aligned correctly.
+	 */
+	NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
+	NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
+	for (i = 0; i < PKTBUFSRX; i++)
+		NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
 
 	eth_halt();
 	eth_set_current();
@@ -422,6 +432,7 @@ restart:
 		 *	Abort if ctrl-c was pressed.
 		 */
 		if (ctrlc()) {
+			NetCleanupLoop();
 			eth_halt();
 			puts("\nAbort\n");
 			goto done;
@@ -464,6 +475,7 @@ restart:
 			goto restart;
 
 		case NETLOOP_SUCCESS:
+			NetCleanupLoop();
 			if (NetBootFileXferSize > 0) {
 				char buf[20];
 				printf("Bytes transferred = %ld (%lx hex)\n",
@@ -480,6 +492,7 @@ restart:
 			goto done;
 
 		case NETLOOP_FAIL:
+			NetCleanupLoop();
 			goto done;
 		}
 	}
@@ -501,13 +514,6 @@ startAgainTimeout(void)
 	NetSetState(NETLOOP_RESTART);
 }
 
-static void
-startAgainHandler(uchar *pkt, unsigned dest, IPaddr_t sip,
-		  unsigned src, unsigned len)
-{
-	/* Totally ignore the packet */
-}
-
 void NetStartAgain(void)
 {
 	char *nretry;
@@ -544,7 +550,7 @@ void NetStartAgain(void)
 		NetRestartWrap = 0;
 		if (NetDevExists) {
 			NetSetTimeout(10000UL, startAgainTimeout);
-			NetSetHandler(startAgainHandler);
+			NetSetUDPHandler(NULL);
 		} else {
 			NetSetState(NETLOOP_FAIL);
 		}
@@ -559,16 +565,27 @@ void NetStartAgain(void)
  */
 
 rxhand_f *
-NetGetHandler(void)
+NetGetUDPHandler(void)
+{
+	return udpPacketHandler;
+}
+
+void
+NetSetUDPHandler(rxhand_f *f)
 {
-	return packetHandler;
+	udpPacketHandler = f;
 }
 
+rxhand_f *
+NetGetARPHandler(void)
+{
+	return arpPacketHandler;
+}
 
 void
-NetSetHandler(rxhand_f *f)
+NetSetARPHandler(rxhand_f *f)
 {
-	packetHandler = f;
+	arpPacketHandler = f;
 }
 
 #ifdef CONFIG_CMD_TFTPPUT
@@ -1101,7 +1118,8 @@ NetReceive(volatile uchar *inpkt, int len)
 		/*
 		 *	IP header OK.  Pass the packet to the current handler.
 		 */
-		(*packetHandler)((uchar *)ip + IP_UDP_HDR_SIZE,
+		if (udpPacketHandler != NULL)
+			(*udpPacketHandler)((uchar *)ip + IP_UDP_HDR_SIZE,
 					ntohs(ip->udp_dst),
 					src_ip,
 					ntohs(ip->udp_src),
diff --git a/net/nfs.c b/net/nfs.c
index e366505..752ab33 100644
--- a/net/nfs.c
+++ b/net/nfs.c
@@ -735,7 +735,7 @@ NfsStart(void)
 		"Loading: *\b", load_addr);
 
 	NetSetTimeout(NFS_TIMEOUT, NfsTimeout);
-	NetSetHandler(NfsHandler);
+	NetSetUDPHandler(NfsHandler);
 
 	NfsTimeoutCount = 0;
 	NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
diff --git a/net/sntp.c b/net/sntp.c
index f5fa6f7..4d902cb 100644
--- a/net/sntp.c
+++ b/net/sntp.c
@@ -85,7 +85,7 @@ SntpStart(void)
 	debug("%s\n", __func__);
 
 	NetSetTimeout(SNTP_TIMEOUT, SntpTimeout);
-	NetSetHandler(SntpHandler);
+	NetSetUDPHandler(SntpHandler);
 	memset(NetServerEther, 0, 6);
 
 	SntpSend();
diff --git a/net/tftp.c b/net/tftp.c
index 49ddbd7..ae8abc7 100644
--- a/net/tftp.c
+++ b/net/tftp.c
@@ -778,7 +778,7 @@ void TftpStart(enum proto_t protocol)
 	TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
 
 	NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
-	NetSetHandler(TftpHandler);
+	NetSetUDPHandler(TftpHandler);
 #ifdef CONFIG_CMD_TFTPPUT
 	net_set_icmp_handler(icmp_handler);
 #endif
@@ -840,7 +840,7 @@ TftpStartServer(void)
 #endif
 
 	TftpState = STATE_RECV_WRQ;
-	NetSetHandler(TftpHandler);
+	NetSetUDPHandler(TftpHandler);
 }
 #endif /* CONFIG_CMD_TFTPSRV */
 
-- 
1.6.0.2



More information about the U-Boot mailing list