【Linux4.1.12源码分析】vxlan报文发送之iptunnel_xmit

iptunnel_xmit函数是发送vxlan报文时,封装UDP报文头之后被调用的,主要作用是封装IP头,并调用三层发包函数,完成报文的发送,该函数相对比较简单。

1、iptunnel_xmit函数

int iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
		  __be32 src, __be32 dst, __u8 proto,
		  __u8 tos, __u8 ttl, __be16 df, bool xnet)
{
	int pkt_len = skb->len;
	struct iphdr *iph;
	int err;

	skb_scrub_packet(skb, xnet);			//清空skb相关信息

	skb_clear_hash(skb);				//清空skb hash值
	skb_dst_set(skb, &rt->dst);			//rt必须根据外层报文的IP地址及相关信息获取到的
	memset(IPCB(skb), 0, sizeof(*IPCB(skb)));	//清空skb IPCB的内容

	/* Push down and install the IP header. */
	skb_push(skb, sizeof(struct iphdr));		//skb报文添加IP头
	skb_reset_network_header(skb);			//设置IP头的偏移

	iph = ip_hdr(skb);

	iph->version	=	4;
	iph->ihl	=	sizeof(struct iphdr) >> 2;
	iph->frag_off	=	df;			//OVS2.5默认有df标记
	iph->protocol	=	proto;			//vxlan,该协议为UDP
	iph->tos	=	tos;
	iph->daddr	=	dst;
	iph->saddr	=	src;
	iph->ttl	=	ttl;
	__ip_select_ident(dev_net(rt->dst.dev), iph,		//计算IP报文的ID值
			  skb_shinfo(skb)->gso_segs ?: 1);

	err = ip_local_out_sk(sk, skb);				//调用ip层发送函数
	if (unlikely(net_xmit_eval(err)))
		pkt_len = 0;
	return pkt_len;
}
vxlan报文的发送流程到此,就进入了标准的三层报文发送,与非vxlan封装的报文是基本一致的(此类报文会标记encapsulation,会影响GSO分段)。

你可能感兴趣的:(Linux4.1.12源码分析)