4ae47edcb3c95920e8af3418053d4747c3cd57b3
[vpp.git] / src / plugins / ioam / ipfixcollector / ipfixcollector.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 <vnet/ip/ip.h>
17 #include <vnet/plugin/plugin.h>
18 #include <vnet/ip/udp.h>
19 #include <ioam/ipfixcollector/ipfixcollector.h>
20
21 ipfix_collector_main_t ipfix_collector_main;
22
23 /**
24  * @brief IP-FIX SetID registration function.
25  *
26  * This function can be used by other VPP graph nodes to receive IP-FIX packets
27  * with a particular setid.
28  *
29  * @param vlib_main_t Vlib main of the graph node which is interseted in
30  *                    getting IP-Fix packet.
31  * @param ipfix_client_add_del_t Structure describing the client node which
32  *                               is interested in getting the IP-Fix packets for
33  *                               a SetID.
34  *
35  * @returns 0 on success.
36  * @returns Error codes(<0) otherwise.
37  */
38 int
39 ipfix_collector_reg_setid (vlib_main_t * vm, ipfix_client_add_del_t * info)
40 {
41   ipfix_collector_main_t *cm = &ipfix_collector_main;
42   uword *p = NULL;
43   int i;
44   ipfix_client *client = 0;
45
46   if ((!info) || (!info->client_name))
47     return IPFIX_COLLECTOR_ERR_INVALID_PARAM;
48
49   p = hash_get (cm->client_reg_table, info->ipfix_setid);
50   client = p ? pool_elt_at_index (cm->client_reg_pool, (*p)) : NULL;
51
52   if (info->del)
53     {
54       if (!client)
55         return 0;               //There is no registered handler, so send success
56
57       hash_unset (cm->client_reg_table, info->ipfix_setid);
58       vec_free (client->client_name);
59       pool_put (cm->client_reg_pool, client);
60       return 0;
61     }
62
63   if (client)
64     return IPFIX_COLLECTOR_ERR_REG_EXISTS;
65
66   pool_get (cm->client_reg_pool, client);
67   i = client - cm->client_reg_pool;
68   client->client_name = vec_dup (info->client_name);
69   client->client_node = info->client_node;
70   client->client_next_node = vlib_node_add_next (vm,
71                                                  ipfix_collector_node.index,
72                                                  client->client_node);
73   client->set_id = info->ipfix_setid;
74
75   hash_set (cm->client_reg_table, info->ipfix_setid, i);
76   return 0;
77 }
78
79 static clib_error_t *
80 ipfix_collector_init (vlib_main_t * vm)
81 {
82   clib_error_t *error = 0;
83   ipfix_collector_main_t *cm = &ipfix_collector_main;
84
85   cm->vlib_main = vm;
86   cm->vnet_main = vnet_get_main ();
87
88   cm->client_reg_pool = NULL;
89   cm->client_reg_table = hash_create (0, sizeof (uword));
90
91   udp_register_dst_port (vm,
92                          UDP_DST_PORT_ipfix,
93                          ipfix_collector_node.index, 1 /* is_ip4 */ );
94   return error;
95 }
96
97 VLIB_INIT_FUNCTION (ipfix_collector_init);
98
99 /*
100  * fd.io coding-style-patch-verification: ON
101  *
102  * Local Variables:
103  * eval: (c-set-style "gnu")
104  * End:
105  */