Skip to content

Commit ff2cd3c

Browse files
committed
simplify code
Signed-off-by: Florian Lehner <[email protected]>
1 parent fa96917 commit ff2cd3c

File tree

2 files changed

+33
-84
lines changed

2 files changed

+33
-84
lines changed

m_action.go

+30-82
Original file line numberDiff line numberDiff line change
@@ -168,95 +168,60 @@ func marshalAction(cmd int, info *Action, actOption uint16) ([]byte, error) {
168168
if len(info.Kind) == 0 {
169169
return []byte{}, fmt.Errorf("kind is missing")
170170
}
171-
var multiError error
171+
var err error
172+
var data []byte
172173

173174
// TODO: improve logic and check combinations
174175
switch info.Kind {
175176
case "bpf":
176-
data, err := marshalActBpf(info.Bpf)
177-
multiError = concatError(multiError, err)
178-
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
177+
data, err = marshalActBpf(info.Bpf)
179178
case "connmark":
180-
data, err := marshalConnmark(info.ConnMark)
181-
multiError = concatError(multiError, err)
182-
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
179+
data, err = marshalConnmark(info.ConnMark)
183180
case "csum":
184-
data, err := marshalCsum(info.CSum)
185-
multiError = concatError(multiError, err)
186-
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
181+
data, err = marshalCsum(info.CSum)
187182
case "ct":
188-
data, err := marshalCt(info.Ct)
189-
multiError = concatError(multiError, err)
190-
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
183+
data, err = marshalCt(info.Ct)
191184
case "ctinfo":
192-
data, err := marshalCtInfo(info.CtInfo)
193-
multiError = concatError(multiError, err)
194-
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
185+
data, err = marshalCtInfo(info.CtInfo)
195186
case "defact":
196-
data, err := marshalDefact(info.Defact)
197-
multiError = concatError(multiError, err)
198-
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
187+
data, err = marshalDefact(info.Defact)
199188
case "gact":
200-
data, err := marshalGact(info.Gact)
201-
multiError = concatError(multiError, err)
202-
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
189+
data, err = marshalGact(info.Gact)
203190
case "gate":
204-
data, err := marshalGate(info.Gate)
205-
multiError = concatError(multiError, err)
206-
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
191+
data, err = marshalGate(info.Gate)
207192
case "ife":
208-
data, err := marshalIfe(info.Ife)
209-
multiError = concatError(multiError, err)
210-
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
193+
data, err = marshalIfe(info.Ife)
211194
case "ipt":
212-
data, err := marshalIpt(info.Ipt)
213-
multiError = concatError(multiError, err)
214-
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
195+
data, err = marshalIpt(info.Ipt)
215196
case "mirred":
216-
data, err := marshalMirred(info.Mirred)
217-
multiError = concatError(multiError, err)
218-
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
197+
data, err = marshalMirred(info.Mirred)
219198
case "nat":
220-
data, err := marshalNat(info.Nat)
221-
multiError = concatError(multiError, err)
222-
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
199+
data, err = marshalNat(info.Nat)
223200
case "sample":
224-
data, err := marshalSample(info.Sample)
225-
multiError = concatError(multiError, err)
226-
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
201+
data, err = marshalSample(info.Sample)
227202
case "vlan":
228-
data, err := marshalVlan(info.VLan)
229-
multiError = concatError(multiError, err)
230-
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
203+
data, err = marshalVlan(info.VLan)
231204
case "police":
232-
data, err := marshalPolice(info.Police)
233-
multiError = concatError(multiError, err)
234-
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
205+
data, err = marshalPolice(info.Police)
235206
case "tunnel_key":
236-
data, err := marshalTunnelKey(info.TunnelKey)
237-
multiError = concatError(multiError, err)
238-
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
207+
data, err = marshalTunnelKey(info.TunnelKey)
239208
case "mpls":
240-
data, err := marshalMPLS(info.MPLS)
241-
multiError = concatError(multiError, err)
242-
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
209+
data, err = marshalMPLS(info.MPLS)
243210
case "skbedit":
244-
data, err := marshalSkbEdit(info.SkbEdit)
245-
multiError = concatError(multiError, err)
246-
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
211+
data, err = marshalSkbEdit(info.SkbEdit)
247212
case "skbmod":
248-
data, err := marshalSkbMod(info.SkbMod)
249-
multiError = concatError(multiError, err)
250-
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
213+
data, err = marshalSkbMod(info.SkbMod)
251214
default:
252215
return []byte{}, fmt.Errorf("unknown kind '%s'", info.Kind)
253216
}
254-
options = append(options, tcOption{Interpretation: vtString, Type: tcaActKind, Data: info.Kind})
255217

256-
if multiError != nil && !errors.Is(multiError, ErrNoArg) && cmd != unix.RTM_DELACTION {
257-
return []byte{}, multiError
218+
if err != nil && !errors.Is(err, ErrNoArg) && cmd != unix.RTM_DELACTION {
219+
return []byte{}, err
258220
}
259221

222+
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
223+
options = append(options, tcOption{Interpretation: vtString, Type: tcaActKind, Data: info.Kind})
224+
260225
if info.Index != 0 {
261226
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaActIndex, Data: info.Index})
262227
}
@@ -270,111 +235,94 @@ func marshalAction(cmd int, info *Action, actOption uint16) ([]byte, error) {
270235
if info.Cookie != nil {
271236
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaActCookie, Data: bytesValue(info.Cookie)})
272237
}
238+
if info.Flags != nil {
239+
options = append(options, tcOption{Interpretation: vtUint64, Type: tcaActFlags, Data: uint64Value(info.Flags)})
240+
}
273241
return marshalAttributes(options)
274242
}
275243

276244
func extractActOptions(data []byte, act *Action, kind string) error {
277-
var multiError error
278245
var err error
279246
switch kind {
280247
case "bpf":
281248
info := &ActBpf{}
282249
err = unmarshalActBpf(data, info)
283-
multiError = concatError(multiError, err)
284250
act.Bpf = info
285251
case "connmark":
286252
info := &Connmark{}
287253
err = unmarshalConnmark(data, info)
288-
multiError = concatError(multiError, err)
289254
act.ConnMark = info
290255
case "csum":
291256
info := &Csum{}
292257
err = unmarshalCsum(data, info)
293-
multiError = concatError(multiError, err)
294258
act.CSum = info
295259
case "ct":
296260
info := &Ct{}
297261
err = unmarshalCt(data, info)
298-
multiError = concatError(multiError, err)
299262
act.Ct = info
300263
case "ctinfo":
301264
info := &CtInfo{}
302265
err = unmarshalCtInfo(data, info)
303-
multiError = concatError(multiError, err)
304266
act.CtInfo = info
305267
case "defact":
306268
info := &Defact{}
307269
err = unmarshalDefact(data, info)
308-
multiError = concatError(multiError, err)
309270
act.Defact = info
310271
case "gact":
311272
info := &Gact{}
312273
err = unmarshalGact(data, info)
313-
multiError = concatError(multiError, err)
314274
act.Gact = info
315275
case "gate":
316276
info := &Gate{}
317277
err = unmarshalGate(data, info)
318-
multiError = concatError(multiError, err)
319278
act.Gate = info
320279
case "ife":
321280
info := &Ife{}
322281
err = unmarshalIfe(data, info)
323-
multiError = concatError(multiError, err)
324282
act.Ife = info
325283
case "ipt":
326284
info := &Ipt{}
327285
err = unmarshalIpt(data, info)
328-
multiError = concatError(multiError, err)
329286
act.Ipt = info
330287
case "mirred":
331288
info := &Mirred{}
332289
err = unmarshalMirred(data, info)
333-
multiError = concatError(multiError, err)
334290
act.Mirred = info
335291
case "nat":
336292
info := &Nat{}
337293
err = unmarshalNat(data, info)
338-
multiError = concatError(multiError, err)
339294
act.Nat = info
340295
case "sample":
341296
info := &Sample{}
342297
err = unmarshalSample(data, info)
343-
multiError = concatError(multiError, err)
344298
act.Sample = info
345299
case "vlan":
346300
info := &VLan{}
347301
err = unmarshalVLan(data, info)
348-
multiError = concatError(multiError, err)
349302
act.VLan = info
350303
case "police":
351304
info := &Police{}
352305
err = unmarshalPolice(data, info)
353-
multiError = concatError(multiError, err)
354306
act.Police = info
355307
case "tunnel_key":
356308
info := &TunnelKey{}
357309
err = unmarshalTunnelKey(data, info)
358-
multiError = concatError(multiError, err)
359310
act.TunnelKey = info
360311
case "mpls":
361312
info := &MPLS{}
362313
err = unmarshalMPLS(data, info)
363-
multiError = concatError(multiError, err)
364314
act.MPLS = info
365315
case "skbedit":
366316
info := &SkbEdit{}
367317
err = unmarshalSkbEdit(data, info)
368-
multiError = concatError(multiError, err)
369318
act.SkbEdit = info
370319
case "skbmod":
371320
info := &SkbMod{}
372321
err = unmarshalSkbMod(data, info)
373-
multiError = concatError(multiError, err)
374322
act.SkbMod = info
375323
default:
376324
return fmt.Errorf("extractActOptions(): unsupported kind: %s", kind)
377325

378326
}
379-
return multiError
327+
return err
380328
}

m_action_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ func TestAction(t *testing.T) {
1818
"unknown Kind": {val: Action{Kind: "test"}, err1: fmt.Errorf("unknown kind 'test'")},
1919
"bpf Without Options": {val: Action{Kind: "bpf", Index: 123}, err1: ErrNoArg},
2020
"simple Bpf": {val: Action{
21-
Kind: "bpf",
22-
Bpf: &ActBpf{FD: uint32Ptr(12), Name: stringPtr("simpleTest"), Parms: &ActBpfParms{Action: 2, Index: 4}},
21+
Kind: "bpf",
22+
Bpf: &ActBpf{FD: uint32Ptr(12), Name: stringPtr("simpleTest"), Parms: &ActBpfParms{Action: 2, Index: 4}},
23+
Flags: uint64Ptr(73),
2324
}},
2425
"connmark": {val: Action{
2526
Kind: "connmark",

0 commit comments

Comments
 (0)