Skip to content

Commit

Permalink
Merge pull request #186 from francescomessina/rename_functions
Browse files Browse the repository at this point in the history
libpolycube: rename functions in the utils library
  • Loading branch information
frisso authored Jul 29, 2019
2 parents 79e0cd2 + fc2403c commit c6751b9
Show file tree
Hide file tree
Showing 30 changed files with 126 additions and 124 deletions.
16 changes: 9 additions & 7 deletions src/libs/polycube/include/polycube/services/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ namespace polycube {
namespace service {
namespace utils {

/* ip (a.b.c.d) to string and viceversa in big endian */
uint32_t ip_string_to_be_uint(const std::string &ip);
std::string be_uint_to_ip_string(uint32_t ip);

/* mac (aa:bb:cc:dd:ee:ff) to string and vicersa in big endian */
uint64_t mac_string_to_be_uint(const std::string &mac);
std::string be_uint_to_mac_string(uint64_t mac);
/* ip (a.b.c.d) to string and viceversa
* Number is in network byte order (nbo), i.e., big endian */
uint32_t ip_string_to_nbo_uint(const std::string &ip);
std::string nbo_uint_to_ip_string(uint32_t ip);

/* mac (aa:bb:cc:dd:ee:ff) to string and vicersa
* Number is in network byte order (nbo), i.e., big endian */
uint64_t mac_string_to_nbo_uint(const std::string &mac);
std::string nbo_uint_to_mac_string(uint64_t mac);

/* transforms an ipv4 dotted representation into a hexadecimal big endian */
/* deprecated */
Expand Down
8 changes: 4 additions & 4 deletions src/libs/polycube/src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace service {
namespace utils {

// new set of functions
uint32_t ip_string_to_be_uint(const std::string &ip) {
uint32_t ip_string_to_nbo_uint(const std::string &ip) {
unsigned char a[4];
int last = -1;
int rc = std::sscanf(ip.c_str(), "%hhu.%hhu.%hhu.%hhu%n", a + 0, a + 1, a + 2,
Expand All @@ -52,14 +52,14 @@ uint32_t ip_string_to_be_uint(const std::string &ip) {
uint32_t(a[0]);
}

std::string be_uint_to_ip_string(uint32_t ip) {
std::string nbo_uint_to_ip_string(uint32_t ip) {
struct in_addr ip_addr;
ip_addr.s_addr = ip;
return std::string(inet_ntoa(ip_addr));
}

/* https://stackoverflow.com/a/7326381 */
uint64_t mac_string_to_be_uint(const std::string &mac) {
uint64_t mac_string_to_nbo_uint(const std::string &mac) {
uint8_t a[6];
int last = -1;
int rc = sscanf(mac.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx%n", a + 0, a + 1,
Expand All @@ -71,7 +71,7 @@ uint64_t mac_string_to_be_uint(const std::string &mac) {
uint64_t(a[2]) << 16 | uint64_t(a[1]) << 8 | uint64_t(a[0]);
}

std::string be_uint_to_mac_string(uint64_t mac) {
std::string nbo_uint_to_mac_string(uint64_t mac) {
uint8_t a[6];
for (int i = 0; i < 6; i++) {
a[i] = (mac >> i * 8) & 0xFF;
Expand Down
2 changes: 1 addition & 1 deletion src/polycubed/src/utils/netlink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ std::string Netlink::get_iface_mac(const std::string &iface) {

uint64_t mac_;
memcpy(&mac_, mac, sizeof mac_);
return polycube::service::utils::be_uint_to_mac_string(mac_);
return polycube::service::utils::nbo_uint_to_mac_string(mac_);
}

void Netlink::set_iface_cidr(const std::string &iface,
Expand Down
6 changes: 3 additions & 3 deletions src/services/pcn-bridge/src/Fdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ std::shared_ptr<FdbEntry> Fdb::getEntry(const uint16_t &vlan,
auto fwdtable = parent_.get_hash_table<fwd_key, fwd_entry>("fwdtable");
fwd_key key{
.vlan = vlan,
.mac = polycube::service::utils::mac_string_to_be_uint(mac),
.mac = polycube::service::utils::mac_string_to_nbo_uint(mac),
};

try {
Expand Down Expand Up @@ -136,7 +136,7 @@ void Fdb::addEntry(const uint16_t &vlan, const std::string &mac,

fwd_key key{
.vlan = vlan,
.mac = polycube::service::utils::mac_string_to_be_uint(mac),
.mac = polycube::service::utils::mac_string_to_nbo_uint(mac),
};

fwd_entry value{
Expand Down Expand Up @@ -181,7 +181,7 @@ void Fdb::delEntry(const uint16_t &vlan, const std::string &mac) {

fwd_key key{
.vlan = vlan,
.mac = polycube::service::utils::mac_string_to_be_uint(mac),
.mac = polycube::service::utils::mac_string_to_nbo_uint(mac),
};

try {
Expand Down
4 changes: 2 additions & 2 deletions src/services/pcn-bridge/src/FdbEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void FdbEntry::setPort(const std::string &value) {

fwd_key key{
.vlan = vlan_,
.mac = polycube::service::utils::mac_string_to_be_uint(mac_),
.mac = polycube::service::utils::mac_string_to_nbo_uint(mac_),
};

struct timespec now_timespec;
Expand Down Expand Up @@ -132,7 +132,7 @@ std::shared_ptr<FdbEntry> FdbEntry::constructFromMap(Fdb &parent,
}

uint16_t vlan = key.vlan;
std::string mac = polycube::service::utils::be_uint_to_mac_string(key.mac);
std::string mac = polycube::service::utils::nbo_uint_to_mac_string(key.mac);
uint32_t entry_age = now - timestamp;
uint32_t port_no = value.port;

Expand Down
6 changes: 3 additions & 3 deletions src/services/pcn-bridge/src/Stp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Stp::Stp(Bridge &parent, const StpJsonObject &conf)
vlan_ = conf.getVlan();

stp_ = stp_create(parent.get_name().c_str(),
reverseMac(polycube::service::utils::mac_string_to_be_uint(
reverseMac(polycube::service::utils::mac_string_to_nbo_uint(
parent.getMac())),
sendBPDUproxy, this);

Expand Down Expand Up @@ -122,7 +122,7 @@ void Stp::decrementCounter() {

void Stp::changeMac(const std::string &mac) {
stp_set_bridge_id(
stp_, reverseMac(polycube::service::utils::mac_string_to_be_uint(mac)));
stp_, reverseMac(polycube::service::utils::mac_string_to_nbo_uint(mac)));

stp_set_bridge_priority(stp_, priority_);
}
Expand Down Expand Up @@ -161,7 +161,7 @@ void Stp::sendBPDU(struct ofpbuf *bpdu, int port_no, void *aux) {
uint8_t *buf = (uint8_t *)ofpbuf_base(bpdu);

uint64_t mac =
polycube::service::utils::mac_string_to_be_uint(parent_.getMac());
polycube::service::utils::mac_string_to_nbo_uint(parent_.getMac());

memcpy(buf + 6, &mac, ETH_ADDR_LEN); // set source mac

Expand Down
4 changes: 2 additions & 2 deletions src/services/pcn-ddosmitigator/src/BlacklistDst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ BlacklistDst::~BlacklistDst() {
parent_.get_percpuhash_table<uint32_t, uint64_t>("dstblacklist");
logger()->debug("BlacklistDst Destructor. ip {0} ", ip_);
try {
dstblacklist.remove(utils::ip_string_to_be_uint(ip_));
dstblacklist.remove(utils::ip_string_to_nbo_uint(ip_));
} catch (...) {
}
}
Expand Down Expand Up @@ -71,7 +71,7 @@ uint64_t BlacklistDst::getDropPkts() {
try {
auto dstblacklist =
parent_.get_percpuhash_table<uint32_t, uint64_t>("dstblacklist");
auto values = dstblacklist.get(utils::ip_string_to_be_uint(ip_));
auto values = dstblacklist.get(utils::ip_string_to_nbo_uint(ip_));

pkts = std::accumulate(values.begin(), values.end(), pkts);

Expand Down
4 changes: 2 additions & 2 deletions src/services/pcn-ddosmitigator/src/BlacklistSrc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ BlacklistSrc::~BlacklistSrc() {
parent_.get_percpuhash_table<uint32_t, uint64_t>("srcblacklist");
logger()->debug("BlacklistSrc Destructor. ip {0} ", ip_);
try {
srcblacklist.remove(utils::ip_string_to_be_uint(ip_));
srcblacklist.remove(utils::ip_string_to_nbo_uint(ip_));
} catch (...) {
}
}
Expand Down Expand Up @@ -70,7 +70,7 @@ uint64_t BlacklistSrc::getDropPkts() {
try {
auto srcblacklist =
parent_.get_percpuhash_table<uint32_t, uint64_t>("srcblacklist");
auto values = srcblacklist.get(utils::ip_string_to_be_uint(ip_));
auto values = srcblacklist.get(utils::ip_string_to_nbo_uint(ip_));

pkts = std::accumulate(values.begin(), values.end(), pkts);

Expand Down
4 changes: 2 additions & 2 deletions src/services/pcn-ddosmitigator/src/Ddosmitigator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void Ddosmitigator::addBlacklistSrc(const std::string &ip,

auto srcblacklist =
get_percpuhash_table<uint32_t, uint64_t>("srcblacklist");
srcblacklist.set(utils::ip_string_to_be_uint(ip), 0);
srcblacklist.set(utils::ip_string_to_nbo_uint(ip), 0);
} catch (...) {
throw std::runtime_error("unable to add element to map");
}
Expand Down Expand Up @@ -277,7 +277,7 @@ void Ddosmitigator::addBlacklistDst(const std::string &ip,

auto dstblacklist =
get_percpuhash_table<uint32_t, uint64_t>("dstblacklist");
dstblacklist.set(utils::ip_string_to_be_uint(ip), 0);
dstblacklist.set(utils::ip_string_to_nbo_uint(ip), 0);
} catch (...) {
throw std::runtime_error("unable to add element to map");
}
Expand Down
4 changes: 2 additions & 2 deletions src/services/pcn-firewall/src/Firewall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ std::vector<std::shared_ptr<SessionTable>> Firewall::getSessionTableList() {
auto key = connection.first;
auto value = connection.second;

conf.setSrc(utils::be_uint_to_ip_string(key.srcIp));
conf.setDst(utils::be_uint_to_ip_string(key.dstIp));
conf.setSrc(utils::nbo_uint_to_ip_string(key.srcIp));
conf.setDst(utils::nbo_uint_to_ip_string(key.dstIp));
conf.setL4proto(ChainRule::protocol_from_int_to_string(key.l4proto));
conf.setSport(ntohs(key.srcPort));
conf.setDport(ntohs(key.dstPort));
Expand Down
4 changes: 2 additions & 2 deletions src/services/pcn-firewall/src/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ struct IpAddr {
uint8_t netmask;
uint32_t ruleId;
std::string toString() const {
return utils::be_uint_to_ip_string(ip) + "/" + std::to_string(netmask);
return utils::nbo_uint_to_ip_string(ip) + "/" + std::to_string(netmask);
}
void fromString(std::string ipnetmask) {
std::string ip_;
Expand All @@ -86,7 +86,7 @@ struct IpAddr {
throw std::runtime_error("Netmask can't be bigger than 32");

ip_ = ipnetmask.substr(0, found);
ip = utils::ip_string_to_be_uint(ip_);
ip = utils::ip_string_to_nbo_uint(ip_);
netmask = netmask_;
}
bool operator<(const IpAddr &that) const {
Expand Down
2 changes: 1 addition & 1 deletion src/services/pcn-firewall/src/modules/IpLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void Firewall::IpLookup::updateTableValue(uint8_t netmask, std::string ip,
tableName += "Trie";

lpm_k key{
.netmask_len = netmask, .ip = utils::ip_string_to_be_uint(ip),
.netmask_len = netmask, .ip = utils::ip_string_to_nbo_uint(ip),
};

auto table = firewall.get_raw_table(tableName, index, getProgramType());
Expand Down
8 changes: 4 additions & 4 deletions src/services/pcn-iptables/src/Iptables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,11 @@ std::vector<std::shared_ptr<SessionTable>> Iptables::getSessionTableList() {
auto value = connection.second;

if (value.ipRev) {
conf.setSrc(utils::be_uint_to_ip_string(key.dstIp));
conf.setDst(utils::be_uint_to_ip_string(key.srcIp));
conf.setSrc(utils::nbo_uint_to_ip_string(key.dstIp));
conf.setDst(utils::nbo_uint_to_ip_string(key.srcIp));
} else {
conf.setSrc(utils::be_uint_to_ip_string(key.srcIp));
conf.setDst(utils::be_uint_to_ip_string(key.dstIp));
conf.setSrc(utils::nbo_uint_to_ip_string(key.srcIp));
conf.setDst(utils::nbo_uint_to_ip_string(key.dstIp));
}

conf.setL4proto(ChainRule::protocolFromIntToString(key.l4proto));
Expand Down
4 changes: 2 additions & 2 deletions src/services/pcn-iptables/src/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ struct IpAddr {
uint8_t netmask;
uint32_t rule_id;
std::string toString() const {
return utils::be_uint_to_ip_string(ip) + "/" + std::to_string(netmask);
return utils::nbo_uint_to_ip_string(ip) + "/" + std::to_string(netmask);
}
void fromString(std::string ipnetmask) {
std::string ip_;
Expand All @@ -104,7 +104,7 @@ struct IpAddr {
throw std::runtime_error("Netmask can't be bigger than 32");

ip_ = ipnetmask.substr(0, found);
ip = utils::ip_string_to_be_uint(ip_);
ip = utils::ip_string_to_nbo_uint(ip_);
netmask = netmask_;
}
bool operator<(const IpAddr &that) const {
Expand Down
4 changes: 2 additions & 2 deletions src/services/pcn-iptables/src/modules/ChainSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void Iptables::ChainSelector::updateLocalIps() {
iptables_.logger()->info("ip: {0} was not present. ++ ADDING",
new_ip.first);
try {
uint32_t ip_be = polycube::service::utils::ip_string_to_be_uint(
uint32_t ip_be = polycube::service::utils::ip_string_to_nbo_uint(
removeNetFromIp(new_ip.first));
auto localip_table =
iptables_.get_hash_table<uint32_t, int>("localip", getIndex());
Expand All @@ -117,7 +117,7 @@ void Iptables::ChainSelector::updateLocalIps() {
iptables_.logger()->info("ip: {0} is not present. -- REMOVING",
(*old_ip).first);
try {
uint32_t ip_be = polycube::service::utils::ip_string_to_be_uint(
uint32_t ip_be = polycube::service::utils::ip_string_to_nbo_uint(
removeNetFromIp((*old_ip).first));
auto localipTable =
iptables_.get_hash_table<uint32_t, int>("localip", getIndex());
Expand Down
2 changes: 1 addition & 1 deletion src/services/pcn-iptables/src/modules/IpLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void Iptables::IpLookup::updateTableValue(uint8_t netmask, std::string ip,
table_name += "Output";

lpm_k key{
.netmask_len = netmask, .ip = utils::ip_string_to_be_uint(ip),
.netmask_len = netmask, .ip = utils::ip_string_to_nbo_uint(ip),
};

std::lock_guard<std::mutex> guard(program_mutex_);
Expand Down
10 changes: 5 additions & 5 deletions src/services/pcn-k8switch/src/K8switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ std::shared_ptr<FwdTable> K8switch::getFwdTable(const std::string &address) {
auto fwd_table = get_array_table<pod>("fwd_table");
auto entry = fwd_table.get(ip_key);

std::string mac = utils::be_uint_to_ip_string(entry.mac);
std::string mac = utils::nbo_uint_to_ip_string(entry.mac);
std::string port(get_port(entry.port)->name());
return std::make_shared<FwdTable>(FwdTable(*this, address, mac, port));
} catch (std::exception &e) {
Expand All @@ -499,8 +499,8 @@ std::vector<std::shared_ptr<FwdTable>> K8switch::getFwdTableList() {

// TODO: key is only the index, it should be converted back to the full IP
// address
std::string ip = utils::be_uint_to_ip_string(key);
std::string mac = utils::be_uint_to_mac_string(value.mac);
std::string ip = utils::nbo_uint_to_ip_string(key);
std::string mac = utils::nbo_uint_to_mac_string(value.mac);
std::string port(get_port(value.port)->name());

fwd_table_entries.push_back(
Expand All @@ -524,7 +524,7 @@ void K8switch::addFwdTable(const std::string &address,
auto port = get_port(conf.getPort());

pod p{
.mac = utils::mac_string_to_be_uint(conf.getMac()),
.mac = utils::mac_string_to_nbo_uint(conf.getMac()),
.port = uint16_t(port->index()),
};

Expand Down Expand Up @@ -560,4 +560,4 @@ void K8switch::delFwdTable(const std::string &address) {

void K8switch::delFwdTableList() {
throw std::runtime_error("FwdTable::remove non supported");
}
}
16 changes: 8 additions & 8 deletions src/services/pcn-k8switch/src/Service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ void Service::removeServiceFromKernelMap() {

for (uint16_t index = 0; index < backend_size_ * BACKEND_REPLICAS; index++) {
vip key{
.ip = utils::ip_string_to_be_uint(getVip()),
.ip = utils::ip_string_to_nbo_uint(getVip()),
.port = htons(getVport()),
.proto = htons(Service::convertProtoToNumber(getProto())),
.index = index,
Expand All @@ -285,7 +285,7 @@ void Service::removeServiceFromKernelMap() {
parent_.get_hash_table<backend, vip>(EBPF_BACKEND_TO_SERVICE_MAP);
for (auto &bck : service_backends_) {
backend key{
.ip = utils::ip_string_to_be_uint(bck.second.getIp()),
.ip = utils::ip_string_to_nbo_uint(bck.second.getIp()),
.port = htons(bck.second.getPort()),
.proto = htons(Service::convertProtoToNumber(getProto())),
};
Expand Down Expand Up @@ -314,7 +314,7 @@ void Service::updateKernelServiceMap() {
uint16_t index = 0;

vip key{
.ip = utils::ip_string_to_be_uint(getVip()),
.ip = utils::ip_string_to_nbo_uint(getVip()),
.port = htons(getVport()),
.proto = htons(Service::convertProtoToNumber(getProto())),
.index = index,
Expand All @@ -324,7 +324,7 @@ void Service::updateKernelServiceMap() {
// pool.
// In doesn't indicate a real backend
backend value{
.ip = utils::ip_string_to_be_uint(getVip()),
.ip = utils::ip_string_to_nbo_uint(getVip()),
.port = uint8_t(consistent_array.size()),
.proto = 0,
};
Expand All @@ -341,14 +341,14 @@ void Service::updateKernelServiceMap() {
auto &bck = service_backends_.at(key);

vip backend_key{
.ip = utils::ip_string_to_be_uint(getVip()),
.ip = utils::ip_string_to_nbo_uint(getVip()),
.port = htons(getVport()),
.proto = htons(Service::convertProtoToNumber(getProto())),
.index = index,
};

backend backend_value{
.ip = utils::ip_string_to_be_uint(bck.getIp()),
.ip = utils::ip_string_to_nbo_uint(bck.getIp()),
.port = htons(bck.getPort()),
.proto = htons(Service::convertProtoToNumber(getProto())),
};
Expand Down Expand Up @@ -540,7 +540,7 @@ std::vector<int> Service::getRandomIntVector(int vect_size) {

void Service::insertAsKubeProxyService() {
vip key{
.ip = utils::ip_string_to_be_uint(getVip()),
.ip = utils::ip_string_to_nbo_uint(getVip()),
.port = htons(getVport()),
.proto = htons(Service::convertProtoToNumber(getProto())),
.index = 0,
Expand All @@ -549,7 +549,7 @@ void Service::insertAsKubeProxyService() {
// This is a special value used to indicate that the service is implemented by
// kube-proxy
backend value{
.ip = utils::ip_string_to_be_uint(getVip()), .port = KUBE_PROXY_FLAG,
.ip = utils::ip_string_to_nbo_uint(getVip()), .port = KUBE_PROXY_FLAG,
};

auto services_map = parent_.get_hash_table<vip, backend>(EBPF_SERVICE_MAP);
Expand Down
Loading

0 comments on commit c6751b9

Please sign in to comment.