Skip to content

Commit 08c2b55

Browse files
committed
codelab: reformat
1 parent 0f2526a commit 08c2b55

File tree

5 files changed

+275
-163
lines changed

5 files changed

+275
-163
lines changed

.clang-format

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
Language: Cpp
3+
# BasedOnStyle: Google
4+
AccessModifierOffset: -4
5+
AlignAfterOpenBracket: Align
6+
AlignConsecutiveAssignments: true
7+
AlignConsecutiveDeclarations: false
8+
AlignEscapedNewlines: Right
9+
AlignOperands: true
10+
AlignTrailingComments: true
11+
AllowAllParametersOfDeclarationOnNextLine: true
12+
AllowShortBlocksOnASingleLine: false
13+
AllowShortCaseLabelsOnASingleLine: false
14+
AllowShortFunctionsOnASingleLine: All
15+
AllowShortIfStatementsOnASingleLine: false
16+
AllowShortLoopsOnASingleLine: false
17+
AlwaysBreakAfterDefinitionReturnType: None
18+
AlwaysBreakAfterReturnType: TopLevelDefinitions
19+
AlwaysBreakBeforeMultilineStrings: false
20+
AlwaysBreakTemplateDeclarations: true
21+
BinPackArguments: true
22+
BinPackParameters: true
23+
BraceWrapping:
24+
AfterClass: false
25+
AfterControlStatement: false
26+
AfterEnum: false
27+
AfterFunction: true
28+
AfterNamespace: false
29+
AfterObjCDeclaration: false
30+
AfterStruct: false
31+
AfterUnion: false
32+
BeforeCatch: false
33+
BeforeElse: false
34+
IndentBraces: false
35+
SplitEmptyFunction: true
36+
SplitEmptyRecord: true
37+
SplitEmptyNamespace: true
38+
BreakBeforeBinaryOperators: None
39+
BreakBeforeBraces: Custom
40+
BreakBeforeInheritanceComma: false
41+
BreakBeforeTernaryOperators: true
42+
BreakConstructorInitializersBeforeComma: false
43+
BreakConstructorInitializers: BeforeColon
44+
BreakAfterJavaFieldAnnotations: false
45+
BreakStringLiterals: true
46+
ColumnLimit: 80
47+
CommentPragmas: '^ IWYU pragma:'
48+
CompactNamespaces: false
49+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
50+
ConstructorInitializerIndentWidth: 4
51+
ContinuationIndentWidth: 4
52+
Cpp11BracedListStyle: true
53+
DerivePointerAlignment: false
54+
DisableFormat: false
55+
ExperimentalAutoDetectBinPacking: false
56+
FixNamespaceComments: true
57+
ForEachMacros:
58+
- foreach
59+
- Q_FOREACH
60+
- BOOST_FOREACH
61+
- list_for_each_entry
62+
- list_for_each_entry_safe
63+
- hlist_for_each_entry
64+
- rb_list_foreach
65+
- rb_list_foreach_safe
66+
IncludeCategories:
67+
- Regex: '^<.*\.h>'
68+
Priority: 1
69+
- Regex: '^<.*'
70+
Priority: 2
71+
- Regex: '.*'
72+
Priority: 3
73+
IncludeIsMainRegex: '([-_](test|unittest))?$'
74+
IndentCaseLabels: false
75+
IndentWidth: 4
76+
IndentWrappedFunctionNames: false
77+
JavaScriptQuotes: Leave
78+
JavaScriptWrapImports: true
79+
KeepEmptyLinesAtTheStartOfBlocks: false
80+
MacroBlockBegin: ''
81+
MacroBlockEnd: ''
82+
MaxEmptyLinesToKeep: 1
83+
NamespaceIndentation: None
84+
ObjCBlockIndentWidth: 4
85+
ObjCSpaceAfterProperty: false
86+
ObjCSpaceBeforeProtocolList: false
87+
PenaltyBreakAssignment: 2
88+
PenaltyBreakBeforeFirstCallParameter: 1
89+
PenaltyBreakComment: 300
90+
PenaltyBreakFirstLessLess: 120
91+
PenaltyBreakString: 1000
92+
PenaltyExcessCharacter: 1000000
93+
PenaltyReturnTypeOnItsOwnLine: 200
94+
PointerAlignment: Right
95+
ReflowComments: true
96+
SortIncludes: false
97+
SortUsingDeclarations: true
98+
SpaceAfterCStyleCast: false
99+
SpaceAfterTemplateKeyword: true
100+
SpaceBeforeAssignmentOperators: true
101+
SpaceBeforeParens: ControlStatements
102+
SpaceInEmptyParentheses: false
103+
SpacesBeforeTrailingComments: 1
104+
SpacesInAngles: false
105+
SpacesInContainerLiterals: false
106+
SpacesInCStyleCastParentheses: false
107+
SpacesInParentheses: false
108+
SpacesInSquareBrackets: false
109+
Standard: Auto
110+
TabWidth: 4
111+
UseTab: Never
112+
...

codelab/fe.c

+68-69
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
#include <netinet/udp.h>
2424
#include <netinet/tcp.h>
2525

26-
static int stop = 0;
26+
static int stop = 0;
2727
static unsigned long long fwdback = 0;
28-
static unsigned long long fwda = 0;
29-
static unsigned long long fwdb = 0;
30-
static unsigned long long tot = 0;
28+
static unsigned long long fwda = 0;
29+
static unsigned long long fwdb = 0;
30+
static unsigned long long tot = 0;
3131

3232
static void
3333
sigint_handler(int signum)
@@ -40,13 +40,13 @@ rx_ready(struct nm_desc *nmd)
4040
{
4141
unsigned int ri;
4242

43-
for (ri = nmd->first_rx_ring; ri <= nmd->last_rx_ring; ri ++) {
44-
struct netmap_ring *ring;
43+
for (ri = nmd->first_rx_ring; ri <= nmd->last_rx_ring; ri++) {
44+
struct netmap_ring *ring;
4545

46-
ring = NETMAP_RXRING(nmd->nifp, ri);
47-
if (nm_ring_space(ring)) {
48-
return 1; /* there is something to read */
49-
}
46+
ring = NETMAP_RXRING(nmd->nifp, ri);
47+
if (nm_ring_space(ring)) {
48+
return 1; /* there is something to read */
49+
}
5050
}
5151

5252
return 0;
@@ -75,7 +75,6 @@ pkt_get_udp_port(const char *buf)
7575
return ntohs(udph->dest);
7676
}
7777

78-
7978
static void
8079
forward_pkts(struct nm_desc *src, struct nm_desc *dst)
8180
{
@@ -90,8 +89,8 @@ forward_pkts(struct nm_desc *src, struct nm_desc *dst)
9089

9190
rxring = NETMAP_RXRING(src->nifp, si);
9291
txring = NETMAP_TXRING(dst->nifp, di);
93-
nrx = nm_ring_space(rxring);
94-
ntx = nm_ring_space(txring);
92+
nrx = nm_ring_space(rxring);
93+
ntx = nm_ring_space(txring);
9594
if (nrx == 0) {
9695
si++;
9796
continue;
@@ -101,25 +100,25 @@ forward_pkts(struct nm_desc *src, struct nm_desc *dst)
101100
continue;
102101
}
103102

104-
rxhead = rxring->head;
105-
txhead = txring->head;
106-
for (; nrx > 0 && ntx > 0;
107-
nrx --, rxhead = nm_ring_next(rxring, rxhead), tot ++) {
103+
rxhead = rxring->head;
104+
txhead = txring->head;
105+
for (; nrx > 0 && ntx > 0;
106+
nrx--, rxhead = nm_ring_next(rxring, rxhead), tot++) {
108107
struct netmap_slot *rs = &rxring->slot[rxhead];
109108
struct netmap_slot *ts = &txring->slot[txhead];
110-
char *rxbuf = NETMAP_BUF(rxring, rs->buf_idx);
111-
char *txbuf = NETMAP_BUF(txring, ts->buf_idx);
109+
char *rxbuf = NETMAP_BUF(rxring, rs->buf_idx);
110+
char *txbuf = NETMAP_BUF(txring, ts->buf_idx);
112111

113112
ts->len = rs->len;
114113
memcpy(txbuf, rxbuf, ts->len);
115114
txhead = nm_ring_next(txring, txhead);
116-
ntx --;
117-
fwdback ++;
118-
tot ++;
115+
ntx--;
116+
fwdback++;
117+
tot++;
119118
}
120119
/* Update state of netmap ring. */
121-
rxring->head = rxring->cur = rxhead;
122-
txring->head = txring->cur = txhead;
120+
rxring->head = rxring->cur = rxhead;
121+
txring->head = txring->cur = txhead;
123122
}
124123
}
125124

@@ -134,11 +133,11 @@ main_loop(const char *netmap_port_one, const char *netmap_port_two,
134133
nmd_one = nm_open(netmap_port_one, NULL, 0, NULL);
135134
if (nmd_one == NULL) {
136135
if (!errno) {
137-
printf("Failed to nm_open(%s): not a netmap port\n",
138-
netmap_port_one);
136+
printf("Failed to nm_open(%s): not a netmap port\n",
137+
netmap_port_one);
139138
} else {
140-
printf("Failed to nm_open(%s): %s\n", netmap_port_one,
141-
strerror(errno));
139+
printf("Failed to nm_open(%s): %s\n", netmap_port_one,
140+
strerror(errno));
142141
}
143142
return -1;
144143
}
@@ -159,7 +158,7 @@ main_loop(const char *netmap_port_one, const char *netmap_port_two,
159158
if (nmd_three == NULL) {
160159
if (!errno) {
161160
printf("Failed to nm_open(%s): not a netmap port\n",
162-
netmap_port_three);
161+
netmap_port_three);
163162
} else {
164163
printf("Failed to nm_open(%s): %s\n", netmap_port_three,
165164
strerror(errno));
@@ -168,7 +167,6 @@ main_loop(const char *netmap_port_one, const char *netmap_port_two,
168167
}
169168

170169
while (!stop) {
171-
172170
/* Forward traffic from ports two and three back to port one. */
173171
forward_pkts(nmd_two, nmd_one);
174172
forward_pkts(nmd_three, nmd_one);
@@ -191,61 +189,62 @@ usage(char **argv)
191189
{
192190
printf("usage: %s [-h] [-i NETMAP_PORT_ONE] "
193191
"[-i NETMAP_PORT_TWO] [-i NETMAP_PORT_THREE] "
194-
"[-p UDP_PORT_A] [-p UDP_PORT_B]\n", argv[0]);
192+
"[-p UDP_PORT_A] [-p UDP_PORT_B]\n",
193+
argv[0]);
195194
exit(EXIT_SUCCESS);
196195
}
197196

198197
int
199198
main(int argc, char **argv)
200199
{
201-
const char *netmap_port_one = NULL;
202-
const char *netmap_port_two = NULL;
200+
const char *netmap_port_one = NULL;
201+
const char *netmap_port_two = NULL;
203202
const char *netmap_port_three = NULL;
204203
int udp_port;
205-
int udp_port_a = 8000;
206-
int udp_port_b = 8001;
204+
int udp_port_a = 8000;
205+
int udp_port_b = 8001;
207206
int udp_port_args = 0;
208207
struct sigaction sa;
209208
int opt;
210209
int ret;
211210

212211
while ((opt = getopt(argc, argv, "hi:p:")) != -1) {
213212
switch (opt) {
214-
case 'h':
213+
case 'h':
214+
usage(argv);
215+
return 0;
216+
217+
case 'i':
218+
if (netmap_port_one == NULL) {
219+
netmap_port_one = optarg;
220+
} else if (netmap_port_two == NULL) {
221+
netmap_port_two = optarg;
222+
} else if (netmap_port_three == NULL) {
223+
netmap_port_three = optarg;
224+
}
225+
break;
226+
227+
case 'p':
228+
udp_port = atoi(optarg);
229+
if (udp_port <= 0 || udp_port >= 65535) {
230+
printf(" invalid UDP port %s\n", optarg);
215231
usage(argv);
216-
return 0;
217-
218-
case 'i':
219-
if (netmap_port_one == NULL) {
220-
netmap_port_one = optarg;
221-
} else if (netmap_port_two == NULL) {
222-
netmap_port_two = optarg;
223-
} else if (netmap_port_three == NULL) {
224-
netmap_port_three = optarg;
225-
}
232+
}
233+
switch (udp_port_args) {
234+
case 0:
235+
udp_port_a = udp_port;
226236
break;
227-
228-
case 'p':
229-
udp_port = atoi(optarg);
230-
if (udp_port <= 0 || udp_port >= 65535) {
231-
printf(" invalid UDP port %s\n", optarg);
232-
usage(argv);
233-
}
234-
switch (udp_port_args) {
235-
case 0:
236-
udp_port_a = udp_port;
237-
break;
238-
case 1:
239-
udp_port_b = udp_port;
240-
break;
241-
}
242-
udp_port_args ++;
237+
case 1:
238+
udp_port_b = udp_port;
243239
break;
240+
}
241+
udp_port_args++;
242+
break;
244243

245-
default:
246-
printf(" unrecognized option '-%c'\n", opt);
247-
usage(argv);
248-
return -1;
244+
default:
245+
printf(" unrecognized option '-%c'\n", opt);
246+
usage(argv);
247+
return -1;
249248
}
250249
}
251250

@@ -263,7 +262,7 @@ main(int argc, char **argv)
263262
sa.sa_handler = sigint_handler;
264263
sigemptyset(&sa.sa_mask);
265264
sa.sa_flags = SA_RESTART;
266-
ret = sigaction(SIGINT, &sa, NULL);
265+
ret = sigaction(SIGINT, &sa, NULL);
267266
if (ret) {
268267
perror("sigaction(SIGINT)");
269268
exit(EXIT_FAILURE);
@@ -276,8 +275,8 @@ main(int argc, char **argv)
276275
printf("UDP port A: %d\n", udp_port_a);
277276
printf("UDP port B: %d\n", udp_port_b);
278277

279-
main_loop(netmap_port_one, netmap_port_two, netmap_port_three,
280-
udp_port_a, udp_port_b);
278+
main_loop(netmap_port_one, netmap_port_two, netmap_port_three, udp_port_a,
279+
udp_port_b);
281280

282281
return 0;
283282
}

0 commit comments

Comments
 (0)