toplevel: remove error handling from qemu_malloc() callers (Avi Kivity)
[qemu] / net.c
diff --git a/net.c b/net.c
index 8bba09c..e7c097e 100644 (file)
--- a/net.c
+++ b/net.c
@@ -333,8 +333,6 @@ VLANClientState *qemu_new_vlan_client(VLANState *vlan,
 {
     VLANClientState *vc, **pvc;
     vc = qemu_mallocz(sizeof(VLANClientState));
-    if (!vc)
-        return NULL;
     vc->model = strdup(model);
     if (name)
         vc->name = strdup(name);
@@ -421,6 +419,16 @@ static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
     return offset;
 }
 
+static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
+{
+    size_t offset = 0;
+    int i;
+
+    for (i = 0; i < iovcnt; i++)
+        offset += iov[i].iov_len;
+    return offset;
+}
+
 ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov,
                           int iovcnt)
 {
@@ -428,12 +436,17 @@ ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov,
     VLANClientState *vc;
     ssize_t max_len = 0;
 
+    if (vc1->link_down)
+        return calc_iov_length(iov, iovcnt);
+
     for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
         ssize_t len = 0;
 
         if (vc == vc1)
             continue;
 
+        if (vc->link_down)
+            len = calc_iov_length(iov, iovcnt);
         if (vc->fd_readv)
             len = vc->fd_readv(vc->opaque, iov, iovcnt);
         else if (vc->fd_read)
@@ -652,6 +665,7 @@ typedef struct TAPState {
     VLANClientState *vc;
     int fd;
     char down_script[1024];
+    char down_script_arg[128];
 } TAPState;
 
 #ifdef HAVE_IOVEC
@@ -712,8 +726,6 @@ static TAPState *net_tap_fd_init(VLANState *vlan,
     TAPState *s;
 
     s = qemu_mallocz(sizeof(TAPState));
-    if (!s)
-        return NULL;
     s->fd = fd;
     s->vc = qemu_new_vlan_client(vlan, model, name, tap_receive, NULL, s);
 #ifdef HAVE_IOVEC
@@ -978,8 +990,10 @@ static int net_tap_init(VLANState *vlan, const char *model,
     snprintf(s->vc->info_str, sizeof(s->vc->info_str),
              "ifname=%s,script=%s,downscript=%s",
              ifname, setup_script, down_script);
-    if (down_script && strcmp(down_script, "no"))
+    if (down_script && strcmp(down_script, "no")) {
         snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
+        snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
+    }
     return 0;
 }
 
@@ -1031,8 +1045,6 @@ static int net_vde_init(VLANState *vlan, const char *model,
     };
 
     s = qemu_mallocz(sizeof(VDEState));
-    if (!s)
-        return -1;
     s->vde = vde_open(init_sock, "QEMU", &args);
     if (!s->vde){
         free(s);
@@ -1256,8 +1268,6 @@ static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
     }
 
     s = qemu_mallocz(sizeof(NetSocketState));
-    if (!s)
-        return NULL;
     s->fd = fd;
 
     s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive_dgram, NULL, s);
@@ -1286,8 +1296,6 @@ static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
 {
     NetSocketState *s;
     s = qemu_mallocz(sizeof(NetSocketState));
-    if (!s)
-        return NULL;
     s->fd = fd;
     s->vc = qemu_new_vlan_client(vlan, model, name,
                                  net_socket_receive, NULL, s);
@@ -1365,8 +1373,6 @@ static int net_socket_listen_init(VLANState *vlan,
         return -1;
 
     s = qemu_mallocz(sizeof(NetSocketListenState));
-    if (!s)
-        return -1;
 
     fd = socket(PF_INET, SOCK_STREAM, 0);
     if (fd < 0) {
@@ -1486,8 +1492,6 @@ VLANState *qemu_find_vlan(int id)
             return vlan;
     }
     vlan = qemu_mallocz(sizeof(VLANState));
-    if (!vlan)
-        return NULL;
     vlan->id = id;
     vlan->next = NULL;
     pvlan = &first_vlan;
@@ -1770,13 +1774,10 @@ void net_cleanup(void)
 
         for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
             if (vc->fd_read == tap_receive) {
-                char ifname[64];
                 TAPState *s = vc->opaque;
 
-                if (strcmp(vc->model, "tap") == 0 &&
-                    sscanf(vc->info_str, "ifname=%63s ", ifname) == 1 &&
-                    s->down_script[0])
-                    launch_script(s->down_script, ifname, s->fd);
+                if (s->down_script[0])
+                    launch_script(s->down_script, s->down_script_arg, s->fd);
             }
 #if defined(CONFIG_VDE)
             if (vc->fd_read == vde_from_qemu) {