93687fd67ecaf2b334158edb67c5f5191e411ddf
[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 "gateway.h"
21 #include "pipeline_arpicmp_be.h"
22 #ifndef VNF_ACL
23 #include "lib_arp.h"
24 #endif
25
26 uint8_t in_port_dir_a[PIPELINE_MAX_PORT_IN];
27 uint8_t prv_to_pub_map[PIPELINE_MAX_PORT_IN];
28 uint8_t pub_to_prv_map[PIPELINE_MAX_PORT_IN];
29 uint8_t prv_in_port_a[PIPELINE_MAX_PORT_IN];
30 uint8_t prv_que_port_index[PIPELINE_MAX_PORT_IN];
31 uint8_t in_port_egress_prv[PIPELINE_MAX_PORT_IN];
32
33 uint8_t get_in_port_dir(uint8_t in_port_id)
34 {
35         return in_port_dir_a[in_port_id];
36 }
37
38 uint8_t is_phy_port_privte(uint16_t phy_port)
39 {
40         return in_port_dir_a[phy_port];
41 }
42
43 uint8_t is_port_index_privte(uint16_t phy_port)
44 {
45         return in_port_egress_prv[phy_port];
46 }
47
48 uint32_t get_prv_to_pub_port(uint32_t *ip_addr, uint8_t type)
49 {
50         uint32_t dest_if = 0xff;
51
52         switch (type) {
53         case 4:
54         {
55                 uint32_t nhip;
56                 gw_get_nh_port_ipv4(*ip_addr, &dest_if, &nhip);
57
58                 if (nhip)
59                         return dest_if;
60                 return 0xff;
61         }
62         break;
63         case 6:
64         {
65                 uint8_t nhipv6[16];
66                 gw_get_nh_port_ipv6((uint8_t *)ip_addr, &dest_if, nhipv6);
67
68                 if (dest_if != 0xff)
69                         return dest_if;
70                 return 0xff;
71         }
72         break;
73         }
74         return 0xff;
75 }
76
77 uint32_t get_pub_to_prv_port(uint32_t *ip_addr, uint8_t type)
78 {
79         uint32_t dest_if = 0xff;
80
81         switch (type) {
82         case 4:
83         {
84                 uint32_t nhip;
85
86                 gw_get_nh_port_ipv4(*ip_addr, &dest_if, &nhip);
87
88                 if (nhip)
89                         return dest_if;
90                 return 0xff;
91         }
92         break;
93         case 6:
94         {
95                 uint8_t nhipv6[16];
96
97                 gw_get_nh_port_ipv6((uint8_t *)ip_addr, &dest_if, nhipv6);
98                 if (dest_if != 0xff)
99                         return dest_if;
100                 return 0xff;
101         }
102         break;
103         }
104         return 0xff;
105 }
106
107 void show_ports_info(void)
108 {
109         printf("\nin_port_dir_a: %d %d %d %d %d", in_port_dir_a[0],
110         in_port_dir_a[1], in_port_dir_a[2], in_port_dir_a[3],
111         in_port_dir_a[4]);
112
113         uint8_t i = 0, j = 0;
114
115         printf("\nprv_to_pub_map: ");
116         for (i = 0; i < PIPELINE_MAX_PORT_IN; i++) {
117                 if (prv_to_pub_map[i] != 0xff)
118                         printf("(%d,%d)  ", i, prv_to_pub_map[i]);
119         }
120
121         printf("\npub_to_prv_map: ");
122         for (i = 0; i < PIPELINE_MAX_PORT_IN; i++) {
123                 if (pub_to_prv_map[i] != 0xff)
124                         printf("(%d,%d)  ", i, pub_to_prv_map[i]);
125         }
126
127         printf("\n%d entries in Ports MAC List\n", link_hw_addr_array_idx);
128         for (j = 0; j < link_hw_addr_array_idx; j++) {
129                 struct ether_addr *link_hw_addr = get_link_hw_addr(j);
130
131                 for (i = 0; i < 6; i++)
132                         printf(" %02x ", ((struct ether_addr *)link_hw_addr)->addr_bytes[i]);
133                 printf("\n");
134         }
135 }
136
137 void trim(char *input)
138 {
139         int i, j = 0;
140         int len = strlen(input);
141         char result[len + 1];
142
143         memset(result, 0, sizeof(result));
144         for (i = 0; input[i] != '\0'; i++) {
145                 if (!isspace(input[i]))
146                         result[j++] = input[i];
147         }
148
149         strncpy(input, result, len);
150 }