@@ -49,6 +49,7 @@ extern bool g_skip_rdma_init;
4949
5050DEFINE_int32 (rdma_sq_size, 128 , " SQ size for RDMA" );
5151DEFINE_int32 (rdma_rq_size, 128 , " RQ size for RDMA" );
52+ DEFINE_bool (rdma_send_zerocopy, true , " Enable zerocopy for send side" );
5253DEFINE_bool (rdma_recv_zerocopy, true , " Enable zerocopy for receive side" );
5354DEFINE_int32 (rdma_zerocopy_min_size, 512 , " The minimal size for receive zerocopy" );
5455DEFINE_string (rdma_recv_block_type, " default" , " Default size type for recv WR: "
@@ -801,29 +802,45 @@ ssize_t RdmaEndpoint::CutFromIOBufList(butil::IOBuf** from, size_t ndata) {
801802 wr.sg_list = sglist;
802803 wr.opcode = IBV_WR_SEND_WITH_IMM;
803804
804- RdmaIOBuf* data = (RdmaIOBuf*)from[current];
805805 size_t sge_index = 0 ;
806806 while (sge_index < (uint32_t )max_sge &&
807807 this_len < _remote_recv_block_size) {
808- if (data ->size () == 0 ) {
808+ if (from[current] ->size () == 0 ) {
809809 // The current IOBuf is empty, find next one
810810 ++current;
811811 if (current == ndata) {
812812 break ;
813813 }
814- data = (RdmaIOBuf*)from[current];
815814 continue ;
816815 }
817816
818- ssize_t len = data->cut_into_sglist_and_iobuf (
819- sglist, &sge_index, to, max_sge,
820- _remote_recv_block_size - this_len);
821- if (len < 0 ) {
822- return -1 ;
817+ ssize_t len = 0 ;
818+ if (FLAGS_rdma_send_zerocopy) {
819+ ssize_t len = ((RdmaIOBuf*)from[current])->cut_into_sglist_and_iobuf (
820+ sglist, &sge_index, to, max_sge,
821+ _remote_recv_block_size - this_len);
822+ if (len < 0 ) {
823+ return -1 ;
824+ }
825+ this_len += len;
826+ total_len += len;
827+ } else {
828+ len = _remote_recv_block_size - this_len;
829+ void * buf = AllocBlock (len);
830+ if (!buf) {
831+ return -1 ;
832+ }
833+ len = from[current]->copy_to (buf, len);
834+ from[current]->cutn (to, len);
835+ sglist[sge_index].length = len;
836+ sglist[sge_index].addr = (uint64_t )buf;
837+ sglist[sge_index].lkey = GetLKey (buf);
838+ ++sge_index;
839+ this_len += len;
840+ total_len += len;
841+ _sbuf_data[_sq_current] = buf;
842+ break ;
823843 }
824- CHECK (len > 0 );
825- this_len += len;
826- total_len += len;
827844 }
828845 if (this_len == 0 ) {
829846 continue ;
@@ -951,6 +968,9 @@ ssize_t RdmaEndpoint::HandleCompletion(ibv_wc& wc) {
951968 uint32_t acks = butil::NetToHost32 (wc.imm_data );
952969 uint32_t num = acks;
953970 while (num > 0 ) {
971+ if (!FLAGS_rdma_send_zerocopy) {
972+ DeallocBlock (_sbuf_data[_sq_sent]);
973+ }
954974 _sbuf[_sq_sent++].clear ();
955975 if (_sq_sent == _sq_size - RESERVED_WR_NUM) {
956976 _sq_sent = 0 ;
@@ -1139,6 +1159,10 @@ int RdmaEndpoint::AllocateResources() {
11391159 if (_rbuf.size () != _rq_size) {
11401160 return -1 ;
11411161 }
1162+ _sbuf_data.resize (_sq_size, NULL );
1163+ if (_sbuf_data.size () != _sq_size) {
1164+ return -1 ;
1165+ }
11421166 _rbuf_data.resize (_rq_size, NULL );
11431167 if (_rbuf_data.size () != _rq_size) {
11441168 return -1 ;
0 commit comments