diff options
| author | Qiuwen Lu <[email protected]> | 2017-07-12 09:58:57 +0800 |
|---|---|---|
| committer | Qiuwen Lu <[email protected]> | 2017-07-12 09:58:57 +0800 |
| commit | d7eeb5574c2669479b562c43dcb36e6697775ae7 (patch) | |
| tree | 567077829bb10f3af3ad00a1299eb1f36e61bda0 | |
| parent | 2739172656216bca077605d29155181fa95628ef (diff) | |
(1)增加sendpath的Prebuild和Postbuild位置的用户回调函数功能,在sendpath的option中设置。(2)增加buffer的deep-deep克隆操作,克隆buffer全部区域和控制域。(3)修正service的monit信息刷写在获取网卡信息缓慢时没有示数的Bug。
| -rw-r--r-- | app/include/sendpath.h | 14 | ||||
| -rw-r--r-- | app/src/mrb.c | 195 | ||||
| -rw-r--r-- | app/src/sendpath.c | 74 | ||||
| -rw-r--r-- | app/src/version.map | 3 | ||||
| -rw-r--r-- | include/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | include/external/marsio.h | 28 | ||||
| -rw-r--r-- | include/external/mrtunnat.h (renamed from tunnat/include/mrtunnat.h) | 0 | ||||
| -rw-r--r-- | include/internal/mrb_define.h | 4 | ||||
| -rw-r--r-- | service/src/monit.c | 18 | ||||
| -rw-r--r-- | service/src/mrb.c | 4 | ||||
| -rw-r--r-- | tunnat/src/runtime.cc | 3 |
11 files changed, 294 insertions, 52 deletions
diff --git a/app/include/sendpath.h b/app/include/sendpath.h index b38b1fe..03c17f6 100644 --- a/app/include/sendpath.h +++ b/app/include/sendpath.h @@ -4,6 +4,8 @@ #include <marsio.h> typedef int(*fn_layer_construct_t)(struct mr_sendpath * sendpath, struct rte_mbuf * mbuf[], unsigned int nr_mbuf); +typedef int(*fn_hook_t)(struct mr_sendpath * sendpath, struct rte_mbuf * mbuf[], unsigned int nr_mbuf, void * arg); + typedef int(*fn_requery_t)(struct mr_sendpath * sendpath); typedef int(*fn_option_set_t)(struct mr_instance * instance, struct mr_sendpath * sendpath, int opt, va_list va_list); typedef int(*fn_option_get_t)(struct mr_instance * instance, struct mr_sendpath * sendpath, int opt, va_list va_list); @@ -29,7 +31,15 @@ struct mr_sendpath fn_layer_construct_t fn_l3_construct; /* 四层负载构建函数 */ fn_layer_construct_t fn_l4_construct; - + /* Prebuild Hook回调函数 */ + fn_hook_t fn_prebuild_hook; + /* Prebuild Hook回调函数参数 */ + void * prebuild_hook_args; + /* Postbuild Hook 回调函数 */ + fn_hook_t fn_postbuild_hook; + /* Postbuild Hook 回调函数参数 */ + void * postbuild_hook_args; + /* 目标VDI */ struct vdev_instance * target_vdi; /* Instance */ @@ -39,6 +49,8 @@ struct mr_sendpath struct __mr_sendpath_vdev { struct mr_sendpath _father; + struct ether_addr src_eth_addr; + struct ether_addr dst_eth_addr; }; struct __mr_sendpath_route diff --git a/app/src/mrb.c b/app/src/mrb.c index edf18f1..21352e6 100644 --- a/app/src/mrb.c +++ b/app/src/mrb.c @@ -103,7 +103,7 @@ void marsio_buff_chain_pkt(marsio_buff_t * pkt, marsio_buff_t * next) void marsio_buff_reset(marsio_buff_t *m) { - struct mrb_priv_zone_t* priv_zone = mrbuf_priv(m); + struct mrb_priv_zone* priv_zone = mrbuf_priv(m); struct mrb_zone_idx* cz_first = NULL, *cz_last = NULL; /* 引用计数检查,修改报文时,不应该被其他的地方引用 */ @@ -182,51 +182,167 @@ struct rte_mbuf * __pktmbuf_alloc(struct mr_instance * instance, int socket_id, return __mbuf; } + marsio_buff_t * marsio_buff_clone_deep(struct mr_instance * instance, - marsio_buff_t * md, int socket_id, int thread_id) + marsio_buff_t * md, int socket_id, int thread_id) +{ + struct rte_mbuf *__mc, *__mi, **__prev; + struct rte_mbuf * __md = (struct rte_mbuf *)md; + + uint32_t pktlen; + uint8_t nseg; + + if (unlikely((__mc = __pktmbuf_alloc(instance, socket_id, thread_id)) == NULL)) + return NULL; + + __mi = __mc; + __prev = &__mi->next; + pktlen = __md->pkt_len; + nseg = 0; + + do { + nseg++; + + uint16_t __md_datalen = rte_pktmbuf_data_len(__md); + void * __md_data = rte_pktmbuf_mtod(__md, void *); + void * __mi_data = rte_pktmbuf_append(__mi, __md_datalen); + rte_memcpy(__mi_data, __md_data, __md_datalen); + + *__prev = __mi; + __prev = &__mi->next; + + } while ((md = __md->next) != NULL && + (__mi = __pktmbuf_alloc(instance, socket_id, thread_id)) != NULL); + + *__prev = NULL; + __mc->nb_segs = nseg; + __mc->pkt_len = pktlen; + + /* Allocation of new indirect segment failed */ + if (unlikely(__mi == NULL)) { + rte_pktmbuf_free(__mc); + return NULL; + } + + __rte_mbuf_sanity_check(__mc, 1); + return (marsio_buff_t *)__mc; +} + +static void __buff_clone_ctrlzone(marsio_buff_t * mc, marsio_buff_t * md) +{ + struct rte_mbuf * __mc = (struct rte_mbuf *)mc; + struct rte_mbuf * __md = (struct rte_mbuf *)md; + + /* 复制Priv部分的数据 */ + while (__mc != NULL && __md != NULL) + { + struct mrb_priv_zone * __mc_priv_zone = mrbuf_priv(__mc); + struct mrb_priv_zone * __mi_priv_zone = mrbuf_priv(__md); + + /* 断言,MBUF的私有区域一定大于控制域的大小 */ + assert(rte_pktmbuf_priv_size(__mc->pool) >= sizeof(struct mrb_priv_zone)); + memcpy(__mc_priv_zone, __mi_priv_zone, sizeof(struct mrb_priv_zone)); + + /* 拆除控制域中的next指针?*/ + __mc_priv_zone->next = NULL; + } + + /* 断言,__mc和__mi同时到达链表的终点,否则说明链表克隆时出现了问题。 */ + assert(__mc == NULL && __md == NULL); + return; +} + +static void __memcpy_operator_buff(struct rte_mbuf *__mi, struct rte_mbuf * __md) +{ + void * __md_buff = rte_mbuf_to_baddr(__md); + void * __mi_buff = rte_mbuf_to_baddr(__mi); + + assert(__md->buf_len == __mi->buf_len); + size_t bufflen = RTE_MIN(__md->buf_len, __mi->buf_len); + + rte_memcpy(__mi_buff, __md_buff, bufflen); + __mi->data_len = __md->data_len; + __mi->data_off = __md->data_off; + return; +} + +static void __memcpy_operator_data(struct rte_mbuf *__mi, struct rte_mbuf * __md) { - struct rte_mbuf *__mc, *__mi, **__prev; - struct rte_mbuf * __md = (struct rte_mbuf *)md; + uint16_t __md_datalen = rte_pktmbuf_data_len(__md); + void * __md_data = rte_pktmbuf_mtod(__md, void *); + void * __mi_data = rte_pktmbuf_append(__mi, __md_datalen); + rte_memcpy(__mi_data, __md_data, __md_datalen); + return; +} - uint32_t pktlen; - uint8_t nseg; +static marsio_buff_t * __buff_clone_memcpy(struct mr_instance * instance, + marsio_buff_t * md, int socket_id, int thread_id, + void (*memcpy_operator)(struct rte_mbuf *__mi, struct rte_mbuf * __md)) +{ + struct rte_mbuf *__mc, *__mi, **__prev; + struct rte_mbuf * __md = (struct rte_mbuf *)md; + + uint32_t pktlen; + uint8_t nseg; - if (unlikely((__mc = __pktmbuf_alloc(instance, socket_id, thread_id)) == NULL)) - return NULL; + if (unlikely((__mc = __pktmbuf_alloc(instance, socket_id, thread_id)) == NULL)) + return NULL; - __mi = __mc; - __prev = &__mi->next; - pktlen = __md->pkt_len; - nseg = 0; + __mi = __mc; + __prev = &__mi->next; + pktlen = __md->pkt_len; + nseg = 0; - do { - nseg++; + do { + nseg++; + memcpy_operator(__mi, __md); - uint16_t __md_datalen = rte_pktmbuf_data_len(__md); - void * __md_data = rte_pktmbuf_mtod(__md, void *); - void * __mi_data = rte_pktmbuf_append(__mi, __md_datalen); - rte_memcpy(__mi_data, __md_data, __md_datalen); - - *__prev = __mi; - __prev = &__mi->next; + *__prev = __mi; + __prev = &__mi->next; - } while ((md = __md->next) != NULL && - (__mi = __pktmbuf_alloc(instance, socket_id, thread_id)) != NULL); + } while ((md = __md->next) != NULL && + (__mi = __pktmbuf_alloc(instance, socket_id, thread_id)) != NULL); - *__prev = NULL; - __mc->nb_segs = nseg; - __mc->pkt_len = pktlen; + *__prev = NULL; + __mc->nb_segs = nseg; + __mc->pkt_len = pktlen; - /* Allocation of new indirect segment failed */ - if (unlikely(__mi == NULL)) { - rte_pktmbuf_free(__mc); - return NULL; - } + /* Allocation of new indirect segment failed */ + if (unlikely(__mi == NULL)) { + rte_pktmbuf_free(__mc); + return NULL; + } - __rte_mbuf_sanity_check(__mc, 1); - return (marsio_buff_t *)__mc; + __rte_mbuf_sanity_check(__mc, 1); + return (marsio_buff_t *)__mc; } +marsio_buff_t * marsio_buff_clone_with_options(struct mr_instance * instance, + marsio_buff_t * md, int socket_id, int thread_id, uint16_t options) +{ + marsio_buff_t * mi = NULL; + + if (options & MR_BUFF_CLONE_BUFF) + { + mi = __buff_clone_memcpy(instance, md, socket_id, thread_id, + __memcpy_operator_buff); + } + else if (options & MR_BUFF_CLONE_DATA) + { + mi = __buff_clone_memcpy(instance, md, socket_id, thread_id, + __memcpy_operator_data); + } + + if (unlikely(mi == NULL)) return mi; + if (options & MR_BUFF_CLONE_CTRLZONE) + { + __buff_clone_ctrlzone(mi, md); + } + + return mi; +} + + uint16_t marsio_buff_headroom(const marsio_buff_t *m) { return rte_pktmbuf_headroom((const struct rte_mbuf *)m); @@ -242,6 +358,19 @@ uint32_t marsio_get_pkt_type(marsio_buff_t *m) return ((struct rte_mbuf *)m)->packet_type; } +int marsio_buff_is_ctrlbuf(marsio_buff_t * m) +{ + struct rte_mbuf * __m = (struct rte_mbuf *)m; + return rte_is_ctrlmbuf(__m); +} + +void marsio_buff_set_ctrlbuf(marsio_buff_t *m) +{ + struct rte_mbuf * __m = (struct rte_mbuf *)m; + __m->ol_flags = __m->ol_flags & CTRL_MBUF_FLAG; + return; +} + void marsio_pktmbuf_dump(FILE *f, const marsio_buff_t *m, unsigned dump_len) { rte_pktmbuf_dump(f, (const struct rte_mbuf *)m, dump_len); diff --git a/app/src/sendpath.c b/app/src/sendpath.c index e8ca549..0fc6a38 100644 --- a/app/src/sendpath.c +++ b/app/src/sendpath.c @@ -17,6 +17,59 @@ static inline int is_same_subnet(struct in_addr a_addr, struct in_addr b_addr, s /* ======================================================================================= */ +static int sendpath_vdev_l2_construct(struct mr_sendpath * sendpath, + struct rte_mbuf * mbuf[], unsigned int nr_mbuf) +{ + struct __mr_sendpath_vdev * _sendpath = container_of(sendpath, + struct __mr_sendpath_vdev, _father); + + for (int i = 0; i < nr_mbuf; i++) + { + struct ether_hdr * ether_hdr = (struct ether_hdr *)rte_pktmbuf_prepend(mbuf[i], + sizeof(struct ether_hdr)); + + MR_VERIFY_2(ether_hdr != NULL, "Not enough space for ethernet header in mbufs. "); + ether_hdr->s_addr = _sendpath->src_eth_addr; + ether_hdr->d_addr = _sendpath->dst_eth_addr; + ether_hdr->ether_type = htons(ETHER_TYPE_IPv4); + } + + return 0; +} + +static int sendpath_vdev_option_get(struct mr_instance * instance, struct mr_sendpath * sendpath, + int opt, va_list va_list) +{ + return 0; +} + +static int sendpath_vdev_option_set(struct mr_instance * instance, struct mr_sendpath * sendpath, + int opt, va_list va_list) +{ + if (opt == MR_SENDPATH_OPT_BUILD_L2) + { + unsigned int enable = va_arg(va_list, unsigned int); + sendpath->fn_l2_construct = enable ? sendpath_vdev_l2_construct : NULL; + } + else if (opt == MR_SENDPATH_OPT_HOOK_PREBUILD) + { + sendpath->fn_prebuild_hook = va_arg(va_list, void *); + sendpath->prebuild_hook_args = va_arg(va_list, void *); + } + else if (opt == MR_SENDPATH_OPT_HOOK_POSTBUILD) + { + sendpath->fn_postbuild_hook = va_arg(va_list, void *); + sendpath->postbuild_hook_args = va_arg(va_list, void *); + } + else + { + MR_ERROR("Invalided opt type in %s()", __FUNCTION__); + return -EINVAL; + } + + return 0; +} + static void sendpath_vdev_destory(struct mr_sendpath * sendpath) { struct __mr_sendpath_vdev * _sendpath = container_of(sendpath, struct __mr_sendpath_vdev, _father); @@ -31,9 +84,16 @@ static struct mr_sendpath * sendpath_vdev_create(struct mr_instance * instance, struct __mr_sendpath_vdev * _sendpath = malloc(sizeof(struct __mr_sendpath_vdev)); memset(_sendpath, 0, sizeof(struct __mr_sendpath_vdev)); - _sendpath->_father.fn_destory = sendpath_vdev_destory; - _sendpath->_father.target_vdi = dest_vdev->vdi; + /* Parameters */ + _sendpath->_father.target_vdi = dest_vdev->vdi; _sendpath->_father.can_use = 1; + + /* Callback Functions */ + _sendpath->_father.fn_destory = sendpath_vdev_destory; + _sendpath->_father.fn_l2_construct = sendpath_vdev_l2_construct; + _sendpath->_father.fn_option_set = sendpath_vdev_option_set; + _sendpath->_father.fn_option_get = sendpath_vdev_option_get; + return &_sendpath->_father; } @@ -122,6 +182,16 @@ static int sendpath_route_option_set(struct mr_instance * instance, struct mr_se unsigned int enable = va_arg(va_list, unsigned int); sendpath->fn_l3_construct = enable ? sendpath_route_l3_construct : NULL; } + else if (opt == MR_SENDPATH_OPT_HOOK_PREBUILD) + { + sendpath->fn_prebuild_hook = va_arg(va_list, void *); + sendpath->prebuild_hook_args = va_arg(va_list, void *); + } + else if (opt == MR_SENDPATH_OPT_HOOK_POSTBUILD) + { + sendpath->fn_postbuild_hook = va_arg(va_list, void *); + sendpath->postbuild_hook_args = va_arg(va_list, void *); + } else { MR_ERROR("Invalided opt type in %s()", __FUNCTION__); diff --git a/app/src/version.map b/app/src/version.map index a1bdb6c..f8f8042 100644 --- a/app/src/version.map +++ b/app/src/version.map @@ -51,6 +51,9 @@ global: marsio_sendpath_create; marsio_sendpath_option_set; marsio_sendpath_option_get; + marsio_buff_is_ctrlbuf; + marsio_buff_ctrlbuf_set; + marsio_buff_clone_with_options; local: *; };
\ No newline at end of file diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index b713a06..8fed775 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -1 +1,2 @@ -install(FILES external/marsio.h DESTINATION include COMPONENT Program)
\ No newline at end of file +install(FILES external/marsio.h DESTINATION include COMPONENT Program)
+install(FILES external/mrtunnat.h DESTINATION include COMPONENT Program)
\ No newline at end of file diff --git a/include/external/marsio.h b/include/external/marsio.h index 7578ca1..2ff1d5d 100644 --- a/include/external/marsio.h +++ b/include/external/marsio.h @@ -54,11 +54,23 @@ enum mr_sendpath_type enum mr_sendpath_option { /* 构建四层报文头 */ - MR_SENDPATH_OPT_BUILD_L4, + MR_SENDPATH_OPT_BUILD_L4 = 0, /* 构建三层报文头 */ - MR_SENDPATH_OPT_BUILD_L3, + MR_SENDPATH_OPT_BUILD_L3 = 1, /* 构建二层报文头 */ - MR_SENDPATH_OPT_BUILD_L2, + MR_SENDPATH_OPT_BUILD_L2 = 2, + /* 构建前Hook点回调 */ + MR_SENDPATH_OPT_HOOK_PREBUILD = 50, + /* 构建后Hook点回调 */ + MR_SENDPATH_OPT_HOOK_POSTBUILD = 51, +}; + +enum mr_clone_options +{ + /* 拷贝区域 */ + MR_BUFF_CLONE_DATA = 1 << 0, + MR_BUFF_CLONE_BUFF = 1 << 1, + MR_BUFF_CLONE_CTRLZONE = 1 << 2, }; #ifdef __cplusplus @@ -91,6 +103,9 @@ struct mr_vdev; struct mr_instance * marsio_create(); struct mr_instance * marsio_current(); +typedef int(*fn_sendpath_hook_t)(struct mr_sendpath * sendpath, + marsio_buff_t * mbuf[], unsigned int nr_mbuf, void * arg); + int marsio_option_get(struct mr_instance * instance, int opt_type, void * out_opt, size_t out_opt_buffer); int marsio_option_set(struct mr_instance * instance, marsio_opt_type_t opt_type, void * opt, size_t sz_opt); int marsio_init(struct mr_instance * instance, const char * appsym); @@ -171,6 +186,9 @@ void marsio_pktmbuf_dump(FILE *f, const marsio_buff_t *m, unsigned dump_len); marsio_buff_t * marsio_buff_clone_deep(struct mr_instance * instance, marsio_buff_t * md, int socket_id, int thread_id); +marsio_buff_t * marsio_buff_clone_with_options(struct mr_instance * instance, + marsio_buff_t * md, int socket_id, int thread_id, uint16_t options); + int marsio_buff_malloc_device(struct mr_vdev * vdev, marsio_buff_t *marsio_buff[], unsigned int nr_mbufs, int socket_id, int thread_id); @@ -180,6 +198,10 @@ int marsio_buff_malloc_global(struct mr_instance * instance, marsio_buff_t *mars void marsio_buff_free(struct mr_instance * instance, marsio_buff_t *marsio_buff[], unsigned int nr_mbufs, int socket_id, int thread_id); +int marsio_buff_is_ctrlbuf(marsio_buff_t * m); + +void marsio_buff_set_ctrlbuf(marsio_buff_t *m); + #ifdef __cplusplus } diff --git a/tunnat/include/mrtunnat.h b/include/external/mrtunnat.h index e78a1f8..e78a1f8 100644 --- a/tunnat/include/mrtunnat.h +++ b/include/external/mrtunnat.h diff --git a/include/internal/mrb_define.h b/include/internal/mrb_define.h index 3dfc717..c0f1df9 100644 --- a/include/internal/mrb_define.h +++ b/include/internal/mrb_define.h @@ -12,7 +12,7 @@ struct mrb_zone_idx uint8_t size; }; -struct mrb_priv_zone_t +struct mrb_priv_zone { struct rte_mbuf * next; uint8_t cz_num; @@ -28,7 +28,7 @@ struct mrb_zone_info }; -#define mrbuf_priv(m) ((struct mrb_priv_zone_t*)((char*)m+sizeof(struct rte_mbuf))) +#define mrbuf_priv(m) ((struct mrb_priv_zone*)((char*)m+sizeof(struct rte_mbuf))) #define mrbuf_cz(m,k) (&(mrbuf_priv(m)->idx[k])) #define mrbuf_cz_data(m,k) ((void*)((char*)mrbuf_priv(m)+mrbuf_cz(m,k)->offset)) #define mrbuf_cz_num(m) (mrbuf_priv(m)->cz_num)
\ No newline at end of file diff --git a/service/src/monit.c b/service/src/monit.c index 6503b93..06fa157 100644 --- a/service/src/monit.c +++ b/service/src/monit.c @@ -255,17 +255,19 @@ static cJSON * monit_root(struct sc_main * sc) int sc_monit_loop(struct sc_main * sc) { - FILE * fp_monit = fopen(sc->local_monitfile, "w"); - if (fp_monit == NULL) - { - MR_WARNING("monit file %s open failed, cannot dump program stat info : %s", - sc->local_monitfile, strerror(errno)); return 0; - } - cJSON * j_root = monit_root(sc); char * str_json_print = cJSON_Print(j_root); + + FILE * fp_monit = fopen(sc->local_monitfile, "w"); + if (fp_monit == NULL) + { + MR_WARNING("monit file %s open failed, cannot dump program stat info : %s", + sc->local_monitfile, strerror(errno)); return 0; + } + fprintf(fp_monit, "%s", str_json_print); - free(j_root); fclose(fp_monit); + free(j_root); + return 0; }
\ No newline at end of file diff --git a/service/src/mrb.c b/service/src/mrb.c index c46f25b..182f192 100644 --- a/service/src/mrb.c +++ b/service/src/mrb.c @@ -114,7 +114,7 @@ static void element_ctrlzone_setup(__attribute__((unused))struct rte_mempool *mp void *opaque, void *obj, __attribute__((unused)) unsigned obj_idx) { struct mrb_main *_handle = (struct mrb_main *)opaque; - struct mrb_priv_zone_t* private = mrbuf_priv(obj); + struct mrb_priv_zone* private = mrbuf_priv(obj); private->cz_num = _handle->ctrlzone_num; private->next = NULL; @@ -536,7 +536,7 @@ int mrb_init(struct sc_main * sc) sc->mrb_pool_main = ZMALLOC(sizeof(struct mrb_main)); MR_VERIFY_MALLOC(sc->mrb_pool_main); - sc->mrb_pool_main->sz_total_ctrlzone = sizeof(struct mrb_priv_zone_t); + sc->mrb_pool_main->sz_total_ctrlzone = sizeof(struct mrb_priv_zone); TAILQ_INIT(&sc->mrb_pool_main->pool_list); /* 读控制域配置 */ diff --git a/tunnat/src/runtime.cc b/tunnat/src/runtime.cc index 9512aa2..7baf945 100644 --- a/tunnat/src/runtime.cc +++ b/tunnat/src/runtime.cc @@ -220,6 +220,8 @@ static int __virt_to_phy_pkt_forward(TunnatInstance * instance, TunnatThreadInst TunnelContainer::PacketForwardModify(tun_start, ctrlzone->__encap_len, tun_type); TUNNAT_THREAD_STAT_ADD(TUNNAT_STAT_FORWARD_TUNNEL_OUTPUT, 1); + + (void)__adj_check; return RT_SUCCESS; } @@ -269,6 +271,7 @@ static int __virt_to_phy_pkt_encap(TunnatInstance * instance, TunnatThreadInstan tun_array.tun_array[i].PacketConstruct(pkt_ptr, pkt_len); } + (void)__adj_check; return RT_SUCCESS; } |
