common: Adding common library for sample vnf
[samplevnf.git] / common / vnf_common / vnf_common.c
1 /*
2 // Copyright (c) 2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #include <stdint.h>
18 #include <stdio.h>
19 #include "vnf_common.h"
20 #include "pipeline_arpicmp_be.h"
21 #ifndef VNF_ACL
22 #include "lib_arp.h"
23 #endif
24
25 uint8_t in_port_dir_a[PIPELINE_MAX_PORT_IN];
26 uint8_t prv_to_pub_map[PIPELINE_MAX_PORT_IN];
27 uint8_t pub_to_prv_map[PIPELINE_MAX_PORT_IN];
28 uint8_t prv_in_port_a[PIPELINE_MAX_PORT_IN];
29 uint8_t prv_que_port_index[PIPELINE_MAX_PORT_IN];
30 uint8_t in_port_egress_prv[PIPELINE_MAX_PORT_IN];
31
32 uint8_t get_in_port_dir(uint8_t in_port_id)
33 {
34         return in_port_dir_a[in_port_id];
35 }
36
37 uint8_t is_phy_port_privte(uint16_t phy_port)
38 {
39         return in_port_dir_a[phy_port];
40 }
41
42 uint8_t is_port_index_privte(uint16_t phy_port)
43 {
44         return in_port_egress_prv[phy_port];
45 }
46
47 uint32_t get_prv_to_pub_port(uint32_t *ip_addr, uint8_t type)
48 {
49         uint32_t dest_if = 0xff;
50
51         switch (type) {
52         case 4:
53         {
54                 uint32_t nhip;
55                 nhip = get_nh(ip_addr[0], &dest_if);
56
57                 if (nhip)
58                         return dest_if;
59                 return 0xff;
60         }
61         break;
62         case 6:
63         {
64                 uint8_t nhipv6[16];
65                 get_nh_ipv6((uint8_t *)ip_addr, &dest_if, &nhipv6[0]);
66                 if (dest_if != 0xff)
67                         return dest_if;
68                 return 0xff;
69         }
70         break;
71         }
72         return 0xff;
73 }
74
75 uint32_t get_pub_to_prv_port(uint32_t *ip_addr, uint8_t type)
76 {
77         uint32_t dest_if = 0xff;
78
79         switch (type) {
80         case 4:
81         {
82                 uint32_t nhip;
83                 nhip = get_nh(ip_addr[0], &dest_if);
84
85                 if (nhip)
86                         return dest_if;
87                 return 0xff;
88         }
89         break;
90         case 6:
91         {
92                 uint8_t nhipv6[16];
93                 get_nh_ipv6((uint8_t *)ip_addr, &dest_if, &nhipv6[0]);
94                 if (dest_if != 0xff)
95                         return dest_if;
96                 return 0xff;
97         }
98         break;
99         }
100         return 0xff;
101 }
102
103 void show_ports_info(void)
104 {
105         printf("\nin_port_dir_a: %d %d %d %d %d", in_port_dir_a[0],
106         in_port_dir_a[1], in_port_dir_a[2], in_port_dir_a[3],
107         in_port_dir_a[4]);
108
109         uint8_t i = 0, j = 0;
110
111         printf("\nprv_to_pub_map: ");
112         for (i = 0; i < PIPELINE_MAX_PORT_IN; i++) {
113                 if (prv_to_pub_map[i] != 0xff)
114                         printf("(%d,%d)  ", i, prv_to_pub_map[i]);
115         }
116
117         printf("\npub_to_prv_map: ");
118         for (i = 0; i < PIPELINE_MAX_PORT_IN; i++) {
119                 if (pub_to_prv_map[i] != 0xff)
120                         printf("(%d,%d)  ", i, pub_to_prv_map[i]);
121         }
122
123         printf("\n%d entries in Ports MAC List\n", link_hw_addr_array_idx);
124         for (j = 0; j < link_hw_addr_array_idx; j++) {
125                 struct ether_addr *link_hw_addr = get_link_hw_addr(j);
126
127                 for (i = 0; i < 6; i++)
128                         printf(" %02x ", ((struct ether_addr *)link_hw_addr)->addr_bytes[i]);
129                 printf("\n");
130         }
131 }
132
133 void trim(char *input)
134 {
135         int i, j = 0;
136         int len = strlen(input);
137         char result[len + 1];
138
139         memset(result, 0, sizeof(result));
140         for (i = 0; input[i] != '\0'; i++) {
141                 if (!isspace(input[i]))
142                         result[j++] = input[i];
143         }
144
145         strncpy(input, result, len);
146 }