2 * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved.
4 * This program is free software; you may redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
9 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
10 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
11 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
12 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
13 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
14 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18 #include <linux/bitmap.h>
19 #include <linux/file.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <net/inet_sock.h>
24 #include "usnic_transport.h"
25 #include "usnic_log.h"
28 static unsigned long *roce_bitmap;
29 static u16 roce_next_port = 1;
30 #define ROCE_BITMAP_SZ ((1 << (8 /*CHAR_BIT*/ * sizeof(u16)))/8 /*CHAR BIT*/)
31 static DEFINE_SPINLOCK(roce_bitmap_lock);
33 const char *usnic_transport_to_str(enum usnic_transport_type type)
36 case USNIC_TRANSPORT_UNKNOWN:
38 case USNIC_TRANSPORT_ROCE_CUSTOM:
40 case USNIC_TRANSPORT_IPV4_UDP:
42 case USNIC_TRANSPORT_MAX:
49 int usnic_transport_sock_to_str(char *buf, int buf_sz,
57 memset(buf, 0, buf_sz);
58 err = usnic_transport_sock_get_addr(sock, &proto, &addr, &port);
63 return scnprintf(buf, buf_sz, "Proto:%u Addr:%pI4 Port:%hu",
68 * reserve a port number. if "0" specified, we will try to pick one
69 * starting at roce_next_port. roce_next_port will take on the values
72 u16 usnic_transport_rsrv_port(enum usnic_transport_type type, u16 port_num)
74 if (type == USNIC_TRANSPORT_ROCE_CUSTOM) {
75 spin_lock(&roce_bitmap_lock);
77 port_num = bitmap_find_next_zero_area(roce_bitmap,
79 roce_next_port /* start */,
82 roce_next_port = (port_num & 4095) + 1;
83 } else if (test_bit(port_num, roce_bitmap)) {
84 usnic_err("Failed to allocate port for %s\n",
85 usnic_transport_to_str(type));
86 spin_unlock(&roce_bitmap_lock);
89 bitmap_set(roce_bitmap, port_num, 1);
90 spin_unlock(&roce_bitmap_lock);
92 usnic_err("Failed to allocate port - transport %s unsupported\n",
93 usnic_transport_to_str(type));
97 usnic_dbg("Allocating port %hu for %s\n", port_num,
98 usnic_transport_to_str(type));
105 void usnic_transport_unrsrv_port(enum usnic_transport_type type, u16 port_num)
107 if (type == USNIC_TRANSPORT_ROCE_CUSTOM) {
108 spin_lock(&roce_bitmap_lock);
110 usnic_err("Unreserved unvalid port num 0 for %s\n",
111 usnic_transport_to_str(type));
112 goto out_roce_custom;
115 if (!test_bit(port_num, roce_bitmap)) {
116 usnic_err("Unreserving invalid %hu for %s\n",
118 usnic_transport_to_str(type));
119 goto out_roce_custom;
121 bitmap_clear(roce_bitmap, port_num, 1);
122 usnic_dbg("Freeing port %hu for %s\n", port_num,
123 usnic_transport_to_str(type));
125 spin_unlock(&roce_bitmap_lock);
127 usnic_err("Freeing invalid port %hu for %d\n", port_num, type);
131 struct socket *usnic_transport_get_socket(int sock_fd)
137 /* sockfd_lookup will internally do a fget */
138 sock = sockfd_lookup(sock_fd, &err);
140 usnic_err("Unable to lookup socket for fd %d with err %d\n",
142 return ERR_PTR(-ENOENT);
145 usnic_transport_sock_to_str(buf, sizeof(buf), sock);
146 usnic_dbg("Get sock %s\n", buf);
151 void usnic_transport_put_socket(struct socket *sock)
155 usnic_transport_sock_to_str(buf, sizeof(buf), sock);
156 usnic_dbg("Put sock %s\n", buf);
160 int usnic_transport_sock_get_addr(struct socket *sock, int *proto,
161 uint32_t *addr, uint16_t *port)
165 struct sockaddr_in sock_addr;
167 err = sock->ops->getname(sock,
168 (struct sockaddr *)&sock_addr,
173 if (sock_addr.sin_family != AF_INET)
177 *proto = sock->sk->sk_protocol;
179 *port = ntohs(((struct sockaddr_in *)&sock_addr)->sin_port);
181 *addr = ntohl(((struct sockaddr_in *)
182 &sock_addr)->sin_addr.s_addr);
188 int usnic_transport_init(void)
190 roce_bitmap = kzalloc(ROCE_BITMAP_SZ, GFP_KERNEL);
192 usnic_err("Failed to allocate bit map");
196 /* Do not ever allocate bit 0, hence set it here */
197 bitmap_set(roce_bitmap, 0, 1);
201 void usnic_transport_fini(void)