+ rc = -EINVAL;
+ goto failed0;
+ }
+ }
+
+ lnet_net_lock(LNET_LOCK_EX);
+ lnd->lnd_refcount++;
+ lnet_net_unlock(LNET_LOCK_EX);
+
+ net->net_lnd = lnd;
+
+ mutex_unlock(&the_lnet.ln_lnd_mutex);
+
+ net_l = net;
+ }
+
+ /*
+ * net_l: if the network being added is unique then net_l
+ * will point to that network
+ * if the network being added is not unique then
+ * net_l points to the existing network.
+ *
+ * When we enter the loop below, we'll pick NIs off he
+ * network beign added and start them up, then add them to
+ * a local ni list. Once we've successfully started all
+ * the NIs then we join the local NI list (of started up
+ * networks) with the net_l->net_ni_list, which should
+ * point to the correct network to add the new ni list to
+ *
+ * If any of the new NIs fail to start up, then we want to
+ * iterate through the local ni list, which should include
+ * any NIs which were successfully started up, and shut
+ * them down.
+ *
+ * After than we want to delete the network being added,
+ * to avoid a memory leak.
+ */
+
+ /*
+ * When a network uses TCP bonding then all its interfaces
+ * must be specified when the network is first defined: the
+ * TCP bonding code doesn't allow for interfaces to be added
+ * or removed.
+ */
+ if (net_l != net && net_l != NULL && use_tcp_bonding &&
+ LNET_NETTYP(net_l->net_id) == SOCKLND) {
+ rc = -EINVAL;
+ goto failed0;
+ }
+
+ while (!list_empty(&net->net_ni_added)) {
+ ni = list_entry(net->net_ni_added.next, struct lnet_ni,
+ ni_netlist);
+ list_del_init(&ni->ni_netlist);
+
+ /* make sure that the the NI we're about to start
+ * up is actually unique. if it's not fail. */
+ if (!lnet_ni_unique_net(&net_l->net_ni_list,
+ ni->ni_interfaces[0])) {
+ rc = -EINVAL;
+ goto failed1;
+ }
+
+ /* adjust the pointer the parent network, just in case it
+ * the net is a duplicate */
+ ni->ni_net = net_l;
+
+ rc = lnet_startup_lndni(ni, tun);
+
+ LASSERT(ni->ni_net->net_tunables.lct_peer_timeout <= 0 ||
+ ni->ni_net->net_lnd->lnd_query != NULL);
+
+ if (rc < 0)
+ goto failed1;
+
+ lnet_ni_addref(ni);
+ list_add_tail(&ni->ni_netlist, &local_ni_list);
+
+ ni_count++;
+ }
+
+ lnet_net_lock(LNET_LOCK_EX);
+ list_splice_tail(&local_ni_list, &net_l->net_ni_list);
+ lnet_incr_dlc_seq();
+ lnet_net_unlock(LNET_LOCK_EX);
+
+ /* if the network is not unique then we don't want to keep
+ * it around after we're done. Free it. Otherwise add that
+ * net to the global the_lnet.ln_nets */
+ if (net_l != net && net_l != NULL) {
+ /*
+ * TODO - note. currently the tunables can not be updated
+ * once added
+ */
+ lnet_net_free(net);
+ } else {
+ net->net_state = LNET_NET_STATE_ACTIVE;
+ /*
+ * restore tunables after it has been overwitten by the
+ * lnd
+ */
+ if (peer_timeout != -1)
+ net->net_tunables.lct_peer_timeout = peer_timeout;
+ if (maxtxcredits != -1)
+ net->net_tunables.lct_max_tx_credits = maxtxcredits;
+ if (peerrtrcredits != -1)
+ net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
+
+ lnet_net_lock(LNET_LOCK_EX);
+ list_add_tail(&net->net_list, &the_lnet.ln_nets);
+ lnet_net_unlock(LNET_LOCK_EX);
+ }
+
+ return ni_count;
+
+failed1:
+ /*
+ * shutdown the new NIs that are being started up
+ * free the NET being started
+ */
+ while (!list_empty(&local_ni_list)) {
+ ni = list_entry(local_ni_list.next, struct lnet_ni,
+ ni_netlist);
+
+ lnet_shutdown_lndni(ni);
+ }
+
+failed0:
+ lnet_net_free(net);
+
+ return rc;
+}
+
+static int
+lnet_startup_lndnets(struct list_head *netlist)
+{
+ struct lnet_net *net;
+ int rc;
+ int ni_count = 0;
+
+ while (!list_empty(netlist)) {
+ net = list_entry(netlist->next, struct lnet_net, net_list);
+ list_del_init(&net->net_list);
+
+ rc = lnet_startup_lndnet(net, NULL);
+
+ if (rc < 0)
+ goto failed;
+
+ ni_count += rc;
+ }
+
+ return ni_count;
+failed:
+ lnet_shutdown_lndnets();
+
+ return rc;
+}
+
+/**
+ * Initialize LNet library.
+ *
+ * Automatically called at module loading time. Caller has to call
+ * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
+ * latter returned 0. It must be called exactly once.
+ *
+ * \retval 0 on success
+ * \retval -ve on failures.
+ */
+int lnet_lib_init(void)
+{
+ int rc;
+
+ lnet_assert_wire_constants();
+
+ memset(&the_lnet, 0, sizeof(the_lnet));
+
+ /* refer to global cfs_cpt_table for now */
+ the_lnet.ln_cpt_table = cfs_cpt_table;
+ the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
+
+ LASSERT(the_lnet.ln_cpt_number > 0);
+ if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
+ /* we are under risk of consuming all lh_cookie */
+ CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
+ "please change setting of CPT-table and retry\n",
+ the_lnet.ln_cpt_number, LNET_CPT_MAX);
+ return -E2BIG;
+ }
+
+ while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
+ the_lnet.ln_cpt_bits++;
+
+ rc = lnet_create_locks();
+ if (rc != 0) {
+ CERROR("Can't create LNet global locks: %d\n", rc);
+ return rc;
+ }
+
+ the_lnet.ln_refcount = 0;
+ LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
+ INIT_LIST_HEAD(&the_lnet.ln_lnds);
+ INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
+ INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
+ INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
+
+ /* The hash table size is the number of bits it takes to express the set
+ * ln_num_routes, minus 1 (better to under estimate than over so we
+ * don't waste memory). */
+ if (rnet_htable_size <= 0)
+ rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
+ else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
+ rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
+ the_lnet.ln_remote_nets_hbits = max_t(int, 1,
+ order_base_2(rnet_htable_size) - 1);
+
+ /* All LNDs apart from the LOLND are in separate modules. They
+ * register themselves when their module loads, and unregister
+ * themselves when their module is unloaded. */
+ lnet_register_lnd(&the_lolnd);
+ return 0;
+}
+
+/**
+ * Finalize LNet library.
+ *
+ * \pre lnet_lib_init() called with success.
+ * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
+ */
+void lnet_lib_exit(void)
+{
+ LASSERT(the_lnet.ln_refcount == 0);
+
+ while (!list_empty(&the_lnet.ln_lnds))
+ lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
+ lnd_t, lnd_list));
+ lnet_destroy_locks();
+}
+
+/**
+ * Set LNet PID and start LNet interfaces, routing, and forwarding.
+ *
+ * Users must call this function at least once before any other functions.
+ * For each successful call there must be a corresponding call to
+ * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
+ * ignored.
+ *
+ * The PID used by LNet may be different from the one requested.
+ * See LNetGetId().
+ *
+ * \param requested_pid PID requested by the caller.
+ *
+ * \return >= 0 on success, and < 0 error code on failures.
+ */
+int
+LNetNIInit(lnet_pid_t requested_pid)
+{
+ int im_a_router = 0;
+ int rc;
+ int ni_count;
+ struct lnet_ping_info *pinfo;
+ lnet_handle_md_t md_handle;
+ struct list_head net_head;
+ struct lnet_net *net;
+
+ INIT_LIST_HEAD(&net_head);
+
+ mutex_lock(&the_lnet.ln_api_mutex);
+
+ CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
+
+ if (the_lnet.ln_refcount > 0) {
+ rc = the_lnet.ln_refcount++;
+ mutex_unlock(&the_lnet.ln_api_mutex);
+ return rc;
+ }
+
+ rc = lnet_prepare(requested_pid);
+ if (rc != 0) {
+ mutex_unlock(&the_lnet.ln_api_mutex);
+ return rc;
+ }
+
+ /* create a network for Loopback network */
+ net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
+ if (net == NULL) {
+ rc = -ENOMEM;
+ goto err_empty_list;
+ }
+
+ /* Add in the loopback NI */
+ if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
+ rc = -ENOMEM;
+ goto err_empty_list;
+ }
+
+ /* If LNet is being initialized via DLC it is possible
+ * that the user requests not to load module parameters (ones which
+ * are supported by DLC) on initialization. Therefore, make sure not
+ * to load networks, routes and forwarding from module parameters
+ * in this case. On cleanup in case of failure only clean up
+ * routes if it has been loaded */
+ if (!the_lnet.ln_nis_from_mod_params) {
+ rc = lnet_parse_networks(&net_head, lnet_get_networks(),
+ use_tcp_bonding);
+ if (rc < 0)
+ goto err_empty_list;
+ }
+
+ ni_count = lnet_startup_lndnets(&net_head);
+ if (ni_count < 0) {
+ rc = ni_count;
+ goto err_empty_list;
+ }
+
+ if (!the_lnet.ln_nis_from_mod_params) {
+ rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
+ if (rc != 0)
+ goto err_shutdown_lndnis;
+
+ rc = lnet_check_routes();
+ if (rc != 0)
+ goto err_destroy_routes;
+
+ rc = lnet_rtrpools_alloc(im_a_router);
+ if (rc != 0)
+ goto err_destroy_routes;
+ }
+
+ rc = lnet_acceptor_start();
+ if (rc != 0)
+ goto err_destroy_routes;
+
+ the_lnet.ln_refcount = 1;
+ /* Now I may use my own API functions... */
+
+ rc = lnet_ping_info_setup(&pinfo, &md_handle, ni_count, true);
+ if (rc != 0)
+ goto err_acceptor_stop;
+
+ lnet_ping_target_update(pinfo, md_handle);
+
+ rc = lnet_router_checker_start();
+ if (rc != 0)
+ goto err_stop_ping;
+
+ lnet_fault_init();
+ lnet_proc_init();
+
+ mutex_unlock(&the_lnet.ln_api_mutex);
+
+ return 0;
+
+err_stop_ping:
+ lnet_ping_target_fini();
+err_acceptor_stop:
+ the_lnet.ln_refcount = 0;
+ lnet_acceptor_stop();
+err_destroy_routes:
+ if (!the_lnet.ln_nis_from_mod_params)
+ lnet_destroy_routes();
+err_shutdown_lndnis:
+ lnet_shutdown_lndnets();
+err_empty_list:
+ lnet_unprepare();
+ LASSERT(rc < 0);
+ mutex_unlock(&the_lnet.ln_api_mutex);
+ while (!list_empty(&net_head)) {
+ struct lnet_net *net;
+
+ net = list_entry(net_head.next, struct lnet_net, net_list);
+ list_del_init(&net->net_list);
+ lnet_net_free(net);
+ }
+ return rc;
+}
+EXPORT_SYMBOL(LNetNIInit);
+
+/**
+ * Stop LNet interfaces, routing, and forwarding.
+ *
+ * Users must call this function once for each successful call to LNetNIInit().
+ * Once the LNetNIFini() operation has been started, the results of pending
+ * API operations are undefined.
+ *
+ * \return always 0 for current implementation.
+ */
+int
+LNetNIFini()
+{
+ mutex_lock(&the_lnet.ln_api_mutex);
+
+ LASSERT(the_lnet.ln_refcount > 0);
+
+ if (the_lnet.ln_refcount != 1) {
+ the_lnet.ln_refcount--;
+ } else {
+ LASSERT(!the_lnet.ln_niinit_self);
+
+ lnet_fault_fini();
+
+ lnet_proc_fini();
+ lnet_router_checker_stop();
+ lnet_ping_target_fini();
+
+ /* Teardown fns that use my own API functions BEFORE here */
+ the_lnet.ln_refcount = 0;
+
+ lnet_acceptor_stop();
+ lnet_destroy_routes();
+ lnet_shutdown_lndnets();
+ lnet_unprepare();
+ }
+
+ mutex_unlock(&the_lnet.ln_api_mutex);
+ return 0;
+}
+EXPORT_SYMBOL(LNetNIFini);
+
+
+static int lnet_handle_dbg_task(struct lnet_ioctl_dbg *dbg,
+ struct lnet_dbg_task_info *dbg_info)
+{
+ switch (dbg->dbg_task) {
+ case LNET_DBG_INCR_DLC_SEQ:
+ lnet_incr_dlc_seq();
+ }
+
+ return 0;
+}
+/**
+ * Grabs the ni data from the ni structure and fills the out
+ * parameters
+ *
+ * \param[in] ni network interface structure
+ * \param[out] cfg_ni NI config information
+ * \param[out] tun network and LND tunables
+ */
+static void
+lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_ni *cfg_ni,
+ struct lnet_ioctl_config_lnd_tunables *tun,
+ struct lnet_ioctl_element_stats *stats,
+ __u32 tun_size)
+{
+ size_t min_size = 0;
+ int i;
+
+ if (!ni || !cfg_ni || !tun)
+ return;
+
+ if (ni->ni_interfaces[0] != NULL) {
+ for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
+ if (ni->ni_interfaces[i] != NULL) {
+ strncpy(cfg_ni->lic_ni_intf[i],
+ ni->ni_interfaces[i],
+ sizeof(cfg_ni->lic_ni_intf[i]));
+ }
+ }
+ }
+
+ cfg_ni->lic_nid = ni->ni_nid;
+ cfg_ni->lic_status = ni->ni_status->ns_status;
+ cfg_ni->lic_tcp_bonding = use_tcp_bonding;
+ cfg_ni->lic_dev_cpt = ni->dev_cpt;
+
+ memcpy(&tun->lt_cmn, &ni->ni_net->net_tunables, sizeof(tun->lt_cmn));
+
+ if (stats) {
+ stats->send_count = atomic_read(&ni->ni_stats.send_count);
+ stats->recv_count = atomic_read(&ni->ni_stats.recv_count);
+ }
+
+ /*
+ * tun->lt_tun will always be present, but in order to be
+ * backwards compatible, we need to deal with the cases when
+ * tun->lt_tun is smaller than what the kernel has, because it
+ * comes from an older version of a userspace program, then we'll
+ * need to copy as much information as we have available space.
+ */
+ min_size = tun_size - sizeof(tun->lt_cmn);
+ memcpy(&tun->lt_tun, &ni->ni_lnd_tunables, min_size);
+
+ /* copy over the cpts */
+ if (ni->ni_ncpts == LNET_CPT_NUMBER &&
+ ni->ni_cpts == NULL) {
+ for (i = 0; i < ni->ni_ncpts; i++)
+ cfg_ni->lic_cpts[i] = i;
+ } else {
+ for (i = 0;
+ ni->ni_cpts != NULL && i < ni->ni_ncpts &&
+ i < LNET_MAX_SHOW_NUM_CPT;
+ i++)
+ cfg_ni->lic_cpts[i] = ni->ni_cpts[i];
+ }
+ cfg_ni->lic_ncpts = ni->ni_ncpts;
+}
+
+/**
+ * NOTE: This is a legacy function left in the code to be backwards
+ * compatible with older userspace programs. It should eventually be
+ * removed.
+ *
+ * Grabs the ni data from the ni structure and fills the out
+ * parameters
+ *
+ * \param[in] ni network interface structure
+ * \param[out] config config information
+ */
+static void
+lnet_fill_ni_info_legacy(struct lnet_ni *ni,
+ struct lnet_ioctl_config_data *config)
+{
+ struct lnet_ioctl_net_config *net_config;
+ struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
+ size_t min_size, tunable_size = 0;
+ int i;
+
+ if (!ni || !config)
+ return;
+
+ net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
+ if (!net_config)
+ return;
+
+ BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
+ ARRAY_SIZE(net_config->ni_interfaces));
+
+ for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
+ if (!ni->ni_interfaces[i])
+ break;
+
+ strncpy(net_config->ni_interfaces[i],
+ ni->ni_interfaces[i],
+ sizeof(net_config->ni_interfaces[i]));
+ }
+
+ config->cfg_nid = ni->ni_nid;
+ config->cfg_config_u.cfg_net.net_peer_timeout =
+ ni->ni_net->net_tunables.lct_peer_timeout;
+ config->cfg_config_u.cfg_net.net_max_tx_credits =
+ ni->ni_net->net_tunables.lct_max_tx_credits;
+ config->cfg_config_u.cfg_net.net_peer_tx_credits =
+ ni->ni_net->net_tunables.lct_peer_tx_credits;
+ config->cfg_config_u.cfg_net.net_peer_rtr_credits =
+ ni->ni_net->net_tunables.lct_peer_rtr_credits;
+
+ net_config->ni_status = ni->ni_status->ns_status;
+
+ if (ni->ni_cpts) {
+ int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
+
+ for (i = 0; i < num_cpts; i++)
+ net_config->ni_cpts[i] = ni->ni_cpts[i];
+
+ config->cfg_ncpts = num_cpts;
+ }
+
+ /*
+ * See if user land tools sent in a newer and larger version
+ * of struct lnet_tunables than what the kernel uses.
+ */
+ min_size = sizeof(*config) + sizeof(*net_config);
+
+ if (config->cfg_hdr.ioc_len > min_size)
+ tunable_size = config->cfg_hdr.ioc_len - min_size;
+
+ /* Don't copy too much data to user space */
+ min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
+ lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
+
+ if (lnd_cfg && min_size) {
+ memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
+ config->cfg_config_u.cfg_net.net_interface_count = 1;
+
+ /* Tell user land that kernel side has less data */
+ if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
+ min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
+ config->cfg_hdr.ioc_len -= min_size;
+ }
+ }
+}
+
+struct lnet_ni *
+lnet_get_ni_idx_locked(int idx)
+{
+ struct lnet_ni *ni;
+ struct lnet_net *net;
+
+ list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
+ list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
+ if (idx-- == 0)
+ return ni;
+ }
+ }
+
+ return NULL;
+}
+
+struct lnet_ni *
+lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
+{
+ struct lnet_ni *ni;
+ struct lnet_net *net = mynet;
+
+ if (prev == NULL) {
+ if (net == NULL)
+ net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
+ net_list);
+ ni = list_entry(net->net_ni_list.next, struct lnet_ni,
+ ni_netlist);
+
+ return ni;
+ }
+
+ if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
+ /* if you reached the end of the ni list and the net is
+ * specified, then there are no more nis in that net */
+ if (net != NULL)
+ return NULL;
+
+ /* we reached the end of this net ni list. move to the
+ * next net */
+ if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
+ /* no more nets and no more NIs. */
+ return NULL;
+
+ /* get the next net */
+ net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
+ net_list);
+ /* get the ni on it */
+ ni = list_entry(net->net_ni_list.next, struct lnet_ni,
+ ni_netlist);
+
+ return ni;
+ }
+
+ /* there are more nis left */
+ ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
+
+ return ni;
+}
+
+int
+lnet_get_net_config(struct lnet_ioctl_config_data *config)
+{
+ struct lnet_ni *ni;
+ int cpt;
+ int rc = -ENOENT;
+ int idx = config->cfg_count;
+
+ cpt = lnet_net_lock_current();
+
+ ni = lnet_get_ni_idx_locked(idx);
+
+ if (ni != NULL) {
+ rc = 0;
+ lnet_ni_lock(ni);
+ lnet_fill_ni_info_legacy(ni, config);
+ lnet_ni_unlock(ni);
+ }
+
+ lnet_net_unlock(cpt);
+ return rc;
+}
+
+int
+lnet_get_ni_config(struct lnet_ioctl_config_ni *cfg_ni,
+ struct lnet_ioctl_config_lnd_tunables *tun,
+ struct lnet_ioctl_element_stats *stats,
+ __u32 tun_size)
+{
+ struct lnet_ni *ni;
+ int cpt;
+ int rc = -ENOENT;
+
+ if (!cfg_ni || !tun || !stats)
+ return -EINVAL;
+
+ cpt = lnet_net_lock_current();
+
+ ni = lnet_get_ni_idx_locked(cfg_ni->lic_idx);
+
+ if (ni) {
+ rc = 0;
+ lnet_ni_lock(ni);
+ lnet_fill_ni_info(ni, cfg_ni, tun, stats, tun_size);
+ lnet_ni_unlock(ni);
+ }
+
+ lnet_net_unlock(cpt);
+ return rc;
+}
+
+static int lnet_add_net_common(struct lnet_net *net,
+ struct lnet_ioctl_config_lnd_tunables *tun)
+{
+ struct lnet_net *netl = NULL;
+ __u32 net_id;
+ lnet_ping_info_t *pinfo;
+ lnet_handle_md_t md_handle;
+ int rc;
+ lnet_remotenet_t *rnet;
+ int net_ni_count;
+ int num_acceptor_nets;
+
+ lnet_net_lock(LNET_LOCK_EX);
+ rnet = lnet_find_rnet_locked(net->net_id);
+ lnet_net_unlock(LNET_LOCK_EX);
+ /*
+ * make sure that the net added doesn't invalidate the current
+ * configuration LNet is keeping
+ */
+ if (rnet) {
+ CERROR("Adding net %s will invalidate routing configuration\n",
+ libcfs_net2str(net->net_id));
+ rc = -EUSERS;
+ goto failed1;
+ }
+
+ /*
+ * make sure you calculate the correct number of slots in the ping
+ * info. Since the ping info is a flattened list of all the NIs,
+ * we should allocate enough slots to accomodate the number of NIs
+ * which will be added.
+ *
+ * since ni hasn't been configured yet, use
+ * lnet_get_net_ni_count_pre() which checks the net_ni_added list
+ */
+ net_ni_count = lnet_get_net_ni_count_pre(net);
+
+ rc = lnet_ping_info_setup(&pinfo, &md_handle,
+ net_ni_count + lnet_get_ni_count(),
+ false);
+ if (rc < 0)
+ goto failed1;
+
+ if (tun)
+ memcpy(&net->net_tunables,
+ &tun->lt_cmn, sizeof(net->net_tunables));
+ else
+ memset(&net->net_tunables, -1, sizeof(net->net_tunables));
+
+ /*
+ * before starting this network get a count of the current TCP
+ * networks which require the acceptor thread running. If that
+ * count is == 0 before we start up this network, then we'd want to
+ * start up the acceptor thread after starting up this network
+ */
+ num_acceptor_nets = lnet_count_acceptor_nets();
+
+ net_id = net->net_id;
+
+ rc = lnet_startup_lndnet(net,
+ (tun) ? &tun->lt_tun : NULL);
+ if (rc < 0)
+ goto failed;
+
+ lnet_net_lock(LNET_LOCK_EX);
+ netl = lnet_get_net_locked(net_id);
+ lnet_net_unlock(LNET_LOCK_EX);
+
+ LASSERT(netl);
+
+ /*
+ * Start the acceptor thread if this is the first network
+ * being added that requires the thread.
+ */
+ if (netl->net_lnd->lnd_accept &&
+ num_acceptor_nets == 0)
+ {
+ rc = lnet_acceptor_start();
+ if (rc < 0) {
+ /* shutdown the net that we just started */
+ CERROR("Failed to start up acceptor thread\n");
+ lnet_shutdown_lndnet(net);
+ goto failed;
+ }
+ }
+
+ lnet_net_lock(LNET_LOCK_EX);
+ lnet_peer_net_added(netl);
+ lnet_net_unlock(LNET_LOCK_EX);
+
+ lnet_ping_target_update(pinfo, md_handle);
+
+ return 0;
+
+failed:
+ lnet_ping_md_unlink(pinfo, &md_handle);
+ lnet_ping_info_free(pinfo);
+failed1:
+ lnet_net_free(net);
+ return rc;
+}
+
+static int lnet_handle_legacy_ip2nets(char *ip2nets,
+ struct lnet_ioctl_config_lnd_tunables *tun)
+{
+ struct lnet_net *net;
+ char *nets;
+ int rc;
+ struct list_head net_head;
+
+ INIT_LIST_HEAD(&net_head);
+
+ rc = lnet_parse_ip2nets(&nets, ip2nets);
+ if (rc < 0)
+ return rc;
+
+ rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
+ if (rc < 0)
+ return rc;
+
+ mutex_lock(&the_lnet.ln_api_mutex);
+ while (!list_empty(&net_head)) {
+ net = list_entry(net_head.next, struct lnet_net, net_list);
+ list_del_init(&net->net_list);
+ rc = lnet_add_net_common(net, tun);
+ if (rc < 0)
+ goto out;
+ }
+
+out:
+ mutex_unlock(&the_lnet.ln_api_mutex);
+
+ while (!list_empty(&net_head)) {
+ net = list_entry(net_head.next, struct lnet_net, net_list);
+ list_del_init(&net->net_list);
+ lnet_net_free(net);
+ }
+ return rc;
+}
+
+int lnet_dyn_add_ni(struct lnet_ioctl_config_ni *conf)
+{
+ struct lnet_net *net;
+ struct lnet_ni *ni;
+ struct lnet_ioctl_config_lnd_tunables *tun = NULL;
+ int rc;
+ __u32 net_id;
+
+ /* get the tunables if they are available */
+ if (conf->lic_cfg_hdr.ioc_len >=
+ sizeof(*conf) + sizeof(*tun))
+ tun = (struct lnet_ioctl_config_lnd_tunables *)
+ conf->lic_bulk;
+
+ /* handle legacy ip2nets from DLC */
+ if (conf->lic_legacy_ip2nets[0] != '\0')
+ return lnet_handle_legacy_ip2nets(conf->lic_legacy_ip2nets,
+ tun);
+
+ net_id = LNET_NIDNET(conf->lic_nid);
+
+ net = lnet_net_alloc(net_id, NULL);
+ if (!net)
+ return -ENOMEM;
+
+ ni = lnet_ni_alloc_w_cpt_array(net, conf->lic_cpts, conf->lic_ncpts,
+ conf->lic_ni_intf[0]);
+ if (!ni)
+ return -ENOMEM;
+
+ mutex_lock(&the_lnet.ln_api_mutex);
+
+ rc = lnet_add_net_common(net, tun);
+
+ mutex_unlock(&the_lnet.ln_api_mutex);
+
+ return rc;
+}
+
+int lnet_dyn_del_ni(struct lnet_ioctl_config_ni *conf)
+{
+ struct lnet_net *net;
+ struct lnet_ni *ni;
+ __u32 net_id = LNET_NIDNET(conf->lic_nid);
+ lnet_ping_info_t *pinfo;
+ lnet_handle_md_t md_handle;
+ int rc;
+ int net_count;
+ __u32 addr;
+
+ /* don't allow userspace to shutdown the LOLND */
+ if (LNET_NETTYP(net_id) == LOLND)
+ return -EINVAL;
+
+ mutex_lock(&the_lnet.ln_api_mutex);
+
+ lnet_net_lock(0);
+
+ net = lnet_get_net_locked(net_id);
+ if (!net) {
+ CERROR("net %s not found\n",
+ libcfs_net2str(net_id));
+ rc = -ENOENT;
+ goto net_unlock;
+ }
+
+ addr = LNET_NIDADDR(conf->lic_nid);
+ if (addr == 0) {
+ /* remove the entire net */
+ net_count = lnet_get_net_ni_count_locked(net);
+
+ lnet_net_unlock(0);
+
+ /* create and link a new ping info, before removing the old one */
+ rc = lnet_ping_info_setup(&pinfo, &md_handle,
+ lnet_get_ni_count() - net_count,
+ false);
+ if (rc != 0)
+ goto out;
+
+ lnet_shutdown_lndnet(net);
+
+ if (lnet_count_acceptor_nets() == 0)
+ lnet_acceptor_stop();
+
+ lnet_ping_target_update(pinfo, md_handle);
+
+ goto out;
+ }
+
+ ni = lnet_nid2ni_locked(conf->lic_nid, 0);
+ if (!ni) {
+ CERROR("nid %s not found \n",
+ libcfs_nid2str(conf->lic_nid));
+ rc = -ENOENT;
+ goto net_unlock;
+ }