Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / drivers / infiniband / core / iwpm_util.c
blob9b2bf2fb2b00674287e74e7cb3048a5fa296b91e
1 /*
2 * Copyright (c) 2014 Chelsio, Inc. All rights reserved.
3 * Copyright (c) 2014 Intel Corporation. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
34 #include "iwpm_util.h"
36 #define IWPM_MAPINFO_HASH_SIZE 512
37 #define IWPM_MAPINFO_HASH_MASK (IWPM_MAPINFO_HASH_SIZE - 1)
38 #define IWPM_REMINFO_HASH_SIZE 64
39 #define IWPM_REMINFO_HASH_MASK (IWPM_REMINFO_HASH_SIZE - 1)
41 static LIST_HEAD(iwpm_nlmsg_req_list);
42 static DEFINE_SPINLOCK(iwpm_nlmsg_req_lock);
44 static struct hlist_head *iwpm_hash_bucket;
45 static DEFINE_SPINLOCK(iwpm_mapinfo_lock);
47 static struct hlist_head *iwpm_reminfo_bucket;
48 static DEFINE_SPINLOCK(iwpm_reminfo_lock);
50 static DEFINE_MUTEX(iwpm_admin_lock);
51 static struct iwpm_admin_data iwpm_admin;
53 int iwpm_init(u8 nl_client)
55 int ret = 0;
56 if (iwpm_valid_client(nl_client))
57 return -EINVAL;
58 mutex_lock(&iwpm_admin_lock);
59 if (atomic_read(&iwpm_admin.refcount) == 0) {
60 iwpm_hash_bucket = kzalloc(IWPM_MAPINFO_HASH_SIZE *
61 sizeof(struct hlist_head), GFP_KERNEL);
62 if (!iwpm_hash_bucket) {
63 ret = -ENOMEM;
64 pr_err("%s Unable to create mapinfo hash table\n", __func__);
65 goto init_exit;
67 iwpm_reminfo_bucket = kzalloc(IWPM_REMINFO_HASH_SIZE *
68 sizeof(struct hlist_head), GFP_KERNEL);
69 if (!iwpm_reminfo_bucket) {
70 kfree(iwpm_hash_bucket);
71 ret = -ENOMEM;
72 pr_err("%s Unable to create reminfo hash table\n", __func__);
73 goto init_exit;
76 atomic_inc(&iwpm_admin.refcount);
77 init_exit:
78 mutex_unlock(&iwpm_admin_lock);
79 if (!ret) {
80 iwpm_set_valid(nl_client, 1);
81 iwpm_set_registration(nl_client, IWPM_REG_UNDEF);
82 pr_debug("%s: Mapinfo and reminfo tables are created\n",
83 __func__);
85 return ret;
87 EXPORT_SYMBOL(iwpm_init);
89 static void free_hash_bucket(void);
90 static void free_reminfo_bucket(void);
92 int iwpm_exit(u8 nl_client)
95 if (!iwpm_valid_client(nl_client))
96 return -EINVAL;
97 mutex_lock(&iwpm_admin_lock);
98 if (atomic_read(&iwpm_admin.refcount) == 0) {
99 mutex_unlock(&iwpm_admin_lock);
100 pr_err("%s Incorrect usage - negative refcount\n", __func__);
101 return -EINVAL;
103 if (atomic_dec_and_test(&iwpm_admin.refcount)) {
104 free_hash_bucket();
105 free_reminfo_bucket();
106 pr_debug("%s: Resources are destroyed\n", __func__);
108 mutex_unlock(&iwpm_admin_lock);
109 iwpm_set_valid(nl_client, 0);
110 iwpm_set_registration(nl_client, IWPM_REG_UNDEF);
111 return 0;
113 EXPORT_SYMBOL(iwpm_exit);
115 static struct hlist_head *get_mapinfo_hash_bucket(struct sockaddr_storage *,
116 struct sockaddr_storage *);
118 int iwpm_create_mapinfo(struct sockaddr_storage *local_sockaddr,
119 struct sockaddr_storage *mapped_sockaddr,
120 u8 nl_client)
122 struct hlist_head *hash_bucket_head;
123 struct iwpm_mapping_info *map_info;
124 unsigned long flags;
125 int ret = -EINVAL;
127 if (!iwpm_valid_client(nl_client))
128 return ret;
129 map_info = kzalloc(sizeof(struct iwpm_mapping_info), GFP_KERNEL);
130 if (!map_info) {
131 pr_err("%s: Unable to allocate a mapping info\n", __func__);
132 return -ENOMEM;
134 memcpy(&map_info->local_sockaddr, local_sockaddr,
135 sizeof(struct sockaddr_storage));
136 memcpy(&map_info->mapped_sockaddr, mapped_sockaddr,
137 sizeof(struct sockaddr_storage));
138 map_info->nl_client = nl_client;
140 spin_lock_irqsave(&iwpm_mapinfo_lock, flags);
141 if (iwpm_hash_bucket) {
142 hash_bucket_head = get_mapinfo_hash_bucket(
143 &map_info->local_sockaddr,
144 &map_info->mapped_sockaddr);
145 if (hash_bucket_head) {
146 hlist_add_head(&map_info->hlist_node, hash_bucket_head);
147 ret = 0;
150 spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags);
151 return ret;
153 EXPORT_SYMBOL(iwpm_create_mapinfo);
155 int iwpm_remove_mapinfo(struct sockaddr_storage *local_sockaddr,
156 struct sockaddr_storage *mapped_local_addr)
158 struct hlist_node *tmp_hlist_node;
159 struct hlist_head *hash_bucket_head;
160 struct iwpm_mapping_info *map_info = NULL;
161 unsigned long flags;
162 int ret = -EINVAL;
164 spin_lock_irqsave(&iwpm_mapinfo_lock, flags);
165 if (iwpm_hash_bucket) {
166 hash_bucket_head = get_mapinfo_hash_bucket(
167 local_sockaddr,
168 mapped_local_addr);
169 if (!hash_bucket_head)
170 goto remove_mapinfo_exit;
172 hlist_for_each_entry_safe(map_info, tmp_hlist_node,
173 hash_bucket_head, hlist_node) {
175 if (!iwpm_compare_sockaddr(&map_info->mapped_sockaddr,
176 mapped_local_addr)) {
178 hlist_del_init(&map_info->hlist_node);
179 kfree(map_info);
180 ret = 0;
181 break;
185 remove_mapinfo_exit:
186 spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags);
187 return ret;
189 EXPORT_SYMBOL(iwpm_remove_mapinfo);
191 static void free_hash_bucket(void)
193 struct hlist_node *tmp_hlist_node;
194 struct iwpm_mapping_info *map_info;
195 unsigned long flags;
196 int i;
198 /* remove all the mapinfo data from the list */
199 spin_lock_irqsave(&iwpm_mapinfo_lock, flags);
200 for (i = 0; i < IWPM_MAPINFO_HASH_SIZE; i++) {
201 hlist_for_each_entry_safe(map_info, tmp_hlist_node,
202 &iwpm_hash_bucket[i], hlist_node) {
204 hlist_del_init(&map_info->hlist_node);
205 kfree(map_info);
208 /* free the hash list */
209 kfree(iwpm_hash_bucket);
210 iwpm_hash_bucket = NULL;
211 spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags);
214 static void free_reminfo_bucket(void)
216 struct hlist_node *tmp_hlist_node;
217 struct iwpm_remote_info *rem_info;
218 unsigned long flags;
219 int i;
221 /* remove all the remote info from the list */
222 spin_lock_irqsave(&iwpm_reminfo_lock, flags);
223 for (i = 0; i < IWPM_REMINFO_HASH_SIZE; i++) {
224 hlist_for_each_entry_safe(rem_info, tmp_hlist_node,
225 &iwpm_reminfo_bucket[i], hlist_node) {
227 hlist_del_init(&rem_info->hlist_node);
228 kfree(rem_info);
231 /* free the hash list */
232 kfree(iwpm_reminfo_bucket);
233 iwpm_reminfo_bucket = NULL;
234 spin_unlock_irqrestore(&iwpm_reminfo_lock, flags);
237 static struct hlist_head *get_reminfo_hash_bucket(struct sockaddr_storage *,
238 struct sockaddr_storage *);
240 void iwpm_add_remote_info(struct iwpm_remote_info *rem_info)
242 struct hlist_head *hash_bucket_head;
243 unsigned long flags;
245 spin_lock_irqsave(&iwpm_reminfo_lock, flags);
246 if (iwpm_reminfo_bucket) {
247 hash_bucket_head = get_reminfo_hash_bucket(
248 &rem_info->mapped_loc_sockaddr,
249 &rem_info->mapped_rem_sockaddr);
250 if (hash_bucket_head)
251 hlist_add_head(&rem_info->hlist_node, hash_bucket_head);
253 spin_unlock_irqrestore(&iwpm_reminfo_lock, flags);
256 int iwpm_get_remote_info(struct sockaddr_storage *mapped_loc_addr,
257 struct sockaddr_storage *mapped_rem_addr,
258 struct sockaddr_storage *remote_addr,
259 u8 nl_client)
261 struct hlist_node *tmp_hlist_node;
262 struct hlist_head *hash_bucket_head;
263 struct iwpm_remote_info *rem_info = NULL;
264 unsigned long flags;
265 int ret = -EINVAL;
267 if (!iwpm_valid_client(nl_client)) {
268 pr_info("%s: Invalid client = %d\n", __func__, nl_client);
269 return ret;
271 spin_lock_irqsave(&iwpm_reminfo_lock, flags);
272 if (iwpm_reminfo_bucket) {
273 hash_bucket_head = get_reminfo_hash_bucket(
274 mapped_loc_addr,
275 mapped_rem_addr);
276 if (!hash_bucket_head)
277 goto get_remote_info_exit;
278 hlist_for_each_entry_safe(rem_info, tmp_hlist_node,
279 hash_bucket_head, hlist_node) {
281 if (!iwpm_compare_sockaddr(&rem_info->mapped_loc_sockaddr,
282 mapped_loc_addr) &&
283 !iwpm_compare_sockaddr(&rem_info->mapped_rem_sockaddr,
284 mapped_rem_addr)) {
286 memcpy(remote_addr, &rem_info->remote_sockaddr,
287 sizeof(struct sockaddr_storage));
288 iwpm_print_sockaddr(remote_addr,
289 "get_remote_info: Remote sockaddr:");
291 hlist_del_init(&rem_info->hlist_node);
292 kfree(rem_info);
293 ret = 0;
294 break;
298 get_remote_info_exit:
299 spin_unlock_irqrestore(&iwpm_reminfo_lock, flags);
300 return ret;
302 EXPORT_SYMBOL(iwpm_get_remote_info);
304 struct iwpm_nlmsg_request *iwpm_get_nlmsg_request(__u32 nlmsg_seq,
305 u8 nl_client, gfp_t gfp)
307 struct iwpm_nlmsg_request *nlmsg_request = NULL;
308 unsigned long flags;
310 nlmsg_request = kzalloc(sizeof(struct iwpm_nlmsg_request), gfp);
311 if (!nlmsg_request) {
312 pr_err("%s Unable to allocate a nlmsg_request\n", __func__);
313 return NULL;
315 spin_lock_irqsave(&iwpm_nlmsg_req_lock, flags);
316 list_add_tail(&nlmsg_request->inprocess_list, &iwpm_nlmsg_req_list);
317 spin_unlock_irqrestore(&iwpm_nlmsg_req_lock, flags);
319 kref_init(&nlmsg_request->kref);
320 kref_get(&nlmsg_request->kref);
321 nlmsg_request->nlmsg_seq = nlmsg_seq;
322 nlmsg_request->nl_client = nl_client;
323 nlmsg_request->request_done = 0;
324 nlmsg_request->err_code = 0;
325 sema_init(&nlmsg_request->sem, 1);
326 down(&nlmsg_request->sem);
327 return nlmsg_request;
330 void iwpm_free_nlmsg_request(struct kref *kref)
332 struct iwpm_nlmsg_request *nlmsg_request;
333 unsigned long flags;
335 nlmsg_request = container_of(kref, struct iwpm_nlmsg_request, kref);
337 spin_lock_irqsave(&iwpm_nlmsg_req_lock, flags);
338 list_del_init(&nlmsg_request->inprocess_list);
339 spin_unlock_irqrestore(&iwpm_nlmsg_req_lock, flags);
341 if (!nlmsg_request->request_done)
342 pr_debug("%s Freeing incomplete nlmsg request (seq = %u).\n",
343 __func__, nlmsg_request->nlmsg_seq);
344 kfree(nlmsg_request);
347 struct iwpm_nlmsg_request *iwpm_find_nlmsg_request(__u32 echo_seq)
349 struct iwpm_nlmsg_request *nlmsg_request;
350 struct iwpm_nlmsg_request *found_request = NULL;
351 unsigned long flags;
353 spin_lock_irqsave(&iwpm_nlmsg_req_lock, flags);
354 list_for_each_entry(nlmsg_request, &iwpm_nlmsg_req_list,
355 inprocess_list) {
356 if (nlmsg_request->nlmsg_seq == echo_seq) {
357 found_request = nlmsg_request;
358 kref_get(&nlmsg_request->kref);
359 break;
362 spin_unlock_irqrestore(&iwpm_nlmsg_req_lock, flags);
363 return found_request;
366 int iwpm_wait_complete_req(struct iwpm_nlmsg_request *nlmsg_request)
368 int ret;
370 ret = down_timeout(&nlmsg_request->sem, IWPM_NL_TIMEOUT);
371 if (ret) {
372 ret = -EINVAL;
373 pr_info("%s: Timeout %d sec for netlink request (seq = %u)\n",
374 __func__, (IWPM_NL_TIMEOUT/HZ), nlmsg_request->nlmsg_seq);
375 } else {
376 ret = nlmsg_request->err_code;
378 kref_put(&nlmsg_request->kref, iwpm_free_nlmsg_request);
379 return ret;
382 int iwpm_get_nlmsg_seq(void)
384 return atomic_inc_return(&iwpm_admin.nlmsg_seq);
387 int iwpm_valid_client(u8 nl_client)
389 if (nl_client >= RDMA_NL_NUM_CLIENTS)
390 return 0;
391 return iwpm_admin.client_list[nl_client];
394 void iwpm_set_valid(u8 nl_client, int valid)
396 if (nl_client >= RDMA_NL_NUM_CLIENTS)
397 return;
398 iwpm_admin.client_list[nl_client] = valid;
401 /* valid client */
402 u32 iwpm_get_registration(u8 nl_client)
404 return iwpm_admin.reg_list[nl_client];
407 /* valid client */
408 void iwpm_set_registration(u8 nl_client, u32 reg)
410 iwpm_admin.reg_list[nl_client] = reg;
413 /* valid client */
414 u32 iwpm_check_registration(u8 nl_client, u32 reg)
416 return (iwpm_get_registration(nl_client) & reg);
419 int iwpm_compare_sockaddr(struct sockaddr_storage *a_sockaddr,
420 struct sockaddr_storage *b_sockaddr)
422 if (a_sockaddr->ss_family != b_sockaddr->ss_family)
423 return 1;
424 if (a_sockaddr->ss_family == AF_INET) {
425 struct sockaddr_in *a4_sockaddr =
426 (struct sockaddr_in *)a_sockaddr;
427 struct sockaddr_in *b4_sockaddr =
428 (struct sockaddr_in *)b_sockaddr;
429 if (!memcmp(&a4_sockaddr->sin_addr,
430 &b4_sockaddr->sin_addr, sizeof(struct in_addr))
431 && a4_sockaddr->sin_port == b4_sockaddr->sin_port)
432 return 0;
434 } else if (a_sockaddr->ss_family == AF_INET6) {
435 struct sockaddr_in6 *a6_sockaddr =
436 (struct sockaddr_in6 *)a_sockaddr;
437 struct sockaddr_in6 *b6_sockaddr =
438 (struct sockaddr_in6 *)b_sockaddr;
439 if (!memcmp(&a6_sockaddr->sin6_addr,
440 &b6_sockaddr->sin6_addr, sizeof(struct in6_addr))
441 && a6_sockaddr->sin6_port == b6_sockaddr->sin6_port)
442 return 0;
444 } else {
445 pr_err("%s: Invalid sockaddr family\n", __func__);
447 return 1;
450 struct sk_buff *iwpm_create_nlmsg(u32 nl_op, struct nlmsghdr **nlh,
451 int nl_client)
453 struct sk_buff *skb = NULL;
455 skb = dev_alloc_skb(NLMSG_GOODSIZE);
456 if (!skb) {
457 pr_err("%s Unable to allocate skb\n", __func__);
458 goto create_nlmsg_exit;
460 if (!(ibnl_put_msg(skb, nlh, 0, 0, nl_client, nl_op,
461 NLM_F_REQUEST))) {
462 pr_warn("%s: Unable to put the nlmsg header\n", __func__);
463 dev_kfree_skb(skb);
464 skb = NULL;
466 create_nlmsg_exit:
467 return skb;
470 int iwpm_parse_nlmsg(struct netlink_callback *cb, int policy_max,
471 const struct nla_policy *nlmsg_policy,
472 struct nlattr *nltb[], const char *msg_type)
474 int nlh_len = 0;
475 int ret;
476 const char *err_str = "";
478 ret = nlmsg_validate(cb->nlh, nlh_len, policy_max-1, nlmsg_policy);
479 if (ret) {
480 err_str = "Invalid attribute";
481 goto parse_nlmsg_error;
483 ret = nlmsg_parse(cb->nlh, nlh_len, nltb, policy_max-1, nlmsg_policy);
484 if (ret) {
485 err_str = "Unable to parse the nlmsg";
486 goto parse_nlmsg_error;
488 ret = iwpm_validate_nlmsg_attr(nltb, policy_max);
489 if (ret) {
490 err_str = "Invalid NULL attribute";
491 goto parse_nlmsg_error;
493 return 0;
494 parse_nlmsg_error:
495 pr_warn("%s: %s (msg type %s ret = %d)\n",
496 __func__, err_str, msg_type, ret);
497 return ret;
500 void iwpm_print_sockaddr(struct sockaddr_storage *sockaddr, char *msg)
502 struct sockaddr_in6 *sockaddr_v6;
503 struct sockaddr_in *sockaddr_v4;
505 switch (sockaddr->ss_family) {
506 case AF_INET:
507 sockaddr_v4 = (struct sockaddr_in *)sockaddr;
508 pr_debug("%s IPV4 %pI4: %u(0x%04X)\n",
509 msg, &sockaddr_v4->sin_addr,
510 ntohs(sockaddr_v4->sin_port),
511 ntohs(sockaddr_v4->sin_port));
512 break;
513 case AF_INET6:
514 sockaddr_v6 = (struct sockaddr_in6 *)sockaddr;
515 pr_debug("%s IPV6 %pI6: %u(0x%04X)\n",
516 msg, &sockaddr_v6->sin6_addr,
517 ntohs(sockaddr_v6->sin6_port),
518 ntohs(sockaddr_v6->sin6_port));
519 break;
520 default:
521 break;
525 static u32 iwpm_ipv6_jhash(struct sockaddr_in6 *ipv6_sockaddr)
527 u32 ipv6_hash = jhash(&ipv6_sockaddr->sin6_addr, sizeof(struct in6_addr), 0);
528 u32 hash = jhash_2words(ipv6_hash, (__force u32) ipv6_sockaddr->sin6_port, 0);
529 return hash;
532 static u32 iwpm_ipv4_jhash(struct sockaddr_in *ipv4_sockaddr)
534 u32 ipv4_hash = jhash(&ipv4_sockaddr->sin_addr, sizeof(struct in_addr), 0);
535 u32 hash = jhash_2words(ipv4_hash, (__force u32) ipv4_sockaddr->sin_port, 0);
536 return hash;
539 static int get_hash_bucket(struct sockaddr_storage *a_sockaddr,
540 struct sockaddr_storage *b_sockaddr, u32 *hash)
542 u32 a_hash, b_hash;
544 if (a_sockaddr->ss_family == AF_INET) {
545 a_hash = iwpm_ipv4_jhash((struct sockaddr_in *) a_sockaddr);
546 b_hash = iwpm_ipv4_jhash((struct sockaddr_in *) b_sockaddr);
548 } else if (a_sockaddr->ss_family == AF_INET6) {
549 a_hash = iwpm_ipv6_jhash((struct sockaddr_in6 *) a_sockaddr);
550 b_hash = iwpm_ipv6_jhash((struct sockaddr_in6 *) b_sockaddr);
551 } else {
552 pr_err("%s: Invalid sockaddr family\n", __func__);
553 return -EINVAL;
556 if (a_hash == b_hash) /* if port mapper isn't available */
557 *hash = a_hash;
558 else
559 *hash = jhash_2words(a_hash, b_hash, 0);
560 return 0;
563 static struct hlist_head *get_mapinfo_hash_bucket(struct sockaddr_storage
564 *local_sockaddr, struct sockaddr_storage
565 *mapped_sockaddr)
567 u32 hash;
568 int ret;
570 ret = get_hash_bucket(local_sockaddr, mapped_sockaddr, &hash);
571 if (ret)
572 return NULL;
573 return &iwpm_hash_bucket[hash & IWPM_MAPINFO_HASH_MASK];
576 static struct hlist_head *get_reminfo_hash_bucket(struct sockaddr_storage
577 *mapped_loc_sockaddr, struct sockaddr_storage
578 *mapped_rem_sockaddr)
580 u32 hash;
581 int ret;
583 ret = get_hash_bucket(mapped_loc_sockaddr, mapped_rem_sockaddr, &hash);
584 if (ret)
585 return NULL;
586 return &iwpm_reminfo_bucket[hash & IWPM_REMINFO_HASH_MASK];
589 static int send_mapinfo_num(u32 mapping_num, u8 nl_client, int iwpm_pid)
591 struct sk_buff *skb = NULL;
592 struct nlmsghdr *nlh;
593 u32 msg_seq;
594 const char *err_str = "";
595 int ret = -EINVAL;
597 skb = iwpm_create_nlmsg(RDMA_NL_IWPM_MAPINFO_NUM, &nlh, nl_client);
598 if (!skb) {
599 err_str = "Unable to create a nlmsg";
600 goto mapinfo_num_error;
602 nlh->nlmsg_seq = iwpm_get_nlmsg_seq();
603 msg_seq = 0;
604 err_str = "Unable to put attribute of mapinfo number nlmsg";
605 ret = ibnl_put_attr(skb, nlh, sizeof(u32), &msg_seq, IWPM_NLA_MAPINFO_SEQ);
606 if (ret)
607 goto mapinfo_num_error;
608 ret = ibnl_put_attr(skb, nlh, sizeof(u32),
609 &mapping_num, IWPM_NLA_MAPINFO_SEND_NUM);
610 if (ret)
611 goto mapinfo_num_error;
612 ret = ibnl_unicast(skb, nlh, iwpm_pid);
613 if (ret) {
614 skb = NULL;
615 err_str = "Unable to send a nlmsg";
616 goto mapinfo_num_error;
618 pr_debug("%s: Sent mapping number = %d\n", __func__, mapping_num);
619 return 0;
620 mapinfo_num_error:
621 pr_info("%s: %s\n", __func__, err_str);
622 if (skb)
623 dev_kfree_skb(skb);
624 return ret;
627 static int send_nlmsg_done(struct sk_buff *skb, u8 nl_client, int iwpm_pid)
629 struct nlmsghdr *nlh = NULL;
630 int ret = 0;
632 if (!skb)
633 return ret;
634 if (!(ibnl_put_msg(skb, &nlh, 0, 0, nl_client,
635 RDMA_NL_IWPM_MAPINFO, NLM_F_MULTI))) {
636 pr_warn("%s Unable to put NLMSG_DONE\n", __func__);
637 return -ENOMEM;
639 nlh->nlmsg_type = NLMSG_DONE;
640 ret = ibnl_unicast(skb, (struct nlmsghdr *)skb->data, iwpm_pid);
641 if (ret)
642 pr_warn("%s Unable to send a nlmsg\n", __func__);
643 return ret;
646 int iwpm_send_mapinfo(u8 nl_client, int iwpm_pid)
648 struct iwpm_mapping_info *map_info;
649 struct sk_buff *skb = NULL;
650 struct nlmsghdr *nlh;
651 int skb_num = 0, mapping_num = 0;
652 int i = 0, nlmsg_bytes = 0;
653 unsigned long flags;
654 const char *err_str = "";
655 int ret;
657 skb = dev_alloc_skb(NLMSG_GOODSIZE);
658 if (!skb) {
659 ret = -ENOMEM;
660 err_str = "Unable to allocate skb";
661 goto send_mapping_info_exit;
663 skb_num++;
664 spin_lock_irqsave(&iwpm_mapinfo_lock, flags);
665 for (i = 0; i < IWPM_MAPINFO_HASH_SIZE; i++) {
666 hlist_for_each_entry(map_info, &iwpm_hash_bucket[i],
667 hlist_node) {
668 if (map_info->nl_client != nl_client)
669 continue;
670 nlh = NULL;
671 if (!(ibnl_put_msg(skb, &nlh, 0, 0, nl_client,
672 RDMA_NL_IWPM_MAPINFO, NLM_F_MULTI))) {
673 ret = -ENOMEM;
674 err_str = "Unable to put the nlmsg header";
675 goto send_mapping_info_unlock;
677 err_str = "Unable to put attribute of the nlmsg";
678 ret = ibnl_put_attr(skb, nlh,
679 sizeof(struct sockaddr_storage),
680 &map_info->local_sockaddr,
681 IWPM_NLA_MAPINFO_LOCAL_ADDR);
682 if (ret)
683 goto send_mapping_info_unlock;
685 ret = ibnl_put_attr(skb, nlh,
686 sizeof(struct sockaddr_storage),
687 &map_info->mapped_sockaddr,
688 IWPM_NLA_MAPINFO_MAPPED_ADDR);
689 if (ret)
690 goto send_mapping_info_unlock;
692 iwpm_print_sockaddr(&map_info->local_sockaddr,
693 "send_mapping_info: Local sockaddr:");
694 iwpm_print_sockaddr(&map_info->mapped_sockaddr,
695 "send_mapping_info: Mapped local sockaddr:");
696 mapping_num++;
697 nlmsg_bytes += nlh->nlmsg_len;
699 /* check if all mappings can fit in one skb */
700 if (NLMSG_GOODSIZE - nlmsg_bytes < nlh->nlmsg_len * 2) {
701 /* and leave room for NLMSG_DONE */
702 nlmsg_bytes = 0;
703 skb_num++;
704 spin_unlock_irqrestore(&iwpm_mapinfo_lock,
705 flags);
706 /* send the skb */
707 ret = send_nlmsg_done(skb, nl_client, iwpm_pid);
708 skb = NULL;
709 if (ret) {
710 err_str = "Unable to send map info";
711 goto send_mapping_info_exit;
713 if (skb_num == IWPM_MAPINFO_SKB_COUNT) {
714 ret = -ENOMEM;
715 err_str = "Insufficient skbs for map info";
716 goto send_mapping_info_exit;
718 skb = dev_alloc_skb(NLMSG_GOODSIZE);
719 if (!skb) {
720 ret = -ENOMEM;
721 err_str = "Unable to allocate skb";
722 goto send_mapping_info_exit;
724 spin_lock_irqsave(&iwpm_mapinfo_lock, flags);
728 send_mapping_info_unlock:
729 spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags);
730 send_mapping_info_exit:
731 if (ret) {
732 pr_warn("%s: %s (ret = %d)\n", __func__, err_str, ret);
733 if (skb)
734 dev_kfree_skb(skb);
735 return ret;
737 send_nlmsg_done(skb, nl_client, iwpm_pid);
738 return send_mapinfo_num(mapping_num, nl_client, iwpm_pid);
741 int iwpm_mapinfo_available(void)
743 unsigned long flags;
744 int full_bucket = 0, i = 0;
746 spin_lock_irqsave(&iwpm_mapinfo_lock, flags);
747 if (iwpm_hash_bucket) {
748 for (i = 0; i < IWPM_MAPINFO_HASH_SIZE; i++) {
749 if (!hlist_empty(&iwpm_hash_bucket[i])) {
750 full_bucket = 1;
751 break;
755 spin_unlock_irqrestore(&iwpm_mapinfo_lock, flags);
756 return full_bucket;