当前位置:网站首页>Zero‐Copy API
Zero‐Copy API
2022-06-22 10:25:00 【raindayinrain】
#include <onload/extensions_zc.h
1.1.Zero‐Copy Data Buffers
/* A zc_iovec describes a single buffer */struct onload_zc_iovec {void* iov_base; /* Address within buffer */size_t iov_len; /* Length of data */onload_zc_handle buf; /* (opaque) buffer handle */unsigned iov_flags; /* Not currently used */};/* A msg describes array of iovecs that make up datagram */struct onload_zc_msg {struct onload_zc_iovec* iov; /* Array of buffers */struct msghdr msghdr; /* Message metadata */};/* An mmsg describes a message, the socket, and its result */struct onload_zc_mmsg {struct onload_zc_msg msg; /* Message */int rc; /* Result of send operation */int fd; /* socket to send on */};
1.2.Zero‐Copy TCP Send Overview
Figure 31 illustrates the difference between the normal TCP transmit method and

2.3.Zero‐Copy TCP Send
The zero‐copy send API supports the sending of multiple messages to different
int onload_zc_send(struct onload_zc_mmsg* msgs, int mlen, int flags);
int onload_zc_alloc_buffers(int fd,
struct onload_zc_iovec* iovecs,
int iovecs_len,
onload_zc_buffer_type_flags flags);
int onload_zc_release_buffers(int fd,
onload_zc_handle* bufs,
int bufs_len);
2.4.Zero‐Copy Send ‐ Single Message, Single Buffer
struct onload_zc_iovec iovec;
struct onload_zc_mmsg mmsg;
rc = onload_zc_alloc_buffers(fd, &iovec, 1, ONLOAD_ZC_BUFFER_HDR_TCP);
assert(rc == O);
assert(my_data_len <= iovec.iov_len);
memcpy(iovec.iov_base, my_data, my_data_len);
iovec.iov_len = my_data_len;
mmsg.fd = fd;
mmsg.msg.iov = &iovec;
mmsg.msg.msghdr.msg_iovlen = 1;
rc = onload_zc_send(&mmsg, 1, 0);
if( rc <= 0) {
/* Probably application bug */
return rc;
} else {
/* Only one message, so rc should be 1 */
assert(rc == 1);
/* rc == 1 so we can look at the first (only) mmsg.rc */
if( mmsg.rc < 0 )
/* Error sending message */
onload_zc_release_buffers(fd, &iovec.buf, 1);
else
/* Message sent, single msg, single iovec so
* shouldn't worry about partial sends */
assert(mmsg.rc == my_data_len);
}2.5.Zero‐Copy Send ‐ Multiple Message, Multiple Buffers
#define N_BUFFERS 2
#define N_MSGS 2
struct onload_zc_iovec iovec[N_MSGS][N_BUFFERS];
struct onload_zc_mmsg mmsg[N_MSGS];
for( i = 0; i < N_MSGS; ++i ) {
rc = onload_zc_alloc_buffers(fd, iovec[i], N_BUFFERS, ONLOAD_ZC_BUFFER_HDR_TCP);
assert(rc == 0);
/* TODO store data in iovec[i][j].iov_base,
* set iovec[i][j]iov_len */
mmsg[i]fd = fd; /* Could be different for each message */
mmsg[i].iov = iovec[i];
mmsg[i].msg.msghdr.msg_iovlen = N_BUFFERS;
}
rc = onload_zc_send(mmsg, N_MSGS, 0);
if( rc <= 0 ) {
/* Probably application bug */
return rc;
} else {
for( i = 0; i < N_MSGS; ++i ) {
if( i < rc ) {
/* mmsg[i] is set and we can use it */
if( mmsg[i] < 0) {
/* error sending this message ‐ release buffers */
for( j = 0; j < N_BUFFERS; ++j )
onload_zc_release_buffers(fd, &iovec[i][j].buf, 1);
} else if( mmsg(i] < sum_over_j(iovec[i][j].iov_len) ) {
/* partial success */
/* TODO use mmsg[i] to determine which buffers in
* iovec[i] array are sent and which are still
* owned by application */
} else {
/* Whole message sent, buffers now owned by Onload */
}
} else {
/* mmsg[i] is not set, this message was not sent */
for( j = 0; j < N_BUFFERS; ++j )
onload_zc_release_buffers(fd, &iovec[i][j].buf, 1);
}
} }
2.6.Zero‐Copy Send ‐ Full Example
static struct onload_zc_iovec iovec[NUM_ZC_BUFFERS];
static ssize_t do_send_zc(int fd, const void* buf, size_t len, int flags)
{
int bytes_done, rc, i, bufs_needed;
struct onload_zc_mmsg mmsg;
mmsg.fd = fd;
mmsg.msg.iov = iovec;
bytes_done = 0;
mmsg.msg.msghdr.msg_iovlen = 0;
while( bytes_done < len ) {
if( iovec[mmsg.msg.msghdr.msg_iovlen].iov_len > (len ‐ bytes_done))
iovec[mmsg.msg.msghdr.msg_iovlen].iov_len = (len ‐ bytes_done);
memcpy(iovec[i].iov_base, buf+bytes_done, iov_len);
bytes_done += iovec[mmsg.msg.msghdr.msg_iovlen].iov_len;
++mmsg.msg.msghdr.msg_iovlen;
}
rc = onload_zc_send(&mmsg, 1, 0);
if( rc != 1 /* Number of messages we sent */ ) {
printf("onload_zc_send failed to process msg, %d\n", rc);
return ‐1;
} else {
if( mmsg.rc < 0 )
printf("onload_zc_send message error %d\n", mmsg.rc);
else {
/* Iterate over the iovecs; any that were sent we must
replenish. */
i = 0; bufs_needed= 0;
while( i < mmsg.msg.msghdr.msg_iovlen ) {
if( bytes_done == mmsg.rc ) {
printf(onload_zc_send did not send iovec %d\n", i);
/* In other buffer allocation schemes we would have to
release
* these buffers, but seems pointless as we guarantee at the
* end of this function to have iovec array full, so do
nothing. */
} else {
/* Buffer sent, now owned by Onload, so replenish iovec
array */
++bufs needed;
bytes_done += iovec[i].iov_len;
}
++i;
}
if( bufs_needed ) /* replenish the iovec array */
rc = onload_zc_alloc_buffers(fd, iovec, bufs_needed, ONLOAD_ZC_BUFFER_HDR_TCP);
}
}
/* Set a return code that looks similar enough to send(). NB. we're
* not setting (and neither does onload_zc_send()) errno */
if( mmsg.rc < 0 ) return ‐1;
else return bytes_done;
}边栏推荐
- 抖音实战~个人中心
- [LineCTF2022]BB
- thinkphp5.0.24反序列化漏洞分析
- 麒麟软件携手格尔软件聚焦网络数据安全发展
- 关于 GIN 的路由树
- After using Matplotlib for so long, I didn't know that the data could move
- LeetCode Algorithm 21. 合并两个有序链表
- 追更这个做嵌入式的大佬
- Zuckerberg's latest VR prototype is coming. It is necessary to confuse virtual reality with reality
- 加密市场暴跌,stETH引发新一轮担忧
猜你喜欢

被曝泄露超 1.7 亿条隐私数据,学习通回应:尚未发现明确证据

IPO Configuration Guide

加密市场暴跌,stETH引发新一轮担忧

普乐蛙VR台风逃生体验VR灾害自救模拟系统VR科普知识设备
![[popular science] to understand supervised learning, unsupervised learning and reinforcement learning](/img/24/d26c656135219a38fd64e4d370c9ee.png)
[popular science] to understand supervised learning, unsupervised learning and reinforcement learning

麒麟软件携手格尔软件聚焦网络数据安全发展

这不会又是一个Go的BUG吧?

中坚力量!优炫软件入选2022年中国数字安全百强

Basic principles of the Internet

The future of Dao: an organization primitive for building Web3
随机推荐
抖音实战~手机号一键注册登录流程(验证码)
被曝泄露超 1.7 亿条隐私数据,学习通回应:尚未发现明确证据
Cobalt strike from starting to Imprisonment (3)
How to transfer the values in the database to JSP pages through servlets and display them in El expressions?
Tiktok announces data storage on Oracle server!
神经网络训练trick总结
【jenkins】shell脚本调jenkins api接口
TikTok 宣布将数据存储于 Oracle 服务器!
Web Configuration of Visual Studio Code
如何进行高效简洁的电子文档管理
工作中的帕累托定律:关注结果,而不是输出
jg_使用easyexcel读取excel_20220619
免费易用 ,腾讯Arm云实例评测 - AI推理加速
Tiktok practice ~ personal Center
Cobalt Strike 從入門到入獄(三)
CISP教材更新:2019年八大知识域新知识体系介绍
Quel est le risque de divulgation d'un certificat de signature de code?
thinkphp5.0.24反序列化漏洞分析
普乐蛙VR台风逃生体验VR灾害自救模拟系统VR科普知识设备
批量创建/删除文件、文件夹、修改文件名 后缀名