]> granicus.if.org Git - strace/blob - tests/nlattr_smc_diag_msg.c
strace: terminate itself if interrupted by a signal
[strace] / tests / nlattr_smc_diag_msg.c
1 /*
2  * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3  * Copyright (c) 2017-2018 The strace developers.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8
9 #include "tests.h"
10 #include <sys/socket.h>
11
12 #ifndef AF_SMC
13 # define AF_SMC 43
14 #endif
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdint.h>
19 #include <arpa/inet.h>
20 #include "test_nlattr.h"
21 #include <linux/rtnetlink.h>
22 #include <linux/smc_diag.h>
23 #include <linux/sock_diag.h>
24
25 #ifndef SMC_CLNT
26 # define SMC_CLNT 0
27 #endif
28 #ifndef SMC_ACTIVE
29 # define SMC_ACTIVE 1
30 #endif
31
32 static const char address[] = "12.34.56.78";
33
34 static void
35 init_smc_diag_msg(struct nlmsghdr *const nlh, const unsigned int msg_len)
36 {
37         SET_STRUCT(struct nlmsghdr, nlh,
38                 .nlmsg_len = msg_len,
39                 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
40                 .nlmsg_flags = NLM_F_DUMP
41         );
42
43         struct smc_diag_msg *const msg = NLMSG_DATA(nlh);
44         SET_STRUCT(struct smc_diag_msg, msg,
45                 .diag_family = AF_SMC,
46                 .diag_state = SMC_ACTIVE
47         );
48
49         if (!inet_pton(AF_INET, address, msg->id.idiag_src) ||
50             !inet_pton(AF_INET, address, msg->id.idiag_dst))
51                 perror_msg_and_skip("inet_pton");
52 }
53
54 static void
55 print_smc_diag_msg(const unsigned int msg_len)
56 {
57         printf("{len=%u, type=SOCK_DIAG_BY_FAMILY"
58                ", flags=NLM_F_DUMP, seq=0, pid=0}"
59                ", {diag_family=AF_SMC, diag_state=SMC_ACTIVE"
60                ", diag_fallback=SMC_DIAG_MODE_SMCR, diag_shutdown=0"
61                ", id={idiag_sport=htons(0), idiag_dport=htons(0)"
62                ", idiag_src=inet_addr(\"%s\")"
63                ", idiag_dst=inet_addr(\"%s\")"
64                ", idiag_if=0, idiag_cookie=[0, 0]}"
65                ", diag_uid=0, diag_inode=0}",
66                msg_len, address, address);
67 }
68
69 #define PRINT_FIELD_SMC_DIAG_CURSOR(prefix_, where_, field_)            \
70         do {                                                            \
71                 printf("%s%s=", (prefix_), #field_);                    \
72                 PRINT_FIELD_U("{", (where_).field_, reserved);          \
73                 PRINT_FIELD_U(", ", (where_).field_, wrap);             \
74                 PRINT_FIELD_U(", ", (where_).field_, count);            \
75                 printf("}");                                            \
76         } while (0)
77
78 int main(void)
79 {
80         skip_if_unavailable("/proc/self/fd/");
81
82         static const struct smc_diag_conninfo cinfo = {
83                 .token = 0xabcdefac,
84                 .sndbuf_size = 0xbcdaefad,
85                 .rmbe_size = 0xcdbaefab,
86                 .peer_rmbe_size = 0xdbcdedaf,
87                 .rx_prod = {
88                         .reserved = 0xabc1,
89                         .wrap = 0xbca1,
90                         .count = 0xcdedbad1
91                 },
92                 .rx_cons = {
93                         .reserved = 0xabc2,
94                         .wrap = 0xbca2,
95                         .count = 0xcdedbad2
96                 },
97                 .tx_prod = {
98                         .reserved = 0xabc3,
99                         .wrap = 0xbca3,
100                         .count = 0xcdedbad3
101                 },
102                 .tx_cons = {
103                         .reserved = 0xabc4,
104                         .wrap = 0xbca4,
105                         .count = 0xcdedbad4
106                 },
107                 .rx_prod_flags = 0xff,
108                 .rx_conn_state_flags = 0xff,
109                 .tx_prod_flags = 0xff,
110                 .tx_conn_state_flags = 0xff,
111                 .tx_prep = {
112                         .reserved = 0xabc5,
113                         .wrap = 0xbca5,
114                         .count = 0xcdedbad5
115                 },
116                 .tx_sent = {
117                         .reserved = 0xabc6,
118                         .wrap = 0xbca6,
119                         .count = 0xcdedbad6
120                 },
121                 .tx_fin = {
122                         .reserved = 0xabc7,
123                         .wrap = 0xbca7,
124                         .count = 0xcdedbad7
125                 }
126         };
127         static const struct smc_diag_lgrinfo linfo = {
128                 .lnk[0] = {
129                         .link_id = 0xaf,
130                         .ibport = 0xfa,
131                         .ibname = "123",
132                         .gid = "456",
133                         .peer_gid = "789"
134                 },
135                 .role = SMC_CLNT
136         };
137         static const struct smcd_diag_dmbinfo dinfo = {
138                 .linkid     = 0xdeadc0de,
139                 .peer_gid   = 0xbefeededbadc0dedULL,
140                 .my_gid     = 0xdeec0dedfacebeefULL,
141                 .token      = 0xcafedecaffeedeedULL,
142                 .peer_token = 0xfeedfacebeeff00dULL,
143         };
144         static const struct smc_diag_fallback fb1 = {
145                 .reason         = 0,
146                 .peer_diagnosis = 0x03020000,
147         };
148         static const struct smc_diag_fallback fb2 = {
149                 .reason         = 0x03060000,
150                 .peer_diagnosis = 0x99999999,
151         };
152         static uint8_t sd1 = 0x23;
153         static uint8_t sd2 = 0x40;
154
155         int fd = create_nl_socket(NETLINK_SOCK_DIAG);
156         const unsigned int hdrlen = sizeof(struct smc_diag_msg);
157         void *const nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
158                                          NLA_HDRLEN +
159                                          MAX(sizeof(cinfo), sizeof(linfo)));
160
161         static char pattern[4096];
162         fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
163
164         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
165                            init_smc_diag_msg, print_smc_diag_msg,
166                            SMC_DIAG_SHUTDOWN, pattern, sd1,
167                            printf("RCV_SHUTDOWN|SEND_SHUTDOWN|0x20"));
168
169         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
170                            init_smc_diag_msg, print_smc_diag_msg,
171                            SMC_DIAG_SHUTDOWN, pattern, sd2,
172                            printf("0x40 /* ???_SHUTDOWN */"));
173
174         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
175                            init_smc_diag_msg, print_smc_diag_msg,
176                            SMC_DIAG_CONNINFO, pattern, cinfo,
177                            PRINT_FIELD_U("{", cinfo, token);
178                            PRINT_FIELD_U(", ", cinfo, sndbuf_size);
179                            PRINT_FIELD_U(", ", cinfo, rmbe_size);
180                            PRINT_FIELD_U(", ", cinfo, peer_rmbe_size);
181                            PRINT_FIELD_SMC_DIAG_CURSOR(", ", cinfo, rx_prod);
182                            PRINT_FIELD_SMC_DIAG_CURSOR(", ", cinfo, rx_cons);
183                            PRINT_FIELD_SMC_DIAG_CURSOR(", ", cinfo, tx_prod);
184                            PRINT_FIELD_SMC_DIAG_CURSOR(", ", cinfo, tx_cons);
185                            printf(", rx_prod_flags=0xff");
186                            printf(", rx_conn_state_flags=0xff");
187                            printf(", tx_prod_flags=0xff");
188                            printf(", tx_conn_state_flags=0xff");
189                            PRINT_FIELD_SMC_DIAG_CURSOR(", ", cinfo, tx_prep);
190                            PRINT_FIELD_SMC_DIAG_CURSOR(", ", cinfo, tx_sent);
191                            PRINT_FIELD_SMC_DIAG_CURSOR(", ", cinfo, tx_fin);
192                            printf("}"));
193
194         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
195                            init_smc_diag_msg, print_smc_diag_msg,
196                            SMC_DIAG_LGRINFO, pattern, linfo,
197                            PRINT_FIELD_U("{lnk[0]={", linfo.lnk[0], link_id);
198                            printf(", ibname=\"%s\"", linfo.lnk[0].ibname);
199                            PRINT_FIELD_U(", ", linfo.lnk[0], ibport);
200                            printf(", gid=\"%s\"", linfo.lnk[0].gid);
201                            printf(", peer_gid=\"%s\"}", linfo.lnk[0].peer_gid);
202                            printf(", role=SMC_CLNT}"));
203
204         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
205                            init_smc_diag_msg, print_smc_diag_msg,
206                            SMC_DIAG_DMBINFO, pattern, dinfo,
207                            PRINT_FIELD_U("{", dinfo, linkid);
208                            PRINT_FIELD_X(", ", dinfo, peer_gid);
209                            PRINT_FIELD_X(", ", dinfo, my_gid);
210                            PRINT_FIELD_X(", ", dinfo, token);
211                            PRINT_FIELD_X(", ", dinfo, peer_token);
212                            printf("}"));
213
214         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
215                            init_smc_diag_msg, print_smc_diag_msg,
216                            SMC_DIAG_FALLBACK, pattern, fb1,
217                            printf("{reason=0 /* SMC_CLC_DECL_??? */");
218                            printf(", peer_diagnosis=0x3020000"
219                                   " /* SMC_CLC_DECL_IPSEC */}"));
220
221         TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
222                            init_smc_diag_msg, print_smc_diag_msg,
223                            SMC_DIAG_FALLBACK, pattern, fb2,
224                            printf("{reason=0x3060000"
225                                   " /* SMC_CLC_DECL_OPTUNSUPP */");
226                            printf(", peer_diagnosis=0x99999999"
227                                   " /* SMC_CLC_DECL_??? */}"));
228
229         printf("+++ exited with 0 +++\n");
230         return 0;
231 }