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
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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
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)
40 #define IWPM_MSG_SIZE 512
42 static LIST_HEAD(iwpm_nlmsg_req_list
);
43 static DEFINE_SPINLOCK(iwpm_nlmsg_req_lock
);
45 static struct hlist_head
*iwpm_hash_bucket
;
46 static DEFINE_SPINLOCK(iwpm_mapinfo_lock
);
48 static struct hlist_head
*iwpm_reminfo_bucket
;
49 static DEFINE_SPINLOCK(iwpm_reminfo_lock
);
51 static DEFINE_MUTEX(iwpm_admin_lock
);
52 static struct iwpm_admin_data iwpm_admin
;
55 * iwpm_init - Allocate resources for the iwarp port mapper
56 * @nl_client: The index of the netlink client
58 * Should be called when network interface goes up.
60 int iwpm_init(u8 nl_client
)
63 mutex_lock(&iwpm_admin_lock
);
64 if (atomic_read(&iwpm_admin
.refcount
) == 0) {
65 iwpm_hash_bucket
= kcalloc(IWPM_MAPINFO_HASH_SIZE
,
66 sizeof(struct hlist_head
),
68 if (!iwpm_hash_bucket
) {
72 iwpm_reminfo_bucket
= kcalloc(IWPM_REMINFO_HASH_SIZE
,
73 sizeof(struct hlist_head
),
75 if (!iwpm_reminfo_bucket
) {
76 kfree(iwpm_hash_bucket
);
81 atomic_inc(&iwpm_admin
.refcount
);
83 mutex_unlock(&iwpm_admin_lock
);
85 iwpm_set_valid(nl_client
, 1);
86 iwpm_set_registration(nl_client
, IWPM_REG_UNDEF
);
87 pr_debug("%s: Mapinfo and reminfo tables are created\n",
93 static void free_hash_bucket(void);
94 static void free_reminfo_bucket(void);
97 * iwpm_exit - Deallocate resources for the iwarp port mapper
98 * @nl_client: The index of the netlink client
100 * Should be called when network interface goes down.
102 int iwpm_exit(u8 nl_client
)
105 if (!iwpm_valid_client(nl_client
))
107 mutex_lock(&iwpm_admin_lock
);
108 if (atomic_read(&iwpm_admin
.refcount
) == 0) {
109 mutex_unlock(&iwpm_admin_lock
);
110 pr_err("%s Incorrect usage - negative refcount\n", __func__
);
113 if (atomic_dec_and_test(&iwpm_admin
.refcount
)) {
115 free_reminfo_bucket();
116 pr_debug("%s: Resources are destroyed\n", __func__
);
118 mutex_unlock(&iwpm_admin_lock
);
119 iwpm_set_valid(nl_client
, 0);
120 iwpm_set_registration(nl_client
, IWPM_REG_UNDEF
);
124 static struct hlist_head
*get_mapinfo_hash_bucket(struct sockaddr_storage
*,
125 struct sockaddr_storage
*);
128 * iwpm_create_mapinfo - Store local and mapped IPv4/IPv6 address
129 * info in a hash table
130 * @local_addr: Local ip/tcp address
131 * @mapped_addr: Mapped local ip/tcp address
132 * @nl_client: The index of the netlink client
133 * @map_flags: IWPM mapping flags
135 int iwpm_create_mapinfo(struct sockaddr_storage
*local_sockaddr
,
136 struct sockaddr_storage
*mapped_sockaddr
,
137 u8 nl_client
, u32 map_flags
)
139 struct hlist_head
*hash_bucket_head
= NULL
;
140 struct iwpm_mapping_info
*map_info
;
144 if (!iwpm_valid_client(nl_client
))
146 map_info
= kzalloc(sizeof(struct iwpm_mapping_info
), GFP_KERNEL
);
150 memcpy(&map_info
->local_sockaddr
, local_sockaddr
,
151 sizeof(struct sockaddr_storage
));
152 memcpy(&map_info
->mapped_sockaddr
, mapped_sockaddr
,
153 sizeof(struct sockaddr_storage
));
154 map_info
->nl_client
= nl_client
;
155 map_info
->map_flags
= map_flags
;
157 spin_lock_irqsave(&iwpm_mapinfo_lock
, flags
);
158 if (iwpm_hash_bucket
) {
159 hash_bucket_head
= get_mapinfo_hash_bucket(
160 &map_info
->local_sockaddr
,
161 &map_info
->mapped_sockaddr
);
162 if (hash_bucket_head
) {
163 hlist_add_head(&map_info
->hlist_node
, hash_bucket_head
);
167 spin_unlock_irqrestore(&iwpm_mapinfo_lock
, flags
);
169 if (!hash_bucket_head
)
175 * iwpm_remove_mapinfo - Remove local and mapped IPv4/IPv6 address
176 * info from the hash table
177 * @local_addr: Local ip/tcp address
178 * @mapped_local_addr: Mapped local ip/tcp address
180 * Returns err code if mapping info is not found in the hash table,
181 * otherwise returns 0
183 int iwpm_remove_mapinfo(struct sockaddr_storage
*local_sockaddr
,
184 struct sockaddr_storage
*mapped_local_addr
)
186 struct hlist_node
*tmp_hlist_node
;
187 struct hlist_head
*hash_bucket_head
;
188 struct iwpm_mapping_info
*map_info
= NULL
;
192 spin_lock_irqsave(&iwpm_mapinfo_lock
, flags
);
193 if (iwpm_hash_bucket
) {
194 hash_bucket_head
= get_mapinfo_hash_bucket(
197 if (!hash_bucket_head
)
198 goto remove_mapinfo_exit
;
200 hlist_for_each_entry_safe(map_info
, tmp_hlist_node
,
201 hash_bucket_head
, hlist_node
) {
203 if (!iwpm_compare_sockaddr(&map_info
->mapped_sockaddr
,
204 mapped_local_addr
)) {
206 hlist_del_init(&map_info
->hlist_node
);
214 spin_unlock_irqrestore(&iwpm_mapinfo_lock
, flags
);
218 static void free_hash_bucket(void)
220 struct hlist_node
*tmp_hlist_node
;
221 struct iwpm_mapping_info
*map_info
;
225 /* remove all the mapinfo data from the list */
226 spin_lock_irqsave(&iwpm_mapinfo_lock
, flags
);
227 for (i
= 0; i
< IWPM_MAPINFO_HASH_SIZE
; i
++) {
228 hlist_for_each_entry_safe(map_info
, tmp_hlist_node
,
229 &iwpm_hash_bucket
[i
], hlist_node
) {
231 hlist_del_init(&map_info
->hlist_node
);
235 /* free the hash list */
236 kfree(iwpm_hash_bucket
);
237 iwpm_hash_bucket
= NULL
;
238 spin_unlock_irqrestore(&iwpm_mapinfo_lock
, flags
);
241 static void free_reminfo_bucket(void)
243 struct hlist_node
*tmp_hlist_node
;
244 struct iwpm_remote_info
*rem_info
;
248 /* remove all the remote info from the list */
249 spin_lock_irqsave(&iwpm_reminfo_lock
, flags
);
250 for (i
= 0; i
< IWPM_REMINFO_HASH_SIZE
; i
++) {
251 hlist_for_each_entry_safe(rem_info
, tmp_hlist_node
,
252 &iwpm_reminfo_bucket
[i
], hlist_node
) {
254 hlist_del_init(&rem_info
->hlist_node
);
258 /* free the hash list */
259 kfree(iwpm_reminfo_bucket
);
260 iwpm_reminfo_bucket
= NULL
;
261 spin_unlock_irqrestore(&iwpm_reminfo_lock
, flags
);
264 static struct hlist_head
*get_reminfo_hash_bucket(struct sockaddr_storage
*,
265 struct sockaddr_storage
*);
267 void iwpm_add_remote_info(struct iwpm_remote_info
*rem_info
)
269 struct hlist_head
*hash_bucket_head
;
272 spin_lock_irqsave(&iwpm_reminfo_lock
, flags
);
273 if (iwpm_reminfo_bucket
) {
274 hash_bucket_head
= get_reminfo_hash_bucket(
275 &rem_info
->mapped_loc_sockaddr
,
276 &rem_info
->mapped_rem_sockaddr
);
277 if (hash_bucket_head
)
278 hlist_add_head(&rem_info
->hlist_node
, hash_bucket_head
);
280 spin_unlock_irqrestore(&iwpm_reminfo_lock
, flags
);
284 * iwpm_get_remote_info - Get the remote connecting peer address info
286 * @mapped_loc_addr: Mapped local address of the listening peer
287 * @mapped_rem_addr: Mapped remote address of the connecting peer
288 * @remote_addr: To store the remote address of the connecting peer
289 * @nl_client: The index of the netlink client
291 * The remote address info is retrieved and provided to the client in
292 * the remote_addr. After that it is removed from the hash table
294 int iwpm_get_remote_info(struct sockaddr_storage
*mapped_loc_addr
,
295 struct sockaddr_storage
*mapped_rem_addr
,
296 struct sockaddr_storage
*remote_addr
,
299 struct hlist_node
*tmp_hlist_node
;
300 struct hlist_head
*hash_bucket_head
;
301 struct iwpm_remote_info
*rem_info
= NULL
;
305 if (!iwpm_valid_client(nl_client
)) {
306 pr_info("%s: Invalid client = %d\n", __func__
, nl_client
);
309 spin_lock_irqsave(&iwpm_reminfo_lock
, flags
);
310 if (iwpm_reminfo_bucket
) {
311 hash_bucket_head
= get_reminfo_hash_bucket(
314 if (!hash_bucket_head
)
315 goto get_remote_info_exit
;
316 hlist_for_each_entry_safe(rem_info
, tmp_hlist_node
,
317 hash_bucket_head
, hlist_node
) {
319 if (!iwpm_compare_sockaddr(&rem_info
->mapped_loc_sockaddr
,
321 !iwpm_compare_sockaddr(&rem_info
->mapped_rem_sockaddr
,
324 memcpy(remote_addr
, &rem_info
->remote_sockaddr
,
325 sizeof(struct sockaddr_storage
));
326 iwpm_print_sockaddr(remote_addr
,
327 "get_remote_info: Remote sockaddr:");
329 hlist_del_init(&rem_info
->hlist_node
);
336 get_remote_info_exit
:
337 spin_unlock_irqrestore(&iwpm_reminfo_lock
, flags
);
341 struct iwpm_nlmsg_request
*iwpm_get_nlmsg_request(__u32 nlmsg_seq
,
342 u8 nl_client
, gfp_t gfp
)
344 struct iwpm_nlmsg_request
*nlmsg_request
= NULL
;
347 nlmsg_request
= kzalloc(sizeof(struct iwpm_nlmsg_request
), gfp
);
351 spin_lock_irqsave(&iwpm_nlmsg_req_lock
, flags
);
352 list_add_tail(&nlmsg_request
->inprocess_list
, &iwpm_nlmsg_req_list
);
353 spin_unlock_irqrestore(&iwpm_nlmsg_req_lock
, flags
);
355 kref_init(&nlmsg_request
->kref
);
356 kref_get(&nlmsg_request
->kref
);
357 nlmsg_request
->nlmsg_seq
= nlmsg_seq
;
358 nlmsg_request
->nl_client
= nl_client
;
359 nlmsg_request
->request_done
= 0;
360 nlmsg_request
->err_code
= 0;
361 sema_init(&nlmsg_request
->sem
, 1);
362 down(&nlmsg_request
->sem
);
363 return nlmsg_request
;
366 void iwpm_free_nlmsg_request(struct kref
*kref
)
368 struct iwpm_nlmsg_request
*nlmsg_request
;
371 nlmsg_request
= container_of(kref
, struct iwpm_nlmsg_request
, kref
);
373 spin_lock_irqsave(&iwpm_nlmsg_req_lock
, flags
);
374 list_del_init(&nlmsg_request
->inprocess_list
);
375 spin_unlock_irqrestore(&iwpm_nlmsg_req_lock
, flags
);
377 if (!nlmsg_request
->request_done
)
378 pr_debug("%s Freeing incomplete nlmsg request (seq = %u).\n",
379 __func__
, nlmsg_request
->nlmsg_seq
);
380 kfree(nlmsg_request
);
383 struct iwpm_nlmsg_request
*iwpm_find_nlmsg_request(__u32 echo_seq
)
385 struct iwpm_nlmsg_request
*nlmsg_request
;
386 struct iwpm_nlmsg_request
*found_request
= NULL
;
389 spin_lock_irqsave(&iwpm_nlmsg_req_lock
, flags
);
390 list_for_each_entry(nlmsg_request
, &iwpm_nlmsg_req_list
,
392 if (nlmsg_request
->nlmsg_seq
== echo_seq
) {
393 found_request
= nlmsg_request
;
394 kref_get(&nlmsg_request
->kref
);
398 spin_unlock_irqrestore(&iwpm_nlmsg_req_lock
, flags
);
399 return found_request
;
402 int iwpm_wait_complete_req(struct iwpm_nlmsg_request
*nlmsg_request
)
406 ret
= down_timeout(&nlmsg_request
->sem
, IWPM_NL_TIMEOUT
);
409 pr_info("%s: Timeout %d sec for netlink request (seq = %u)\n",
410 __func__
, (IWPM_NL_TIMEOUT
/HZ
), nlmsg_request
->nlmsg_seq
);
412 ret
= nlmsg_request
->err_code
;
414 kref_put(&nlmsg_request
->kref
, iwpm_free_nlmsg_request
);
418 int iwpm_get_nlmsg_seq(void)
420 return atomic_inc_return(&iwpm_admin
.nlmsg_seq
);
423 int iwpm_valid_client(u8 nl_client
)
425 return iwpm_admin
.client_list
[nl_client
];
428 void iwpm_set_valid(u8 nl_client
, int valid
)
430 iwpm_admin
.client_list
[nl_client
] = valid
;
434 u32
iwpm_get_registration(u8 nl_client
)
436 return iwpm_admin
.reg_list
[nl_client
];
440 void iwpm_set_registration(u8 nl_client
, u32 reg
)
442 iwpm_admin
.reg_list
[nl_client
] = reg
;
446 u32
iwpm_check_registration(u8 nl_client
, u32 reg
)
448 return (iwpm_get_registration(nl_client
) & reg
);
451 int iwpm_compare_sockaddr(struct sockaddr_storage
*a_sockaddr
,
452 struct sockaddr_storage
*b_sockaddr
)
454 if (a_sockaddr
->ss_family
!= b_sockaddr
->ss_family
)
456 if (a_sockaddr
->ss_family
== AF_INET
) {
457 struct sockaddr_in
*a4_sockaddr
=
458 (struct sockaddr_in
*)a_sockaddr
;
459 struct sockaddr_in
*b4_sockaddr
=
460 (struct sockaddr_in
*)b_sockaddr
;
461 if (!memcmp(&a4_sockaddr
->sin_addr
,
462 &b4_sockaddr
->sin_addr
, sizeof(struct in_addr
))
463 && a4_sockaddr
->sin_port
== b4_sockaddr
->sin_port
)
466 } else if (a_sockaddr
->ss_family
== AF_INET6
) {
467 struct sockaddr_in6
*a6_sockaddr
=
468 (struct sockaddr_in6
*)a_sockaddr
;
469 struct sockaddr_in6
*b6_sockaddr
=
470 (struct sockaddr_in6
*)b_sockaddr
;
471 if (!memcmp(&a6_sockaddr
->sin6_addr
,
472 &b6_sockaddr
->sin6_addr
, sizeof(struct in6_addr
))
473 && a6_sockaddr
->sin6_port
== b6_sockaddr
->sin6_port
)
477 pr_err("%s: Invalid sockaddr family\n", __func__
);
482 struct sk_buff
*iwpm_create_nlmsg(u32 nl_op
, struct nlmsghdr
**nlh
,
485 struct sk_buff
*skb
= NULL
;
487 skb
= dev_alloc_skb(IWPM_MSG_SIZE
);
489 goto create_nlmsg_exit
;
491 if (!(ibnl_put_msg(skb
, nlh
, 0, 0, nl_client
, nl_op
,
493 pr_warn("%s: Unable to put the nlmsg header\n", __func__
);
501 int iwpm_parse_nlmsg(struct netlink_callback
*cb
, int policy_max
,
502 const struct nla_policy
*nlmsg_policy
,
503 struct nlattr
*nltb
[], const char *msg_type
)
507 const char *err_str
= "";
509 ret
= nlmsg_validate_deprecated(cb
->nlh
, nlh_len
, policy_max
- 1,
512 err_str
= "Invalid attribute";
513 goto parse_nlmsg_error
;
515 ret
= nlmsg_parse_deprecated(cb
->nlh
, nlh_len
, nltb
, policy_max
- 1,
518 err_str
= "Unable to parse the nlmsg";
519 goto parse_nlmsg_error
;
521 ret
= iwpm_validate_nlmsg_attr(nltb
, policy_max
);
523 err_str
= "Invalid NULL attribute";
524 goto parse_nlmsg_error
;
528 pr_warn("%s: %s (msg type %s ret = %d)\n",
529 __func__
, err_str
, msg_type
, ret
);
533 void iwpm_print_sockaddr(struct sockaddr_storage
*sockaddr
, char *msg
)
535 struct sockaddr_in6
*sockaddr_v6
;
536 struct sockaddr_in
*sockaddr_v4
;
538 switch (sockaddr
->ss_family
) {
540 sockaddr_v4
= (struct sockaddr_in
*)sockaddr
;
541 pr_debug("%s IPV4 %pI4: %u(0x%04X)\n",
542 msg
, &sockaddr_v4
->sin_addr
,
543 ntohs(sockaddr_v4
->sin_port
),
544 ntohs(sockaddr_v4
->sin_port
));
547 sockaddr_v6
= (struct sockaddr_in6
*)sockaddr
;
548 pr_debug("%s IPV6 %pI6: %u(0x%04X)\n",
549 msg
, &sockaddr_v6
->sin6_addr
,
550 ntohs(sockaddr_v6
->sin6_port
),
551 ntohs(sockaddr_v6
->sin6_port
));
558 static u32
iwpm_ipv6_jhash(struct sockaddr_in6
*ipv6_sockaddr
)
560 u32 ipv6_hash
= jhash(&ipv6_sockaddr
->sin6_addr
, sizeof(struct in6_addr
), 0);
561 u32 hash
= jhash_2words(ipv6_hash
, (__force u32
) ipv6_sockaddr
->sin6_port
, 0);
565 static u32
iwpm_ipv4_jhash(struct sockaddr_in
*ipv4_sockaddr
)
567 u32 ipv4_hash
= jhash(&ipv4_sockaddr
->sin_addr
, sizeof(struct in_addr
), 0);
568 u32 hash
= jhash_2words(ipv4_hash
, (__force u32
) ipv4_sockaddr
->sin_port
, 0);
572 static int get_hash_bucket(struct sockaddr_storage
*a_sockaddr
,
573 struct sockaddr_storage
*b_sockaddr
, u32
*hash
)
577 if (a_sockaddr
->ss_family
== AF_INET
) {
578 a_hash
= iwpm_ipv4_jhash((struct sockaddr_in
*) a_sockaddr
);
579 b_hash
= iwpm_ipv4_jhash((struct sockaddr_in
*) b_sockaddr
);
581 } else if (a_sockaddr
->ss_family
== AF_INET6
) {
582 a_hash
= iwpm_ipv6_jhash((struct sockaddr_in6
*) a_sockaddr
);
583 b_hash
= iwpm_ipv6_jhash((struct sockaddr_in6
*) b_sockaddr
);
585 pr_err("%s: Invalid sockaddr family\n", __func__
);
589 if (a_hash
== b_hash
) /* if port mapper isn't available */
592 *hash
= jhash_2words(a_hash
, b_hash
, 0);
596 static struct hlist_head
*get_mapinfo_hash_bucket(struct sockaddr_storage
597 *local_sockaddr
, struct sockaddr_storage
603 ret
= get_hash_bucket(local_sockaddr
, mapped_sockaddr
, &hash
);
606 return &iwpm_hash_bucket
[hash
& IWPM_MAPINFO_HASH_MASK
];
609 static struct hlist_head
*get_reminfo_hash_bucket(struct sockaddr_storage
610 *mapped_loc_sockaddr
, struct sockaddr_storage
611 *mapped_rem_sockaddr
)
616 ret
= get_hash_bucket(mapped_loc_sockaddr
, mapped_rem_sockaddr
, &hash
);
619 return &iwpm_reminfo_bucket
[hash
& IWPM_REMINFO_HASH_MASK
];
622 static int send_mapinfo_num(u32 mapping_num
, u8 nl_client
, int iwpm_pid
)
624 struct sk_buff
*skb
= NULL
;
625 struct nlmsghdr
*nlh
;
627 const char *err_str
= "";
630 skb
= iwpm_create_nlmsg(RDMA_NL_IWPM_MAPINFO_NUM
, &nlh
, nl_client
);
632 err_str
= "Unable to create a nlmsg";
633 goto mapinfo_num_error
;
635 nlh
->nlmsg_seq
= iwpm_get_nlmsg_seq();
637 err_str
= "Unable to put attribute of mapinfo number nlmsg";
638 ret
= ibnl_put_attr(skb
, nlh
, sizeof(u32
), &msg_seq
, IWPM_NLA_MAPINFO_SEQ
);
640 goto mapinfo_num_error
;
641 ret
= ibnl_put_attr(skb
, nlh
, sizeof(u32
),
642 &mapping_num
, IWPM_NLA_MAPINFO_SEND_NUM
);
644 goto mapinfo_num_error
;
648 ret
= rdma_nl_unicast(&init_net
, skb
, iwpm_pid
);
651 err_str
= "Unable to send a nlmsg";
652 goto mapinfo_num_error
;
654 pr_debug("%s: Sent mapping number = %d\n", __func__
, mapping_num
);
657 pr_info("%s: %s\n", __func__
, err_str
);
662 static int send_nlmsg_done(struct sk_buff
*skb
, u8 nl_client
, int iwpm_pid
)
664 struct nlmsghdr
*nlh
= NULL
;
669 if (!(ibnl_put_msg(skb
, &nlh
, 0, 0, nl_client
,
670 RDMA_NL_IWPM_MAPINFO
, NLM_F_MULTI
))) {
671 pr_warn("%s Unable to put NLMSG_DONE\n", __func__
);
675 nlh
->nlmsg_type
= NLMSG_DONE
;
676 ret
= rdma_nl_unicast(&init_net
, skb
, iwpm_pid
);
678 pr_warn("%s Unable to send a nlmsg\n", __func__
);
682 int iwpm_send_mapinfo(u8 nl_client
, int iwpm_pid
)
684 struct iwpm_mapping_info
*map_info
;
685 struct sk_buff
*skb
= NULL
;
686 struct nlmsghdr
*nlh
;
687 int skb_num
= 0, mapping_num
= 0;
688 int i
= 0, nlmsg_bytes
= 0;
690 const char *err_str
= "";
693 skb
= dev_alloc_skb(NLMSG_GOODSIZE
);
696 err_str
= "Unable to allocate skb";
697 goto send_mapping_info_exit
;
700 spin_lock_irqsave(&iwpm_mapinfo_lock
, flags
);
702 for (i
= 0; i
< IWPM_MAPINFO_HASH_SIZE
; i
++) {
703 hlist_for_each_entry(map_info
, &iwpm_hash_bucket
[i
],
705 if (map_info
->nl_client
!= nl_client
)
708 if (!(ibnl_put_msg(skb
, &nlh
, 0, 0, nl_client
,
709 RDMA_NL_IWPM_MAPINFO
, NLM_F_MULTI
))) {
711 err_str
= "Unable to put the nlmsg header";
712 goto send_mapping_info_unlock
;
714 err_str
= "Unable to put attribute of the nlmsg";
715 ret
= ibnl_put_attr(skb
, nlh
,
716 sizeof(struct sockaddr_storage
),
717 &map_info
->local_sockaddr
,
718 IWPM_NLA_MAPINFO_LOCAL_ADDR
);
720 goto send_mapping_info_unlock
;
722 ret
= ibnl_put_attr(skb
, nlh
,
723 sizeof(struct sockaddr_storage
),
724 &map_info
->mapped_sockaddr
,
725 IWPM_NLA_MAPINFO_MAPPED_ADDR
);
727 goto send_mapping_info_unlock
;
729 if (iwpm_ulib_version
> IWPM_UABI_VERSION_MIN
) {
730 ret
= ibnl_put_attr(skb
, nlh
, sizeof(u32
),
731 &map_info
->map_flags
,
732 IWPM_NLA_MAPINFO_FLAGS
);
734 goto send_mapping_info_unlock
;
739 iwpm_print_sockaddr(&map_info
->local_sockaddr
,
740 "send_mapping_info: Local sockaddr:");
741 iwpm_print_sockaddr(&map_info
->mapped_sockaddr
,
742 "send_mapping_info: Mapped local sockaddr:");
744 nlmsg_bytes
+= nlh
->nlmsg_len
;
746 /* check if all mappings can fit in one skb */
747 if (NLMSG_GOODSIZE
- nlmsg_bytes
< nlh
->nlmsg_len
* 2) {
748 /* and leave room for NLMSG_DONE */
751 spin_unlock_irqrestore(&iwpm_mapinfo_lock
,
754 ret
= send_nlmsg_done(skb
, nl_client
, iwpm_pid
);
757 err_str
= "Unable to send map info";
758 goto send_mapping_info_exit
;
760 if (skb_num
== IWPM_MAPINFO_SKB_COUNT
) {
762 err_str
= "Insufficient skbs for map info";
763 goto send_mapping_info_exit
;
765 skb
= dev_alloc_skb(NLMSG_GOODSIZE
);
768 err_str
= "Unable to allocate skb";
769 goto send_mapping_info_exit
;
771 spin_lock_irqsave(&iwpm_mapinfo_lock
, flags
);
775 send_mapping_info_unlock
:
776 spin_unlock_irqrestore(&iwpm_mapinfo_lock
, flags
);
777 send_mapping_info_exit
:
779 pr_warn("%s: %s (ret = %d)\n", __func__
, err_str
, ret
);
783 send_nlmsg_done(skb
, nl_client
, iwpm_pid
);
784 return send_mapinfo_num(mapping_num
, nl_client
, iwpm_pid
);
787 int iwpm_mapinfo_available(void)
790 int full_bucket
= 0, i
= 0;
792 spin_lock_irqsave(&iwpm_mapinfo_lock
, flags
);
793 if (iwpm_hash_bucket
) {
794 for (i
= 0; i
< IWPM_MAPINFO_HASH_SIZE
; i
++) {
795 if (!hlist_empty(&iwpm_hash_bucket
[i
])) {
801 spin_unlock_irqrestore(&iwpm_mapinfo_lock
, flags
);
805 int iwpm_send_hello(u8 nl_client
, int iwpm_pid
, u16 abi_version
)
807 struct sk_buff
*skb
= NULL
;
808 struct nlmsghdr
*nlh
;
809 const char *err_str
= "";
812 skb
= iwpm_create_nlmsg(RDMA_NL_IWPM_HELLO
, &nlh
, nl_client
);
814 err_str
= "Unable to create a nlmsg";
815 goto hello_num_error
;
817 nlh
->nlmsg_seq
= iwpm_get_nlmsg_seq();
818 err_str
= "Unable to put attribute of abi_version into nlmsg";
819 ret
= ibnl_put_attr(skb
, nlh
, sizeof(u16
), &abi_version
,
820 IWPM_NLA_HELLO_ABI_VERSION
);
822 goto hello_num_error
;
825 ret
= rdma_nl_unicast(&init_net
, skb
, iwpm_pid
);
828 err_str
= "Unable to send a nlmsg";
829 goto hello_num_error
;
831 pr_debug("%s: Sent hello abi_version = %u\n", __func__
, abi_version
);
834 pr_info("%s: %s\n", __func__
, err_str
);