FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
stn_api.c
Go to the documentation of this file.
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 #include <vnet/format_fns.h>
22 #include <vnet/ip/ip_types_api.h>
23 #include <vppinfra/byte_order.h>
24 
25 /* define message IDs */
26 #include <stn/stn.api_enum.h>
27 #include <stn/stn.api_types.h>
28 
29 #define REPLY_MSG_ID_BASE stn_main.msg_id_base
31 
32 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
33 /* Macro to finish up custom dump fns */
34 #define FINISH \
35  vec_add1 (s, 0); \
36  vl_print (handle, (char *)s); \
37  vec_free (s); \
38  return handle;
39 
40 static void
42 {
44  vl_api_stn_add_del_rule_reply_t *rmp;
45  int rv = 0;
46 
47  ip_address_decode (&mp->ip_address, &args.address);
48  args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
49  args.del = !mp->is_add;
50 
51  rv = stn_rule_add_del (&args);
52 
53  REPLY_MACRO (VL_API_STN_ADD_DEL_RULE_REPLY);
54 }
55 
56 static void
58  u32 context)
59 {
61 
62  rmp = vl_msg_api_alloc (sizeof (*rmp));
63  clib_memset (rmp, 0, sizeof (*rmp));
64  rmp->_vl_msg_id =
65  clib_host_to_net_u16 (VL_API_STN_RULES_DETAILS + stn_main.msg_id_base);
66 
67  ip_address_encode (&r->address,
68  ip46_address_is_ip4 (&r->address) ? IP46_TYPE_IP4 :
69  IP46_TYPE_IP6, &rmp->ip_address);
70  rmp->context = context;
71  rmp->sw_if_index = clib_host_to_net_u32 (r->sw_if_index);
72 
73  vl_api_send_msg (reg, (u8 *) rmp);
74 }
75 
76 static void
78 {
80  stn_main_t *stn = &stn_main;
81  stn_rule_t *r;
82 
84  if (reg == 0)
85  return;
86 
87  /* *INDENT-OFF* */
88  pool_foreach (r, stn->rules) {
89  send_stn_rules_details (r, reg, mp->context);
90  }
91  /* *INDENT-ON* */
92 }
93 
94 #include <stn/stn.api.c>
97 {
98  /* Ask for a correctly-sized block of API message decode slots */
100 
101  return 0;
102 }
103 
104 /*
105  * fd.io coding-style-patch-verification: ON
106  *
107  * Local Variables:
108  * eval: (c-set-style "gnu")
109  * End:
110  */
vl_api_client_index_to_registration
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:79
api.h
vl_api_stn_rules_details_t::ip_address
vl_api_address_t ip_address
Definition: stn.api:58
stn_api_init
clib_error_t * stn_api_init(vlib_main_t *vm, stn_main_t *sm)
Definition: stn_api.c:96
stn_main
stn_main_t stn_main
Definition: stn.c:24
stn_main_t::rules
stn_rule_t * rules
Definition: stn.h:32
vl_api_send_msg
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
vl_api_stn_rules_dump_t_handler
static void vl_api_stn_rules_dump_t_handler(vl_api_stn_rules_dump_t *mp)
Definition: stn_api.c:77
send_stn_rules_details
static void send_stn_rules_details(stn_rule_t *r, vl_api_registration_t *reg, u32 context)
Definition: stn_api.c:57
stn_rule_t
Definition: stn.h:24
ip46_address_is_ip4
static u8 ip46_address_is_ip4(const ip46_address_t *ip46)
Definition: ip46_address.h:55
IP46_TYPE_IP4
@ IP46_TYPE_IP4
Definition: ip46_address.h:26
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
api.h
byte_order.h
stn_rule_add_del
int stn_rule_add_del(stn_rule_add_del_args_t *args)
Add or delete an stn rule.
Definition: stn.c:285
r
vnet_hw_if_output_node_runtime_t * r
Definition: interface_output.c:1089
stn_rule_add_del_args_t::del
u8 del
Whether to delete the rule.
Definition: stn.h:52
vl_api_stn_add_del_rule_t::ip_address
vl_api_address_t ip_address
Definition: stn.api:36
ip_address_decode
ip46_type_t ip_address_decode(const vl_api_address_t *in, ip46_address_t *out)
Decode/Encode for struct/union types.
Definition: ip_types_api.c:172
stn_rule_add_del_args_t::address
ip46_address_t address
Destination address of intercepted packets.
Definition: stn.h:48
stn_rule_add_del_args_t::sw_if_index
u32 sw_if_index
TX interface to send packets to.
Definition: stn.h:50
stn.h
pool_foreach
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:534
vl_api_stn_rules_dump_t
Dump STN rules.
Definition: stn.api:45
vl_api_stn_rules_details_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: stn.api:59
vl_api_stn_add_del_rule_t
Add/del STN rules.
Definition: stn.api:33
format.h
vl_api_registration_
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
REPLY_MACRO
#define REPLY_MACRO(t)
Definition: api_helper_macros.h:30
setup_message_id_table
static void setup_message_id_table(api_main_t *am)
Definition: sr_mpls_api.c:174
stn_main_t::msg_id_base
u16 msg_id_base
Definition: stn.h:43
vl_api_stn_rules_dump_t::context
u32 context
Definition: stn.api:47
IP46_TYPE_IP6
@ IP46_TYPE_IP6
Definition: ip46_address.h:27
stn_rule_add_del_args_t
Definition: stn.h:46
vl_api_stn_add_del_rule_t_handler
static void vl_api_stn_add_del_rule_t_handler(vl_api_stn_add_del_rule_t *mp)
Definition: stn_api.c:41
format_fns.h
vl_api_stn_rules_details_t::context
u32 context
Definition: stn.api:57
u32
unsigned int u32
Definition: types.h:88
api_helper_macros.h
vl_api_stn_rules_dump_t::client_index
u32 client_index
Definition: stn.api:46
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
ip_address_encode
void ip_address_encode(const ip46_address_t *in, ip46_type_t type, vl_api_address_t *out)
Definition: ip_types_api.c:206
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
stn_main_t
Definition: stn.h:30
vl_api_stn_rules_details_t
STN response to rules request.
Definition: stn.api:56
context
u32 context
Definition: ip.api:852
rv
int __clib_unused rv
Definition: application.c:491
vl_api_stn_add_del_rule_t::is_add
bool is_add
Definition: stn.api:38
vl_api_stn_add_del_rule_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: stn.api:37
ip_types_api.h
vl_msg_api_alloc
void * vl_msg_api_alloc(int nbytes)
Definition: memory_shared.c:199