]> git.proxmox.com Git - ovs.git/blame - tests/test-netlink-conntrack.c
raft: Fix the problem of stuck in candidate role forever.
[ovs.git] / tests / test-netlink-conntrack.c
CommitLineData
2c06d9a9
DDP
1/*
2 * Copyright (c) 2015 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <config.h>
18
b4409ed6 19#include <stdlib.h>
2c06d9a9
DDP
20#include <linux/netfilter/nfnetlink.h>
21
22#include "ct-dpif.h"
23#include "netlink-conntrack.h"
24#include "netlink-notifier.h"
25#include "ovstest.h"
fd016ae3 26#include "openvswitch/poll-loop.h"
2c06d9a9
DDP
27
28/* Monitor command */
29struct test_change {
30 enum nl_ct_event_type type;
31 struct ct_dpif_entry entry;
32};
33
77ee67e4 34static int
2c06d9a9
DDP
35event_parse(struct ofpbuf *buf, void *change_)
36{
37 struct test_change *change = change_;
38
77ee67e4
JR
39 if (nl_ct_parse_entry(buf, &change->entry, &change->type)) {
40 switch (change->type) {
41 case NL_CT_EVENT_NEW:
42 return NFNLGRP_CONNTRACK_NEW;
43 case NL_CT_EVENT_UPDATE:
44 return NFNLGRP_CONNTRACK_UPDATE;
45 case NL_CT_EVENT_DELETE:
46 return NFNLGRP_CONNTRACK_DESTROY;
47 }
48 }
49 return 0;
2c06d9a9
DDP
50}
51
52static void
53event_print(const void *change_, void *aux OVS_UNUSED)
54{
55 const struct test_change *change = change_;
56
57 if (change) {
58 struct ds ds = DS_EMPTY_INITIALIZER;
59
60 nl_ct_format_event_entry(&change->entry, change->type, &ds, true,
61 true);
62 printf("%s\n", ds_cstr(&ds));
63 ds_destroy(&ds);
64 }
65}
66
67static void
68test_nl_ct_monitor(struct ovs_cmdl_context *ctx OVS_UNUSED)
69{
70 int groups [] = {
71 NFNLGRP_CONNTRACK_DESTROY,
72 NFNLGRP_CONNTRACK_NEW,
73 NFNLGRP_CONNTRACK_UPDATE,
74 };
75
77ee67e4 76 struct nln *nln;
2c06d9a9
DDP
77 struct nln_notifier *notifiers[ARRAY_SIZE(groups)];
78
79 struct test_change change;
80
81 unsigned i;
82
77ee67e4 83 nln = nln_create(NETLINK_NETFILTER, event_parse, &change);
2c06d9a9 84
77ee67e4
JR
85 for (i = 0; i < ARRAY_SIZE(groups); i++) {
86 notifiers[i] = nln_notifier_create(nln, groups[i], event_print, NULL);
2c06d9a9
DDP
87 }
88
89 for (;;) {
77ee67e4
JR
90 nln_run(nln);
91 nln_wait(nln);
2c06d9a9
DDP
92 poll_block();
93 }
94
95 for (i = 0; i < ARRAY_SIZE(groups); i++) {
96 nln_notifier_destroy(notifiers[i]);
2c06d9a9 97 }
77ee67e4 98 nln_destroy(nln);
2c06d9a9
DDP
99}
100\f
101/* Dump command */
102static void
103test_nl_ct_dump(struct ovs_cmdl_context *ctx)
104{
105 struct nl_ct_dump_state *dump;
106 uint16_t zone, *pzone = NULL;
107 struct ct_dpif_entry entry;
108 int err;
ded30c74 109 int tot_bkts;
2c06d9a9
DDP
110
111 if (ctx->argc >= 2) {
112 if (!ovs_scan(ctx->argv[1], "zone=%"SCNu16, &zone)) {
113 ovs_fatal(0, "Error parsing zone= specifier");
114 }
115 pzone = &zone;
116 }
ded30c74 117 err = nl_ct_dump_start(&dump, pzone, &tot_bkts);
2c06d9a9
DDP
118 if (err) {
119 ovs_fatal(err, "Error creating conntrack netlink dump");
120 }
121
122 do {
123 err = nl_ct_dump_next(dump, &entry);
124 if (!err) {
125 struct ds ds = DS_EMPTY_INITIALIZER;
126
127 ct_dpif_format_entry(&entry, &ds, true, true);
128 printf("%s\n", ds_cstr(&ds));
129 ds_destroy(&ds);
130 }
131 } while (!err);
132
133 if (err != EOF) {
134 ovs_fatal(err, "Error dumping conntrack netlink entry");
135 }
136 nl_ct_dump_done(dump);
137}
138\f
139/* Flush command */
140static void
141test_nl_ct_flush(struct ovs_cmdl_context *ctx OVS_UNUSED)
142{
143 int err;
144
145 if (ctx->argc >= 2) {
146 uint16_t zone;
147
148 if (ovs_scan(ctx->argv[1], "zone=%"SCNu16, &zone)) {
149 err = nl_ct_flush_zone(zone);
150 } else {
151 ovs_fatal(0, "Error parsing zone= specifier");
152 }
153 } else {
154 err = nl_ct_flush();
155 }
156 if (err) {
157 ovs_fatal(err, "Error flushing conntrack netlink");
158 }
159}
160\f
161static const struct ovs_cmdl_command commands[] = {
162 /* Linux netlink connection tracker interface test. */
163
164 /* Prints all the entries in the connection table and exits. */
1f4a7252 165 {"dump", "[zone=zone]", 0, 1, test_nl_ct_dump, OVS_RO},
2c06d9a9
DDP
166 /* Listens to all the connection tracking events and prints them to
167 * standard output until killed. */
1f4a7252 168 {"monitor", "", 0, 0, test_nl_ct_monitor, OVS_RO},
2c06d9a9 169 /* Flushes all the entries from all the tables.. */
1f4a7252 170 {"flush", "[zone=zone]", 0, 1, test_nl_ct_flush, OVS_RO},
2c06d9a9 171
1f4a7252 172 {NULL, NULL, 0, 0, NULL, OVS_RO},
2c06d9a9
DDP
173};
174
175static void
176test_netlink_conntrack(int argc, char *argv[])
177{
178 struct ovs_cmdl_context ctx = {
179 .argc = argc - 1,
180 .argv = argv + 1,
181 };
182 set_program_name(argv[0]);
183 ovs_cmdl_run_command(&ctx, commands);
184}
185
186OVSTEST_REGISTER("test-netlink-conntrack", test_netlink_conntrack);