dpdk: Add support for Mellanox ConnectX-4 devices
[vpp.git] / plugins / sixrd-plugin / sixrd / sixrd_dpo.c
1 /*
2  * Copyright (c) 2016 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 "sixrd_dpo.h"
17 #include <vnet/ip/ip.h>
18
19 /**
20  * pool of all MPLS Label DPOs
21  */
22 sixrd_dpo_t *sixrd_dpo_pool;
23
24 /**
25  * The register SIXRD DPO type
26  */
27 dpo_type_t sixrd_dpo_type;
28
29 static sixrd_dpo_t *
30 sixrd_dpo_alloc (void)
31 {
32     sixrd_dpo_t *sd;
33
34     pool_get_aligned(sixrd_dpo_pool, sd, CLIB_CACHE_LINE_BYTES);
35     memset(sd, 0, sizeof(*sd));
36
37     return (sd);
38 }
39
40 static index_t
41 sixrd_dpo_get_index (sixrd_dpo_t *sd)
42 {
43     return (sd - sixrd_dpo_pool);
44 }
45
46 void
47 sixrd_dpo_create (dpo_proto_t dproto,
48                 u32 domain_index,
49                 dpo_id_t *dpo)
50 {
51     sixrd_dpo_t *sd;
52
53     sd = sixrd_dpo_alloc();
54     sd->sd_domain = domain_index;
55     sd->sd_proto = dproto;
56
57     dpo_set(dpo,
58             sixrd_dpo_type,
59             dproto,
60             sixrd_dpo_get_index(sd));
61 }
62
63 u8*
64 format_sixrd_dpo (u8 *s, va_list *args)
65 {
66     index_t index = va_arg (*args, index_t);
67     CLIB_UNUSED(u32 indent) = va_arg (*args, u32);
68     sixrd_dpo_t *sd;
69
70     sd = sixrd_dpo_get(index);
71
72     return (format(s, "sixrd:[%d]:%U domain:%d",
73                    index,
74                    format_dpo_proto, sd->sd_proto,
75                    sd->sd_domain));
76 }
77
78
79 static void
80 sixrd_dpo_lock (dpo_id_t *dpo)
81 {
82     sixrd_dpo_t *sd;
83
84     sd = sixrd_dpo_get(dpo->dpoi_index);
85
86     sd->sd_locks++;
87 }
88
89 static void
90 sixrd_dpo_unlock (dpo_id_t *dpo)
91 {
92     sixrd_dpo_t *sd;
93
94     sd = sixrd_dpo_get(dpo->dpoi_index);
95
96     sd->sd_locks--;
97
98     if (0 == sd->sd_locks)
99     {
100         pool_put(sixrd_dpo_pool, sd);
101     }
102 }
103
104 const static dpo_vft_t sd_vft = {
105     .dv_lock = sixrd_dpo_lock,
106     .dv_unlock = sixrd_dpo_unlock,
107     .dv_format = format_sixrd_dpo,
108 };
109
110 const static char* const sixrd_ip4_nodes[] =
111 {
112     "ip4-sixrd",
113     NULL,
114 };
115 const static char* const sixrd_ip6_nodes[] =
116 {
117     "ip6-sixrd",
118     NULL,
119 };
120
121 const static char* const * const sixrd_nodes[DPO_PROTO_NUM] =
122 {
123     [DPO_PROTO_IP4]  = sixrd_ip4_nodes,
124     [DPO_PROTO_IP6]  = sixrd_ip6_nodes,
125     [DPO_PROTO_MPLS] = NULL,
126 };
127
128 void
129 sixrd_dpo_module_init (void)
130 {
131     sixrd_dpo_type = dpo_register_new_type(&sd_vft, sixrd_nodes);
132 }