kk Blog —— 通用基础

date [-d @int|str] [+%s|"+%F %T"]

skb 数据异或

skb 数据异或,可以改进成单独的option

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
diff --git a/run.sh b/run.sh
index 138e664..0eb5286 100755
--- a/run.sh
+++ b/run.sh
@@ -19,3 +19,4 @@ echo 10000 > /proc/sys/net/tcp/digest/digest_err_retry
 echo 0 > /proc/sys/net/ipv4/tcp_timestamps # for mptcp+digest
 
 echo 0 > /proc/sys/net/tcp/mss_adjust
+echo 0 > /proc/sys/net/tcp/digest/digest_xor
diff --git a/src/4.15.18/tcp_ipv4.c b/src/4.15.18/tcp_ipv4.c
index b1b5c9d..b94ba6e 100644
--- a/src/4.15.18/tcp_ipv4.c
+++ b/src/4.15.18/tcp_ipv4.c
@@ -1848,6 +1848,12 @@ int tcp_v4_rcv(struct sk_buff *skb)
  th = (const struct tcphdr *)skb->data;
  iph = ip_hdr(skb);
 
+ if (digest_xor && digest_skb_check(NULL, NULL, skb, 0) != 0) {
+     int idx = 0;
+     skb->ip_summed = CHECKSUM_UNNECESSARY;
+     skb_change_bits(skb, th->doff * 4, skb->len - th->doff * 4, &idx);
+ }
+
 lookup:
  sk = __inet_lookup_skb(&tcp_hashinfo, skb, __tcp_hdrlen(th), th->source,
                 th->dest, sdif, &refcounted);
diff --git a/src/4.15.18/tcp_ipv6.c b/src/4.15.18/tcp_ipv6.c
index 3e27967..e975356 100644
--- a/src/4.15.18/tcp_ipv6.c
+++ b/src/4.15.18/tcp_ipv6.c
@@ -1619,6 +1619,12 @@ int tcp_v6_rcv(struct sk_buff *skb)
  th = (const struct tcphdr *)skb->data;
  hdr = ipv6_hdr(skb);
 
+ if (digest_xor && digest_skb_check(NULL, NULL, skb, 0) != 0) {
+     int idx = 0;
+     skb->ip_summed = CHECKSUM_UNNECESSARY;
+     skb_change_bits(skb, th->doff * 4, skb->len - th->doff * 4, &idx);
+ }
+
 lookup:
  sk = __inet6_lookup_skb(&tcp_hashinfo, skb, __tcp_hdrlen(th),
              th->source, th->dest, inet6_iif(skb), sdif,
diff --git a/src/4.15.18/tcp_output.c b/src/4.15.18/tcp_output.c
index dab8703..f766ff4 100644
--- a/src/4.15.18/tcp_output.c
+++ b/src/4.15.18/tcp_output.c
@@ -1118,6 +1118,9 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb,
      oskb = skb;
 
      tcp_skb_tsorted_save(oskb) {
+         if (digest_xor)
+             skb = skb_copy(oskb, gfp_mask);
+         else
          if (unlikely(skb_cloned(oskb)))
              skb = pskb_copy(oskb, gfp_mask);
          else
@@ -1264,6 +1267,13 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb,
      sk_nocaps_add(sk, NETIF_F_GSO_MASK);
 
      merge = !(tcb->tcp_flags & TCPHDR_SYN);
+
+     if (digest_xor) {
+         int idx = 0;
+         skb_change_bits(skb, tcp_header_size, skb->len - tcp_header_size, &idx);
+         skb->ip_summed = CHECKSUM_NONE;
+         skb->csum = skb_checksum(skb, tcp_header_size, skb->len - tcp_header_size, 0);
+     }
  }
 
  tcp_options_write((__be32 *)(th + 1), tp, &opts, skb, merge);
@@ -3122,6 +3132,9 @@ int __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs)
      struct sk_buff *nskb;
 
      tcp_skb_tsorted_save(skb) {
+         if (digest_xor)
+             nskb = skb_copy(skb, GFP_ATOMIC);
+         else
          nskb = __pskb_copy(skb, MAX_TCP_HEADER, GFP_ATOMIC);
          err = nskb ? tcp_transmit_skb(sk, nskb, 0, GFP_ATOMIC) :
                   -ENOBUFS;
diff --git a/src/digest_core.c b/src/digest_core.c
index 54af869..50840b1 100644
--- a/src/digest_core.c
+++ b/src/digest_core.c
@@ -419,9 +419,106 @@ out:
  return 0;
 }
 
+
+int digest_xor = 0;
+unsigned char MY_XOR[65536];
+
+int __skb_change_bits(unsigned char *from, int len, int *idx)
+{
+ int i;
+ for (i = 0; i < len; i ++) {
+     *(from + i) ^= MY_XOR[*idx];
+     *idx = *idx + 1;
+ }
+ return 0;
+}
+
+int skb_change_bits(struct sk_buff *skb, int offset, int len, int *idx)
+{
+ int start = skb_headlen(skb);
+ struct sk_buff *frag_iter;
+ int i, copy;
+
+ if (offset > (int)skb->len - len)
+     goto fault;
+
+ /* Copy header. */
+ if ((copy = start - offset) > 0) {
+     if (copy > len)
+         copy = len;
+     // skb_copy_from_linear_data_offset(skb, offset, to, copy);
+     __skb_change_bits(skb->data + offset, copy, idx);
+     if ((len -= copy) == 0)
+         return 0;
+     offset += copy;
+ }
+
+ for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
+     int end;
+     skb_frag_t *f = &skb_shinfo(skb)->frags[i];
+
+     WARN_ON(start > offset + len);
+
+     end = start + skb_frag_size(f);
+     if ((copy = end - offset) > 0) {
+         u32 p_off, p_len, copied;
+         struct page *p;
+         u8 *vaddr;
+
+         if (copy > len)
+             copy = len;
+
+#if LINUX_VERSION_CODE > KERNEL_VERSION(4, 0, 0)
+         skb_frag_foreach_page(f,
+                       f->page_offset + offset - start,
+                       copy, p, p_off, p_len, copied) {
+             vaddr = kmap_atomic(p);
+             //memcpy(to + copied, vaddr + p_off, p_len);
+             __skb_change_bits(vaddr + p_off, p_len, idx);
+             kunmap_atomic(vaddr);
+         }
+#else
+         vaddr = kmap_atomic(skb_frag_page(f));
+         __skb_change_bits(vaddr + f->page_offset + offset - start, copy, idx);
+         kunmap_atomic(vaddr);
+#endif
+
+         if ((len -= copy) == 0)
+             return 0;
+         offset += copy;
+     }
+     start = end;
+ }
+
+ skb_walk_frags(skb, frag_iter) {
+     int end;
+
+     WARN_ON(start > offset + len);
+
+     end = start + frag_iter->len;
+     if ((copy = end - offset) > 0) {
+         if (copy > len)
+             copy = len;
+         if (skb_change_bits(frag_iter, offset - start, copy, idx))
+             goto fault;
+         if ((len -= copy) == 0)
+             return 0;
+         offset += copy;
+     }
+     start = end;
+ }
+
+ if (!len)
+     return 0;
+
+fault:
+ return -EFAULT;
+}
+
+
 int digest_init(void)
 {
  int ret;
+ int i;
+ unsigned long int next = 1;
+ for (i = 0; i < 65536; i ++) {
+     next = next * 2 + 1; // TODO
+     MY_XOR[i] = next % 251;
+ }
 
  ret = digest_trace_init();
  if (ret) {
diff --git a/src/digest_core.h b/src/digest_core.h
index 007628c..3ba8c80 100644
--- a/src/digest_core.h
+++ b/src/digest_core.h
@@ -79,6 +79,8 @@ extern atomic_t digest_trace_head_num;
 extern atomic_t digest_trace_free_num;
 extern int digest_trace_log_total;
 
+extern int digest_xor;
+
 extern atomic64_t digest_current, digest_total1, digest_total2, digest_drop_total;
 extern atomic64_t digest_drop_connect;
 
diff --git a/src/digest_sysctl.c b/src/digest_sysctl.c
index 27adfa3..3bfe0ae 100644
--- a/src/digest_sysctl.c
+++ b/src/digest_sysctl.c
@@ -478,6 +478,13 @@ static struct ctl_table digest_sysctl_table[] = {
      .mode = 0644,
      .proc_handler = digest_trace_show_handler,
  },
+ {
+     .procname = "digest_xor",
+     .data = &digest_xor,
+     .maxlen = sizeof(int),
+     .mode = 0644,
+     .proc_handler = proc_dointvec
+ },
  {}
 };

MPTCP sched

1
2
3
4
5
$ git grep mptcp_register_scheduler net/mptcp/
net/mptcp/mptcp_blest.c:        if (mptcp_register_scheduler(&mptcp_sched_blest))
net/mptcp/mptcp_redundant.c:    if (mptcp_register_scheduler(&mptcp_sched_red))
net/mptcp/mptcp_rr.c:   if (mptcp_register_scheduler(&mptcp_sched_rr))
net/mptcp/mptcp_sched.c:int mptcp_register_scheduler(struct mptcp_sched_ops *sched)

blest

redundant

rr

MPTCP cong

不设置时,默认的就是cubic

1
2
3
4
5
6
$ git grep tcp_congestion_ops net/mptcp/
net/mptcp/mctcp_desync.c:static struct tcp_congestion_ops mctcp_desync = {
net/mptcp/mptcp_balia.c:static struct tcp_congestion_ops mptcp_balia = {
net/mptcp/mptcp_coupled.c:static struct tcp_congestion_ops mptcp_ccc = {
net/mptcp/mptcp_olia.c:static struct tcp_congestion_ops mptcp_olia = {
net/mptcp/mptcp_wvegas.c:static struct tcp_congestion_ops mptcp_wvegas __read_mostly = {

desync

balia

ccc

olia

wvegas

MPTCP pm

https://www.cnblogs.com/fenglt/p/8570343.html

http://www.doc88.com/p-2354903996876.html

1
2
3
4
5
6
$ git grep mptcp_register_path_manager net/
net/mptcp/mptcp_binder.c:       if (mptcp_register_path_manager(&binder))
net/mptcp/mptcp_fullmesh.c:     ret = mptcp_register_path_manager(&full_mesh);
net/mptcp/mptcp_ndiffports.c:   if (mptcp_register_path_manager(&ndiffports))
net/mptcp/mptcp_netlink.c:      ret = mptcp_register_path_manager(&mptcp_nl_pm_ops);
net/mptcp/mptcp_pm.c:int mptcp_register_path_manager(struct mptcp_pm_ops *pm)

fullmesh

两边IP都会建连,即建立 n*m 条连接

IP查看 cat /proc/net/mptcp_fullmesh

ndiffports 两边只在一组IP上建立多条连接。

1
2
echo ndiffports > /proc/sys/net/mptcp/mptcp_path_manager
echo 5 > /sys/module/mptcp_ndiffports/parameters/num_subflows # 总共建5条,即额外再建4条

netlink

通过genetlink和应用层交互,在应用层实现pm功能。

binder

将松散源路由(Loose Source and Record Routing, LSRR)选项添加到MPTCP,以确保MPTCP子流使用所有可用网关,即采用LSRR分发子流的数据包。Binder路径管理算法允许用户终端设备从网关直接获得更高的网络吞吐量,而不需要进行任何修改,可用图a和b谁明其工作原理。理论上,只有在网络中设置了LSRR选项,才能使其B>0,从而使得Binder路径管理算法发挥良好效果。

松散源路由:允许相邻两个IP地址之间跳过多个网络。

通俗解释:发送端指明了流量或者数据包必须经过的IP地址清单,但如果需要,也可以经过一些其他的地址。换句话说,不用考虑数据包经过的确切地址,只要它经过这些地址就可以。

数据交换 genlink, 使用nl库

3.x 内核

kernel

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
#include <linux/module.h>
#include <net/netlink.h>
#include <net/genetlink.h>
#include <linux/version.h>

#define MSG_KER "Hello from kernel space!!!"

struct family_hdr {
	int ff;
};

struct data_hdr {
	int len;
	int kk;
	char str[0];
};

/* netlink attributes */
enum {
	DOC_EXMPL_A_UNSPEC,
	DOC_EXMPL_A_MSG,
	DOC_EXMPL_A_MSG2,
	__DOC_EXMPL_A_MAX,
};
#define DOC_EXMPL_A_MAX (__DOC_EXMPL_A_MAX - 1)

/* commands 定义命令类型,用户空间以此来表明需要执行的命令 */
enum {
	DOC_EXMPL_C_UNSPEC,
	DOC_EXMPL_C_ECHO,
	__DOC_EXMPL_C_MAX,
};
#define DOC_EXMPL_C_MAX (__DOC_EXMPL_C_MAX - 1)

static int nl_pre_doit(struct genl_ops*  ops, struct sk_buff* skb, struct genl_info* info)
{
	return 0;
}
static void nl_post_doit(struct genl_ops*  ops, struct sk_buff* skb, struct genl_info* info)
{
	return;
}

/* family definition */
static struct genl_family nl_family = {
	.id = GENL_ID_GENERATE,   //这里不指定family ID,由内核进行分配
	.hdrsize = sizeof(struct family_hdr),             //自定义的头部长度,参考genl数据包结构
	.name = "DOC_EXMPL",      //这里定义family的名称,user program需要根据这个名字来找到对应的family ID。
	.version = 1,
	.maxattr = DOC_EXMPL_A_MAX,
	.pre_doit  = nl_pre_doit,
	.post_doit = nl_post_doit,
};

static int rcv_handler(struct genl_info *info)
{
	struct nlattr *tb[DOC_EXMPL_A_MAX + 1] = {0};
	struct family_hdr *fhdr = (struct family_hdr *)info->userhdr;
	struct data_hdr *hdr;
	int rc, i;
	int rcvlen;

	//printk("kernel recv: %d %d %d %s\n", fhdr->ff, hdr->len, hdr->kk, hdr->str);
	//printk("gg %d\n", info->nlhdr->nlmsg_len - ((u64)info->userhdr - (u64)info->nlhdr) - sizeof(struct family_hdr));
	rc = nla_parse(tb, DOC_EXMPL_A_MAX,
			info->userhdr + sizeof(struct family_hdr),
			info->nlhdr->nlmsg_len - ((u64)info->userhdr - (u64)info->nlhdr) - sizeof(struct family_hdr),
			NULL);
	if (rc) {
		printk("nla_parse err\n");
		return -1;
	}

	for (i = 1; i <= DOC_EXMPL_A_MAX; i ++) {
		if (!tb[i])
			continue;
		rcvlen = nla_len(tb[i]);
		hdr = (struct data_hdr*)nla_data(tb[i]);
		printk("ker-rcv: %d %d %d %d rcvlen=%d %s\n", i, fhdr->ff, hdr->len, hdr->kk, rcvlen, hdr->str);
	}
	return 0;
}


static int snd_handler(struct genl_info *info)
{
	struct sk_buff *reply = NULL;
	char sss[123];
	struct family_hdr *snd_fhdr;
	struct data_hdr *snd_hdr = (struct data_hdr *)sss;
	int err;

	reply = genlmsg_new(NLMSG_ALIGN(sizeof(struct family_hdr)) + NLMSG_ALIGN(sizeof(struct data_hdr)) + nla_total_size(strlen(MSG_KER)), GFP_KERNEL);
	if (!reply) {
		printk("genlmsg_new err\n");
		return -1;
	}

	snd_fhdr = (struct family_hdr *)genlmsg_put(reply, info->snd_portid, info->snd_seq, &nl_family, 0, DOC_EXMPL_C_ECHO);
	snd_fhdr->ff = 222;
	snd_hdr->len = 321;
	snd_hdr->kk = 654;
	strcpy(snd_hdr->str, MSG_KER);
	err = nla_put(reply, DOC_EXMPL_A_MSG, sizeof(struct data_hdr) + strlen(snd_hdr->str), snd_hdr);
	if (err) {
		printk("nla_put err\n");
		genlmsg_cancel(reply, snd_fhdr);
		return -2;
	}

	snd_hdr->len = 987;
	snd_hdr->kk = 789;
	err = nla_put(reply, DOC_EXMPL_A_MSG2, sizeof(struct data_hdr) + strlen(snd_hdr->str), snd_hdr);
	if (err) {
		printk("nla_put err2\n");
		genlmsg_cancel(reply, snd_fhdr);
		return -2;
	}

	genlmsg_end(reply, snd_fhdr);

	err = genlmsg_reply(reply, info);
	if (err)
		printk("genlmsg_reply err\n");

	return 0;
}

static int nl_echo(struct sk_buff *skb, struct genl_info *info)
{
	rcv_handler(info);
	snd_handler(info);
	return 0;
}

static struct genl_ops nl_ops[] = {
	{
		.cmd  = DOC_EXMPL_C_ECHO,
		.doit = nl_echo,
		.flags    = GENL_ADMIN_PERM,
	},
};

int genetlink_init(void)
{
	return genl_register_family_with_ops(&nl_family, nl_ops, ARRAY_SIZE(nl_ops));
}

void genetlink_exit(void)
{
	genl_unregister_family(&nl_family);
	printk("Generic Netlink_nl Example Module unloaded.\n");
}

module_init(genetlink_init);
module_exit(genetlink_exit);
MODULE_LICENSE("GPL");
1
2
3
4
5
6
7
8
9
10
11
obj-m += genlink_nl.o

KDIR := /usr/src/kernels/`uname -r`/

PWD := `pwd`

default:
	make -C $(KDIR) M=$(PWD) modules

clean:
	rm -rf *.ko *.o *.mod.c .*.cmd .tmp_versions Module.symvers modules.order

user

gcc user_nl.c -lnl

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
// gcc user_nl.c -lnl
//
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <poll.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <signal.h>
#include <asm/types.h>

//#include <linux/genetlink.h>
#include <netlink/socket.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/family.h>
#include <netlink/genl/ctrl.h>
#include <netlink/msg.h>

#define MESSAGE_TO_KERNEL   "Hello World from user space!!"

struct family_hdr {
	int ff;
};

struct data_hdr {
	int len;
	int kk;
	char str[0];
};

/* netlink attributes */
enum {
	DOC_EXMPL_A_UNSPEC,
	DOC_EXMPL_A_MSG,
	DOC_EXMPL_A_MSG2,
	__DOC_EXMPL_A_MAX,
};
#define DOC_EXMPL_A_MAX (__DOC_EXMPL_A_MAX - 1)

//copy from kernel driver genl_ops's cmd
enum {
	DOC_EXMPL_C_UNSPEC,
	DOC_EXMPL_C_ECHO,
	__DOC_EXMPL_C_MAX,
};
#define DOC_EXMPL_C_MAX (__DOC_EXMPL_C_MAX - 1)

static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
{
	int *ret = arg;
	fprintf(stderr, "error\n");
	*ret = err->error;
	return NL_STOP;
}

static int finish_handler(struct nl_msg *msg, void *arg)
{
	int *ret = arg;
	*ret = 0;
	fprintf(stderr, "finish\n");
	return NL_SKIP;
}

static int ack_handler(struct nl_msg *msg, void *arg)
{
	int *ret = arg;
	*ret = 0;
	fprintf(stderr, "ack\n");
	return NL_STOP;
}

static int reply_handler(struct nl_msg *msg, void *arg)
{
	struct nlattr *tb[DOC_EXMPL_A_MAX + 1] = {0};
	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
	struct family_hdr *fhdr = genlmsg_attrdata(gnlh, 0);
	struct data_hdr *hdr;
	int rc, i;
	int rcvlen;

	rc = nla_parse(tb, DOC_EXMPL_A_MAX,
			genlmsg_attrdata(gnlh, sizeof(struct family_hdr)),
			genlmsg_attrlen(gnlh, sizeof(struct family_hdr)), NULL);
	if (rc) {
		printf("nla_parse err\n");
		return NL_STOP;
	}

	for (i = 1; i <= DOC_EXMPL_A_MAX; i ++) {
		rcvlen = nla_len(tb[i]);
		hdr = (struct data_hdr*)nla_data(tb[i]);
		printf("rcv: %d %d %d %d rcvlen=%d %s\n", i, fhdr->ff, hdr->len, hdr->kk, rcvlen, hdr->str);
	}
	return NL_OK;
}

static int test_genlink_nl(void)
{
	struct nl_handle *nlsk;
	int nlid, err, ret;

	struct nl_msg *msg = NULL;
	struct nl_cb *cb = NULL;

	char cbdata[123], sss[123];
	struct family_hdr *fhdr;
	struct data_hdr *hdr = (struct data_hdr *)sss;

	nlsk = nl_handle_alloc();
	if (!nlsk) {
		printf("nl_socket_alloc err\n");
		return -1;
	}

	nl_set_buffer_size(nlsk, 8192, 8192);

	if (genl_connect(nlsk)) {
		printf("genl_connect err\n");
		goto free_nlsk;
	}

	nlid = genl_ctrl_resolve(nlsk, "DOC_EXMPL");
	if (nlid < 0) {
		printf("genl_ctrl_resolve err\n");
		goto free_nlsk;
	}

	msg = nlmsg_alloc();
	if (!msg) {
		printf("nlmsg_alloc err\n");
		goto free_nlsk;
	}

	cb = nl_cb_alloc(NL_CB_DEFAULT);
	if (!cb) {
		printf("nl_cb_alloc err\n");
		goto free_msg;
	}
	nl_socket_set_cb(nlsk, cb);

	fhdr = (struct family_hdr *)genlmsg_put(msg, 0, 0, nlid, sizeof(struct family_hdr), 0, DOC_EXMPL_C_ECHO, 0);
	if (!fhdr) {
		printf("genlmsg_put err\n");
		goto free_cb;
	}
	fhdr->ff = 111;

	hdr->len = 123;
	hdr->kk = 456;
	strcpy(hdr->str, MESSAGE_TO_KERNEL);
	ret = nla_put(msg, DOC_EXMPL_A_MSG, sizeof(struct data_hdr)+strlen(MESSAGE_TO_KERNEL), hdr);
	if (ret < 0) {
		printf("nla_put err\n");
		goto free_cb;
	}

	hdr->len = 700;
	hdr->kk = 800;
	ret = nla_put(msg, DOC_EXMPL_A_MSG2, sizeof(struct data_hdr)+strlen(MESSAGE_TO_KERNEL), hdr);
	if (ret < 0) {
		printf("nla_put err2\n");
		goto free_cb;
	}

	ret = nl_send_auto_complete(nlsk, msg);
	if (ret < 0) {
		printf("nl_send_auto_complete err\n");
		goto free_cb;
	}

	err = 1;
	nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
	nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
	nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
	nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, reply_handler, &cbdata);

	ret = nl_recvmsgs(nlsk, cb);
	if (ret) {
		printf("nl_recvmsgs err\n");
	}

	return err;

free_cb:
	nl_cb_put(cb);
free_msg:
	nlmsg_free(msg);
free_nlsk:
	nl_handle_destroy(nlsk);
	return -2;
}

int main(int argc, char *argv[])
{
	return test_genlink_nl();
}