diff --git a/subsys/net/ip/net_pkt.c b/subsys/net/ip/net_pkt.c index 501ee43d5963f..94b0a9a6c90be 100644 --- a/subsys/net/ip/net_pkt.c +++ b/subsys/net/ip/net_pkt.c @@ -1163,6 +1163,8 @@ u16_t net_pkt_append(struct net_pkt *pkt, u16_t len, const u8_t *data, s32_t timeout) { struct net_buf *frag; + struct net_context *ctx; + size_t max_len; if (!pkt || !data) { return 0; @@ -1177,6 +1179,40 @@ u16_t net_pkt_append(struct net_pkt *pkt, u16_t len, const u8_t *data, net_pkt_frag_add(pkt, frag); } + /* Make sure we don't send more data in one packet than + * MTU allows. + * This check really belongs to net_pkt_append_bytes(), + * but instead done here for efficiency, we assume + * (at least for now) that that callers of + * net_pkt_append_bytes() are smart enough to not + * overflow MTU. + */ + max_len = 0x10000; + + ctx = net_pkt_context(pkt); + if (ctx) { +#if defined(CONFIG_NET_TCP) + if (ctx->tcp) { + max_len = ctx->tcp->send_mss; + } else +#endif + { + struct net_if *iface = net_context_get_iface(ctx); + max_len = net_if_get_mtu(iface); + /* Optimize for number of jumps in the code ("if" + * instead of "if/else"). + */ + max_len -= NET_IPV4TCPH_LEN; + if (net_context_get_family(ctx) != AF_INET) { + max_len -= NET_IPV6TCPH_LEN - NET_IPV4TCPH_LEN; + } + } + } + + if (len > max_len) { + len = max_len; + } + return net_pkt_append_bytes(pkt, data, len, timeout); } diff --git a/subsys/net/lib/sockets/sockets.c b/subsys/net/lib/sockets/sockets.c index a2f59e15fdd42..dd7d0b845dc94 100644 --- a/subsys/net/lib/sockets/sockets.c +++ b/subsys/net/lib/sockets/sockets.c @@ -235,7 +235,6 @@ ssize_t zsock_sendto(int sock, const void *buf, size_t len, int flags, struct net_pkt *send_pkt; s32_t timeout = K_FOREVER; struct net_context *ctx = INT_TO_POINTER(sock); - size_t max_len = net_if_get_mtu(net_context_get_iface(ctx)); ARG_UNUSED(flags); @@ -249,18 +248,6 @@ ssize_t zsock_sendto(int sock, const void *buf, size_t len, int flags, return -1; } - /* Make sure we don't send more data in one packet than - * MTU allows. Optimize for number of branches in the code. - */ - max_len -= NET_IPV4TCPH_LEN; - if (net_context_get_family(ctx) != AF_INET) { - max_len -= NET_IPV6TCPH_LEN - NET_IPV4TCPH_LEN; - } - - if (len > max_len) { - len = max_len; - } - len = net_pkt_append(send_pkt, len, buf, timeout); if (!len) { net_pkt_unref(send_pkt);