udp: fix csum computation when offload disabled
[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/udp/udp_local.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 vm Vlib main of the graph node which is interested in
30  *                    getting IP-Fix packet.
31  * @param info 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
77   if (!udp_is_valid_dst_port (UDP_DST_PORT_ipfix, 1))
78     udp_register_dst_port (vm, UDP_DST_PORT_ipfix,
79                            ipfix_collector_node.index, 1);
80
81   return 0;
82 }
83
84 static clib_error_t *
85 ipfix_collector_init (vlib_main_t * vm)
86 {
87   clib_error_t *error = 0;
88   ipfix_collector_main_t *cm = &ipfix_collector_main;
89
90   cm->vlib_main = vm;
91   cm->vnet_main = vnet_get_main ();
92
93   cm->client_reg_pool = NULL;
94   cm->client_reg_table = hash_create (0, sizeof (uword));
95
96   return error;
97 }
98
99 VLIB_INIT_FUNCTION (ipfix_collector_init);
100
101 /*
102  * fd.io coding-style-patch-verification: ON
103  *
104  * Local Variables:
105  * eval: (c-set-style "gnu")
106  * End:
107  */