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);
62 return scnprintf(buf, buf_sz, "Proto:%u Addr:%pI4h Port:%hu",
67 * reserve a port number. if "0" specified, we will try to pick one
68 * starting at roce_next_port. roce_next_port will take on the values
71 u16 usnic_transport_rsrv_port(enum usnic_transport_type type, u16 port_num)
73 if (type == USNIC_TRANSPORT_ROCE_CUSTOM) {
74 spin_lock(&roce_bitmap_lock);
76 port_num = bitmap_find_next_zero_area(roce_bitmap,
78 roce_next_port /* start */,
81 roce_next_port = (port_num & 4095) + 1;
82 } else if (test_bit(port_num, roce_bitmap)) {
83 usnic_err("Failed to allocate port for %s\n",
84 usnic_transport_to_str(type));
85 spin_unlock(&roce_bitmap_lock);
88 bitmap_set(roce_bitmap, port_num, 1);
89 spin_unlock(&roce_bitmap_lock);
91 usnic_err("Failed to allocate port - transport %s unsupported\n",
92 usnic_transport_to_str(type));
96 usnic_dbg("Allocating port %hu for %s\n", port_num,
97 usnic_transport_to_str(type));
104 void usnic_transport_unrsrv_port(enum usnic_transport_type type, u16 port_num)
106 if (type == USNIC_TRANSPORT_ROCE_CUSTOM) {
107 spin_lock(&roce_bitmap_lock);
109 usnic_err("Unreserved unvalid port num 0 for %s\n",
110 usnic_transport_to_str(type));
111 goto out_roce_custom;
114 if (!test_bit(port_num, roce_bitmap)) {
115 usnic_err("Unreserving invalid %hu for %s\n",
117 usnic_transport_to_str(type));
118 goto out_roce_custom;
120 bitmap_clear(roce_bitmap, port_num, 1);
121 usnic_dbg("Freeing port %hu for %s\n", port_num,
122 usnic_transport_to_str(type));
124 spin_unlock(&roce_bitmap_lock);
126 usnic_err("Freeing invalid port %hu for %d\n", port_num, type);
130 struct socket *usnic_transport_get_socket(int sock_fd)
136 /* sockfd_lookup will internally do a fget */
137 sock = sockfd_lookup(sock_fd, &err);
139 usnic_err("Unable to lookup socket for fd %d with err %d\n",
141 return ERR_PTR(-ENOENT);
144 usnic_transport_sock_to_str(buf, sizeof(buf), sock);
145 usnic_dbg("Get sock %s\n", buf);
150 void usnic_transport_put_socket(struct socket *sock)
154 usnic_transport_sock_to_str(buf, sizeof(buf), sock);
155 usnic_dbg("Put sock %s\n", buf);
159 int usnic_transport_sock_get_addr(struct socket *sock, int *proto,
160 uint32_t *addr, uint16_t *port)
164 struct sockaddr_in sock_addr;
166 err = sock->ops->getname(sock,
167 (struct sockaddr *)&sock_addr,
172 if (sock_addr.sin_family != AF_INET)
176 *proto = sock->sk->sk_protocol;
178 *port = ntohs(((struct sockaddr_in *)&sock_addr)->sin_port);
180 *addr = ntohl(((struct sockaddr_in *)
181 &sock_addr)->sin_addr.s_addr);
186 int usnic_transport_init(void)
188 roce_bitmap = kzalloc(ROCE_BITMAP_SZ, GFP_KERNEL);
190 usnic_err("Failed to allocate bit map");
194 /* Do not ever allocate bit 0, hence set it here */
195 bitmap_set(roce_bitmap, 0, 1);
199 void usnic_transport_fini(void)