kk Blog —— 通用基础


date [-d @int|str] [+%s|"+%F %T"]
netstat -ltunp
sar -n DEV 1

ipt_CLUSTERIP

Linux内核自带了一种多机高可用性的解决方法。在每台机器上配置一个相同ip、mac的多播地址。可以给CLUSTERIP设置多个node,经过这个地址的数据流会按一定算法分配到某个node上。只有持有某个node的机器会收、发属于这个node的数据流,其他机器会drop数据包。

说明:

1) 同一个CLUSTERIP的同一个node可以配置在多台机器上,这样每台都会处理包,但这种情形不是想要的。

2) node 可以只有一个,这种情况下流经CLUSTERIP的数据流都属于这个node

3) 添加CLUSTERIP命令:

1
iptables -I INPUT -d 100.64.1.2 -i eth2 -p icmp -j CLUSTERIP --new --clustermac 01:00:5E:44:55:66 --total-nodes 2 --local-node 1 --hashmode sourceip

4) 切换node的方法:

1
2
echo +1 > /proc/net/ipt_CLUSTERIP/xx.xx.xx.xx
echo -2 > /proc/net/ipt_CLUSTERIP/xx.xx.xx.xx

http://www.linux-ha.org/ClusterIP

http://www.rkeene.org/projects/info/wiki/102

https://www.douban.com/note/389412837/

modprobe ipt_CLUSTERIP

这样就会在/proc/net下创建个ipt_CLUSTERIP目录。

使用iptables创建一条规则,这条规则同时也会创建一个clusterip_config,和规则是一一对应的,同样在

/proc/net/ipt_CLUSTERIP下也会创建一个文件,文件名是虚拟服务器的ip (vip)。在iptables里指定。

在node1上执行

iptables -I INPUT -d 192.168.122.33(这个就个vip) -i eth0 (哪个接口属于集群) -p icmp -j CLUSTERIP –new(必须有的) –hashmode sourceip(用source ip做hash,还有其他的?) –clustermac 01:00:5E:7F:18:0A(虚拟服务器的mac地址,必须为多播地址) –total-nodes 2(这个集群里一共有几台机器) –local-node 1(本地机器编号,只有编号为1的机器才有权力与客户端通信)。这个命令会将为eth0设备添加这个多播地址,见dev_mc_add。

添加完命令后,给这个Node加个ip

ifconfig eth0:0 192.168.122.33, 这样node的内核才回相应arp请求。 所以,客户端在想访问192.168.122.33前,会发送一个arp请求,node1收到后,看到这个ip是自己的,于是便回复你找的就是我,把自己的mac地址发送给客户端。当这个arp回复经过OUTPUT链的arp_mangle钩子时,这个函数会判断 这个arp回复里的消息,如果发现客户端想知道vip的mac地址,就把这个mac地址修改为clustermac,也就是01:00:5E:7F:18:0A。

这样以后客户端发的数据包就会变成广播包了, 所有的node ip都是192.168.122.33,所以所有的node都会收到这个消息,但是只有node1才有权力回复消息。其他node会在clusterip target里把数据包丢掉。

CLUSTERIP TARGET还有一个作用,就是把数据包pky_type修改为PACKET_HOST,让上层乐意收包。

shell 获取脚本的绝对路径

https://www.cnblogs.com/FlyFive/p/3640267.html

常见的一种误区,是使用 pwd 命令

该命令的作用是“print name of current/working directory”,这才是此命令的真实含义,当前的工作目录,这里没有任何意思说明,这个目录就是脚本存放的目录。所以,这是不对的。你可以试试 bash shell/a.sh,a.sh 内容是 pwd,你会发现,显示的是执行命令的路径 /home/june,并不是 a.sh 所在路径:/home/june/shell/a.sh

另一个误人子弟的答案,是 $0

这个也是不对的,这个$0是Bash环境下的特殊变量,其真实含义是:

Expands to the name of the shell or shell script. This is set at shell initialization. If bash is invoked with a file of commands, $0 is set to the name of that file. If bash is started with the -c option, then $0 is set to the first argument after the string to be executed, if one is present. Otherwise, it is set to the file name used to invoke bash, as given by argument zero.

这个$0有可能是好几种值,跟调用的方式有关系:

使用一个文件调用bash,那$0的值,是那个文件的名字(没说是绝对路径噢)

使用-c选项启动bash的话,真正执行的命令会从一个字符串中读取,字符串后面如果还有别的参数的话,使用从$0开始的特殊变量引用(跟路径无关了)

除此以外,$0会被设置成调用bash的那个文件的名字(没说是绝对路径)

正确:

1
basepath=$(cd `dirname $0`; pwd)

dirname $0,取得当前执行的脚本文件的父目录

cd dirname $0,进入这个目录(切换当前工作目录)

pwd,显示当前工作目录(cd执行后的)

SKB路由缓存与SOCK路由缓存

https://www.2cto.com/kf/201805/745174.html

skb结构体中的成员 _skb_refdst 与sock结构体中成员sk_rx_dst(缓存入口路由)和sk_dst_cache(缓存出口路由)成员之间的交互操作。

SOCK入口路由与SKB路由缓存

内核在接收流程中,调用early_demux函数提前在IP层做established状态的sock查找,并负责将sock结构体成员sk_rx_dst的路由缓存赋值给skb成员_skb_refdst,对于UDP协议,先判断DST_NOCACHE标志,如果成立,增加dst引用计数,设置skb的dst;否则,调用skb_dst_set_noref直接进行设置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void udp_v4_early_demux(struct sk_buff *skb)
{
	dst = READ_ONCE(sk->sk_rx_dst);
 
	if (dst)
		dst = dst_check(dst, 0);
	if (dst) {
		/* DST_NOCACHE can not be used without taking a reference */
		if (dst->flags & DST_NOCACHE) {
			if (likely(atomic_inc_not_zero(&dst->__refcnt)))
				skb_dst_set(skb, dst);
		} else {
			skb_dst_set_noref(skb, dst);
		}
	}
}

对于TCP协议,直接调用skb_dst_set_noref函数,将sock结构体成员sk_rx_dst缓存到skb结构体中。

1
2
3
4
5
6
7
8
9
10
11
12
void tcp_v4_early_demux(struct sk_buff *skb)
{
	if (sk->sk_state != TCP_TIME_WAIT) {
		struct dst_entry *dst = sk->sk_rx_dst;
 
		if (dst)
			dst = dst_check(dst, 0);
		if (dst &&
			inet_sk(sk)->rx_dst_ifindex == skb->skb_iif)
			skb_dst_set_noref(skb, dst);
	}
}

同样都为early_demux函数,都是从sk->sk_rx_dst获取路由缓存,tcp和udp的存在明显差别。TCP直接赋值,UDP需要先判断DST_NOCACHE标志。此情况是由UDP与TCP在sock中缓存dst时的处理不同造成的,TCP预先调用了dst_hold_safe函数,进行了DST_NOCACHE标志的判断处理,如未缓存则增加了引用计数。然而,UDP在缓存路由dst时,使用xchg函数,未判断也未增加引用计数,所以需要在后续判断处理。

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
static inline bool dst_hold_safe(struct dst_entry *dst)
{
	if (dst->flags & DST_NOCACHE)
		return atomic_inc_not_zero(&dst->__refcnt);
	dst_hold(dst);
}
 
void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb)
{
	struct dst_entry *dst = skb_dst(skb);
 
	if (dst && dst_hold_safe(dst)) {
		sk->sk_rx_dst = dst;
		inet_sk(sk)->rx_dst_ifindex = skb->skb_iif;
	}
}
 
static void udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
{
	struct dst_entry *old;
 
	dst_hold(dst);
	old = xchg(&sk->sk_rx_dst, dst);
	dst_release(old);
}

SOCK出口路由与SKB路由缓存

对于UDP协议客户端,其在connect时(UDP客户端connect不同于TCP,仅绑定通信端地址),查询路由,缓存到sock结构体的sk_dst_cache中。

1
2
3
4
5
int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
{
	rt = ip_route_connect(...);
	sk_dst_set(sk, &rt->dst);
}

之后,发送UDP数据包时,检查sock结构体中的出口路由是否有效,有效的话可不用再次查询路由表,在函数ip_make_skb中直接使用rt,并且调用skb_dst_set赋值给skb的_skb_refdst结构体,以便在发送过程中使用。

对于UDP服务端,在首次发包检测到rt为空时,查询路由表得到出口路由,缓存在sock结构中,之后发包时rt有效,省去再次查询。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct sk_buff *__ip_make_skb(...)
{
	skb_dst_set(skb, &rt->dst);
}
 
int udp_sendmsg(...)
{
	if (connected)
		rt = (struct rtable *)sk_dst_check(sk, 0);
	if (rt == NULL) {
		rt = ip_route_output_flow(net, fl4, sk);
		if (connected)
			sk_dst_set(sk, dst_clone(&rt->dst));
	}
 
	skb = ip_make_skb(sk, fl4, getfrag, msg->msg_iov, ulen,
			sizeof(struct udphdr), &ipc, &rt,
			msg->msg_flags);
}

IP层发送数据包时(调用ip_queue_xmit),检测sock结构中出口路由缓存,如果有效,设置到skb结构体中。否则重新进行出口路由查找。

1
2
3
4
5
6
7
8
9
int ip_queue_xmit(struct sk_buff *skb, struct flowi *fl)
{
	rt = (struct rtable *)__sk_dst_check(sk, 0);
	if (rt == NULL) {
		rt = ip_route_output_ports(...);
		sk_setup_caps(sk, &rt->dst);
	}
	skb_dst_set_noref(skb, &rt->dst);
}

内核版本

linux-3.10.0