Zero-Copy Receive Example #1

Onload User Guide (UG1586)

Document ID
UG1586
Release Date
2023-07-31
Revision
1.2 English
Figure 1. Zero-Copy Receive Example #1
struct onload_zc_recv_args args;
struct zc_recv_state state;
int rc;
state.bytes = bytes_to_wait_for;
/* Easy way to set msg_control* and msg_name* to zero */
memset(&args.msg, 0, sizeof(args.msg));
args.cb = &zc_recv_callback;
args.user_ptr = &state;
args.flags = ONLOAD_ZC_RECV_OS_INLINE;
rc = onload_zc_recv(fd, &args);
//---
enum onload_zc_callback_rc
zc_recv_callback(struct onload_zc_recv_args *args, int flags)
{
  int i;
  struct zc_recv_state *state = args->user_ptr;
  for( i = 0; i < args->msg.msghdr.msg_iovlen; ++i ) {
    printf("zc callback iov %d: %p, %d", i,
           args->msg.iov[i].iov_base,
           args->msg.iov[i].iov_len);
    state->bytes -= args->msg.iov[i].iov_len;
  }
  if( state->bytes <= 0 ) return ONLOAD_ZC_TERMINATE;
  else return ONLOAD_ZC_CONTINUE;
}