teib: Add adj-fibs for peers/adjacencies on p2mp interface
[vpp.git] / src / vnet / teib / teib_api.c
1 /*
2  *------------------------------------------------------------------
3  * teib_api.c - teib api
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22
23 #include <vnet/api_errno.h>
24 #include <vnet/teib/teib.h>
25 #include <vnet/ip/ip_types_api.h>
26 #include <vnet/fib/fib_table.h>
27
28 /* define message IDs */
29 #include <vnet/format_fns.h>
30 #include <vnet/teib/teib.api_enum.h>
31 #include <vnet/teib/teib.api_types.h>
32
33 static u32 teib_base_msg_id;
34 #define REPLY_MSG_ID_BASE teib_base_msg_id
35
36 #include <vlibapi/api_helper_macros.h>
37
38 static void
39 vl_api_teib_entry_add_del_t_handler (vl_api_teib_entry_add_del_t * mp)
40 {
41   vl_api_teib_entry_add_del_reply_t *rmp;
42   ip46_address_t nh;
43   ip_address_t peer;
44   int rv;
45
46   VALIDATE_SW_IF_INDEX ((&mp->entry));
47
48   ip_address_decode2 (&mp->entry.peer, &peer);
49   ip_address_decode (&mp->entry.nh, &nh);
50
51   if (mp->is_add)
52     rv = teib_entry_add (ntohl (mp->entry.sw_if_index),
53                          ip_address_family_to_fib_proto (ip_addr_version
54                                                          (&peer)),
55                          &ip_addr_46 (&peer), ntohl (mp->entry.nh_table_id),
56                          &nh);
57   else
58     rv = teib_entry_del (ntohl (mp->entry.sw_if_index),
59                          ip_address_family_to_fib_proto (ip_addr_version
60                                                          (&peer)),
61                          &ip_addr_46 (&peer));
62
63   BAD_SW_IF_INDEX_LABEL;
64
65   REPLY_MACRO (VL_API_TEIB_ENTRY_ADD_DEL_REPLY);
66 }
67
68 typedef struct vl_api_teib_send_t_
69 {
70   vl_api_registration_t *reg;
71   u32 context;
72 } vl_api_teib_send_t;
73
74 static walk_rc_t
75 vl_api_teib_send_one (index_t nei, void *arg)
76 {
77   vl_api_teib_details_t *mp;
78   vl_api_teib_send_t *ctx = arg;
79   const teib_entry_t *ne;
80   const fib_prefix_t *pfx;
81
82   mp = vl_msg_api_alloc_zero (sizeof (*mp));
83   mp->_vl_msg_id = ntohs (VL_API_TEIB_DETAILS + REPLY_MSG_ID_BASE);
84   mp->context = ctx->context;
85
86   ne = teib_entry_get (nei);
87   pfx = teib_entry_get_nh (ne);
88
89   ip_address_encode (teib_entry_get_peer (ne), IP46_TYPE_ANY,
90                      &mp->entry.peer);
91   ip_address_encode (&pfx->fp_addr, IP46_TYPE_ANY, &mp->entry.nh);
92   mp->entry.nh_table_id =
93     htonl (fib_table_get_table_id
94            (teib_entry_get_fib_index (ne), pfx->fp_proto));
95   mp->entry.sw_if_index = htonl (teib_entry_get_sw_if_index (ne));
96
97   vl_api_send_msg (ctx->reg, (u8 *) mp);
98
99   return (WALK_CONTINUE);
100 }
101
102 static void
103 vl_api_teib_dump_t_handler (vl_api_teib_dump_t * mp)
104 {
105   vl_api_registration_t *reg;
106
107   reg = vl_api_client_index_to_registration (mp->client_index);
108   if (!reg)
109     return;
110
111   vl_api_teib_send_t ctx = {
112     .reg = reg,
113     .context = mp->context,
114   };
115
116   teib_walk (vl_api_teib_send_one, &ctx);
117 }
118
119 /*
120  * teib_api_hookup
121  * Add vpe's API message handlers to the table.
122  * vlib has already mapped shared memory and
123  * added the client registration handlers.
124  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
125  */
126 #include <vnet/teib/teib.api.c>
127
128 static clib_error_t *
129 teib_api_hookup (vlib_main_t * vm)
130 {
131   teib_base_msg_id = setup_message_id_table ();
132
133   return (NULL);
134 }
135
136 VLIB_API_INIT_FUNCTION (teib_api_hookup);
137
138 /*
139  * fd.io coding-style-patch-verification: ON
140  *
141  * Local Variables:
142  * eval: (c-set-style "gnu")
143  * End:
144  */