Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 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 <vnet/ip/ip.h> |
| 17 | #include <vnet/map/map_dpo.h> |
| 18 | |
| 19 | /** |
| 20 | * pool of all MPLS Label DPOs |
| 21 | */ |
| 22 | map_dpo_t *map_dpo_pool; |
| 23 | |
| 24 | /** |
| 25 | * The register MAP DPO type |
| 26 | */ |
| 27 | dpo_type_t map_dpo_type; |
| 28 | dpo_type_t map_t_dpo_type; |
| 29 | |
| 30 | static map_dpo_t * |
| 31 | map_dpo_alloc (void) |
| 32 | { |
| 33 | map_dpo_t *md; |
| 34 | |
| 35 | pool_get_aligned(map_dpo_pool, md, CLIB_CACHE_LINE_BYTES); |
| 36 | memset(md, 0, sizeof(*md)); |
| 37 | |
| 38 | return (md); |
| 39 | } |
| 40 | |
| 41 | static index_t |
| 42 | map_dpo_get_index (map_dpo_t *md) |
| 43 | { |
| 44 | return (md - map_dpo_pool); |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | map_dpo_create (dpo_proto_t dproto, |
| 49 | u32 domain_index, |
| 50 | dpo_id_t *dpo) |
| 51 | { |
| 52 | map_dpo_t *md; |
| 53 | |
| 54 | md = map_dpo_alloc(); |
| 55 | md->md_domain = domain_index; |
| 56 | md->md_proto = dproto; |
| 57 | |
| 58 | dpo_set(dpo, |
| 59 | map_dpo_type, |
| 60 | dproto, |
| 61 | map_dpo_get_index(md)); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | map_t_dpo_create (dpo_proto_t dproto, |
| 66 | u32 domain_index, |
| 67 | dpo_id_t *dpo) |
| 68 | { |
| 69 | map_dpo_t *md; |
| 70 | |
| 71 | md = map_dpo_alloc(); |
| 72 | md->md_domain = domain_index; |
| 73 | md->md_proto = dproto; |
| 74 | |
| 75 | dpo_set(dpo, |
| 76 | map_t_dpo_type, |
| 77 | dproto, |
| 78 | map_dpo_get_index(md)); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | u8* |
| 83 | format_map_dpo (u8 *s, va_list *args) |
| 84 | { |
| 85 | index_t index = va_arg (*args, index_t); |
| 86 | CLIB_UNUSED(u32 indent) = va_arg (*args, u32); |
| 87 | map_dpo_t *md; |
| 88 | |
| 89 | md = map_dpo_get(index); |
| 90 | |
| 91 | return (format(s, "map:[%d]:%U domain:%d", |
| 92 | index, |
| 93 | format_dpo_proto, md->md_proto, |
| 94 | md->md_domain)); |
| 95 | } |
| 96 | |
| 97 | u8* |
| 98 | format_map_t_dpo (u8 *s, va_list *args) |
| 99 | { |
| 100 | index_t index = va_arg (*args, index_t); |
| 101 | CLIB_UNUSED(u32 indent) = va_arg (*args, u32); |
| 102 | map_dpo_t *md; |
| 103 | |
| 104 | md = map_dpo_get(index); |
| 105 | |
| 106 | return (format(s, "map-t:[%d]:%U domain:%d", |
| 107 | index, |
| 108 | format_dpo_proto, md->md_proto, |
| 109 | md->md_domain)); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | static void |
| 114 | map_dpo_lock (dpo_id_t *dpo) |
| 115 | { |
| 116 | map_dpo_t *md; |
| 117 | |
| 118 | md = map_dpo_get(dpo->dpoi_index); |
| 119 | |
| 120 | md->md_locks++; |
| 121 | } |
| 122 | |
| 123 | static void |
| 124 | map_dpo_unlock (dpo_id_t *dpo) |
| 125 | { |
| 126 | map_dpo_t *md; |
| 127 | |
| 128 | md = map_dpo_get(dpo->dpoi_index); |
| 129 | |
| 130 | md->md_locks--; |
| 131 | |
| 132 | if (0 == md->md_locks) |
| 133 | { |
| 134 | pool_put(map_dpo_pool, md); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | const static dpo_vft_t md_vft = { |
| 139 | .dv_lock = map_dpo_lock, |
| 140 | .dv_unlock = map_dpo_unlock, |
| 141 | .dv_format = format_map_dpo, |
| 142 | }; |
| 143 | |
| 144 | const static char* const map_ip4_nodes[] = |
| 145 | { |
| 146 | "ip4-map", |
| 147 | NULL, |
| 148 | }; |
| 149 | const static char* const map_ip6_nodes[] = |
| 150 | { |
| 151 | "ip6-map", |
| 152 | NULL, |
| 153 | }; |
| 154 | |
| 155 | const static char* const * const map_nodes[DPO_PROTO_NUM] = |
| 156 | { |
| 157 | [DPO_PROTO_IP4] = map_ip4_nodes, |
| 158 | [DPO_PROTO_IP6] = map_ip6_nodes, |
| 159 | [DPO_PROTO_MPLS] = NULL, |
| 160 | }; |
| 161 | |
| 162 | const static dpo_vft_t md_t_vft = { |
| 163 | .dv_lock = map_dpo_lock, |
| 164 | .dv_unlock = map_dpo_unlock, |
| 165 | .dv_format = format_map_t_dpo, |
| 166 | }; |
| 167 | |
| 168 | const static char* const map_t_ip4_nodes[] = |
| 169 | { |
| 170 | "ip4-map-t", |
| 171 | NULL, |
| 172 | }; |
| 173 | const static char* const map_t_ip6_nodes[] = |
| 174 | { |
| 175 | "ip6-map-t", |
| 176 | NULL, |
| 177 | }; |
| 178 | |
| 179 | const static char* const * const map_t_nodes[DPO_PROTO_NUM] = |
| 180 | { |
| 181 | [DPO_PROTO_IP4] = map_t_ip4_nodes, |
| 182 | [DPO_PROTO_IP6] = map_t_ip6_nodes, |
| 183 | [DPO_PROTO_MPLS] = NULL, |
| 184 | }; |
| 185 | |
| 186 | void |
| 187 | map_dpo_module_init (void) |
| 188 | { |
| 189 | map_dpo_type = dpo_register_new_type(&md_vft, map_nodes); |
| 190 | map_t_dpo_type = dpo_register_new_type(&md_t_vft, map_t_nodes); |
| 191 | } |