[l2l3 stack] implements new nd state machine & nd buffering
[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         struct ether_addr addr;
51
52         switch (type) {
53         case 4:
54         {
55                 uint32_t nhip;
56                 nhip = get_nh(ip_addr[0], &dest_if, &addr);
57
58                 if (nhip)
59                         return dest_if;
60                 return 0xff;
61         }
62         break;
63         case 6:
64         {
65                 uint8_t nhipv6[16];
66                 get_nh_ipv6((uint8_t *)ip_addr, &dest_if, &nhipv6[0], &addr);
67                 if (dest_if != 0xff)
68                         return dest_if;
69                 return 0xff;
70         }
71         break;
72         }
73         return 0xff;
74 }
75
76 uint32_t get_pub_to_prv_port(uint32_t *ip_addr, uint8_t type)
77 {
78         uint32_t dest_if = 0xff;
79         struct ether_addr addr;
80
81         switch (type) {
82         case 4:
83         {
84                 uint32_t nhip;
85                 nhip = get_nh(ip_addr[0], &dest_if, &addr);
86
87                 if (nhip)
88                         return dest_if;
89                 return 0xff;
90         }
91         break;
92         case 6:
93         {
94                 uint8_t nhipv6[16];
95                 get_nh_ipv6((uint8_t *)ip_addr, &dest_if, &nhipv6[0], &addr);
96                 if (dest_if != 0xff)
97                         return dest_if;
98                 return 0xff;
99         }
100         break;
101         }
102         return 0xff;
103 }
104
105 void show_ports_info(void)
106 {
107         printf("\nin_port_dir_a: %d %d %d %d %d", in_port_dir_a[0],
108         in_port_dir_a[1], in_port_dir_a[2], in_port_dir_a[3],
109         in_port_dir_a[4]);
110
111         uint8_t i = 0, j = 0;
112
113         printf("\nprv_to_pub_map: ");
114         for (i = 0; i < PIPELINE_MAX_PORT_IN; i++) {
115                 if (prv_to_pub_map[i] != 0xff)
116                         printf("(%d,%d)  ", i, prv_to_pub_map[i]);
117         }
118
119         printf("\npub_to_prv_map: ");
120         for (i = 0; i < PIPELINE_MAX_PORT_IN; i++) {
121                 if (pub_to_prv_map[i] != 0xff)
122                         printf("(%d,%d)  ", i, pub_to_prv_map[i]);
123         }
124
125         printf("\n%d entries in Ports MAC List\n", link_hw_addr_array_idx);
126         for (j = 0; j < link_hw_addr_array_idx; j++) {
127                 struct ether_addr *link_hw_addr = get_link_hw_addr(j);
128
129                 for (i = 0; i < 6; i++)
130                         printf(" %02x ", ((struct ether_addr *)link_hw_addr)->addr_bytes[i]);
131                 printf("\n");
132         }
133 }
134
135 void trim(char *input)
136 {
137         int i, j = 0;
138         int len = strlen(input);
139         char result[len + 1];
140
141         memset(result, 0, sizeof(result));
142         for (i = 0; input[i] != '\0'; i++) {
143                 if (!isspace(input[i]))
144                         result[j++] = input[i];
145         }
146
147         strncpy(input, result, len);
148 }