Skip to content

Commit

Permalink
[Bug Fix] Add Check for Validating Ports
Browse files Browse the repository at this point in the history
This fixes the invalid port print error message that would occur when
an NF attempted to use retrieve the mac address of a port that wasn't
bound to DPDK. Developers can now retrieve a fake mac addr for
testing purposes or a real mac address in case the request port is
bound.

Commit log:

* Add check for invalid port

* Update bound ports detection

* Format code

* Conform with bash linter

* Add onvm_macaddr_get

* Remove redundant warning

* Update with call to onvm_macaddr_get

* Update examples/load_balancer/load_balancer.c

* Update onvm_macaddr_get macro to function

* Update onvm_macaddr_get usage

* Add onvm_get_fake_macaddr

* Update onvm_get_macaddr usage

* Seperate onvm_get_macaddr and onvm_get_fake_macaddr

* Update examples/speed_tester/speed_tester.c

* Update style

* Try to get real MAC address before using a fake

* Remove redundant else statement
  • Loading branch information
rohit-mp authored Apr 23, 2020
1 parent c0969b5 commit 3858c27
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 5 deletions.
8 changes: 6 additions & 2 deletions examples/load_balancer/load_balancer.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,15 +531,19 @@ packet_handler(struct rte_mbuf *pkt, struct onvm_pkt_meta *meta,
}

if (pkt->port == lb->server_port) {
rte_eth_macaddr_get(lb->client_port, &ehdr->s_addr);
if (onvm_get_macaddr(lb->client_port, &ehdr->s_addr) == -1) {
rte_exit(EXIT_FAILURE, "Failed to obtain MAC address\n");
}
for (i = 0; i < ETHER_ADDR_LEN; i++) {
ehdr->d_addr.addr_bytes[i] = flow_info->s_addr_bytes[i];
}

ip->src_addr = lb->ip_lb_client;
meta->destination = lb->client_port;
} else {
rte_eth_macaddr_get(lb->server_port, &ehdr->s_addr);
if (onvm_get_macaddr(lb->server_port, &ehdr->s_addr) == -1) {
rte_exit(EXIT_FAILURE, "Failed to obtain MAC address\n");
}
for (i = 0; i < ETHER_ADDR_LEN; i++) {
ehdr->d_addr.addr_bytes[i] = lb->server[flow_info->dest].d_addr_bytes[i];
}
Expand Down
4 changes: 3 additions & 1 deletion examples/load_generator/load_generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,9 @@ nf_setup(struct onvm_nf_local_ctx *nf_local_ctx) {
rte_exit(EXIT_FAILURE, "Failed to allocate common ehdr\n");
}

rte_eth_macaddr_get(0, &ehdr->s_addr);
if (onvm_get_macaddr(0, &ehdr->s_addr) == -1) {
rte_exit(EXIT_FAILURE, "Failed to obtain MAC address\n");
}
for (j = 0; j < ETHER_ADDR_LEN; ++j) {
ehdr->d_addr.addr_bytes[j] = d_addr_bytes[j];
}
Expand Down
4 changes: 3 additions & 1 deletion examples/scaling_example/scaling.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ nf_setup(__attribute__((unused)) struct onvm_nf_local_ctx *nf_local_ctx) {
ehdr = (struct ether_hdr *)rte_pktmbuf_append(pkt, packet_size);

/* Using manager mac addr for source*/
rte_eth_macaddr_get(0, &ehdr->s_addr);
if (onvm_get_macaddr(0, &ehdr->s_addr) == -1) {
onvm_get_fake_macaddr(&ehdr->s_addr);
}
for (j = 0; j < ETHER_ADDR_LEN; ++j) {
ehdr->d_addr.addr_bytes[j] = d_addr_bytes[j];
}
Expand Down
4 changes: 3 additions & 1 deletion examples/speed_tester/speed_tester.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,9 @@ nf_setup(struct onvm_nf_local_ctx *nf_local_ctx) {
/*using manager mac addr for source
*using input string for dest addr
*/
rte_eth_macaddr_get(0, &ehdr->s_addr);
if (onvm_get_macaddr(0, &ehdr->s_addr) == -1) {
onvm_get_fake_macaddr(&ehdr->s_addr);
}
for (j = 0; j < ETHER_ADDR_LEN; ++j) {
ehdr->d_addr.addr_bytes[j] = d_addr_bytes[j];
}
Expand Down
7 changes: 7 additions & 0 deletions onvm/go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ then
usage
fi

ports_detected=$("$RTE_SDK"/usertools/dpdk-devbind.py --status-dev net | sed '/Network devices using kernel driver/q' | grep -c "drv")
if [[ $ports_detected -lt $ports ]]
then
echo "Error: Invalid port mask. Insufficient NICs bound."
exit 1
fi

while getopts "a:r:d:s:t:l:p:z:cv" opt; do
case $opt in
a) virt_addr="--base-virtaddr=$OPTARG";;
Expand Down
26 changes: 26 additions & 0 deletions onvm/onvm_nflib/onvm_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include <rte_ether.h>
#include <rte_mbuf.h>
#include <rte_hash.h>
#include <rte_ethdev.h>

#include "onvm_config_common.h"
#include "onvm_msg_common.h"
Expand Down Expand Up @@ -495,4 +496,29 @@ whether_wakeup_client(struct onvm_nf *nf, struct nf_wakeup_info *nf_wakeup_info)

#define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1

/*
* Updates the ether_addr struct with a fake, safe MAC address.
*/
static inline int
onvm_get_fake_macaddr(struct ether_addr *mac_addr) {
uint16_t *mac_addr_bytes = (uint16_t *)((struct ether_addr *)(mac_addr)->addr_bytes);
mac_addr_bytes[0] = 2;
mac_addr_bytes[1] = 0;
mac_addr_bytes[2] = 0;
return 0;
}

/*
* Tries to fetch the MAC address of the port_id.
* Return 0 if port is valid, -1 if port is invalid.
*/
static inline int
onvm_get_macaddr(uint8_t port_id, struct ether_addr *mac_addr) {
if (!rte_eth_dev_is_valid_port(port_id)) {
return -1;
}
rte_eth_macaddr_get(port_id, mac_addr);
return 0;
}

#endif // _ONVM_COMMON_H_

0 comments on commit 3858c27

Please sign in to comment.