|
| 1 | +#include <string.h> |
| 2 | +#include <errno.h> |
| 3 | +#include <system_error> |
| 4 | +#include <sys/socket.h> |
| 5 | +#include <linux/if.h> |
| 6 | +#include <netlink/route/link.h> |
| 7 | +#include "logger.h" |
| 8 | +#include "netmsg.h" |
| 9 | +#include "dbconnector.h" |
| 10 | +#include "producertable.h" |
| 11 | +#include "scheme.h" |
| 12 | +#include "teamsync.h" |
| 13 | + |
| 14 | +using namespace std; |
| 15 | +using namespace swss; |
| 16 | + |
| 17 | +/* Taken from drivers/net/team/team.c */ |
| 18 | +#define TEAM_DRV_NAME "team" |
| 19 | + |
| 20 | +TeamSync::TeamSync(DBConnector *db, Select *select) : |
| 21 | + m_select(select), |
| 22 | + m_lagTable(db, APP_LAG_TABLE_NAME) |
| 23 | +{ |
| 24 | +} |
| 25 | + |
| 26 | +void TeamSync::onMsg(int nlmsg_type, struct nl_object *obj) |
| 27 | +{ |
| 28 | + struct rtnl_link *link = (struct rtnl_link *)obj; |
| 29 | + if ((nlmsg_type != RTM_NEWLINK) && (nlmsg_type != RTM_DELLINK)) |
| 30 | + return; |
| 31 | + |
| 32 | + string lagName = rtnl_link_get_name(link); |
| 33 | + /* Listens to LAG messages */ |
| 34 | + char *type = rtnl_link_get_type(link); |
| 35 | + if (!type || (strcmp(type, TEAM_DRV_NAME) != 0)) |
| 36 | + return; |
| 37 | + |
| 38 | + bool tracked = m_teamPorts.find(lagName) != m_teamPorts.end(); |
| 39 | + |
| 40 | + if ((nlmsg_type == RTM_DELLINK) && tracked) |
| 41 | + { |
| 42 | + /* Remove LAG ports and delete LAG */ |
| 43 | + removeLag(lagName); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + if ((nlmsg_type == RTM_NEWLINK) && tracked) |
| 48 | + return; |
| 49 | + |
| 50 | + /* |
| 51 | + * New LAG was dedcated for the first time. Sync admin and oper state since |
| 52 | + * portsyncd reflects only changes |
| 53 | + */ |
| 54 | + addLag(lagName, rtnl_link_get_ifindex(link), |
| 55 | + rtnl_link_get_flags(link) & IFF_UP, |
| 56 | + rtnl_link_get_flags(link) & IFF_LOWER_UP, |
| 57 | + rtnl_link_get_mtu(link)); |
| 58 | +} |
| 59 | + |
| 60 | +void TeamSync::addLag(const string &lagName, int ifindex, bool admin_state, |
| 61 | + bool oper_state, unsigned int mtu) |
| 62 | +{ |
| 63 | + /* First add the LAG itself */ |
| 64 | + std::vector<FieldValueTuple> fvVector; |
| 65 | + FieldValueTuple a("admin_status", admin_state ? "up" : "down"); |
| 66 | + FieldValueTuple o("oper_status", oper_state ? "up" : "down"); |
| 67 | + FieldValueTuple m("mtu", to_string(mtu)); |
| 68 | + fvVector.push_back(a); |
| 69 | + fvVector.push_back(o); |
| 70 | + fvVector.push_back(m); |
| 71 | + m_lagTable.set(lagName, fvVector); |
| 72 | + |
| 73 | + /* Start adding ports to LAG */ |
| 74 | + TeamPortSync *sync = new TeamPortSync(lagName, ifindex, &m_lagTable); |
| 75 | + m_select->addSelectable(sync); |
| 76 | + m_teamPorts[lagName] = shared_ptr<TeamPortSync>(sync); |
| 77 | +} |
| 78 | + |
| 79 | +void TeamSync::removeLag(const string &lagName) |
| 80 | +{ |
| 81 | + m_select->removeSelectable(m_teamPorts[lagName].get()); |
| 82 | + m_teamPorts.erase(lagName); |
| 83 | + m_lagTable.del(lagName); |
| 84 | +} |
| 85 | + |
| 86 | +const struct team_change_handler TeamSync::TeamPortSync::gPortChangeHandler = { |
| 87 | + .func = TeamSync::TeamPortSync::teamdHandler, |
| 88 | + .type_mask = TEAM_PORT_CHANGE |
| 89 | +}; |
| 90 | + |
| 91 | +TeamSync::TeamPortSync::TeamPortSync(const string &lagName, int ifindex, |
| 92 | + ProducerTable *lagTable) : |
| 93 | + m_lagTable(lagTable), |
| 94 | + m_lagName(lagName), |
| 95 | + m_ifindex(ifindex) |
| 96 | +{ |
| 97 | + m_team = team_alloc(); |
| 98 | + if (!m_team) |
| 99 | + { |
| 100 | + SWSS_LOG_ERROR("Unable to allocated team socket"); |
| 101 | + throw system_error(make_error_code(errc::address_not_available), |
| 102 | + "Unable to allocated team socket"); |
| 103 | + } |
| 104 | + |
| 105 | + int err = team_init(m_team, ifindex); |
| 106 | + if (err) { |
| 107 | + team_free(m_team); |
| 108 | + m_team = NULL; |
| 109 | + SWSS_LOG_ERROR("Unable to init team socket"); |
| 110 | + throw system_error(make_error_code(errc::address_not_available), |
| 111 | + "Unable to init team socket"); |
| 112 | + } |
| 113 | + |
| 114 | + err = team_change_handler_register(m_team, &gPortChangeHandler, this); |
| 115 | + if (err) { |
| 116 | + team_free(m_team); |
| 117 | + m_team = NULL; |
| 118 | + SWSS_LOG_ERROR("Unable to register port change event"); |
| 119 | + throw system_error(make_error_code(errc::address_not_available), |
| 120 | + "Unable to register port change event"); |
| 121 | + } |
| 122 | + |
| 123 | + onPortChange(true); |
| 124 | +} |
| 125 | + |
| 126 | +TeamSync::TeamPortSync::~TeamPortSync() |
| 127 | +{ |
| 128 | + if (m_team) |
| 129 | + { |
| 130 | + team_change_handler_unregister(m_team, &gPortChangeHandler, this); |
| 131 | + team_free(m_team); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +int TeamSync::TeamPortSync::onPortChange(bool isInit) |
| 136 | +{ |
| 137 | + struct team_port *port; |
| 138 | + team_for_each_port(port, m_team) |
| 139 | + { |
| 140 | + if (isInit || team_is_port_changed(port)) |
| 141 | + { |
| 142 | + string key = m_lagName; |
| 143 | + key += ":"; |
| 144 | + uint32_t ifindex = team_get_port_ifindex(port); |
| 145 | + char ifname[MAX_IFNAME + 1] = {0}; |
| 146 | + key += team_ifindex2ifname(m_team, ifindex, ifname, MAX_IFNAME); |
| 147 | + |
| 148 | + if (team_is_port_removed(port)) |
| 149 | + { |
| 150 | + m_lagTable->del(key); |
| 151 | + } else |
| 152 | + { |
| 153 | + std::vector<FieldValueTuple> fvVector; |
| 154 | + FieldValueTuple l("linkup", team_is_port_link_up(port) ? "up" : "down"); |
| 155 | + FieldValueTuple s("speed", to_string(team_get_port_speed(port)) + "Mbit"); |
| 156 | + FieldValueTuple d("duplex", team_get_port_duplex(port) ? "full" : "half"); |
| 157 | + fvVector.push_back(l); |
| 158 | + fvVector.push_back(s); |
| 159 | + fvVector.push_back(d); |
| 160 | + m_lagTable->set(key, fvVector); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + return 0; |
| 165 | +} |
| 166 | + |
| 167 | +int TeamSync::TeamPortSync::teamdHandler(struct team_handle *team, void *arg, |
| 168 | + team_change_type_mask_t type_mask) |
| 169 | +{ |
| 170 | + return ((TeamSync::TeamPortSync *)arg)->onPortChange(false); |
| 171 | +} |
| 172 | + |
| 173 | +void TeamSync::TeamPortSync::addFd(fd_set *fd) |
| 174 | +{ |
| 175 | + FD_SET(team_get_event_fd(m_team), fd); |
| 176 | +} |
| 177 | + |
| 178 | +bool TeamSync::TeamPortSync::isMe(fd_set *fd) |
| 179 | +{ |
| 180 | + return FD_ISSET(team_get_event_fd(m_team), fd); |
| 181 | +} |
| 182 | + |
| 183 | +int TeamSync::TeamPortSync::readCache() |
| 184 | +{ |
| 185 | + return NODATA; |
| 186 | +} |
| 187 | + |
| 188 | +void TeamSync::TeamPortSync::readMe() |
| 189 | +{ |
| 190 | + team_handle_events(m_team); |
| 191 | +} |
0 commit comments