@@ -92,72 +92,64 @@ Ports::Ports(polycube::service::Cube<Ports> &parent,
9292
9393 // lambda function to align IP and Netmask between Port and ExtIface
9494 ParameterEventCallback f_ip;
95- f_ip = [&](const std::string name_iface, const std::string cidr) {
96- if (peer () == name_iface) {
97- try {
98- if (cidr.empty ()) {
99- // set the ip address of the port on the netdev
100- std::string ip = getIp ();
101- int prefix = utils::get_netmask_length (getNetmask ());
102- parent_.netlink_instance_router_ .add_iface_ip (peer (), ip, prefix);
103- } else {
104- // set the ip address of the netdev on the port
105- // cidr = ip_address/prefix
106- std::istringstream split (cidr);
107- std::vector<std::string> info;
108-
109- char split_char = ' /' ;
110- for (std::string each; std::getline (split, each, split_char);
111- info.push_back (each));
112- std::string new_ip = info[0 ];
113- std::string new_netmask =
114- utils::get_netmask_from_CIDR (std::stoi (info[1 ]));
115-
116- std::string old_ip = getIp ();
117- std::string old_netmask = getNetmask ();
118-
119- logger ()->debug (
120- " Align ip and netmask of port {0} with those of interface {1}" ,
121- name (), name_iface);
122-
123- if (old_ip != new_ip)
124- setIp_Netlink (new_ip);
125- if (old_netmask != new_netmask)
126- setNetmask_Netlink (new_netmask);
127- }
128- } catch (std::exception &e) {
129- logger ()->trace (" iface_ip_notification - False ip notification: {0}" ,
130- e.what ());
95+ f_ip = [&](const std::string param_name, const std::string cidr) {
96+ try {
97+ if (!cidr.empty ()) {
98+ // set the ip address of the netdev on the port
99+ // cidr = ip_address/prefix
100+ std::istringstream split (cidr);
101+ std::vector<std::string> info;
102+
103+ char split_char = ' /' ;
104+ for (std::string each; std::getline (split, each, split_char);
105+ info.push_back (each))
106+ ;
107+ std::string new_ip = info[0 ];
108+ std::string new_netmask =
109+ utils::get_netmask_from_CIDR (std::stoi (info[1 ]));
110+
111+ std::string old_ip = getIp ();
112+ std::string old_netmask = getNetmask ();
113+
114+ logger ()->debug (" Align ip and netmask of port {0}" , name ());
115+
116+ if (old_ip != new_ip)
117+ setIp_Netlink (new_ip);
118+ if (old_netmask != new_netmask)
119+ setNetmask_Netlink (new_netmask);
131120 }
121+ } catch (std::exception &e) {
122+ logger ()->trace (" iface_ip_notification - False ip notification: {0}" ,
123+ e.what ());
132124 }
133125 };
134126
135127 // lambda function to align MAC between Port and ExtIface
136128 ParameterEventCallback f_mac;
137- f_mac = [&](const std::string name_iface, const std::string mac) {
138- if (peer () == name_iface) {
139- try {
140- std::string old_mac = getMac ();
141- if (old_mac != mac) {
142- logger ()->debug (" Align mac of port {0} with those of interface {1}" ,
143- name (), name_iface);
144- doSetMac (mac);
145- }
146- } catch (std::exception &e) {
147- logger ()->trace (" iface_mac_notification - False mac notification: {0}" ,
148- e.what ());
129+ f_mac = [&](const std::string param_name, const std::string mac) {
130+ try {
131+ if (mac_ != mac) {
132+ logger ()->debug (" Align mac of port {0}" , name ());
133+ doSetMac (mac);
149134 }
135+ } catch (std::exception &e) {
136+ logger ()->trace (" iface_mac_notification - False mac notification: {0}" ,
137+ e.what ());
150138 }
151139 };
152140
153- // Register the new port to IP and MAC notifications arriving from ExtIface
154- subscribe_peer_parameter (" IP" , f_ip);
155- subscribe_peer_parameter (" MAC" , f_mac);
141+ if (!parent_.get_shadow ()) {
142+ // Register the new port to IP and MAC notifications arriving from ExtIface
143+ subscribe_peer_parameter (" IP" , f_ip);
144+ subscribe_peer_parameter (" MAC" , f_mac);
145+ }
156146}
157147
158148Ports::~Ports () {
159- unsubscribe_peer_parameter (" IP" );
160- unsubscribe_peer_parameter (" MAC" );
149+ if (!parent_.get_shadow ()) {
150+ unsubscribe_peer_parameter (" IP" );
151+ unsubscribe_peer_parameter (" MAC" );
152+ }
161153}
162154
163155void Ports::update (const PortsJsonObject &conf) {
@@ -355,26 +347,24 @@ void Ports::setMac(const std::string &value) {
355347 set_peer_parameter (" MAC" , value);
356348}
357349
358- void Ports::doSetMac (const std::string &value) {
359- if (mac_ != value) {
360- std::string new_mac = value;
361- /* Update the port in the datapath */
362- uint16_t index = this ->index ();
363- auto router_port = parent_.get_hash_table <uint16_t , r_port>(" router_port" );
350+ void Ports::doSetMac (const std::string &new_mac) {
351+ if (mac_ == new_mac) {
352+ return ;
353+ }
364354
365- try {
366- r_port value = router_port.get (index);
367- value.mac = utils::mac_string_to_be_uint (new_mac);
368- } catch (...) {
369- logger ()->error (" Port {0} not found in the data path" , this ->name ());
370- }
355+ /* Update the port in the datapath */
356+ uint16_t index = this ->index ();
357+ auto router_port = parent_.get_hash_table <uint16_t , r_port>(" router_port" );
371358
372- logger ()-> debug (
373- " Updated mac port: {0} (index: {4}) [mac: {1} - ip: {2} - netmask: {3}] " ,
374- getName (), new_mac, getIp (), getNetmask (), index );
359+ r_port map_value = router_port. get (index);
360+ map_value. mac = utils::mac_string_to_be_uint (new_mac);
361+ router_port. set (index, map_value );
375362
376- mac_ = new_mac;
377- }
363+ logger ()->debug (
364+ " Updated mac port: {0} (index: {4}) [mac: {1} - ip: {2} - netmask: {3}]" ,
365+ getName (), new_mac, getIp (), getNetmask (), index);
366+
367+ mac_ = new_mac;
378368}
379369
380370std::shared_ptr<spdlog::logger> Ports::logger () {
0 commit comments