📄 dev_c2691.c
字号:
} bay->dev_type = nm_driver->dev_type; bay->nm_driver = nm_driver; return(0); }/* Remove a NM binding */int c2691_nm_remove_binding(c2691_t *router,u_int nm_bay){ struct c2691_nm_bay *bay; if (!(bay = c2691_nm_get_info(router,nm_bay))) return(-1); /* stop if this bay is still active */ if (bay->drv_info != NULL) { vm_error(router->vm,"slot %u still active.\n",nm_bay); return(-1); } /* check that this bay is not empty */ if (bay->dev_type == NULL) { vm_error(router->vm,"slot %u is empty.\n",nm_bay); return(-1); } /* remove all NIOs bindings */ c2691_nm_remove_all_nio_bindings(router,nm_bay); bay->dev_type = NULL; bay->nm_driver = NULL; return(0);}/* Find a NIO binding */struct c2691_nio_binding *c2691_nm_find_nio_binding(c2691_t *router,u_int nm_bay,u_int port_id){ struct c2691_nio_binding *nb; struct c2691_nm_bay *bay; if (!(bay = c2691_nm_get_info(router,nm_bay))) return NULL; for(nb=bay->nio_list;nb;nb=nb->next) if (nb->port_id == port_id) return nb; return NULL;}/* Add a network IO binding */int c2691_nm_add_nio_binding(c2691_t *router,u_int nm_bay,u_int port_id, char *nio_name){ struct c2691_nio_binding *nb; struct c2691_nm_bay *bay; netio_desc_t *nio; if (!(bay = c2691_nm_get_info(router,nm_bay))) return(-1); /* check that a NIO is not already bound to this port */ if (c2691_nm_find_nio_binding(router,nm_bay,port_id) != NULL) { vm_error(router->vm,"a NIO already exists for interface %u/%u.\n", nm_bay,port_id); return(-1); } /* acquire a reference on the NIO object */ if (!(nio = netio_acquire(nio_name))) { vm_error(router->vm,"unable to find NIO '%s'.\n",nio_name); return(-1); } /* create a new binding */ if (!(nb = malloc(sizeof(*nb)))) { vm_error(router->vm,"unable to create NIO binding " "for interface %u/%u.\n",nm_bay,port_id); netio_release(nio_name); return(-1); } memset(nb,0,sizeof(*nb)); nb->nio = nio; nb->port_id = port_id; nb->next = bay->nio_list; if (nb->next) nb->next->prev = nb; bay->nio_list = nb; return(0);}/* Remove a NIO binding */int c2691_nm_remove_nio_binding(c2691_t *router,u_int nm_bay,u_int port_id){ struct c2691_nio_binding *nb; struct c2691_nm_bay *bay; if (!(bay = c2691_nm_get_info(router,nm_bay))) return(-1); if (!(nb = c2691_nm_find_nio_binding(router,nm_bay,port_id))) return(-1); /* no nio binding for this slot/port */ /* tell the NM driver to stop using this NIO */ if (bay->nm_driver) bay->nm_driver->nm_unset_nio(router,nm_bay,port_id); /* remove this entry from the double linked list */ if (nb->next) nb->next->prev = nb->prev; if (nb->prev) { nb->prev->next = nb->next; } else { bay->nio_list = nb->next; } /* unreference NIO object */ netio_release(nb->nio->name); free(nb); return(0);}/* Remove all NIO bindings for the specified NM */int c2691_nm_remove_all_nio_bindings(c2691_t *router,u_int nm_bay){ struct c2691_nio_binding *nb,*next; struct c2691_nm_bay *bay; if (!(bay = c2691_nm_get_info(router,nm_bay))) return(-1); for(nb=bay->nio_list;nb;nb=next) { next = nb->next; /* tell the NM driver to stop using this NIO */ if (bay->nm_driver) bay->nm_driver->nm_unset_nio(router,nm_bay,nb->port_id); /* unreference NIO object */ netio_release(nb->nio->name); free(nb); } bay->nio_list = NULL; return(0);}/* Enable a Network IO descriptor for a Network Module */int c2691_nm_enable_nio(c2691_t *router,u_int nm_bay,u_int port_id){ struct c2691_nio_binding *nb; struct c2691_nm_bay *bay; if (!(bay = c2691_nm_get_info(router,nm_bay))) return(-1); /* check that we have an NIO binding for this interface */ if (!(nb = c2691_nm_find_nio_binding(router,nm_bay,port_id))) return(-1); /* check that the driver is defined and successfully initialized */ if (!bay->nm_driver || !bay->drv_info) return(-1); return(bay->nm_driver->nm_set_nio(router,nm_bay,port_id,nb->nio));}/* Disable Network IO descriptor of a Network Module */int c2691_nm_disable_nio(c2691_t *router,u_int nm_bay,u_int port_id){ struct c2691_nm_bay *bay; if (!(bay = c2691_nm_get_info(router,nm_bay))) return(-1); /* check that the driver is defined and successfully initialized */ if (!bay->nm_driver || !bay->drv_info) return(-1); return(bay->nm_driver->nm_unset_nio(router,nm_bay,port_id));}/* Enable all NIO of the specified NM */int c2691_nm_enable_all_nio(c2691_t *router,u_int nm_bay){ struct c2691_nio_binding *nb; struct c2691_nm_bay *bay; if (!(bay = c2691_nm_get_info(router,nm_bay))) return(-1); /* check that the driver is defined and successfully initialized */ if (!bay->nm_driver || !bay->drv_info) return(-1); for(nb=bay->nio_list;nb;nb=nb->next) bay->nm_driver->nm_set_nio(router,nm_bay,nb->port_id,nb->nio); return(0);}/* Disable all NIO of the specified NM */int c2691_nm_disable_all_nio(c2691_t *router,u_int nm_bay){ struct c2691_nio_binding *nb; struct c2691_nm_bay *bay; if (!(bay = c2691_nm_get_info(router,nm_bay))) return(-1); /* check that the driver is defined and successfully initialized */ if (!bay->nm_driver || !bay->drv_info) return(-1); for(nb=bay->nio_list;nb;nb=nb->next) bay->nm_driver->nm_unset_nio(router,nm_bay,nb->port_id); return(0);}/* Initialize a Network Module */int c2691_nm_init(c2691_t *router,u_int nm_bay){ struct c2691_nm_bay *bay; size_t len; if (!(bay = c2691_nm_get_info(router,nm_bay))) return(-1); /* Check that a device type is defined for this bay */ if (!bay->dev_type || !bay->nm_driver) { vm_error(router->vm,"trying to init empty slot %u.\n",nm_bay); return(-1); } /* Allocate device name */ len = strlen(bay->dev_type) + 10; if (!(bay->dev_name = malloc(len))) { vm_error(router->vm,"unable to allocate device name.\n"); return(-1); } snprintf(bay->dev_name,len,"%s(%u)",bay->dev_type,nm_bay); /* Initialize NM driver */ if (bay->nm_driver->nm_init(router,bay->dev_name,nm_bay) == 1) { vm_error(router->vm,"unable to initialize NM %u.\n",nm_bay); return(-1); } /* Enable all NIO */ c2691_nm_enable_all_nio(router,nm_bay); return(0);}/* Shutdown a Network Module */int c2691_nm_shutdown(c2691_t *router,u_int nm_bay){ struct c2691_nm_bay *bay; if (!(bay = c2691_nm_get_info(router,nm_bay))) return(-1); /* Check that a device type is defined for this bay */ if (!bay->dev_type || !bay->nm_driver) { vm_error(router->vm,"trying to shut down empty slot %u.\n",nm_bay); return(-1); } /* Disable all NIO */ c2691_nm_disable_all_nio(router,nm_bay); /* Shutdown the NM driver */ if (bay->drv_info && (bay->nm_driver->nm_shutdown(router,nm_bay) == -1)) { vm_error(router->vm,"unable to shutdown NM %u.\n",nm_bay); return(-1); } free(bay->dev_name); bay->dev_name = NULL; bay->drv_info = NULL; return(0);}/* Shutdown all NM of a router */int c2691_nm_shutdown_all(c2691_t *router){ int i; for(i=0;i<C2691_MAX_NM_BAYS;i++) { if (!router->nm_bay[i].dev_type) continue; c2691_nm_shutdown(router,i); } return(0);}/* Show info about all NMs */int c2691_nm_show_all_info(c2691_t *router){ struct c2691_nm_bay *bay; int i; for(i=0;i<C2691_MAX_NM_BAYS;i++) { if (!(bay = c2691_nm_get_info(router,i)) || !bay->nm_driver) continue; if (bay->nm_driver->nm_show_info != NULL) bay->nm_driver->nm_show_info(router,i); } return(0);}/* Maximum number of tokens in a NM description */#define NM_DESC_MAX_TOKENS 8/* Create a Network Module (command line) */int c2691_cmd_nm_create(c2691_t *router,char *str){ char *tokens[NM_DESC_MAX_TOKENS]; int i,count,res; u_int nm_bay; /* A port adapter description is like "1:NM-1FE" */ if ((count = m_strsplit(str,':',tokens,NM_DESC_MAX_TOKENS)) != 2) { vm_error(router->vm,"unable to parse NM description '%s'.\n",str); return(-1); } /* Parse the NM bay id */ nm_bay = atoi(tokens[0]); /* Add this new NM to the current NM list */ res = c2691_nm_add_binding(router,tokens[1],nm_bay); /* The complete array was cleaned by strsplit */ for(i=0;i<NM_DESC_MAX_TOKENS;i++) free(tokens[i]); return(res);}/* Add a Network IO descriptor binding (command line) */int c2691_cmd_add_nio(c2691_t *router,char *str){ char *tokens[NM_DESC_MAX_TOKENS]; int i,count,nio_type,res=-1; u_int nm_bay,port_id; netio_desc_t *nio; char nio_name[128]; /* A port adapter description is like "1:3:tap:tap0" */ if ((count = m_strsplit(str,':',tokens,NM_DESC_MAX_TOKENS)) < 3) { vm_error(router->vm,"unable to parse NIO description '%s'.\n",str); return(-1); } /* Parse the NM bay */ nm_bay = atoi(tokens[0]); /* Parse the NM port id */ port_id = atoi(tokens[1]); /* Autogenerate a NIO name */ snprintf(nio_name,sizeof(nio_name),"c2691-i%u/%u/%u", router->vm->instance_id,nm_bay,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(router->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(router->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(router->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(router->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(router->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(router->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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -