Initial commit of vpp code.
[vpp.git] / vnet / vnet / l2tp / pg.c
1 /*
2  * pg.c: packet generator for L2TPv3 header
3  *
4  * Copyright (c) 2013 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vlib/vlib.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/l2tp/l2tp.h>
21
22 typedef struct {
23   pg_edit_t session_id;
24   pg_edit_t cookie;
25 } pg_l2tp_header_t;
26
27 typedef struct {
28   pg_edit_t l2_sublayer;
29 } pg_l2tp_header_l2_sublayer_t;
30
31 static inline void
32 pg_l2tp_header_init (pg_l2tp_header_t * e)
33 {
34   pg_edit_init (&e->session_id, l2tpv3_header_t, session_id);
35   pg_edit_init (&e->cookie, l2tpv3_header_t, cookie);
36 }
37
38 uword
39 unformat_pg_l2tp_header (unformat_input_t * input, va_list * args)
40 {
41   pg_stream_t * s = va_arg (*args, pg_stream_t *);
42   pg_l2tp_header_t * h;
43   u32 group_index, error;
44   vlib_main_t * vm = vlib_get_main();
45
46   h = pg_create_edit_group (s, sizeof (h[0]), 
47                             sizeof (l2tpv3_header_t) - sizeof(u32),
48                             &group_index);
49   pg_l2tp_header_init (h);
50
51   error = 1;
52
53   // session id and cookie are required
54   if (! unformat (input, "L2TP: session_id %U cookie %U",
55                   unformat_pg_edit, unformat_pg_number, &h->session_id,
56                   unformat_pg_edit, unformat_pg_number, &h->cookie)) {
57     goto done;
58   }
59
60   // "l2_sublayer <value>" is optional
61   if (unformat (input, "l2_sublayer")) {
62     pg_l2tp_header_l2_sublayer_t * h2;
63
64     h2 = pg_add_edits (s, sizeof (h2[0]), sizeof(u32), group_index);
65     pg_edit_init (&h2->l2_sublayer, l2tpv3_header_t, l2_specific_sublayer);
66     if (! unformat_user (input, unformat_pg_edit,
67                          unformat_pg_number, &h2->l2_sublayer)) {
68       goto done;
69     } 
70   }
71
72   // Parse an ethernet header if it is present
73   {
74     pg_node_t * pg_node = 0;
75     vlib_node_t * eth_lookup_node;
76
77     eth_lookup_node = vlib_get_node_by_name (vm, (u8 *)"ethernet-input");
78     ASSERT (eth_lookup_node);
79
80     pg_node = pg_get_node (eth_lookup_node->index);
81
82     if (pg_node && pg_node->unformat_edit
83         && unformat_user (input, pg_node->unformat_edit, s))
84       ;
85   }
86
87   error = 0;
88
89 done:
90   if (error)
91     pg_free_edit_group (s);
92   return error == 0;
93 }
94