Initial commit of vpp code.
[vpp.git] / vnet / example / main_stub.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <vlib/vlib.h>
16 #include <vlib/unix/unix.h>
17 #include <vnet/pg/pg.h>
18 #include <vnet/ethernet/ethernet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ip/tcp.h>
21
22 #include <vlib/unix/cj.h>
23
24 DECLARE_CJ_GLOBAL_LOG
25
26 static clib_error_t *
27 vnet_example_init (vlib_main_t * vm)
28 {
29   clib_error_t * error = 0;
30
31   /* Due to crude comment-out of eliot's smp stuff */
32   vm->heap_size = 256<<20;
33
34   if ((error = vlib_call_init_function (vm, pg_init)))
35     return error;
36   if ((error = vlib_call_init_function (vm, ip_main_init)))
37     return error;
38   if ((error = vlib_call_init_function (vm, ethernet_init)))
39     return error;
40   if ((error = vlib_call_init_function (vm, ethernet_arp_init)))
41     return error;
42   if ((error = vlib_call_init_function (vm, osi_init)))
43     return error;
44   if ((error = vlib_call_init_function (vm, srp_init)))
45     return error;
46 #if DPDK == 0
47   if ((error = vlib_call_init_function (vm, ixge_init)))
48     return error;
49   if ((error = vlib_call_init_function (vm, ixgev_init)))
50     return error;
51   if ((error = vlib_call_init_function (vm, ige_init)))
52     return error;
53 #else
54   if ((error = vlib_call_init_function (vm, dpdk_init)))
55     return error;
56 #endif
57   
58   if ((error = vlib_call_init_function (vm, dhcp_proxy_init)))
59     return error;
60   if ((error = vlib_call_init_function (vm, mpls_init)))
61     return error;
62   if ((error = vlib_call_init_function (vm, mpls_interface_init)))
63     return error;
64
65   if ((error = vlib_call_init_function (vm, l2_init)))
66     return error;
67   if ((error = vlib_call_init_function (vm, l2tp_init)))
68     return error;
69
70   if ((error = unix_physmem_init (vm, /* physical_memory_required */ 0)))
71     return error;
72
73   if ((error = unix_physmem_init (vm, /* physical_memory_required */ 0)))
74     return error;
75
76   if ((error = vlib_call_init_function (vm, tuntap_init)))
77     return error;
78
79   vlib_unix_cli_set_prompt ("VNET: ");
80
81   return error;
82 }
83
84 VLIB_INIT_FUNCTION (vnet_example_init);
85
86 int main (int argc, char * argv[])
87 {
88   clib_mem_init (0, (2ULL << 30));
89   return vlib_unix_main (argc, argv);
90 }
91
92 #if 0
93 #define foreach_tcp_test_error                  \
94   _ (SEGMENTS_RECEIVED, "segments received")
95
96 typedef enum {
97 #define _(sym,str) TCP_TEST_ERROR_##sym,
98   foreach_tcp_test_error
99 #undef _
100   TCP_TEST_N_ERROR,
101 } tcp_test_error_t;
102
103 static char * tcp_test_error_strings[] = {
104 #define _(sym,string) string,
105   foreach_tcp_test_error
106 #undef _
107 };
108
109 static uword
110 tcp_test (vlib_main_t * vm,
111           vlib_node_runtime_t * node,
112           vlib_frame_t * frame)
113 {
114   uword n_packets = frame->n_vectors;
115   u32 * from, * to_next;
116   u32 n_left_from, n_left_to_next, next;
117
118   from = vlib_frame_vector_args (frame);
119   n_left_from = n_packets;
120   next = node->cached_next_index;
121   
122   while (n_left_from > 0)
123     {
124       vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
125
126       while (n_left_from > 0 && n_left_to_next > 0)
127         {
128           vlib_buffer_t * p0;
129           u32 bi0;
130           u8 error0, next0;
131       
132           bi0 = to_next[0] = from[0];
133
134           from += 1;
135           n_left_from -= 1;
136           to_next += 1;
137           n_left_to_next -= 1;
138       
139           p0 = vlib_get_buffer (vm, bi0);
140
141           clib_warning ("got '%U'", format_vlib_buffer_contents, vm, p0);
142
143           error0 = next0 = 0;
144           p0->error = node->errors[error0];
145
146           if (PREDICT_FALSE (next0 != next))
147             {
148               to_next -= 1;
149               n_left_to_next += 1;
150
151               vlib_put_next_frame (vm, node, next, n_left_to_next);
152
153               next = next0;
154               vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
155               to_next[0] = bi0;
156               to_next += 1;
157               n_left_to_next -= 1;
158             }
159         }
160   
161       vlib_put_next_frame (vm, node, next, n_left_to_next);
162     }
163
164   return frame->n_vectors;
165 }
166
167 VLIB_REGISTER_NODE (tcp_test_node) = {
168   .function = tcp_test,
169   .name = "tcp-test",
170
171   .vector_size = sizeof (u32),
172
173   .n_next_nodes = 1,
174   .next_nodes = {
175     [0] = "error-drop",
176   },
177
178   .n_errors = TCP_TEST_N_ERROR,
179   .error_strings = tcp_test_error_strings,
180 };
181
182 static clib_error_t *
183 tcp_test_init (vlib_main_t * vm)
184 {
185   clib_error_t * error = 0;
186
187   {
188     tcp_listener_registration_t r = {
189       .port = 1234,
190       .flags = TCP_LISTENER_IP4,
191       .data_node_index = tcp_test_node.index,
192       .event_function = 0,
193     };
194
195     tcp_register_listener (vm, &r);
196   }
197
198   return error;
199 }
200
201 VLIB_INIT_FUNCTION (tcp_test_init);
202
203 #endif