2 * Shared Memory Communications over RDMA (SMC-R) and RoCE
4 * Basic Transport Functions exploiting Infiniband API
6 * Copyright IBM Corp. 2016
8 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
11 #include <linux/socket.h>
12 #include <linux/if_vlan.h>
13 #include <linux/random.h>
14 #include <linux/workqueue.h>
17 #include <rdma/ib_verbs.h>
26 #include "smc_close.h"
28 #define SMC_LGR_NUM_INCR 256
29 #define SMC_LGR_FREE_DELAY (600 * HZ)
31 static u32 smc_lgr_num
; /* unique link group number */
33 /* Register connection's alert token in our lookup structure.
34 * To use rbtrees we have to implement our own insert core.
35 * Requires @conns_lock
36 * @smc connection to register
37 * Returns 0 on success, != otherwise.
39 static void smc_lgr_add_alert_token(struct smc_connection
*conn
)
41 struct rb_node
**link
, *parent
= NULL
;
42 u32 token
= conn
->alert_token_local
;
44 link
= &conn
->lgr
->conns_all
.rb_node
;
46 struct smc_connection
*cur
= rb_entry(*link
,
47 struct smc_connection
, alert_node
);
50 if (cur
->alert_token_local
> token
)
51 link
= &parent
->rb_left
;
53 link
= &parent
->rb_right
;
55 /* Put the new node there */
56 rb_link_node(&conn
->alert_node
, parent
, link
);
57 rb_insert_color(&conn
->alert_node
, &conn
->lgr
->conns_all
);
60 /* Register connection in link group by assigning an alert token
61 * registered in a search tree.
62 * Requires @conns_lock
63 * Note that '0' is a reserved value and not assigned.
65 static void smc_lgr_register_conn(struct smc_connection
*conn
)
67 struct smc_sock
*smc
= container_of(conn
, struct smc_sock
, conn
);
68 static atomic_t nexttoken
= ATOMIC_INIT(0);
70 /* find a new alert_token_local value not yet used by some connection
73 sock_hold(&smc
->sk
); /* sock_put in smc_lgr_unregister_conn() */
74 while (!conn
->alert_token_local
) {
75 conn
->alert_token_local
= atomic_inc_return(&nexttoken
);
76 if (smc_lgr_find_conn(conn
->alert_token_local
, conn
->lgr
))
77 conn
->alert_token_local
= 0;
79 smc_lgr_add_alert_token(conn
);
80 conn
->lgr
->conns_num
++;
83 /* Unregister connection and reset the alert token of the given connection<
85 static void __smc_lgr_unregister_conn(struct smc_connection
*conn
)
87 struct smc_sock
*smc
= container_of(conn
, struct smc_sock
, conn
);
88 struct smc_link_group
*lgr
= conn
->lgr
;
90 rb_erase(&conn
->alert_node
, &lgr
->conns_all
);
92 conn
->alert_token_local
= 0;
94 sock_put(&smc
->sk
); /* sock_hold in smc_lgr_register_conn() */
97 /* Unregister connection and trigger lgr freeing if applicable
99 static void smc_lgr_unregister_conn(struct smc_connection
*conn
)
101 struct smc_link_group
*lgr
= conn
->lgr
;
104 write_lock_bh(&lgr
->conns_lock
);
105 if (conn
->alert_token_local
) {
107 __smc_lgr_unregister_conn(conn
);
109 write_unlock_bh(&lgr
->conns_lock
);
110 if (reduced
&& !lgr
->conns_num
)
111 schedule_delayed_work(&lgr
->free_work
, SMC_LGR_FREE_DELAY
);
114 static void smc_lgr_free_work(struct work_struct
*work
)
116 struct smc_link_group
*lgr
= container_of(to_delayed_work(work
),
117 struct smc_link_group
,
121 spin_lock_bh(&smc_lgr_list
.lock
);
122 read_lock_bh(&lgr
->conns_lock
);
123 conns
= RB_EMPTY_ROOT(&lgr
->conns_all
);
124 read_unlock_bh(&lgr
->conns_lock
);
125 if (!conns
) { /* number of lgr connections is no longer zero */
126 spin_unlock_bh(&smc_lgr_list
.lock
);
129 list_del_init(&lgr
->list
); /* remove from smc_lgr_list */
130 spin_unlock_bh(&smc_lgr_list
.lock
);
134 /* create a new SMC link group */
135 static int smc_lgr_create(struct smc_sock
*smc
, __be32 peer_in_addr
,
136 struct smc_ib_device
*smcibdev
, u8 ibport
,
137 char *peer_systemid
, unsigned short vlan_id
)
139 struct smc_link_group
*lgr
;
140 struct smc_link
*lnk
;
145 lgr
= kzalloc(sizeof(*lgr
), GFP_KERNEL
);
150 lgr
->role
= smc
->listen_smc
? SMC_SERV
: SMC_CLNT
;
151 lgr
->sync_err
= false;
152 lgr
->daddr
= peer_in_addr
;
153 memcpy(lgr
->peer_systemid
, peer_systemid
, SMC_SYSTEMID_LEN
);
154 lgr
->vlan_id
= vlan_id
;
155 rwlock_init(&lgr
->sndbufs_lock
);
156 rwlock_init(&lgr
->rmbs_lock
);
157 for (i
= 0; i
< SMC_RMBE_SIZES
; i
++) {
158 INIT_LIST_HEAD(&lgr
->sndbufs
[i
]);
159 INIT_LIST_HEAD(&lgr
->rmbs
[i
]);
161 smc_lgr_num
+= SMC_LGR_NUM_INCR
;
162 memcpy(&lgr
->id
, (u8
*)&smc_lgr_num
, SMC_LGR_ID_SIZE
);
163 INIT_DELAYED_WORK(&lgr
->free_work
, smc_lgr_free_work
);
164 lgr
->conns_all
= RB_ROOT
;
166 lnk
= &lgr
->lnk
[SMC_SINGLE_LINK
];
167 /* initialize link */
168 lnk
->smcibdev
= smcibdev
;
169 lnk
->ibport
= ibport
;
170 lnk
->path_mtu
= smcibdev
->pattr
[ibport
- 1].active_mtu
;
171 if (!smcibdev
->initialized
)
172 smc_ib_setup_per_ibdev(smcibdev
);
173 get_random_bytes(rndvec
, sizeof(rndvec
));
174 lnk
->psn_initial
= rndvec
[0] + (rndvec
[1] << 8) + (rndvec
[2] << 16);
175 rc
= smc_wr_alloc_link_mem(lnk
);
178 init_waitqueue_head(&lnk
->wr_tx_wait
);
179 rc
= smc_ib_create_protection_domain(lnk
);
182 rc
= smc_ib_create_queue_pair(lnk
);
185 rc
= smc_wr_create_link(lnk
);
188 init_completion(&lnk
->llc_confirm
);
189 init_completion(&lnk
->llc_confirm_resp
);
192 rwlock_init(&lgr
->conns_lock
);
193 spin_lock_bh(&smc_lgr_list
.lock
);
194 list_add(&lgr
->list
, &smc_lgr_list
.list
);
195 spin_unlock_bh(&smc_lgr_list
.lock
);
199 smc_ib_destroy_queue_pair(lnk
);
201 smc_ib_dealloc_protection_domain(lnk
);
203 smc_wr_free_link_mem(lnk
);
210 static void smc_sndbuf_unuse(struct smc_connection
*conn
)
212 if (conn
->sndbuf_desc
) {
213 conn
->sndbuf_desc
->used
= 0;
214 conn
->sndbuf_size
= 0;
218 static void smc_rmb_unuse(struct smc_connection
*conn
)
220 if (conn
->rmb_desc
) {
221 conn
->rmb_desc
->used
= 0;
226 /* remove a finished connection from its link group */
227 void smc_conn_free(struct smc_connection
*conn
)
229 struct smc_link_group
*lgr
= conn
->lgr
;
233 smc_cdc_tx_dismiss_slots(conn
);
234 smc_lgr_unregister_conn(conn
);
236 smc_sndbuf_unuse(conn
);
239 static void smc_link_clear(struct smc_link
*lnk
)
242 smc_ib_modify_qp_reset(lnk
);
243 smc_wr_free_link(lnk
);
244 smc_ib_destroy_queue_pair(lnk
);
245 smc_ib_dealloc_protection_domain(lnk
);
246 smc_wr_free_link_mem(lnk
);
249 static void smc_lgr_free_sndbufs(struct smc_link_group
*lgr
)
251 struct smc_buf_desc
*sndbuf_desc
, *bf_desc
;
254 for (i
= 0; i
< SMC_RMBE_SIZES
; i
++) {
255 list_for_each_entry_safe(sndbuf_desc
, bf_desc
, &lgr
->sndbufs
[i
],
257 list_del(&sndbuf_desc
->list
);
258 smc_ib_buf_unmap(lgr
->lnk
[SMC_SINGLE_LINK
].smcibdev
,
259 smc_uncompress_bufsize(i
),
260 sndbuf_desc
, DMA_TO_DEVICE
);
261 kfree(sndbuf_desc
->cpu_addr
);
267 static void smc_lgr_free_rmbs(struct smc_link_group
*lgr
)
269 struct smc_buf_desc
*rmb_desc
, *bf_desc
;
270 struct smc_link
*lnk
= &lgr
->lnk
[SMC_SINGLE_LINK
];
273 for (i
= 0; i
< SMC_RMBE_SIZES
; i
++) {
274 list_for_each_entry_safe(rmb_desc
, bf_desc
, &lgr
->rmbs
[i
],
276 list_del(&rmb_desc
->list
);
277 smc_ib_buf_unmap(lnk
->smcibdev
,
278 smc_uncompress_bufsize(i
),
279 rmb_desc
, DMA_FROM_DEVICE
);
280 kfree(rmb_desc
->cpu_addr
);
286 /* remove a link group */
287 void smc_lgr_free(struct smc_link_group
*lgr
)
289 smc_lgr_free_rmbs(lgr
);
290 smc_lgr_free_sndbufs(lgr
);
291 smc_link_clear(&lgr
->lnk
[SMC_SINGLE_LINK
]);
295 /* terminate linkgroup abnormally */
296 void smc_lgr_terminate(struct smc_link_group
*lgr
)
298 struct smc_connection
*conn
;
299 struct smc_sock
*smc
;
300 struct rb_node
*node
;
302 spin_lock_bh(&smc_lgr_list
.lock
);
303 if (list_empty(&lgr
->list
)) {
304 /* termination already triggered */
305 spin_unlock_bh(&smc_lgr_list
.lock
);
308 /* do not use this link group for new connections */
309 list_del_init(&lgr
->list
);
310 spin_unlock_bh(&smc_lgr_list
.lock
);
312 write_lock_bh(&lgr
->conns_lock
);
313 node
= rb_first(&lgr
->conns_all
);
315 conn
= rb_entry(node
, struct smc_connection
, alert_node
);
316 smc
= container_of(conn
, struct smc_sock
, conn
);
318 __smc_lgr_unregister_conn(conn
);
319 schedule_work(&conn
->close_work
);
321 node
= rb_first(&lgr
->conns_all
);
323 write_unlock_bh(&lgr
->conns_lock
);
326 /* Determine vlan of internal TCP socket.
327 * @vlan_id: address to store the determined vlan id into
329 static int smc_vlan_by_tcpsk(struct socket
*clcsock
, unsigned short *vlan_id
)
331 struct dst_entry
*dst
= sk_dst_get(clcsock
->sk
);
344 if (is_vlan_dev(dst
->dev
))
345 *vlan_id
= vlan_dev_vlan_id(dst
->dev
);
353 /* determine the link gid matching the vlan id of the link group */
354 static int smc_link_determine_gid(struct smc_link_group
*lgr
)
356 struct smc_link
*lnk
= &lgr
->lnk
[SMC_SINGLE_LINK
];
357 struct ib_gid_attr gattr
;
362 lnk
->gid
= lnk
->smcibdev
->gid
[lnk
->ibport
- 1];
366 for (i
= 0; i
< lnk
->smcibdev
->pattr
[lnk
->ibport
- 1].gid_tbl_len
;
368 if (ib_query_gid(lnk
->smcibdev
->ibdev
, lnk
->ibport
, i
, &gid
,
372 (vlan_dev_vlan_id(gattr
.ndev
) == lgr
->vlan_id
)) {
380 /* create a new SMC connection (and a new link group if necessary) */
381 int smc_conn_create(struct smc_sock
*smc
, __be32 peer_in_addr
,
382 struct smc_ib_device
*smcibdev
, u8 ibport
,
383 struct smc_clc_msg_local
*lcl
, int srv_first_contact
)
385 struct smc_connection
*conn
= &smc
->conn
;
386 struct smc_link_group
*lgr
;
387 unsigned short vlan_id
;
388 enum smc_lgr_role role
;
389 int local_contact
= SMC_FIRST_CONTACT
;
392 role
= smc
->listen_smc
? SMC_SERV
: SMC_CLNT
;
393 rc
= smc_vlan_by_tcpsk(smc
->clcsock
, &vlan_id
);
397 if ((role
== SMC_CLNT
) && srv_first_contact
)
398 /* create new link group as well */
401 /* determine if an existing link group can be reused */
402 spin_lock_bh(&smc_lgr_list
.lock
);
403 list_for_each_entry(lgr
, &smc_lgr_list
.list
, list
) {
404 write_lock_bh(&lgr
->conns_lock
);
405 if (!memcmp(lgr
->peer_systemid
, lcl
->id_for_peer
,
407 !memcmp(lgr
->lnk
[SMC_SINGLE_LINK
].peer_gid
, &lcl
->gid
,
409 !memcmp(lgr
->lnk
[SMC_SINGLE_LINK
].peer_mac
, lcl
->mac
,
412 (lgr
->role
== role
) &&
413 (lgr
->vlan_id
== vlan_id
) &&
414 ((role
== SMC_CLNT
) ||
415 (lgr
->conns_num
< SMC_RMBS_PER_LGR_MAX
))) {
416 /* link group found */
417 local_contact
= SMC_REUSE_CONTACT
;
419 smc_lgr_register_conn(conn
); /* add smc conn to lgr */
420 write_unlock_bh(&lgr
->conns_lock
);
423 write_unlock_bh(&lgr
->conns_lock
);
425 spin_unlock_bh(&smc_lgr_list
.lock
);
427 if (role
== SMC_CLNT
&& !srv_first_contact
&&
428 (local_contact
== SMC_FIRST_CONTACT
)) {
429 /* Server reuses a link group, but Client wants to start
431 * send out_of_sync decline, reason synchr. error
437 if (local_contact
== SMC_FIRST_CONTACT
) {
438 rc
= smc_lgr_create(smc
, peer_in_addr
, smcibdev
, ibport
,
439 lcl
->id_for_peer
, vlan_id
);
442 smc_lgr_register_conn(conn
); /* add smc conn to lgr */
443 rc
= smc_link_determine_gid(conn
->lgr
);
445 conn
->local_tx_ctrl
.common
.type
= SMC_CDC_MSG_TYPE
;
446 conn
->local_tx_ctrl
.len
= sizeof(struct smc_cdc_msg
);
447 #ifndef KERNEL_HAS_ATOMIC64
448 spin_lock_init(&conn
->acurs_lock
);
452 return rc
? rc
: local_contact
;
455 /* try to reuse a sndbuf description slot of the sndbufs list for a certain
456 * buf_size; if not available, return NULL
459 struct smc_buf_desc
*smc_sndbuf_get_slot(struct smc_link_group
*lgr
,
460 int compressed_bufsize
)
462 struct smc_buf_desc
*sndbuf_slot
;
464 read_lock_bh(&lgr
->sndbufs_lock
);
465 list_for_each_entry(sndbuf_slot
, &lgr
->sndbufs
[compressed_bufsize
],
467 if (cmpxchg(&sndbuf_slot
->used
, 0, 1) == 0) {
468 read_unlock_bh(&lgr
->sndbufs_lock
);
472 read_unlock_bh(&lgr
->sndbufs_lock
);
476 /* try to reuse an rmb description slot of the rmbs list for a certain
477 * rmbe_size; if not available, return NULL
480 struct smc_buf_desc
*smc_rmb_get_slot(struct smc_link_group
*lgr
,
481 int compressed_bufsize
)
483 struct smc_buf_desc
*rmb_slot
;
485 read_lock_bh(&lgr
->rmbs_lock
);
486 list_for_each_entry(rmb_slot
, &lgr
->rmbs
[compressed_bufsize
],
488 if (cmpxchg(&rmb_slot
->used
, 0, 1) == 0) {
489 read_unlock_bh(&lgr
->rmbs_lock
);
493 read_unlock_bh(&lgr
->rmbs_lock
);
497 /* one of the conditions for announcing a receiver's current window size is
498 * that it "results in a minimum increase in the window size of 10% of the
499 * receive buffer space" [RFC7609]
501 static inline int smc_rmb_wnd_update_limit(int rmbe_size
)
503 return min_t(int, rmbe_size
/ 10, SOCK_MIN_SNDBUF
/ 2);
506 /* create the tx buffer for an SMC socket */
507 int smc_sndbuf_create(struct smc_sock
*smc
)
509 struct smc_connection
*conn
= &smc
->conn
;
510 struct smc_link_group
*lgr
= conn
->lgr
;
511 int tmp_bufsize
, tmp_bufsize_short
;
512 struct smc_buf_desc
*sndbuf_desc
;
515 /* use socket send buffer size (w/o overhead) as start value */
516 for (tmp_bufsize_short
= smc_compress_bufsize(smc
->sk
.sk_sndbuf
/ 2);
517 tmp_bufsize_short
>= 0; tmp_bufsize_short
--) {
518 tmp_bufsize
= smc_uncompress_bufsize(tmp_bufsize_short
);
519 /* check for reusable sndbuf_slot in the link group */
520 sndbuf_desc
= smc_sndbuf_get_slot(lgr
, tmp_bufsize_short
);
522 memset(sndbuf_desc
->cpu_addr
, 0, tmp_bufsize
);
523 break; /* found reusable slot */
525 /* try to alloc a new send buffer */
526 sndbuf_desc
= kzalloc(sizeof(*sndbuf_desc
), GFP_KERNEL
);
528 break; /* give up with -ENOMEM */
529 sndbuf_desc
->cpu_addr
= kzalloc(tmp_bufsize
,
530 GFP_KERNEL
| __GFP_NOWARN
|
533 if (!sndbuf_desc
->cpu_addr
) {
536 /* if send buffer allocation has failed,
541 rc
= smc_ib_buf_map(lgr
->lnk
[SMC_SINGLE_LINK
].smcibdev
,
542 tmp_bufsize
, sndbuf_desc
,
545 kfree(sndbuf_desc
->cpu_addr
);
548 continue; /* if mapping failed, try smaller one */
550 sndbuf_desc
->used
= 1;
551 write_lock_bh(&lgr
->sndbufs_lock
);
552 list_add(&sndbuf_desc
->list
,
553 &lgr
->sndbufs
[tmp_bufsize_short
]);
554 write_unlock_bh(&lgr
->sndbufs_lock
);
557 if (sndbuf_desc
&& sndbuf_desc
->cpu_addr
) {
558 conn
->sndbuf_desc
= sndbuf_desc
;
559 conn
->sndbuf_size
= tmp_bufsize
;
560 smc
->sk
.sk_sndbuf
= tmp_bufsize
* 2;
561 atomic_set(&conn
->sndbuf_space
, tmp_bufsize
);
568 /* create the RMB for an SMC socket (even though the SMC protocol
569 * allows more than one RMB-element per RMB, the Linux implementation
570 * uses just one RMB-element per RMB, i.e. uses an extra RMB for every
571 * connection in a link group
573 int smc_rmb_create(struct smc_sock
*smc
)
575 struct smc_connection
*conn
= &smc
->conn
;
576 struct smc_link_group
*lgr
= conn
->lgr
;
577 int tmp_bufsize
, tmp_bufsize_short
;
578 struct smc_buf_desc
*rmb_desc
;
581 /* use socket recv buffer size (w/o overhead) as start value */
582 for (tmp_bufsize_short
= smc_compress_bufsize(smc
->sk
.sk_rcvbuf
/ 2);
583 tmp_bufsize_short
>= 0; tmp_bufsize_short
--) {
584 tmp_bufsize
= smc_uncompress_bufsize(tmp_bufsize_short
);
585 /* check for reusable rmb_slot in the link group */
586 rmb_desc
= smc_rmb_get_slot(lgr
, tmp_bufsize_short
);
588 memset(rmb_desc
->cpu_addr
, 0, tmp_bufsize
);
589 break; /* found reusable slot */
591 /* try to alloc a new RMB */
592 rmb_desc
= kzalloc(sizeof(*rmb_desc
), GFP_KERNEL
);
594 break; /* give up with -ENOMEM */
595 rmb_desc
->cpu_addr
= kzalloc(tmp_bufsize
,
596 GFP_KERNEL
| __GFP_NOWARN
|
599 if (!rmb_desc
->cpu_addr
) {
602 /* if RMB allocation has failed,
607 rc
= smc_ib_buf_map(lgr
->lnk
[SMC_SINGLE_LINK
].smcibdev
,
608 tmp_bufsize
, rmb_desc
,
611 kfree(rmb_desc
->cpu_addr
);
614 continue; /* if mapping failed, try smaller one */
616 rmb_desc
->rkey
[SMC_SINGLE_LINK
] =
617 lgr
->lnk
[SMC_SINGLE_LINK
].roce_pd
->unsafe_global_rkey
;
619 write_lock_bh(&lgr
->rmbs_lock
);
620 list_add(&rmb_desc
->list
,
621 &lgr
->rmbs
[tmp_bufsize_short
]);
622 write_unlock_bh(&lgr
->rmbs_lock
);
625 if (rmb_desc
&& rmb_desc
->cpu_addr
) {
626 conn
->rmb_desc
= rmb_desc
;
627 conn
->rmbe_size
= tmp_bufsize
;
628 conn
->rmbe_size_short
= tmp_bufsize_short
;
629 smc
->sk
.sk_rcvbuf
= tmp_bufsize
* 2;
630 atomic_set(&conn
->bytes_to_rcv
, 0);
631 conn
->rmbe_update_limit
= smc_rmb_wnd_update_limit(tmp_bufsize
);
638 static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group
*lgr
)
642 for_each_clear_bit(i
, lgr
->rtokens_used_mask
, SMC_RMBS_PER_LGR_MAX
) {
643 if (!test_and_set_bit(i
, lgr
->rtokens_used_mask
))
649 /* save rkey and dma_addr received from peer during clc handshake */
650 int smc_rmb_rtoken_handling(struct smc_connection
*conn
,
651 struct smc_clc_msg_accept_confirm
*clc
)
653 u64 dma_addr
= be64_to_cpu(clc
->rmb_dma_addr
);
654 struct smc_link_group
*lgr
= conn
->lgr
;
655 u32 rkey
= ntohl(clc
->rmb_rkey
);
658 for (i
= 0; i
< SMC_RMBS_PER_LGR_MAX
; i
++) {
659 if ((lgr
->rtokens
[i
][SMC_SINGLE_LINK
].rkey
== rkey
) &&
660 (lgr
->rtokens
[i
][SMC_SINGLE_LINK
].dma_addr
== dma_addr
) &&
661 test_bit(i
, lgr
->rtokens_used_mask
)) {
662 conn
->rtoken_idx
= i
;
666 conn
->rtoken_idx
= smc_rmb_reserve_rtoken_idx(lgr
);
667 if (conn
->rtoken_idx
< 0)
668 return conn
->rtoken_idx
;
669 lgr
->rtokens
[conn
->rtoken_idx
][SMC_SINGLE_LINK
].rkey
= rkey
;
670 lgr
->rtokens
[conn
->rtoken_idx
][SMC_SINGLE_LINK
].dma_addr
= dma_addr
;