toplevel: remove error handling from qemu_malloc() callers (Avi Kivity)
[qemu] / net.c
diff --git a/net.c b/net.c
index fa9fa94..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;
@@ -1497,6 +1501,40 @@ VLANState *qemu_find_vlan(int id)
     return vlan;
 }
 
+void qemu_check_nic_model(NICInfo *nd, const char *model)
+{
+    const char *models[2];
+
+    models[0] = model;
+    models[1] = NULL;
+
+    qemu_check_nic_model_list(nd, models, model);
+}
+
+void qemu_check_nic_model_list(NICInfo *nd, const char * const *models,
+                               const char *default_model)
+{
+    int i, exit_status = 0;
+
+    if (!nd->model)
+        nd->model = strdup(default_model);
+
+    if (strcmp(nd->model, "?") != 0) {
+        for (i = 0 ; models[i]; i++)
+            if (strcmp(nd->model, models[i]) == 0)
+                return;
+
+        fprintf(stderr, "qemu: Unsupported NIC model: %s\n", nd->model);
+        exit_status = 1;
+    }
+
+    fprintf(stderr, "qemu: Supported NIC models: ");
+    for (i = 0 ; models[i]; i++)
+        fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
+
+    exit(exit_status);
+}
+
 int net_client_init(const char *device, const char *p)
 {
     char buf[1024];
@@ -1703,7 +1741,8 @@ int do_set_link(const char *name, const char *up_or_down)
     for (vlan = first_vlan; vlan != NULL; vlan = vlan->next)
         for (vc = vlan->first_client; vc != NULL; vc = vc->next)
             if (strcmp(vc->name, name) == 0)
-                break;
+                goto done;
+done:
 
     if (!vc) {
         term_printf("could not find network device '%s'", name);
@@ -1735,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) {