diff mbox

Support for UDP unicast network backend

Message ID 4EB6F42D.5040904@gmail.com
State New
Headers show

Commit Message

Benjamin Nov. 6, 2011, 8:55 p.m. UTC
Follow-up of: 
http://www.mail-archive.com/qemu-devel@nongnu.org/msg81235.html

This enables connections between Qemu, Dynamips and VirtualBox guests.

Test it with:

qemu-system-i386 -netdev 
socket,id=gns3,udp=127.0.0.1:4243,localport=127.0.0.1:4242 -device 
e1000,netdev=gns3 /path/to/hard/drive/img1

qemu-system-i386 -netdev 
socket,id=gns3,udp=127.0.0.1:4242,localport=127.0.0.1:4243 -device 
e1000,netdev=gns3 /path/to/hard/drive/img2

You should be able to set up a network between these two hosts.

Any thoughts?

I noticed an interesting behavior of Qemu and I'd like to investigate.
When an IP address is assigned to an interface it sends a who-has ARP
request with its own address. Which is not what happens on my OS. What
would be the correct and standard behavior?

Signed-off-by: Benjamin MARSILI <marsil_b@epitech.eu>
---
  net.c           |   11 ++++++++-
  net/socket.c    |   66 
+++++++++++++++++++++++++++++++++++++++++++++++++++++-
  qemu-options.hx |    2 +
  3 files changed, 76 insertions(+), 3 deletions(-)

tunnel\n"
  #ifdef CONFIG_VDE
      "-net 
vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
      "                connect the vlan 'n' to port 'n' of a vde switch 
running\n"

Comments

Jan Kiszka Nov. 6, 2011, 1:54 p.m. UTC | #1
On 2011-11-06 21:55, Benjamin wrote:
> Follow-up of:
> http://www.mail-archive.com/qemu-devel@nongnu.org/msg81235.html
> 
> This enables connections between Qemu, Dynamips and VirtualBox guests.
> 
> Test it with:
> 
> qemu-system-i386 -netdev
> socket,id=gns3,udp=127.0.0.1:4243,localport=127.0.0.1:4242 -device
> e1000,netdev=gns3 /path/to/hard/drive/img1
> 
> qemu-system-i386 -netdev
> socket,id=gns3,udp=127.0.0.1:4242,localport=127.0.0.1:4243 -device
> e1000,netdev=gns3 /path/to/hard/drive/img2
> 
> You should be able to set up a network between these two hosts.
> 
> Any thoughts?

Looks good to me. Just one thing: localaddr vs. localport is
unfortunate. localport is actual "local host and port". So localaddr
would be a better fit, even with mcast continuing to expect it without
":port".

> 
> I noticed an interesting behavior of Qemu and I'd like to investigate.
> When an IP address is assigned to an interface it sends a who-has ARP
> request with its own address. Which is not what happens on my OS. What
> would be the correct and standard behavior?

That's done by many OSes to discover IP conflicts early. Has nothing to
do with QEMU.

Jan
Benjamin Nov. 6, 2011, 2:28 p.m. UTC | #2
Here's the link, replace ' AT ' by '@', the server thought it was an
email address.
http://www.mail-archive.com/qemu-devel AT
nongnu.org/msg81235.html<http://www.mail-archive.com/qemu-devel@nongnu.org/msg81235.html>

As I've been notified, localport should disappear and localaddr should be
used to
specify the local host AND port. I'll update the patch and send it back
tomorrow then.

On Mon, Nov 7, 2011 at 5:55 AM, Benjamin <mlspirat42@gmail.com> wrote:

> Follow-up of: http://www.mail-archive.com/**qemu-devel@nongnu.org/**
> msg81235.html<http://www.mail-archive.com/qemu-devel@nongnu.org/msg81235.html>
>
> This enables connections between Qemu, Dynamips and VirtualBox guests.
>
> Test it with:
>
> qemu-system-i386 -netdev socket,id=gns3,udp=127.0.0.1:**4243<http://127.0.0.1:4243>
> ,localport=127.0.0.1:4242 -device e1000,netdev=gns3
> /path/to/hard/drive/img1
>
> qemu-system-i386 -netdev socket,id=gns3,udp=127.0.0.1:**4242<http://127.0.0.1:4242>
> ,localport=127.0.0.1:4243 -device e1000,netdev=gns3
> /path/to/hard/drive/img2
>
> You should be able to set up a network between these two hosts.
>
> Any thoughts?
>
> I noticed an interesting behavior of Qemu and I'd like to investigate.
> When an IP address is assigned to an interface it sends a who-has ARP
> request with its own address. Which is not what happens on my OS. What
> would be the correct and standard behavior?
>
> Signed-off-by: Benjamin MARSILI <marsil_b@epitech.eu>
> ---
>  net.c           |   11 ++++++++-
>  net/socket.c    |   66 ++++++++++++++++++++++++++++++**
> +++++++++++++++++++++++-
>  qemu-options.hx |    2 +
>  3 files changed, 76 insertions(+), 3 deletions(-)
>
> diff --git a/net.c b/net.c
> index cb52050..e70f1c8 100644
> --- a/net.c
> +++ b/net.c
> @@ -1000,6 +1000,14 @@ static const struct {
>                 .name = "localaddr",
>                 .type = QEMU_OPT_STRING,
>                 .help = "source address for multicast packets",
> +            }, {
> +                .name = "udp",
> +                .type = QEMU_OPT_STRING,
> +                .help = "UDP unicast address and port number",
> +            }, {
> +                .name = "localport",
> +                .type = QEMU_OPT_STRING,
> +                .help = "UDP unicast bind address and port number",
>             },
>             { /* end of list */ }
>         },
> @@ -1070,7 +1078,8 @@ int net_client_init(Monitor *mon, QemuOpts *opts,
> int is_netdev)
>  #ifdef CONFIG_VDE
>             strcmp(type, "vde") != 0 &&
>  #endif
> -            strcmp(type, "socket") != 0) {
> +            strcmp(type, "socket") != 0 &&
> +            strcmp(type, "udp") != 0) {
>             qerror_report(QERR_INVALID_**PARAMETER_VALUE, "type",
>                           "a netdev backend type");
>             return -1;
> diff --git a/net/socket.c b/net/socket.c
> index e9ef128..306e54b 100644
> --- a/net/socket.c
> +++ b/net/socket.c
> @@ -524,6 +524,52 @@ static int net_socket_mcast_init(**VLANState *vlan,
>
>  }
>
> +static int net_socket_udp_init(VLANState *vlan,
> +                                 const char *model,
> +                                 const char *name,
> +                                 const char *rhost,
> +                                 const char *lhost)
> +{
> +    NetSocketState *s;
> +    int fd, val, ret;
> +    struct sockaddr_in laddr, raddr;
> +
> +    if (parse_host_port(&laddr, lhost) < 0)
> +        return -1;
> +
> +    if (parse_host_port(&raddr, rhost) < 0)
> +        return -1;
> +
> +    fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
> +    if (fd < 0) {
> +        perror("socket(PF_INET, SOCK_DGRAM)");
> +        return -1;
> +    }
> +    val = 1;
> +    ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
> +                   (const char *)&val, sizeof(val));
> +    if (ret < 0) {
> +       perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
> +        return -1;
> +    }
> +    ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
> +    if (ret < 0) {
> +        perror("bind");
> +        return -1;
> +    }
> +
> +    s = net_socket_fd_init(vlan, model, name, fd, 0);
> +    if (!s)
> +        return -1;
> +
> +    s->dgram_dst = raddr;
> +
> +    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
> +             "socket: udp=%s:%d",
> +             inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port));
> +    return 0;
> +}
> +
>  int net_init_socket(QemuOpts *opts,
>                     Monitor *mon,
>                     const char *name,
> @@ -597,10 +643,26 @@ int net_init_socket(QemuOpts *opts,
>         if (net_socket_mcast_init(vlan, "socket", name, mcast, localaddr)
> == -1) {
>             return -1;
>         }
> +    } else if (qemu_opt_get(opts, "udp")) {
> +        const char *udp, *localport;
> +
> +        if (qemu_opt_get(opts, "fd") ||
> +            qemu_opt_get(opts, "connect") ||
> +            qemu_opt_get(opts, "listen") ||
> +            qemu_opt_get(opts, "mcast")) {
> +            error_report("fd=, connect=, listen=, mcast=, localaddr= is
> invalid with udp=");
> +            return -1;
> +        }
> +
> +        udp = qemu_opt_get(opts, "udp");
> +        localport = qemu_opt_get(opts, "localport");
> +
> +        if (net_socket_udp_init(vlan, "udp", name, udp, localport) == -1)
> {
> +            return -1;
> +        }
>     } else {
> -        error_report("-socket requires fd=, listen=, connect= or mcast=");
> +        error_report("-socket requires fd=, listen=, connect=, mcast= or
> udp=");
>         return -1;
>     }
> -
>     return 0;
>  }
> diff --git a/qemu-options.hx b/qemu-options.hx
> index 681eaf1..109feb0 100644
> --- a/qemu-options.hx
> +++ b/qemu-options.hx
> @@ -1217,6 +1217,8 @@ DEF("net", HAS_ARG, QEMU_OPTION_net,
>     "-net socket[,vlan=n][,name=str][,**fd=h][,mcast=maddr:port[,**
> localaddr=addr]]\n"
>     "                connect the vlan 'n' to multicast maddr and port\n"
>     "                use 'localaddr=addr' to specify the host address to
> send packets from\n"
> +    "-net socket[,vlan=n][,name=str][,**fd=h][,udp=host:port][,**
> localport=host:port]\n"
> +    "                connect the vlan 'n' to another VLAN using an UDP
> tunnel\n"
>  #ifdef CONFIG_VDE
>     "-net vde[,vlan=n][,name=str][,sock=**socketpath][,port=n][,group=**
> groupname][,mode=octalmode]\n"
>     "                connect the vlan 'n' to port 'n' of a vde switch
> running\n"
> --
> 1.7.3.5
>
diff mbox

Patch

diff --git a/net.c b/net.c
index cb52050..e70f1c8 100644
--- a/net.c
+++ b/net.c
@@ -1000,6 +1000,14 @@  static const struct {
                  .name = "localaddr",
                  .type = QEMU_OPT_STRING,
                  .help = "source address for multicast packets",
+            }, {
+                .name = "udp",
+                .type = QEMU_OPT_STRING,
+                .help = "UDP unicast address and port number",
+            }, {
+                .name = "localport",
+                .type = QEMU_OPT_STRING,
+                .help = "UDP unicast bind address and port number",
              },
              { /* end of list */ }
          },
@@ -1070,7 +1078,8 @@  int net_client_init(Monitor *mon, QemuOpts *opts, 
int is_netdev)
  #ifdef CONFIG_VDE
              strcmp(type, "vde") != 0 &&
  #endif
-            strcmp(type, "socket") != 0) {
+            strcmp(type, "socket") != 0 &&
+            strcmp(type, "udp") != 0) {
              qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
                            "a netdev backend type");
              return -1;
diff --git a/net/socket.c b/net/socket.c
index e9ef128..306e54b 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -524,6 +524,52 @@  static int net_socket_mcast_init(VLANState *vlan,

  }

+static int net_socket_udp_init(VLANState *vlan,
+                                 const char *model,
+                                 const char *name,
+                                 const char *rhost,
+                                 const char *lhost)
+{
+    NetSocketState *s;
+    int fd, val, ret;
+    struct sockaddr_in laddr, raddr;
+
+    if (parse_host_port(&laddr, lhost) < 0)
+        return -1;
+
+    if (parse_host_port(&raddr, rhost) < 0)
+        return -1;
+
+    fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
+    if (fd < 0) {
+        perror("socket(PF_INET, SOCK_DGRAM)");
+        return -1;
+    }
+    val = 1;
+    ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
+                   (const char *)&val, sizeof(val));
+    if (ret < 0) {
+       perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
+        return -1;
+    }
+    ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
+    if (ret < 0) {
+        perror("bind");
+        return -1;
+    }
+
+    s = net_socket_fd_init(vlan, model, name, fd, 0);
+    if (!s)
+        return -1;
+
+    s->dgram_dst = raddr;
+
+    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
+             "socket: udp=%s:%d",
+             inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port));
+    return 0;
+}
+
  int net_init_socket(QemuOpts *opts,
                      Monitor *mon,
                      const char *name,
@@ -597,10 +643,26 @@  int net_init_socket(QemuOpts *opts,
          if (net_socket_mcast_init(vlan, "socket", name, mcast, 
localaddr) == -1) {
              return -1;
          }
+    } else if (qemu_opt_get(opts, "udp")) {
+        const char *udp, *localport;
+
+        if (qemu_opt_get(opts, "fd") ||
+            qemu_opt_get(opts, "connect") ||
+            qemu_opt_get(opts, "listen") ||
+            qemu_opt_get(opts, "mcast")) {
+            error_report("fd=, connect=, listen=, mcast=, localaddr= is 
invalid with udp=");
+            return -1;
+        }
+
+        udp = qemu_opt_get(opts, "udp");
+        localport = qemu_opt_get(opts, "localport");
+
+        if (net_socket_udp_init(vlan, "udp", name, udp, localport) == -1) {
+            return -1;
+        }
      } else {
-        error_report("-socket requires fd=, listen=, connect= or mcast=");
+        error_report("-socket requires fd=, listen=, connect=, mcast= 
or udp=");
          return -1;
      }
-
      return 0;
  }
diff --git a/qemu-options.hx b/qemu-options.hx
index 681eaf1..109feb0 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -1217,6 +1217,8 @@  DEF("net", HAS_ARG, QEMU_OPTION_net,
      "-net 
socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port[,localaddr=addr]]\n"
      "                connect the vlan 'n' to multicast maddr and port\n"
      "                use 'localaddr=addr' to specify the host address 
to send packets from\n"
+    "-net 
socket[,vlan=n][,name=str][,fd=h][,udp=host:port][,localport=host:port]\n"
+    "                connect the vlan 'n' to another VLAN using an UDP