在网口bond的时候如何选择slave呢?
这可以从下面的函数看出
static struct slave *bond_find_best_slave(struct bonding *bond)
{
struct slave *slave, *bestslave = NULL;
struct list_head *iter;
int mintime = bond->params.updelay;
#这个函数决定继续用当前的slave还是切换slave
slave = bond_choose_primary_or_current(bond);
if (slave)
return slave;
#如果前面没有决定的话,则遍历bond中的slave,选择第一个link up的slave
bond_for_each_slave(bond, slave, iter) {
if (slave->link == BOND_LINK_UP)
return slave;
if (slave->link == BOND_LINK_BACK && bond_slave_is_up(slave) &&
slave->delay < mintime) {
mintime = slave->delay;
bestslave = slave;
}
}
return bestslave;
}
重点看一下bond_choose_primary_or_current
static struct slave *bond_choose_primary_or_current(struct bonding *bond)
{
struct slave *prim = rtnl_dereference(bond->primary_slave);
struct slave *curr = rtnl_dereference(bond->curr_active_slave);
if (!prim || prim->link != BOND_LINK_UP) {
if (!curr || curr->link != BOND_LINK_UP)
return NULL;
return curr;
}
if (bond->force_primary) {
bond->force_primary = false;
return prim;
}
if (!curr || curr->link != BOND_LINK_UP)
return prim;
#正常情况下可以看到选择slave主要有三种方式
/* At this point, prim and curr are both up */
switch (bond->params.primary_reselect) {
#每次有包来的时候都切换,具体这里就从curr切换到prim
case BOND_PRI_RESELECT_ALWAYS:
return prim;
#选择速度快的,如果速度是一样的话,则选择全双工的
case BOND_PRI_RESELECT_BETTER:
if (prim->speed < curr->speed)
return curr;
if (prim->speed == curr->speed && prim->duplex <= curr->duplex)
return curr;
return prim;
#一直用当前的就不切换
case BOND_PRI_RESELECT_FAILURE:
return curr;
default:
netdev_err(bond->dev, "impossible primary_reselect %d\n",
bond->params.primary_reselect);
return curr;
}
}
从这里知道决定切换的就是params.primary_reselect这个参数,这个参数也只有三个有效值