Re: [U-Boot] [PATCH v7 24/27] sandbox: eth: Add a bridge to a real network for sandbox

2015-03-23 Thread Simon Glass
On 22 March 2015 at 16:09, Joe Hershberger joe.hershber...@ni.com wrote:
 Implement a bridge between U-Boot's network stack and Linux's raw packet
 API allowing the sandbox to send and receive packets using the host
 machine's network interface.

 This raw Ethernet API requires elevated privileges.  You can either run
 as root, or you can add the capability needed like so:

 sudo /sbin/setcap CAP_NET_RAW+ep /path/to/u-boot

 Signed-off-by: Joe Hershberger joe.hershber...@ni.com
 Reviewed-by: Simon Glass s...@chromium.org

 ---

 Changes in v7: None

Applied to u-boot-dm/next, thanks!
___
U-Boot mailing list
U-Boot@lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot


[U-Boot] [PATCH v7 24/27] sandbox: eth: Add a bridge to a real network for sandbox

2015-03-22 Thread Joe Hershberger
Implement a bridge between U-Boot's network stack and Linux's raw packet
API allowing the sandbox to send and receive packets using the host
machine's network interface.

This raw Ethernet API requires elevated privileges.  You can either run
as root, or you can add the capability needed like so:

sudo /sbin/setcap CAP_NET_RAW+ep /path/to/u-boot

Signed-off-by: Joe Hershberger joe.hershber...@ni.com
Reviewed-by: Simon Glass s...@chromium.org

---

Changes in v7: None
Changes in v6:
-Addressed nits

Changes in v5:
-Added fallback for setting promiscuous mode
-Added help to Kconfig
-Added more details and examples in the README
-Check for NULL when reading fdt for host interface
-Check for malloc failure
-Remove cast of pointer passed to free
-Remove the empty sb_eth_raw_remove function
-Return -errno in from send and recv
-Return errno from recv
-Set the socket to non-blocking
-Use net_rx_packets instead of a stack buffer

Changes in v4:
-Add comments to priv struct definition
-Added comments to README.sandbox
-Clean up the interface to sandbox's eth-raw-os by passing priv to raw-os
-Cleanup var definition order
-Fixed the MAC address limitation (now all traffic uses MAC address from env)
-Move os file to arch
-Moved config to Kconfig
-Use accessors for platdata and priv

Changes in v3:
-Made the os raw packet support for sandbox eth build and work.

Changes in v2:
-Added the raw packet proof-of-concept patch.

 arch/sandbox/Kconfig  |   3 +
 arch/sandbox/cpu/Makefile |  10 +++
 arch/sandbox/cpu/eth-raw-os.c | 140 ++
 arch/sandbox/dts/sandbox.dts  |   6 ++
 arch/sandbox/include/asm/eth-raw-os.h |  32 
 board/sandbox/README.sandbox  |  52 +
 drivers/net/Kconfig   |  10 +++
 drivers/net/Makefile  |   1 +
 drivers/net/sandbox-raw.c |  98 
 9 files changed, 352 insertions(+)
 create mode 100644 arch/sandbox/cpu/eth-raw-os.c
 create mode 100644 arch/sandbox/include/asm/eth-raw-os.h
 create mode 100644 drivers/net/sandbox-raw.c

diff --git a/arch/sandbox/Kconfig b/arch/sandbox/Kconfig
index 186b58d..f84b3fc 100644
--- a/arch/sandbox/Kconfig
+++ b/arch/sandbox/Kconfig
@@ -43,4 +43,7 @@ config NETDEVICES
 config DM_ETH
default y
 
+config ETH_SANDBOX_RAW
+   default y
+
 endmenu
diff --git a/arch/sandbox/cpu/Makefile b/arch/sandbox/cpu/Makefile
index 7d4410c..1b42fee 100644
--- a/arch/sandbox/cpu/Makefile
+++ b/arch/sandbox/cpu/Makefile
@@ -8,6 +8,7 @@
 #
 
 obj-y  := cpu.o os.o start.o state.o
+obj-$(CONFIG_ETH_SANDBOX_RAW)  += eth-raw-os.o
 obj-$(CONFIG_SANDBOX_SDL)  += sdl.o
 
 # os.c is build in the system environment, so needs standard includes
@@ -20,3 +21,12 @@ $(obj)/os.o: $(src)/os.c FORCE
$(call if_changed_dep,cc_os.o)
 $(obj)/sdl.o: $(src)/sdl.c FORCE
$(call if_changed_dep,cc_os.o)
+
+# eth-raw-os.c is built in the system env, so needs standard includes
+# CFLAGS_REMOVE_eth-raw-os.o cannot be used to drop header include path
+quiet_cmd_cc_eth-raw-os.o = CC $(quiet_modtag)  $@
+cmd_cc_eth-raw-os.o = $(CC) $(filter-out -nostdinc, \
+   $(patsubst -I%,-idirafter%,$(c_flags))) -c -o $@ $
+
+$(obj)/eth-raw-os.o: $(src)/eth-raw-os.c FORCE
+   $(call if_changed_dep,cc_eth-raw-os.o)
diff --git a/arch/sandbox/cpu/eth-raw-os.c b/arch/sandbox/cpu/eth-raw-os.c
new file mode 100644
index 000..601205a
--- /dev/null
+++ b/arch/sandbox/cpu/eth-raw-os.c
@@ -0,0 +1,140 @@
+/*
+ * Copyright (c) 2015 National Instruments
+ *
+ * (C) Copyright 2015
+ * Joe Hershberger joe.hershber...@ni.com
+ *
+ * SPDX-License-Identifier:GPL-2.0
+ */
+
+#include asm/eth-raw-os.h
+#include errno.h
+#include fcntl.h
+#include net/if.h
+#include netinet/in.h
+#include stdio.h
+#include stdlib.h
+#include string.h
+#include sys/types.h
+#include sys/ioctl.h
+#include sys/socket.h
+#include unistd.h
+
+#include linux/if_ether.h
+#include linux/if_packet.h
+
+int sandbox_eth_raw_os_start(const char *ifname, unsigned char *ethmac,
+   struct eth_sandbox_raw_priv *priv)
+{
+   struct sockaddr_ll *device;
+   struct packet_mreq mr;
+   int ret;
+   int flags;
+
+   /* Prepare device struct */
+   priv-device = malloc(sizeof(struct sockaddr_ll));
+   if (priv-device == NULL)
+   return -ENOMEM;
+   device = priv-device;
+   memset(device, 0, sizeof(struct sockaddr_ll));
+   device-sll_ifindex = if_nametoindex(ifname);
+   device-sll_family = AF_PACKET;
+   memcpy(device-sll_addr, ethmac, 6);
+   device-sll_halen = htons(6);
+
+   /* Open socket */
+   priv-sd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
+   if (priv-sd  0) {
+   printf(Failed to open socket: %d %s\n, errno,
+  strerror(errno));
+   return -errno;
+   }
+   /* Bind to the specified interface */
+   ret =