Skip to content

Commit 4d794fa

Browse files
committed
librdmacm: Fix SOCK_STREAM and SOCK_DGRAM types
Updated type checks to identify socket types even when additional flags are present in the type field. Changed the comparison to use bitwise AND for more accurate detection. Signed-off-by: Batsheva Black <bblack@nvidia.com>
1 parent acfe028 commit 4d794fa

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

librdmacm/rsocket.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ struct ds_qp {
318318
};
319319

320320
struct rsocket {
321-
int type;
321+
int type; /* SOCK_STREAM or SOCK_DGRAM only; flags in fd_flags/fs_flags */
322322
int index;
323323
fastlock_t slock;
324324
fastlock_t rlock;
@@ -702,6 +702,7 @@ static struct rsocket *rs_alloc(struct rsocket *inherited_rs, int type)
702702
return NULL;
703703

704704
rs->type = type;
705+
705706
rs->index = -1;
706707
if (type == SOCK_DGRAM) {
707708
rs->udp_sock = -1;
@@ -1247,19 +1248,23 @@ int rsocket(int domain, int type, int protocol)
12471248
{
12481249
struct rsocket *rs;
12491250
int index, ret;
1251+
int socket_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
12501252

12511253
if ((domain != AF_INET && domain != AF_INET6 && domain != AF_IB) ||
1252-
((type != SOCK_STREAM) && (type != SOCK_DGRAM)) ||
1253-
(type == SOCK_STREAM && protocol && protocol != IPPROTO_TCP) ||
1254-
(type == SOCK_DGRAM && protocol && protocol != IPPROTO_UDP))
1254+
(socket_type != SOCK_STREAM && socket_type != SOCK_DGRAM) ||
1255+
((socket_type == SOCK_STREAM) && protocol && protocol != IPPROTO_TCP) ||
1256+
((socket_type == SOCK_DGRAM) && protocol && protocol != IPPROTO_UDP))
12551257
return ERR(ENOTSUP);
12561258

12571259
rs_configure();
1258-
rs = rs_alloc(NULL, type);
1260+
rs = rs_alloc(NULL, socket_type);
12591261
if (!rs)
12601262
return ERR(ENOMEM);
12611263

1262-
if (type == SOCK_STREAM) {
1264+
rs->fd_flags = (type & SOCK_CLOEXEC) ? FD_CLOEXEC : 0;
1265+
rs->fs_flags = (type & SOCK_NONBLOCK) ? O_NONBLOCK : 0;
1266+
1267+
if (socket_type == SOCK_STREAM) {
12631268
ret = rdma_create_id(NULL, &rs->cm_id, rs, RDMA_PS_TCP);
12641269
if (ret)
12651270
goto err;
@@ -1270,7 +1275,6 @@ int rsocket(int domain, int type, int protocol)
12701275
ret = ds_init(rs, domain);
12711276
if (ret)
12721277
goto err;
1273-
12741278
index = rs->udp_sock;
12751279
}
12761280

@@ -1363,6 +1367,8 @@ static void rs_accept(struct rsocket *rs)
13631367
if (!new_rs)
13641368
goto err;
13651369
new_rs->cm_id = cm_id;
1370+
new_rs->fd_flags = rs->fd_flags;
1371+
new_rs->fs_flags = rs->fs_flags;
13661372

13671373
ret = rs_insert(new_rs, new_rs->cm_id->channel->fd);
13681374
if (ret < 0)
@@ -3712,7 +3718,7 @@ int rsetsockopt(int socket, int level, int optname,
37123718
rs = idm_lookup(&idm, socket);
37133719
if (!rs)
37143720
return ERR(EBADF);
3715-
if (rs->type == SOCK_DGRAM && level != SOL_RDMA) {
3721+
if ((rs->type == SOCK_DGRAM) && level != SOL_RDMA) {
37163722
ret = setsockopt(rs->udp_sock, level, optname, optval, optlen);
37173723
if (ret)
37183724
return ret;
@@ -3735,8 +3741,8 @@ int rsetsockopt(int socket, int level, int optname,
37353741
opt_on = *(int *) optval;
37363742
break;
37373743
case SO_RCVBUF:
3738-
if ((rs->type == SOCK_STREAM && !rs->rbuf) ||
3739-
(rs->type == SOCK_DGRAM && !rs->qp_list))
3744+
if (((rs->type == SOCK_STREAM) && !rs->rbuf) ||
3745+
((rs->type == SOCK_DGRAM) && !rs->qp_list))
37403746
rs->rbuf_size = (*(uint32_t *) optval) << 1;
37413747
ret = 0;
37423748
break;
@@ -4810,7 +4816,7 @@ uint32_t epoll_rs(int fd, uint32_t events)
48104816
struct rsocket *rs = idm_lookup(&idm, fd);
48114817

48124818
check_cq:
4813-
if ((rs->type & SOCK_STREAM) && ((rs->state & rs_connected) ||
4819+
if ((rs->type == SOCK_STREAM) && ((rs->state & rs_connected) ||
48144820
(rs->state == rs_disconnected) || (rs->state & rs_error))) {
48154821
rs_process_cq(rs, 1, rs_poll_all);
48164822

@@ -4826,7 +4832,7 @@ uint32_t epoll_rs(int fd, uint32_t events)
48264832
}
48274833

48284834
return revents;
4829-
} else if (rs->type & SOCK_DGRAM) {
4835+
} else if (rs->type == SOCK_DGRAM) {
48304836
ds_process_cqs(rs, 1, rs_poll_all);
48314837

48324838
if ((events & EPOLLIN) && rs_have_rdata(rs))

0 commit comments

Comments
 (0)