1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
| ##include <stdio.h> ##include <rte_eal.h> ##include <rte_mbuf.h> ##include <rte_ethdev.h> ##include <arpa/inet.h> ##include <string.h> ##include <rte_malloc.h>
##define MBUF_COUNT 4096 ##define BURST_SIZE 32
##define ENABLE_SEND 1
##define ENABLE_TCP 1
int gDpdkPortId = 0; static uint8_t gSrcMac[RTE_ETHER_ADDR_LEN];
static const struct rte_eth_conf port_conf_default = { .rxmode = { .max_rx_pkt_len = RTE_ETHER_MAX_LEN } };
##if ENABLE_TCP
##define TCP_OPTION_LENGTH 10 ##define RING_SIZE 1024 ##define TCP_MAX_SEQ 4294967295 ##define TCP_INITIAL_WINDOW 14600
static uint8_t src_mac[RTE_ETHER_ADDR_LEN]; static uint8_t dst_mac[RTE_ETHER_ADDR_LEN];
static uint32_t src_ip; static uint32_t dst_ip;
static uint16_t src_port; static uint16_t dst_port;
typedef enum _TCP_STATUS{ TCP_STATUS_CLOSED = 0, TCP_STATUS_LISTEN, TCP_STATUS_SYN_RCVD, TCP_STATUS_SYN_SENT, TCP_STATUS_ESTABLISHED,
TCP_STATUS_FIN_WAIT_1, TCP_STATUS_FIN_WAIT_2, TCP_STATUS_CLOSING, TCP_STATUS_TIME_WAIT,
TCP_STATUS_CLOSE_WAIT, TCP_STATUS_LAST_ACK } TCP_STATUS;
struct tcp_control_block{ int fd;
uint32_t dip; uint8_t localmac[RTE_ETHER_ADDR_LEN]; uint32_t dport;
uint8_t protocol;
uint32_t sport; uint32_t sip; uint32_t snd_nxt; uint32_t rcv_nxt; TCP_STATUS status;
struct tcp_control_block* prev; struct tcp_control_block* next;
pthread_cond_t cond; pthread_mutex_t mutex; };
struct tcp_table{ int count; struct tcp_control_block *tcb_set; };
struct tcp_fragment{ uint16_t sport; uint16_t dport; uint32_t seqnum; uint32_t acknum; uint8_t hdrlen_off; uint8_t tcp_flags; uint16_t windows; uint16_t cksum; uint16_t tcp_urp;
int optlen; uint32_t option[TCP_OPTION_LENGTH]; unsigned char *data; uint32_t length; };
struct tcp_table *tInst = NULL; static struct tcp_table* tcpInstance(void){ if(tInst == NULL){ tInst = rte_malloc("tcp_table", sizeof(struct tcp_table), 0); memset(tInst, 0, sizeof(struct tcp_table)); } return tInst; }
static struct tcp_control_block* tcp_block_search(uint32_t sip, uint32_t dip, uint16_t sport, uint16_t dport) { struct tcp_table* table = tcpInstance(); struct tcp_control_block *iter; for(iter = table->tcb_set; iter!=NULL;iter = iter->next){ if(iter->sip == sip && iter->dip == dip && iter->sport == sport && iter->dport == dport ) return iter; } for(iter = table->tcb_set; iter!=NULL;iter = iter->next){ if(iter->dport == dport && iter->status == TCP_STATUS_LISTEN) return iter; }
return NULL; }
static struct tcp_control_block* tcp_control_block_create(uint32_t sip, uint32_t dip, uint16_t sport, uint16_t dport){ struct tcp_control_block* stream = rte_malloc("tcp_control_block", sizeof(struct tcp_control_block), 0); if(stream == NULL) return stream; stream->sip = sip; stream->dip = dip; stream->sport = sport; stream->dport = dport; stream->protocol = IPPROTO_TCP; stream->fd = -1;
stream->status = TCP_STATUS_LISTEN;
uint32_t next_seed = time(NULL); stream->snd_nxt = rand_r(&next_seed) % TCP_MAX_SEQ;
return stream; }
static int encode_tcp_pkt(uint8_t *msg, uint32_t sip, uint32_t dip, uint8_t *srcmac, uint8_t *dstmac, struct tcp_fragment *fragment) { const unsigned total_len = fragment->length + sizeof(struct rte_ether_hdr) + sizeof(struct rte_ipv4_hdr) + sizeof(struct rte_tcp_hdr) + fragment->optlen * sizeof(uint32_t); struct rte_ether_hdr* eth = (struct rte_ether_hdr*)msg; rte_memcpy(eth->s_addr.addr_bytes, srcmac, RTE_ETHER_ADDR_LEN); rte_memcpy(eth->d_addr.addr_bytes, dstmac, RTE_ETHER_ADDR_LEN); eth->ether_type = htons(RTE_ETHER_TYPE_IPV4);
struct rte_ipv4_hdr *ip = (struct rte_ipv4_hdr *)(msg + sizeof(struct rte_ether_hdr)); ip->version_ihl = 0x45; ip->type_of_service = 0; ip->total_length = htons(total_len - sizeof(struct rte_ether_hdr)); ip->packet_id = 0; ip->fragment_offset = 0; ip->time_to_live = 64; ip->next_proto_id = IPPROTO_TCP; ip->src_addr = sip; ip->dst_addr = dip; ip->hdr_checksum = 0; ip->hdr_checksum = rte_ipv4_cksum(ip);
struct rte_tcp_hdr *tcp = (struct rte_tcp_hdr *)(msg + sizeof(struct rte_ether_hdr) + sizeof(struct rte_ipv4_hdr)); tcp->src_port = fragment->sport; tcp->dst_port = fragment->dport; tcp->sent_seq = htonl(fragment->seqnum); tcp->recv_ack = htonl(fragment->acknum);
tcp->data_off = fragment->hdrlen_off; tcp->rx_win = fragment->windows; tcp->tcp_urp = fragment->tcp_urp; tcp->tcp_flags = fragment->tcp_flags;
if (fragment->data != NULL) { uint8_t *payload = (uint8_t*)(tcp+1) + fragment->optlen * sizeof(uint32_t); rte_memcpy(payload, fragment->data, fragment->length); }
tcp->cksum = 0; tcp->cksum = rte_ipv4_udptcp_cksum(ip, tcp);
return 0;
}
static struct rte_mbuf* conduct_tcp_pkt(struct rte_mempool *mbuf_pool, uint32_t sip, uint32_t dip, uint8_t *srcmac, uint8_t *dstmac, struct tcp_fragment *fragment) { const unsigned total_len = fragment->length + sizeof(struct rte_ether_hdr) + sizeof(struct rte_ipv4_hdr) + sizeof(struct rte_tcp_hdr) + fragment->optlen * sizeof(uint32_t); struct rte_mbuf *mbuf = rte_pktmbuf_alloc(mbuf_pool); if (!mbuf) { rte_exit(EXIT_FAILURE, "tcp_pkt rte_pktmbuf_alloc\n"); } mbuf->pkt_len = total_len; mbuf->data_len = total_len; uint8_t *pktdata = rte_pktmbuf_mtod(mbuf, uint8_t*); encode_tcp_pkt(pktdata, sip, dip, srcmac, dstmac, fragment);
return mbuf; }
static struct tcp_fragment * tcp_send_ackpkt(struct tcp_control_block* stream, struct rte_tcp_hdr *tcphdr) { struct tcp_fragment *ackfrag = rte_malloc("tcp_fragment", sizeof(struct tcp_fragment), 0); if (ackfrag == NULL) return NULL; memset(ackfrag, 0, sizeof(struct tcp_fragment)); ackfrag->dport = tcphdr->src_port; ackfrag->sport = tcphdr->dst_port; printf("tcp_send_ackpkt: %d, %d\n", stream->rcv_nxt, ntohs(tcphdr->sent_seq)); ackfrag->acknum = stream->rcv_nxt; ackfrag->seqnum = stream->snd_nxt; ackfrag->tcp_flags = RTE_TCP_ACK_FLAG; ackfrag->windows = TCP_INITIAL_WINDOW; ackfrag->hdrlen_off = 0x50; ackfrag->data = NULL; ackfrag->length = 0;
return ackfrag; }
static int tcp_enqueue_recvbuffer(struct tcp_control_block* stream, struct rte_tcp_hdr* tcphdr, int tcplen) { struct tcp_fragment *rfragment = rte_malloc("tcp_fragment", sizeof(struct tcp_fragment), 0); if(rfragment == NULL) return -1; memset(rfragment, 0, sizeof(struct tcp_fragment)); rfragment->dport = ntohs(tcphdr->dst_port); rfragment->sport = ntohs(tcphdr->src_port);
uint8_t hdrlen = tcphdr->data_off >> 4; int payloadlen = tcplen - hdrlen * 4; if(payloadlen > 0){ uint8_t* payload = (uint8_t*)tcphdr + hdrlen * 4; rfragment->data = rte_malloc("unsigned char*", payloadlen + 1, 0); if(rfragment->data == NULL){ rte_free(rfragment);return -1; } memset(rfragment->data, 0 , payloadlen + 1); rte_memcpy(rfragment->data, 0, payloadlen + 1); rfragment->length = payloadlen; } else if (payloadlen == 0) { rfragment->length = 0; rfragment->data = NULL; } printf("tcp recv : %s\n", rfragment->data);
return 0; }
static struct tcp_fragment * tcp_handle_listen(struct tcp_control_block *stream, struct rte_tcp_hdr *tcphdr, struct rte_ipv4_hdr *iphdr) { if(tcphdr->tcp_flags & RTE_TCP_SYN_FLAG){ if(stream->status == TCP_STATUS_LISTEN){ struct tcp_table* table = tcpInstance(); struct tcp_control_block* syn = tcp_control_block_create(iphdr->src_addr, iphdr->dst_addr, tcphdr->src_port, tcphdr->dst_port); do{ syn->prev = NULL; syn->next = table->tcb_set; if(table->tcb_set != NULL){ table->tcb_set->prev = syn; } table->tcb_set = syn; } while(0); struct tcp_fragment *fragment = rte_malloc("tcp_fragment", sizeof(struct tcp_fragment), 0); if(fragment == NULL) return NULL; memset(fragment, 0, sizeof(struct tcp_fragment));
fragment->sport = tcphdr->dst_port; fragment->dport = tcphdr->src_port; struct in_addr addr; addr.s_addr = syn->sip; printf("tcp ---> src: %s:%d ", inet_ntoa(addr), ntohs(tcphdr->src_port)); addr.s_addr = syn->dip; printf(" ---> dst: %s:%d \n", inet_ntoa(addr), ntohs(tcphdr->dst_port)); fragment->seqnum = syn->snd_nxt; fragment->acknum = ntohl(tcphdr->sent_seq) + 1; syn->rcv_nxt = fragment->acknum;
fragment->tcp_flags = (RTE_TCP_SYN_FLAG | RTE_TCP_ACK_FLAG); fragment->windows = TCP_INITIAL_WINDOW; fragment->hdrlen_off = 0x50;
fragment->data = NULL; fragment->length = 0;
syn->status = TCP_STATUS_SYN_RCVD; return fragment; } } return NULL; }
static int tcp_handle_syn_rcvd(struct tcp_control_block* stream, struct rte_tcp_hdr* tcphdr) { if(tcphdr->tcp_flags & RTE_TCP_ACK_FLAG){ if(stream->status == TCP_STATUS_SYN_RCVD){ uint32_t acknum = ntohl(tcphdr->recv_ack); if(acknum == stream -> snd_nxt + 1){
} stream->status = TCP_STATUS_ESTABLISHED; } } return 0; }
static struct tcp_fragment * tcp_handle_established(struct tcp_control_block* stream, struct rte_tcp_hdr* tcphdr, int tcplen) { struct tcp_fragment * fragment = NULL; if(tcphdr->tcp_flags & RTE_TCP_SYN_FLAG){} if(tcphdr->tcp_flags & RTE_TCP_PSH_FLAG){ tcp_enqueue_recvbuffer(stream, tcphdr, tcplen); uint8_t hdrlen = tcphdr->data_off >> 4; int payloadlen = tcplen - hdrlen * 4; stream->rcv_nxt = stream->rcv_nxt + payloadlen; stream->snd_nxt = ntohl(tcphdr->recv_ack); fragment = tcp_send_ackpkt(stream, tcphdr); } if(tcphdr->tcp_flags & RTE_TCP_ACK_FLAG){} if(tcphdr->tcp_flags & RTE_TCP_FIN_FLAG){ stream->status = TCP_STATUS_CLOSE_WAIT; tcp_enqueue_recvbuffer(stream, tcphdr, tcphdr->data_off >> 4); stream->rcv_nxt = stream->rcv_nxt + 1; stream->snd_nxt = ntohl(tcphdr->recv_ack); fragment = tcp_send_ackpkt(stream, tcphdr); } return fragment; }
static int tcp_handle_last_ack(struct tcp_control_block* stream, struct rte_tcp_hdr* tcphdr) { if(tcphdr->tcp_flags & RTE_TCP_ACK_FLAG) { if(stream->status == TCP_STATUS_LAST_ACK){ stream->status = TCP_STATUS_CLOSED; printf("tcp_handle_last_ack\n"); struct tcp_table* table = tcpInstance(); do{ if(stream->prev != NULL) stream->prev->next = stream->next; if(stream->next != NULL) stream->next->prev = stream->prev; if(table->tcb_set == stream) table->tcb_set = stream->next; stream->prev = stream->next = NULL; }while(0); rte_free(stream); } } return 0; }
struct tcp_fragment *tcp_pkt_process(struct rte_mbuf* tcpmbuf) { struct rte_ipv4_hdr *iphdr = rte_pktmbuf_mtod_offset(tcpmbuf, struct rte_ipv4_hdr*, sizeof(struct rte_ether_hdr)); struct rte_tcp_hdr *tcphdr = (struct rte_tcp_hdr *)(iphdr + 1); uint16_t tcpcksum = tcphdr->cksum; tcphdr->cksum = 0; uint16_t cksum = rte_ipv4_udptcp_cksum(iphdr, tcphdr); if(cksum != tcpcksum) return NULL;
struct tcp_control_block* stream = tcp_block_search(iphdr->src_addr, iphdr->dst_addr, tcphdr->src_port, tcphdr->dst_port); if(stream == NULL){ rte_pktmbuf_free(tcpmbuf); return NULL; }
struct tcp_fragment * fragment = NULL;
rte_memcpy(&src_ip, &iphdr->dst_addr, sizeof(uint32_t)); rte_memcpy(&dst_ip, &iphdr->src_addr, sizeof(uint32_t));
rte_memcpy(&src_port, &tcphdr->dst_port, sizeof(uint16_t)); rte_memcpy(&dst_port, &tcphdr->src_port, sizeof(uint16_t));
switch (stream->status) { case TCP_STATUS_CLOSED : break; case TCP_STATUS_LISTEN : fragment = tcp_handle_listen(stream, tcphdr, iphdr); break; case TCP_STATUS_SYN_RCVD: tcp_handle_syn_rcvd(stream, tcphdr); break; case TCP_STATUS_SYN_SENT: break; case TCP_STATUS_ESTABLISHED: { int tcplen = ntohs(iphdr->total_length) - sizeof(struct rte_ipv4_hdr); fragment = tcp_handle_established(stream, tcphdr, tcplen); } break; case TCP_STATUS_FIN_WAIT_1: break; case TCP_STATUS_FIN_WAIT_2: break; case TCP_STATUS_CLOSING: break; case TCP_STATUS_TIME_WAIT: break;
case TCP_STATUS_CLOSE_WAIT: break; case TCP_STATUS_LAST_ACK: tcp_handle_last_ack(stream, tcphdr); break; default: break; }
rte_pktmbuf_free(tcpmbuf); return fragment; }
##endif
int main(int argc, char *argv[]) { if(rte_eal_init(argc, argv) < 0){ rte_exit(EXIT_FAILURE, "Failed to init EAL\n"); } struct rte_mempool *mbuf_pool = rte_pktmbuf_pool_create("mbufpool", MBUF_COUNT, 0, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); struct rte_eth_conf port_conf = port_conf_default; int num_rx_queues = 1; int num_tx_queues = 1; rte_eth_dev_configure(gDpdkPortId, num_rx_queues, num_tx_queues, &port_conf );
rte_eth_rx_queue_setup(gDpdkPortId, 0, 128, rte_eth_dev_socket_id(gDpdkPortId), NULL, mbuf_pool); rte_eth_tx_queue_setup(gDpdkPortId, 0, 512, rte_eth_dev_socket_id(gDpdkPortId), NULL); rte_eth_dev_start(gDpdkPortId);
rte_eth_macaddr_get(gDpdkPortId, (struct rte_ether_addr *)gSrcMac);
##if ENABLE_TCP struct tcp_control_block *stream = rte_malloc("tcp_control_block", sizeof(struct tcp_control_block), 0); if (stream == NULL) { return -1; } memset(stream, 0, sizeof(struct tcp_control_block)); stream->fd = rand(); stream->protocol = IPPROTO_TCP; stream->next = stream->prev = NULL; struct tcp_table *table = tcpInstance(); do{ stream->prev = NULL; stream->next = table->tcb_set; if(table->tcb_set != NULL){ table->tcb_set->prev = stream; } table->tcb_set = stream; } while(0); struct sockaddr_in servaddr; memset(&servaddr, 0, sizeof(struct sockaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(9999); const struct sockaddr_in *laddr = &servaddr; stream->dport = laddr->sin_port; rte_memcpy(&stream->dip, &laddr->sin_addr.s_addr, sizeof(uint32_t)); rte_memcpy(stream->localmac, gSrcMac, RTE_ETHER_ADDR_LEN); stream->status = TCP_STATUS_CLOSED; stream->status = TCP_STATUS_LISTEN; ##endif while(1) { struct rte_mbuf *mbufs[BURST_SIZE]; unsigned num_recvd = rte_eth_rx_burst(gDpdkPortId, 0, mbufs, BURST_SIZE); if(num_recvd > BURST_SIZE){ rte_exit(EXIT_FAILURE, "Failed to rte_eth_rx_burst\n"); } unsigned i = 0; for(i = 0; i < num_recvd ; i++){ struct rte_ether_hdr *ehdr = rte_pktmbuf_mtod(mbufs[i], struct rte_ether_hdr *); if(ehdr->ether_type == htons(RTE_ETHER_TYPE_IPV4)) { struct rte_ipv4_hdr *iphdr = rte_pktmbuf_mtod_offset(mbufs[i], struct rte_ipv4_hdr *, sizeof(struct rte_ether_hdr)); if(iphdr->next_proto_id == IPPROTO_UDP) { struct rte_udp_hdr *udphdr = (struct rte_udp_hdr *)(iphdr + 1); uint16_t length = ntohs(udphdr->dgram_len); *((char*)udphdr + length) = '\0';
printf("data : %s\n", (char*)(udphdr + 1)); } ##if ENABLE_TCP else if(iphdr->next_proto_id == IPPROTO_TCP) { rte_memcpy(dst_mac, ehdr->s_addr.addr_bytes, RTE_ETHER_ADDR_LEN); rte_memcpy(src_mac, ehdr->d_addr.addr_bytes, RTE_ETHER_ADDR_LEN);
struct tcp_fragment *fragment = tcp_pkt_process(mbufs[i]); if(fragment != NULL){ struct rte_mbuf *tx_buf = conduct_tcp_pkt(mbuf_pool, src_ip, dst_ip, src_mac, dst_mac, fragment); rte_eth_tx_burst(gDpdkPortId, 0, &tx_buf, 1); } } ##endif } } } }
|