Fix compilation issue with older gcc
[samplevnf.git] / common / VIL / l2l3_stack / lib_arp.c
index e2f755f..d59f4b7 100644 (file)
@@ -71,7 +71,7 @@ uint32_t nd_buffer = ARP_BUF_DEFAULT;
 #define IN6ADDRSZ 16
 #define MAX_PORTS      32
 
-static int my_inet_pton_ipv6(int af, const char *src, void *dst);
+int my_inet_pton_ipv6(int af, const char *src, void *dst);
 static int inet_pton_ipv6(const char *src, unsigned char *dst);
 static int inet_pton_ipv4(const char *src, unsigned char *dst);
 static void local_arp_cache_init(void);
@@ -294,13 +294,20 @@ struct arp_entry_data *get_dest_mac_addr_ipv4(const uint32_t nhip,
                                 uint32_t phy_port, struct ether_addr *hw_addr)
 {
        struct arp_entry_data *ret_arp_data = NULL;
+       struct ether_addr *x;
        uint8_t index;
 
        /* as part of optimization we store mac address in cache
         * & thus can be sent without having to retrieve
         */
        if (arp_cache_dest_mac_present(phy_port)) {
-               ether_addr_copy(get_local_cache_hw_addr(phy_port, nhip), hw_addr);
+               x = get_local_cache_hw_addr(phy_port, nhip);
+               if (!x) {
+                       printf("local copy of address not stored\n");
+                       return NULL;
+               }
+
+               ether_addr_copy(x, hw_addr);
                return &arp_entry_data_default;
        }
 
@@ -352,11 +359,17 @@ struct nd_entry_data *get_dest_mac_addr_ipv6(uint8_t nhipv6[],
 
        struct nd_entry_data *ret_nd_data = NULL;
        struct nd_key_ipv6 tmp_nd_key;
+       struct ether_addr *addr;
        tmp_nd_key.port_id = phy_port;
 
        if (nd_cache_dest_mac_present(phy_port)) {
-               ether_addr_copy(get_nd_local_link_hw_addr(
-                                       (uint8_t)phy_port, nhipv6), hw_addr);
+               addr = get_nd_local_link_hw_addr((uint8_t)phy_port, nhipv6);
+               if (!addr) {
+                       printf("local copy not stored\n");
+                       return NULL;
+               }
+
+               ether_addr_copy(addr, hw_addr);
                return &nd_entry_data_default;
        }
 
@@ -743,6 +756,7 @@ struct arp_entry_data *retrieve_arp_entry(struct arp_key_ipv4 arp_key, uint8_t m
 struct nd_entry_data *retrieve_nd_entry(struct nd_key_ipv6 nd_key, uint8_t mode)
 {
        struct nd_entry_data *ret_nd_data = NULL;
+       l2_phy_interface_t *port;
        nd_key.filler1 = 0;
        nd_key.filler2 = 0;
        nd_key.filler3 = 0;
@@ -822,7 +836,14 @@ struct nd_entry_data *retrieve_nd_entry(struct nd_key_ipv6 nd_key, uint8_t mode)
 
                 ret_nd_data->timer_key = callback_key;
                /* send nd request */
-               request_nd(callback_key->ipv6, ifm_get_port(callback_key->port_id));
+               port = ifm_get_port(callback_key->port_id);
+               if (!port) {
+                       printf("port returned is NULL inside retrieve_nd_entry\n");
+                       rte_rwlock_write_unlock(&ret_nd_data->queue_lock);
+                       return NULL;
+               }
+
+               request_nd(callback_key->ipv6, port);
                rte_rwlock_write_unlock(&ret_nd_data->queue_lock);
        } else {
                if (ret_nd_data &&
@@ -849,7 +870,14 @@ struct nd_entry_data *retrieve_nd_entry(struct nd_key_ipv6 nd_key, uint8_t mode)
                        ret_nd_data->timer_key = callback_key;
 
                        /* send nd request */
-                       request_nd(callback_key->ipv6, ifm_get_port(callback_key->port_id));
+                       port = ifm_get_port(callback_key->port_id);
+                       if (!port) {
+                               printf("port returned is NULL inside retrieve_nd_entry\n");
+                               rte_rwlock_write_unlock(&ret_nd_data->queue_lock);
+                               return NULL;
+                       }
+
+                       request_nd(callback_key->ipv6, port);
                        rte_rwlock_write_unlock(&ret_nd_data->queue_lock);
                }
        }
@@ -911,7 +939,7 @@ void print_arp_table(void)
                 lib_arp_populate_called, lib_arp_delete_called,
                 lib_arp_duplicate_found);
 
-       printf("ARP table key len is %lu\n", sizeof(struct arp_key_ipv4));
+       printf("ARP table key len is %d\n", (uint32_t) sizeof(struct arp_key_ipv4));
 }
 
 /* ND IPv6 */
@@ -951,10 +979,10 @@ void print_nd_table(void)
                printf("\n");
        }
 
-       uint32_t i = 0;
+       uint32_t i = 0, p = 0;
        printf("\n\nND IPV6 routing table ...\n");
        printf ("\nNH_IP_Address                                        Depth          Port \n");
-       for(uint32_t p = 0; p < gw_get_num_ports(); p++ ) {
+       for(p = 0; p < gw_get_num_ports(); p++ ) {
                for (i = 0; i < p_nd_route_data[p]->nd_route_ent_cnt; i++) {
                        //              printf("\n");
 
@@ -974,7 +1002,7 @@ void print_nd_table(void)
                         lib_nd_nd_entry_found, lib_nd_no_arp_entry_found,
                         lib_nd_populate_called, lib_nd_delete_called,
                         lib_nd_duplicate_found);
-       printf("ND table key len is %lu\n\n", sizeof(struct nd_key_ipv6));
+       printf("ND table key len is %d\n\n", (uint32_t) sizeof(struct nd_key_ipv6));
 }
 
 void remove_arp_entry(struct arp_entry_data *ret_arp_data, void *arg)
@@ -983,10 +1011,13 @@ void remove_arp_entry(struct arp_entry_data *ret_arp_data, void *arg)
        struct arp_timer_key *arp_key = (struct arp_timer_key *)arg;
        lib_arp_delete_called++;
 
-       rte_timer_stop(ret_arp_data->timer);
-       rte_free(ret_arp_data->timer_key);
-       rte_free(ret_arp_data->buf_pkts);
-        ret_arp_data->buf_pkts = NULL;
+       if (ret_arp_data->timer) {
+               rte_timer_stop(ret_arp_data->timer);
+               rte_free(ret_arp_data->timer_key);
+               rte_free(ret_arp_data->buf_pkts);
+               ret_arp_data->buf_pkts = NULL;
+       }
+
        if (ARPICMP_DEBUG) {
                RTE_LOG(INFO, LIBARP,
                        "ARP Entry Deleted for IP :%d.%d.%d.%d , port %d\n",
@@ -1049,7 +1080,7 @@ arp_send_buffered_pkts(struct arp_entry_data *ret_arp_data,
        int i;
 
 
-       if (!hw_addr || !ret_arp_data)
+       if (!hw_addr || !ret_arp_data || !port)
                return;
 
        rte_rwlock_write_lock(&ret_arp_data->queue_lock);
@@ -1094,7 +1125,7 @@ nd_send_buffered_pkts(struct nd_entry_data *ret_nd_data,
        uint8_t *eth_dest, *eth_src;
        int i;
 
-       if (!hw_addr || !ret_nd_data)
+       if (!hw_addr || !ret_nd_data || !port)
                return;
 
        rte_rwlock_write_lock(&ret_nd_data->queue_lock);
@@ -1164,7 +1195,7 @@ populate_arp_entry(const struct ether_addr *hw_addr, uint32_t ipaddr,
                        if (new_arp_data->status == STALE) {
                                new_arp_data->status = PROBE;
                                if (ifm_chk_port_ipv4_enabled
-                                       (new_arp_data->port)) {
+                                       (new_arp_data->port) != IFM_FAILURE) {
                                        request_arp(new_arp_data->port,
                                                        new_arp_data->ip);
                                } else {
@@ -1190,8 +1221,8 @@ populate_arp_entry(const struct ether_addr *hw_addr, uint32_t ipaddr,
                } else {
                        rte_rwlock_write_lock(&new_arp_data->queue_lock);
                        ether_addr_copy(hw_addr, &new_arp_data->eth_addr);
-                       if ((new_arp_data->status == INCOMPLETE) ||
-                               (new_arp_data->status == PROBE)) {
+                       if (new_arp_data && ((new_arp_data->status == INCOMPLETE) ||
+                               (new_arp_data->status == PROBE))) {
                                new_arp_data->status = COMPLETE;
                                new_arp_data->mode = mode;
                                new_arp_data->n_confirmed = rte_rdtsc();
@@ -1292,6 +1323,7 @@ void populate_nd_entry(const struct ether_addr *hw_addr, uint8_t ipv6[],
        uint8_t i, val = 0;
        struct nd_key_ipv6 nd_key;
        nd_key.port_id = portid;
+       l2_phy_interface_t *port;
 
        for (i = 0; i < ND_IPV6_ADDR_SIZE; i++) {
                nd_key.ipv6[i] = ipv6[i];
@@ -1339,8 +1371,10 @@ void populate_nd_entry(const struct ether_addr *hw_addr, uint8_t ipv6[],
                        if (new_nd_data->status == STALE) {
                                new_nd_data->retry_count = 0;   // Reset
                                new_nd_data->status = PROBE;
-                               request_nd(new_nd_data->ipv6,
-                                        ifm_get_port(new_nd_data->port));
+                               port = ifm_get_port(new_nd_data->port);
+
+                               if (port)
+                                       request_nd(new_nd_data->ipv6, port);
 
                                if (rte_timer_reset(new_nd_data->timer,
                                                (arp_timeout * rte_get_tsc_hz()),
@@ -1357,8 +1391,8 @@ void populate_nd_entry(const struct ether_addr *hw_addr, uint8_t ipv6[],
                } else {
                        rte_rwlock_write_lock(&new_nd_data->queue_lock);
                        ether_addr_copy(hw_addr, &new_nd_data->eth_addr);
-                       if ((new_nd_data->status == INCOMPLETE) ||
-                               (new_nd_data->status == PROBE)) {
+                       if (new_nd_data && ((new_nd_data->status == INCOMPLETE) ||
+                               (new_nd_data->status == PROBE))) {
                                new_nd_data->status = COMPLETE;
                                new_nd_data->mode = mode;
                                new_nd_data->n_confirmed = rte_rdtsc();
@@ -1894,7 +1928,7 @@ void process_arpicmp_pkt(struct rte_mbuf *pkt, l2_phy_interface_t *port)
  * author:
  *      Paul Vixie, 1996.
  */
-static int my_inet_pton_ipv6(int af, const char *src, void *dst)
+int my_inet_pton_ipv6(int af, const char *src, void *dst)
 {
        switch (af) {
        case AF_INET:
@@ -2089,6 +2123,7 @@ static int arp_parse_args(struct pipeline_params *params)
        RTE_SET_USED(nd_route_tbl_present);
        RTE_SET_USED(arp_route_tbl_present);
        for (numArg = 0; numArg < params->n_args; numArg++) {
+
                char *arg_name = params->args_name[numArg];
                char *arg_value = params->args_value[numArg];
 
@@ -2189,12 +2224,15 @@ static int arp_parse_args(struct pipeline_params *params)
                                        tx_phy_port_num, txport);
 
                                if ((rxport >= PIPELINE_MAX_PORT_IN) ||
-                                               (txport >= PIPELINE_MAX_PORT_IN) ||
-                                               (in_port_dir_a[rxport] != 1)) {
+                                               (txport >= PIPELINE_MAX_PORT_IN)) {
                                        printf
-                                                       ("CG-NAPT parse error - incorrect prv-pub translation. Rx %d, Tx %d, Rx Dir %d\n",
-                                                        rxport, txport,
-                                                        in_port_dir_a[rxport]);
+                                                       ("CG-NAPT parse error - incorrect prv-pub translation. Rx %d, Tx %d\n",
+                                                        rxport, txport);
+                                       return -1;
+                               }
+
+                               if (in_port_dir_a[rxport] != 1) {
+                                       printf("CG-NAPT parse error - incorrect rx_port supplied\n");
                                        return -1;
                                }
 
@@ -2232,7 +2270,7 @@ static int arp_parse_args(struct pipeline_params *params)
                if (strcmp(arg_name, "arp_route_tbl") == 0) {
                        arp_route_tbl_present = 1;
                        uint32_t dest_ip = 0, mask = 0, tx_port = 0, nh_ip = 0,
-                                i = 0, j = 0, k = 0;
+                                i = 0, j = 0, k = 0, l = 0;
                        uint32_t arp_route_tbl_str_max_len = 10;
                        char dest_ip_str[arp_route_tbl_str_max_len];
                        char mask_str[arp_route_tbl_str_max_len];
@@ -2240,36 +2278,48 @@ static int arp_parse_args(struct pipeline_params *params)
                        char nh_ip_str[arp_route_tbl_str_max_len];
                        char *token = strtok(arg_value, "(");
                        while (token) {
-                               i = 0;
-                               while ((i < (arp_route_tbl_str_max_len - 1))
-                                                        && (token[i] != ',')) {
-                                       nh_ip_str[i] = token[i];
-                                       i++;
-                               }
-                               nh_ip_str[i] = '\0';
-                               nh_ip = strtoul(nh_ip_str, NULL, 16);
-
-                               i++;
-                               j = 0;
-                               while ((j < (arp_route_tbl_str_max_len - 1))
-                                                        && (token[i + j] != ',')) {
-                                       mask_str[j] = token[i + j];
-                                       j++;
-                               }
-                               mask_str[j] = '\0';
-                               mask = strtoul(mask_str, NULL, 16);
-
-                               j++;
-                               k = 0;
-                               while ((k < (arp_route_tbl_str_max_len - 1))
-                                                        && (token[i + j + k] != ',')) {
-                                       tx_port_str[k] = token[i + j + k];
-                                       k++;
-                               }
-                               tx_port_str[k] = '\0';
-                               tx_port = strtoul(tx_port_str, NULL, 16);       //atoi(tx_port_str);
-
-                               k++;
+                                i = 0;
+                                while ((i < (arp_route_tbl_str_max_len - 1))
+                                       && (token[i] != ',')) {
+                                        dest_ip_str[i] = token[i];
+                                        i++;
+                                }
+                                dest_ip_str[i] = '\0';
+                                dest_ip = strtoul(dest_ip_str, NULL, 16);
+
+                                i++;
+                                j = 0;
+                                while ((j < (arp_route_tbl_str_max_len - 1))
+                                       && (token[i + j] != ',')) {
+                                        mask_str[j] = token[i + j];
+                                        j++;
+                                }
+                                mask_str[j] = '\0';
+                                mask = strtoul(mask_str, NULL, 16);
+
+                                j++;
+                                k = 0;
+                                while ((k < (arp_route_tbl_str_max_len - 1))
+                                       && (token[i + j + k] != ',')) {
+                                        tx_port_str[k] = token[i + j + k];
+                                        k++;
+                                }
+                                tx_port_str[k] = '\0';
+                                tx_port = strtoul(tx_port_str, NULL, 16);       //atoi(tx_port_str);
+                                if (tx_port > MAX_ROUTE_ENTRY_SIZE) {
+                                        printf("tx_port is greater than route entry max size\n");
+                                        continue;
+                                }
+
+                                k++;
+                                l = 0;
+                                while ((l < (arp_route_tbl_str_max_len - 1))
+                                       && (token[i + j + k + l] != ')')) {
+                                        nh_ip_str[l] = token[i + j + k + l];
+                                        l++;
+                                }
+                                nh_ip_str[l] = '\0';
+                                nh_ip = strtoul(nh_ip_str, NULL, 16);   //atoi(nh_ip_str);
 
                                if (1) {
                                        RTE_LOG(INFO, LIBARP, "token: %s, "
@@ -2298,7 +2348,9 @@ static int arp_parse_args(struct pipeline_params *params)
                                lentry->port = tx_port;
                                lentry->nh = nh_ip;
                                lentry->nh_mask = nh_ip & mask;
+                               printf("*********lentry_mask:%x lentry_port:%d lentry_nh:%x mask:%x\n", lentry->mask, lentry->port, lentry->nh, lentry->nh_mask);
                                p_route_data[tx_port]->route_ent_cnt++;
+                               vnf_gateway = 1;
                                token = strtok(NULL, "(");
                        }
 
@@ -2308,9 +2360,10 @@ static int arp_parse_args(struct pipeline_params *params)
                /* nd_route_tbl */
                if (strcmp(arg_name, "nd_route_tbl") == 0) {
                        nd_route_tbl_present = 1;
-
+                       uint8_t dest_ipv6[16];
+                       char dest_ipv6_str[128];
                        uint8_t depth = 0, tx_port = 0, nh_ipv6[16];
-                       uint8_t i = 0, j = 0, k = 0;
+                       uint8_t i = 0, j = 0, k = 0, l = 0;
                        uint8_t nd_route_tbl_str_max_len = 128; //64;
 //                     char dest_ipv6_str[nd_route_tbl_str_max_len];
                        char depth_str[nd_route_tbl_str_max_len];
@@ -2318,39 +2371,50 @@ static int arp_parse_args(struct pipeline_params *params)
                        char nh_ipv6_str[nd_route_tbl_str_max_len];
                        char *token = strtok(arg_value, "(");
                        while (token) {
-                               i = 0;
-                               while ((i < (nd_route_tbl_str_max_len - 1))
-                                                        && (token[i] != ',')) {
-                                       nh_ipv6_str[i] = token[i];
-                                       i++;
-                               }
-                               nh_ipv6_str[i] = '\0';
-                               my_inet_pton_ipv6(AF_INET6, nh_ipv6_str,
-                                                       &nh_ipv6);
-                               i++;
-                               j = 0;
-                               while ((j < (nd_route_tbl_str_max_len - 1))
-                                                        && (token[i + j] != ',')) {
-                                       depth_str[j] = token[i + j];
-                                       j++;
-                               }
-                               depth_str[j] = '\0';
-                               //converting string char to integer
-                               int s;
-                               for (s = 0; depth_str[s] != '\0'; ++s)
-                                       depth = depth * 10 + depth_str[s] - '0';
-
-                               j++;
-                               k = 0;
-                               while ((k < (nd_route_tbl_str_max_len - 1))
-                                                        && (token[i + j + k] != ',')) {
-                                       tx_port_str[k] = token[i + j + k];
-                                       k++;
-                               }
-                               tx_port_str[k] = '\0';
-                               tx_port = strtoul(tx_port_str, NULL, 16);       //atoi(tx_port_str);
+                                i = 0;
+                                while ((i < (nd_route_tbl_str_max_len - 1))
+                                       && (token[i] != ',')) {
+                                        dest_ipv6_str[i] = token[i];
+                                        i++;
+                                }
+                                dest_ipv6_str[i] = '\0';
+                                my_inet_pton_ipv6(AF_INET6, dest_ipv6_str,
+                                                  &dest_ipv6);
+
+                                i++;
+                                j = 0;
+                                while ((j < (nd_route_tbl_str_max_len - 1))
+                                       && (token[i + j] != ',')) {
+                                        depth_str[j] = token[i + j];
+                                        j++;
+                                }
+                                depth_str[j] = '\0';
+                                //converting string char to integer
+                                int s;
+                                for (s = 0; depth_str[s] != '\0'; ++s)
+                                        depth = depth * 10 + depth_str[s] - '0';
+
+                                j++;
+                                k = 0;
+                                while ((k < (nd_route_tbl_str_max_len - 1))
+                                       && (token[i + j + k] != ',')) {
+                                        tx_port_str[k] = token[i + j + k];
+                                        k++;
+                                }
+                                tx_port_str[k] = '\0';
+                                tx_port = strtoul(tx_port_str, NULL, 16);       //atoi(tx_port_str);
+
+                                k++;
+                                l = 0;
+                                while ((l < (nd_route_tbl_str_max_len - 1))
+                                       && (token[i + j + k + l] != ')')) {
+                                        nh_ipv6_str[l] = token[i + j + k + l];
+                                        l++;
+                                }
+                                nh_ipv6_str[l] = '\0';
+                                my_inet_pton_ipv6(AF_INET6, nh_ipv6_str,
+                                                  &nh_ipv6);
 
-                               k++;
                                struct nd_route_table_entry *lentry =
                                        &p_nd_route_data[tx_port]->nd_route_table
                                [p_nd_route_data[tx_port]->nd_route_ent_cnt];
@@ -2363,6 +2427,7 @@ static int arp_parse_args(struct pipeline_params *params)
                                lentry->port = tx_port;
 
                                p_nd_route_data[tx_port]->nd_route_ent_cnt++;
+                               vnf_gateway = 1;
 
                                token = strtok(NULL, "(");
                        }
@@ -2618,7 +2683,7 @@ void arp_timer_callback(struct rte_timer *timer, void *arg)
                                        }
 
                                        if (ifm_chk_port_ipv4_enabled
-                                               (ret_arp_data->port)) {
+                                               (ret_arp_data->port) != IFM_FAILURE) {
                                                request_arp(ret_arp_data->port,
                                                                ret_arp_data->ip);
                                        } else {
@@ -2684,6 +2749,7 @@ void nd_timer_callback(struct rte_timer *timer, void *arg)
        int j;
        struct nd_entry_data *ret_nd_data = NULL;
        uint64_t now;
+       l2_phy_interface_t *port;
 
         nd_key.port_id = timer_key->port_id;
         nd_key.filler1 = 0;
@@ -2729,9 +2795,13 @@ void nd_timer_callback(struct rte_timer *timer, void *arg)
                                                "TIMER STARTED FOR %u seconds\n",
                                                        ARP_TIMER_EXPIRY);
                                        }
+                                       port = ifm_get_port(ret_nd_data->port);
+                                       if (!port) {
+                                               printf("port is NULL in nd_timer_callback\n");
+                                               return;
+                                       }
 
-                                       request_nd(ret_nd_data->ipv6,
-                                                ifm_get_port(ret_nd_data->port));
+                                       request_nd(ret_nd_data->ipv6, port);
                                        if (rte_timer_reset(ret_nd_data->timer,
                                                                (PROBE_TIME *
                                                                 rte_get_tsc_hz()/ 1000),