Merge "TempFix: vCGNAPT/vACL ipv4 perf issue"
[samplevnf.git] / common / vnf_common / rest_api.c
index 62e9646..8efd773 100644 (file)
@@ -52,7 +52,7 @@
 #include "pipeline_arpicmp.h"
 
 #include <civetweb.h>
-#include <json/json.h>
+#include <json-c/json.h>
 #include "app.h"
 #include "lib_arp.h"
 #include "interface.h"
@@ -163,7 +163,7 @@ extern int USE_RTM_LOCKS;
 extern rte_rwlock_t rwlock;
 extern interface_main_t ifm;
 extern struct cmdline *pipe_cl;
-extern uint16_t str2flowtype(char *string);
+extern uint16_t str2flowtype(const char *string);
 extern void app_run_file(cmdline_parse_ctx_t *ctx, const char *file_name);
 extern int parse_flexbytes(const char *q_arg, uint8_t *flexbytes,
                         uint16_t max_num);
@@ -179,7 +179,7 @@ extern int app_routeadd_config_ipv6(__attribute__((unused))
                                        uint32_t port_id, uint8_t ipv6[],
                                         uint32_t depth);
 
-enum rte_eth_input_set_field str2inset(char *string);
+enum rte_eth_input_set_field str2inset(const char *string);
 
 enum {
        MASTER = 0,
@@ -381,11 +381,17 @@ int get_pipelines_tokens(char *buf)
         uint32_t id;
 
         token = strtok(buf, "/ ");
+       if (!token)
+               return -1;
+
         if (strcmp(token, "pipelines")) {
                 return -1;
         }
 
         token = strtok(NULL, "/ ");
+       if (!token)
+               return -1;
+
         id = atoi(token);
         if (id > rapp->n_pipelines) {
                 return -1;
@@ -594,6 +600,11 @@ int set_hash_input_set_2(struct mg_connection *conn, uint32_t port_id,
         mg_printf(conn, "<html><body>");
         mg_printf(conn, "</body></html>\n");
 
+       if (!inset_field0 || !inset_field1 || !flow_type) {
+                mg_printf(conn, "inset_field0/1 or flow_type may be NULL!\n");
+               return 1;
+       }
+
         if (enable_flow_dir) {
                 mg_printf(conn, "FDIR Filter is Defined!\n");
                 mg_printf(conn, "Please undefine FDIR_FILTER flag and define "
@@ -610,10 +621,13 @@ int set_hash_input_set_2(struct mg_connection *conn, uint32_t port_id,
 
         memset(&info, 0, sizeof(info));
         info.info_type = RTE_ETH_HASH_FILTER_INPUT_SET_SELECT;
-        info.info.input_set_conf.flow_type = str2flowtype(strdup(flow_type));
+        info.info.input_set_conf.flow_type = str2flowtype(flow_type);
 
-        info.info.input_set_conf.field[0] = str2inset(strdup(inset_field0));
-        info.info.input_set_conf.field[1] = str2inset(strdup(inset_field1));
+       if (inset_field0)
+               info.info.input_set_conf.field[0] = str2inset(inset_field0);
+
+       if (inset_field1)
+               info.info.input_set_conf.field[1] = str2inset(inset_field1);
 
         info.info.input_set_conf.inset_size = 2;
 
@@ -830,11 +844,17 @@ int get_link_tokens(char *buf)
         int linkid;
 
         token = strtok(buf, "/ ");
+       if (!token)
+               return -1;
+
         if (strcmp(token, "link")) {
                 return -1;
         }
 
         token = strtok(NULL, "/ ");
+       if (!token)
+               return -1;
+
         linkid = atoi(token);
         if (linkid > current_cfg.num_ports) {
                 return -1;
@@ -966,8 +986,8 @@ int arpls_handler(struct mg_connection *conn, __rte_unused void *cbdata)
                         lib_arp_populate_called, lib_arp_delete_called,
                         lib_arp_duplicate_found);
 
-       len += sprintf(buf + len, "ARP table key len is %lu\n<br/>",
-                        sizeof(struct arp_key_ipv4));
+       len += sprintf(buf + len, "ARP table key len is %d\n<br/>",
+                        (uint32_t) sizeof(struct arp_key_ipv4));
         mg_printf(conn, "%s\n<br/>", &buf[0]);
         return 1; 
 }
@@ -994,6 +1014,7 @@ void get_mac(struct ether_addr *mac_addr, char *buf)
        for (i = 0; i < MAC_NUM_BYTES; i++) {
                mac_addr->addr_bytes[i] = strtoul(byteStr[i], NULL, 16);
        }
+       free(buf);
 }
 
 int arp_handler(struct mg_connection *conn, __rte_unused void *cbdata)
@@ -1126,7 +1147,14 @@ int arp_handler(struct mg_connection *conn, __rte_unused void *cbdata)
                                nd_key.filler2 = 0;
                                nd_key.filler3 = 0;
                                new_nd_data = retrieve_nd_entry(nd_key, STATIC_ND);
+
+                               if(new_nd_data == NULL) {
+                                       /* KW Fix */
+                                       mg_printf(conn,"Retrieve ND returned NULL\n");
+                                       return 1;
+                               }
                                remove_nd_entry_ipv6(new_nd_data, &nd_key);
+                               break;
                        case 3:
                                mg_printf(conn, "<p>ND REQ is not supported Yet!!!</p>");
                                break;
@@ -1145,7 +1173,7 @@ int route_handler(struct mg_connection *conn, __rte_unused void *cbdata)
         /* Handler may access the request info using mg_get_request_info */
         const struct mg_request_info *req_info = mg_get_request_info(conn);
         uint32_t portid = 0;
-       uint32_t i, j, status;
+       uint32_t i, j, status, p;
        char buf[MAX_BUF_SIZE];
        uint32_t mask = 0, num = 31;
 
@@ -1167,7 +1195,7 @@ int route_handler(struct mg_connection *conn, __rte_unused void *cbdata)
                mg_printf(conn, "\n\nND IPV6 routing table ...\n<br/>");
                mg_printf(conn, "\nNH_IP_Address                                "
                                "       Depth          Port \n<br/>");
-               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++) {
                                for (j = 0; j < ND_IPV6_ADDR_SIZE; j += 2) {
                                        mg_printf(conn, "%02X%02X ",
@@ -1205,7 +1233,7 @@ int route_handler(struct mg_connection *conn, __rte_unused void *cbdata)
        json_object_object_foreach(jobj, key, val) {
                if (!strcmp(key, "portid")) {
                        portid = atoi(json_object_get_string(val));
-                       if (portid > 64) {
+                       if (portid >= 64) {
                                mg_printf(conn, "Port not supported!!!\n");
                                return 1;
                        } else if (current_route_parms[portid].enable) {
@@ -1295,7 +1323,7 @@ int link_handler(struct mg_connection *conn, __rte_unused void *cbdata)
                if (!strcmp(key, "linkid")) {
                        link = atoi(json_object_get_string(val));
                        mg_printf(conn, "linkid:%d \n", link);
-                       if (link > 64) {
+                       if (link >= 64) {
                                mg_printf(conn, "Link id not supported beyond 64\n");
                                return 1;
                        }
@@ -1459,6 +1487,17 @@ void init_stat_cfg(void)
        }
 }
 
+static void set_port_mask(uint64_t num_ports)
+{
+       uint64_t i;
+       uint64_t mask = 0;
+
+       for (i = 0; i < num_ports; i++) {
+               mask |= (0x1 << i);
+       }
+       rapp->port_mask = mask;
+}
+
 void bind_the_ports(struct mg_connection *conn, char *pci_white_list)
 {
        char *token;
@@ -1469,12 +1508,12 @@ void bind_the_ports(struct mg_connection *conn, char *pci_white_list)
 
        while(token != NULL) {
                mg_printf(conn, "%s ****\n", token);
-               sprintf(buf, "$RTE_SDK/usertools/dpdk-devbind.py -u %s", token);
+               sprintf(buf, "dpdk-devbind -u %s", token);
                ret = system(buf);
                if (ret)
                        mg_printf(conn, "wrong parameter sent\n");
 
-               sprintf(buf, "$RTE_SDK/usertools/dpdk-devbind.py -b igb_uio %s", token);
+               sprintf(buf, "dpdk-devbind -b igb_uio %s", token);
                ret = system(buf);
                if (ret)
                        mg_printf(conn, "wrong parameter sent\n");
@@ -1484,6 +1523,7 @@ void bind_the_ports(struct mg_connection *conn, char *pci_white_list)
                x++;
        }
        current_cfg.num_ports = x;
+       set_port_mask(x);
 }
 
 int static_cfg_handler(struct mg_connection *conn, __rte_unused void *cbdata)
@@ -1543,7 +1583,6 @@ int static_cfg_handler(struct mg_connection *conn, __rte_unused void *cbdata)
        json_object * jobj = json_tokener_parse(buf);
        len = 0;
        struct json_object *values;
-       char *str;
 
        i = 0;
        json_object_object_foreach(jobj, key, val) {
@@ -1563,75 +1602,51 @@ int static_cfg_handler(struct mg_connection *conn, __rte_unused void *cbdata)
 
        json_object_object_get_ex(jobj, "num_worker", &values);
        if (values) {
-               str = strdup(json_object_get_string(values));
-               if (str)
-                       memcpy(&current_cfg.ip_range[pub_ip++].value, str,
-                                sizeof(str));
-       }
-
-       json_object_object_get_ex(jobj, "num_worker", &values);
-       if (values) {
-               str = strdup(json_object_get_string(values));
-               if (str)
-                       current_cfg.num_workers = atoi(str);
+               memcpy(&current_cfg.ip_range[pub_ip++].value, json_object_get_string(values),
+                        strlen(json_object_get_string(values)));
+               current_cfg.num_workers = atoi(json_object_get_string(values));
        }
 
        json_object_object_get_ex(jobj, "pkt_type", &values);
        if (values) {
-               str = strdup(json_object_get_string(values));
-               if (str)
-                       memcpy(&current_cfg.pkt_type, str,
-                                sizeof(current_cfg.pkt_type));
+               memcpy(&current_cfg.pkt_type, json_object_get_string(values),
+                        sizeof(current_cfg.pkt_type));
        }
 
        json_object_object_get_ex(jobj, "num_lb", &values);
        if (values) {
-               str = strdup(json_object_get_string(values));
-               if (str)
-                       current_cfg.num_lb = atoi(str);
+               current_cfg.num_lb = atoi(json_object_get_string(values));
        }
 
        json_object_object_get_ex(jobj, "num_ports", &values);
        if (values) {
-               str = strdup(json_object_get_string(values));
-               if (str)
-                       current_cfg.num_ports = atoi(str);
+               current_cfg.num_ports = atoi(json_object_get_string(values));
        }
        
        json_object_object_get_ex(jobj, "sw_lb", &values);
        if (values) {
-               str = strdup(json_object_get_string(values));
-               if (str)
-                       current_cfg.sw_lb = atoi(str);
+               current_cfg.sw_lb = atoi(json_object_get_string(values));
        }
 
        json_object_object_get_ex(jobj, "sock_in", &values);
        if (values) {
-               str = strdup(json_object_get_string(values));
-               if (str)
-                       current_cfg.sock_in = atoi(str);
+               current_cfg.sock_in = atoi(json_object_get_string(values));
        }
 
        json_object_object_get_ex(jobj, "hyperthread", &values);
        if (values) {
-               str = strdup(json_object_get_string(values));
-               if (str)
-                       current_cfg.hyper_thread = atoi(str);
+               current_cfg.hyper_thread = atoi(json_object_get_string(values));
        }
 
        json_object_object_get_ex(jobj, "vnf_type", &values);
        if (values) {
-               str = strdup(json_object_get_string(values));
-               if (str)
-                       memcpy(&current_cfg.vnf_type, str,
-                                sizeof(current_cfg.vnf_type));
+               memcpy(&current_cfg.vnf_type, json_object_get_string(values),
+                        sizeof(current_cfg.vnf_type));
        }
 
        json_object_object_get_ex(jobj, "pci_white_list", &values);
        if (values) {
-               str = strdup(json_object_get_string(values));
-               if (str)
-                       memcpy(&current_cfg.pci_white_list, str,
+               memcpy(&current_cfg.pci_white_list, json_object_get_string(values),
                        sizeof(current_cfg.pci_white_list));
                mg_printf(conn, " Binding the ports \n");
                bind_the_ports(conn, &current_cfg.pci_white_list[0]);
@@ -1728,8 +1743,7 @@ void fix_pipelines_data_types(FILE *f, const char *sect_name, struct rte_cfgfile
 
                        if (strncmp(entries[i].name, "pkt_type",
                                  strlen("pkt_type")) == 0) {
-                               memcpy(entries[i].value, current_cfg.pkt_type,
-                                strlen(entries[i].value));
+                               memcpy(entries[i].value, current_cfg.pkt_type, 24);
                                if (!strcmp(current_cfg.pkt_type, "ipv4"))
                                        memcpy(&traffic_type, "4", 1);
                                else
@@ -1743,35 +1757,36 @@ void fix_pipelines_data_types(FILE *f, const char *sect_name, struct rte_cfgfile
                }
 
                if (strncmp(entries[i].name, "core", strlen(entries[i].name)) == 0) {
-                       if ((strncmp(sect_name, "MASTER", strlen(sect_name)) == 0) &&
+                       if (((strncmp(sect_name, "MASTER", strlen(sect_name)) == 0) ||
+                            (strncmp(sect_name, "TIMER", strlen(sect_name)) == 0) ||
+                            (strncmp(sect_name, "ARPICMP", strlen(sect_name)) == 0)) && 
                                !current_cfg.sock_in) {
-                               continue;
-                       }
-
-                       if ((current_cfg.hyper_thread) && hyper) {
-                               sprintf(str, "s%dc%dh", current_cfg.sock_in,
+                               sprintf(str, "s%dc%d", current_cfg.sock_in,
                                         sock_cpus[current_cfg.sock_in][sock_index]);
                                memcpy(entries[i].value, &str, 8);
-                               sock_index++;
                                hyper = 0;
                                continue;
                        }
-       
-                       sprintf(str, "s%dc%d", current_cfg.sock_in,
-                                sock_cpus[current_cfg.sock_in][sock_index]);
-
-                       if (!current_cfg.hyper_thread)
-                               sock_index++;
-                       else
-                               hyper = 1;
-
-                       if (current_cfg.sock_in) {
-                               if (sock_index == sock1)
-                                       sock_index = 1;
-                       } else {
-                               if (sock_index == sock0)
-                                       sock_index = 1;
-                       }
+
+                        if ((strncmp(sect_name, "TXRX-BEGIN", strlen(sect_name)) == 0) || (strncmp(sect_name, "LOADB", strlen(sect_name)) == 0) ||
+                            (strncmp(sect_name, "TXRX-END", strlen(sect_name)) == 0)) {
+                             sock_index++;
+                            sprintf(str, "s%dc%d", current_cfg.sock_in,
+                                    sock_cpus[current_cfg.sock_in][sock_index]);
+                            memcpy(entries[i].value, &str, 8);
+                            hyper = 0;
+                             continue;
+                        } else {
+                             if (!hyper) {
+                                  sock_index++;
+                                  sprintf(str, "s%dc%d", current_cfg.sock_in, sock_cpus[current_cfg.sock_in][sock_index]);
+                                  if (current_cfg.hyper_thread)
+                                     hyper = 1;
+                             } else {
+                                 sprintf(str, "s%dc%dh", current_cfg.sock_in, sock_cpus[current_cfg.sock_in][sock_index]);
+                                 hyper = 0;
+                             }
+                        }
                        memcpy(entries[i].value, &str, 8);
                }
        }
@@ -1784,7 +1799,7 @@ void fix_pipelines_data_types(FILE *f, const char *sect_name, struct rte_cfgfile
                                (strncmp(static_cfg[j].key, "nd_route_tbl",
                                 strlen(static_cfg[j].key)) == 0)) {
                                memcpy(&entries[i].name, &static_cfg[j].key,
-                                        strlen(static_cfg[j].key));
+                                        strlen(entries[i].name));
                                memcpy(&entries[i].value, &static_cfg[j].value,
                                 strlen(static_cfg[j].value));
                                i++;
@@ -1976,8 +1991,6 @@ void fix_pipelines_data_types(FILE *f, const char *sect_name, struct rte_cfgfile
                                workers = 0;
                        }
                } else {
-                       //if (((workers % (current_cfg.num_workers/current_cfg.num_lb)) == 0) &&
-                       //       (workers != current_cfg.num_workers)) {
                        if (workers == (current_cfg.num_workers/current_cfg.num_lb)) {
                                tx_start_port += 2;
                                rx_start_port += 2;
@@ -2019,7 +2032,7 @@ void fix_pipelines_data_types(FILE *f, const char *sect_name, struct rte_cfgfile
                                workers = 0;
                        }
                } else {
-                       if (current_cfg.num_ports > 2) {
+                       if (workers == (current_cfg.num_workers/current_cfg.num_lb)) {
                                tx_start_port += 2;
                                rx_start_port += 2;
                                workers = 0;
@@ -2086,7 +2099,7 @@ void build_pipeline(void)
                printf("Wrong VNF TYPE\n");
 
        if (vnf_index == VNF_VCGNAPT)
-               pipe_arr[i++] = 2;
+           pipe_arr[i++] = 2;
 
        if (!current_cfg.sw_lb) {
                for (k = 0; k < current_cfg.num_workers; k++)
@@ -2160,8 +2173,8 @@ int flow_director_handler(struct mg_connection *conn, __rte_unused void *cbdata)
         /* Handler may access the request info using mg_get_request_info */
         const struct mg_request_info *req_info = mg_get_request_info(conn);
         uint32_t port_id = 0, tuple = 0;
-        char trans_type[24], buf[MAX_BUF_SIZE];
-       char *str, field0[MAX_SIZE], field1[MAX_SIZE], field2[MAX_SIZE],
+        char buf[MAX_BUF_SIZE];
+       char field0[MAX_SIZE], field1[MAX_SIZE], field2[MAX_SIZE],
                 field3[MAX_SIZE], flow_type[MAX_SIZE];
 
         if (!strcmp(req_info->request_method, "GET")) {
@@ -2192,7 +2205,6 @@ int flow_director_handler(struct mg_connection *conn, __rte_unused void *cbdata)
        json_object * jobj = json_tokener_parse(buf);
        json_object_object_foreach(jobj, key, val) {
                if (!strcmp(key, "trans_type")) {
-                       memcpy(&trans_type, str, sizeof(trans_type));
                        if (!strcmp(key, "udp")) {
                                memcpy(field2,"udp-src-port", sizeof("udp-src-port"));
                                memcpy(field3,"udp-dst-port", sizeof("udp-dst-port"));
@@ -2365,7 +2377,7 @@ rest_api_init(struct app_params *app)
        if (ctx == NULL) {
                printf("REST server did not start\n");
                printf("REST services will not be supported.. Try again ");
-               printf("by disabling other webservers\n");
+               printf("by disabling other webservers running on port 80\n");
                goto end;
        }
        
@@ -2395,10 +2407,10 @@ rest_api_init(struct app_params *app)
 
         mg_set_request_handler(ctx, "/vnf/quit", cmd_quit_handler, 0);
 
-       printf("Waiting for config input for 30 secs\n");
+       printf("Waiting for config input.... via rest\n");
        index = 0;
        while(1) {
-               if (post_not_received == 0 || (index == 30))
+               if (post_not_received == 0)
                        break;
                sleep(1);
                index++;
@@ -2441,8 +2453,6 @@ rest_api_init(struct app_params *app)
        app->config_file = strdup(buf);
        app->parser_file = strdup(buf);
 
-       printf("Config file loaded :%s %s\n", app->config_file, traffic_type);
-
 end:
        return ctx;
 }