api: Add API support for marvell PP2 plugin
[vpp.git] / src / plugins / stn / stn_api.c
1 /*
2  * Copyright (c) 2017 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
16 #include <vlibapi/api.h>
17 #include <vlibmemory/api.h>
18
19 #include <plugins/stn/stn.h>
20 #include <vnet/ip/format.h>
21
22 #include <vppinfra/byte_order.h>
23
24 /* define message IDs */
25 #include <stn/stn.api_enum.h>
26 #include <stn/stn.api_types.h>
27
28 #define REPLY_MSG_ID_BASE stn_main.msg_id_base
29 #include <vlibapi/api_helper_macros.h>
30
31 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
32 /* Macro to finish up custom dump fns */
33 #define FINISH                                  \
34     vec_add1 (s, 0);                            \
35     vl_print (handle, (char *)s);               \
36     vec_free (s);                               \
37     return handle;
38
39 /**
40  * @brief API message custom-dump function
41  * @param mp vl_api_stn_add_del_rule_t * mp the api message
42  * @param handle void * print function handle
43  * @returns u8 * output string
44  */
45 static void *vl_api_stn_add_del_rule_t_print
46   (vl_api_stn_add_del_rule_t * mp, void *handle)
47 {
48   u8 *s;
49
50   s = format (0, "SCRIPT: stn_add_del_rule ");
51   if (mp->is_ip4)
52     s = format (s, "address %U ", format_ip4_address, mp->ip_address);
53   else
54     s = format (s, "address %U ", format_ip6_address, mp->ip_address);
55   s = format (s, "sw_if_index %d is_add %d", mp->sw_if_index, mp->is_add);
56
57   FINISH;
58 }
59
60 static void
61 vl_api_stn_add_del_rule_t_handler (vl_api_stn_add_del_rule_t * mp)
62 {
63   stn_rule_add_del_args_t args;
64   vl_api_stn_add_del_rule_reply_t *rmp;
65   int rv = 0;
66
67   if (mp->is_ip4)
68     {
69       ip4_address_t a;
70       memcpy (&a, mp->ip_address, sizeof (a));
71       ip46_address_set_ip4 (&args.address, &a);
72     }
73   else
74     memcpy (&args.address.ip6, mp->ip_address, sizeof (ip6_address_t));
75
76   args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
77   args.del = !mp->is_add;
78
79   rv = stn_rule_add_del (&args);
80
81   REPLY_MACRO (VL_API_STN_ADD_DEL_RULE_REPLY);
82 }
83
84 static void
85 send_stn_rules_details (stn_rule_t * r, vl_api_registration_t * reg,
86                         u32 context)
87 {
88   vl_api_stn_rules_details_t *rmp;
89
90   rmp = vl_msg_api_alloc (sizeof (*rmp));
91   clib_memset (rmp, 0, sizeof (*rmp));
92   rmp->_vl_msg_id =
93     clib_host_to_net_u16 (VL_API_STN_RULES_DETAILS + stn_main.msg_id_base);
94
95   if (ip46_address_is_ip4 (&r->address))
96     {
97       clib_memcpy (rmp->ip_address, &r->address.ip4, sizeof (ip4_address_t));
98       rmp->is_ip4 = 1;
99     }
100   else
101     {
102       clib_memcpy (rmp->ip_address, &r->address.ip6, sizeof (ip6_address_t));
103     }
104
105   rmp->context = context;
106   rmp->sw_if_index = clib_host_to_net_u32 (r->sw_if_index);
107
108   vl_api_send_msg (reg, (u8 *) rmp);
109 }
110
111 static void
112 vl_api_stn_rules_dump_t_handler (vl_api_stn_rules_dump_t * mp)
113 {
114   vl_api_registration_t *reg;
115   stn_main_t *stn = &stn_main;
116   stn_rule_t *r;
117
118   reg = vl_api_client_index_to_registration (mp->client_index);
119   if (reg == 0)
120     return;
121
122   /* *INDENT-OFF* */
123   pool_foreach (r, stn->rules,({
124     send_stn_rules_details (r, reg, mp->context);
125   }));
126   /* *INDENT-ON* */
127 }
128
129 #include <stn/stn.api.c>
130 clib_error_t *
131 stn_api_init (vlib_main_t * vm, stn_main_t * sm)
132 {
133   /* Ask for a correctly-sized block of API message decode slots */
134   sm->msg_id_base = setup_message_id_table ();
135
136   return 0;
137 }
138
139 /*
140  * fd.io coding-style-patch-verification: ON
141  *
142  * Local Variables:
143  * eval: (c-set-style "gnu")
144  * End:
145  */