Beispiel #1
0
/* Create a new interface */
static int ethsw_cfg_create_if(ethsw_table_t *t,char **tokens,int count)
{
   netio_desc_t *nio = NULL;
   int nio_type;

   nio_type = netio_get_type(tokens[2]);
   switch(nio_type) {
      case NETIO_TYPE_UNIX:
         if (count != 5) {
            fprintf(stderr,"ETHSW: invalid number of arguments "
                    "for UNIX NIO\n");
            break;
         }

         nio = netio_desc_create_unix(tokens[1],tokens[3],tokens[4]);
         break;

      case NETIO_TYPE_TAP:
         if (count != 4) {
            fprintf(stderr,"ETHSW: invalid number of arguments "
                    "for TAP NIO\n");
            break;
         }

         nio = netio_desc_create_tap(tokens[1],tokens[3]);
         break;

      case NETIO_TYPE_UDP:
         if (count != 6) {
            fprintf(stderr,"ETHSW: invalid number of arguments "
                    "for UDP NIO\n");
            break;
         }

         nio = netio_desc_create_udp(tokens[1],atoi(tokens[3]),
                                     tokens[4],atoi(tokens[5]));
         break;

      case NETIO_TYPE_MCAST:
         if (count != 5) {
            fprintf(stderr,"ETHSW: invalid number of arguments "
                    "for Multicast NIO\n");
            break;
         }

         nio = netio_desc_create_mcast(tokens[1],tokens[3],atoi(tokens[4]));
         break;

      case NETIO_TYPE_TCP_CLI:
         if (count != 5) {
            fprintf(stderr,"ETHSW: invalid number of arguments "
                    "for TCP CLI NIO\n");
            break;
         }

         nio = netio_desc_create_tcp_cli(tokens[1],tokens[3],tokens[4]);
         break;

      case NETIO_TYPE_TCP_SER:
         if (count != 4) {
            fprintf(stderr,"ETHSW: invalid number of arguments "
                    "for TCP SER NIO\n");
            break;
         }

         nio = netio_desc_create_tcp_ser(tokens[1],tokens[3]);
         break;

#ifdef GEN_ETH
      case NETIO_TYPE_GEN_ETH:
         if (count != 4) {
            fprintf(stderr,"ETHSW: invalid number of arguments "
                    "for Generic Ethernet NIO\n");
            break;
         }

         nio = netio_desc_create_geneth(tokens[1],tokens[3]);
         break;
#endif

#ifdef LINUX_ETH
      case NETIO_TYPE_LINUX_ETH:
         if (count != 4) {
            fprintf(stderr,"ETHSW: invalid number of arguments "
                    "for Linux Ethernet NIO\n");
            break;
         }

         nio = netio_desc_create_lnxeth(tokens[1],tokens[3]);
         break;
#endif

      default:
         fprintf(stderr,"ETHSW: unknown/invalid NETIO type '%s'\n",
                 tokens[2]);
   }

   if (!nio) {
      fprintf(stderr,"ETHSW: unable to create NETIO descriptor\n");
      return(-1);
   }

   if (ethsw_add_netio(t,tokens[1]) == -1) {
      fprintf(stderr,"ETHSW: unable to add NETIO descriptor.\n");
      netio_release(nio->name);
      return(-1);
   }

   netio_release(nio->name);
   return(0);
}
/* Add a Network IO descriptor binding (command line) */
int vm_slot_cmd_add_nio(vm_instance_t *vm,char *str)
{
   char *tokens[SLOT_DESC_MAX_TOKENS];
   int i,count,nio_type,res=-1;
   u_int slot_id,port_id;
   netio_desc_t *nio;
   char nio_name[128];

   /* A NIO binding description is like "1:3:tap:tap0" */
   if ((count = m_strsplit(str,':',tokens,SLOT_DESC_MAX_TOKENS)) < 3) {
      vm_error(vm,"unable to parse NIO description '%s'.\n",str);
      return(-1);
   }

   /* Parse the slot id */
   slot_id = atoi(tokens[0]);

   /* Parse the port id */
   port_id = atoi(tokens[1]);

   /* Autogenerate a NIO name */
   snprintf(nio_name,sizeof(nio_name),"%s-i%u/%u/%u",
            vm_get_type(vm),vm->instance_id,slot_id,port_id);

   /* Create the Network IO descriptor */
   nio = NULL;
   nio_type = netio_get_type(tokens[2]);

   switch(nio_type) {
      case NETIO_TYPE_UNIX:
         if (count != 5) {
            vm_error(vm,"invalid number of arguments for UNIX NIO '%s'\n",str);
            goto done;
         }

         nio = netio_desc_create_unix(nio_name,tokens[3],tokens[4]);
         break;

      case NETIO_TYPE_VDE:
         if (count != 5) {
            vm_error(vm,"invalid number of arguments for VDE NIO '%s'\n",str);
            goto done;
         }

         nio = netio_desc_create_vde(nio_name,tokens[3],tokens[4]);
         break;

      case NETIO_TYPE_TAP:
         if (count != 4) {
            vm_error(vm,"invalid number of arguments for TAP NIO '%s'\n",str);
            goto done;
         }

         nio = netio_desc_create_tap(nio_name,tokens[3]);
         break;

      case NETIO_TYPE_UDP:
         if (count != 6) {
            vm_error(vm,"invalid number of arguments for UDP NIO '%s'\n",str);
            goto done;
         }

         nio = netio_desc_create_udp(nio_name,atoi(tokens[3]),
                                     tokens[4],atoi(tokens[5]));
         break;

      case NETIO_TYPE_TCP_CLI:
         if (count != 5) {
            vm_error(vm,"invalid number of arguments for TCP CLI NIO '%s'\n",
                     str);
            goto done;
         }

         nio = netio_desc_create_tcp_cli(nio_name,tokens[3],tokens[4]);
         break;

      case NETIO_TYPE_TCP_SER:
         if (count != 4) {
            vm_error(vm,"invalid number of arguments for TCP SER NIO '%s'\n",
                     str);
            goto done;
         }

         nio = netio_desc_create_tcp_ser(nio_name,tokens[3]);
         break;

      case NETIO_TYPE_NULL:
         nio = netio_desc_create_null(nio_name);
         break;

#ifdef LINUX_ETH
      case NETIO_TYPE_LINUX_ETH:
         if (count != 4) {
            vm_error(vm,"invalid number of arguments for Linux Eth NIO '%s'\n",
                     str);
            goto done;
         }
         
         nio = netio_desc_create_lnxeth(nio_name,tokens[3]);
         break;
#endif

#ifdef GEN_ETH
      case NETIO_TYPE_GEN_ETH:
         if (count != 4) {
            vm_error(vm,
                     "invalid number of arguments for Generic Eth NIO '%s'\n",
                     str);
            goto done;
         }
         
         nio = netio_desc_create_geneth(nio_name,tokens[3]);
         break;
#endif

      default:
         vm_error(vm,"unknown NETIO type '%s'\n",tokens[2]);
         goto done;
   }

   if (!nio) {
      vm_error(vm,"unable to create NETIO descriptor for slot %u\n",slot_id);
      goto done;
   }

   if (vm_slot_add_nio_binding(vm,slot_id,port_id,nio_name) == -1) {
      vm_error(vm,"unable to add NETIO binding for slot %u\n",slot_id);
      netio_release(nio_name);
      netio_delete(nio_name);
      goto done;
   }
   
   netio_release(nio_name);
   res = 0;

 done:
   /* The complete array was cleaned by strsplit */
   for(i=0;i<SLOT_DESC_MAX_TOKENS;i++)
      free(tokens[i]);

   return(res);
}