| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package knftables |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"io" |
| 22 |
"regexp" |
| 23 |
"strconv" |
| 24 |
"strings" |
| 25 |
"time" |
| 26 |
) |
| 27 |
|
| 28 |
func parseInt(numbersOnly string) *int { |
| 29 |
i64, _ := strconv.ParseInt(numbersOnly, 10, 64) |
| 30 |
i := int(i64) |
| 31 |
return &i |
| 32 |
} |
| 33 |
|
| 34 |
func parseUint(numbersOnly string) *uint64 { |
| 35 |
ui64, _ := strconv.ParseUint(numbersOnly, 10, 64) |
| 36 |
return &ui64 |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
func getComment(commentGroup string) *string { |
| 42 |
if commentGroup == "" { |
| 43 |
return nil |
| 44 |
} |
| 45 |
noQuotes := strings.Trim(commentGroup, "\"") |
| 46 |
return &noQuotes |
| 47 |
} |
| 48 |
|
| 49 |
var commentGroup = `(".*")` |
| 50 |
var noSpaceGroup = `([^ ]*)` |
| 51 |
var numberGroup = `([0-9]*)` |
| 52 |
|
| 53 |
|
| 54 |
func (table *Table) validate(verb verb) error { |
| 55 |
switch verb { |
| 56 |
case addVerb, createVerb, flushVerb: |
| 57 |
if table.Handle != nil { |
| 58 |
return fmt.Errorf("cannot specify Handle in %s operation", verb) |
| 59 |
} |
| 60 |
case deleteVerb: |
| 61 |
|
| 62 |
default: |
| 63 |
return fmt.Errorf("%s is not implemented for tables", verb) |
| 64 |
} |
| 65 |
|
| 66 |
return nil |
| 67 |
} |
| 68 |
|
| 69 |
func (table *Table) writeOperation(verb verb, ctx *nftContext, writer io.Writer) { |
| 70 |
|
| 71 |
if verb == deleteVerb && table.Handle != nil { |
| 72 |
fmt.Fprintf(writer, "delete table %s handle %d", ctx.family, *table.Handle) |
| 73 |
return |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
fmt.Fprintf(writer, "%s table %s %s", verb, ctx.family, ctx.table) |
| 78 |
if verb == addVerb || verb == createVerb { |
| 79 |
if table.Comment != nil && !ctx.noObjectComments { |
| 80 |
fmt.Fprintf(writer, " { comment %q ; }", *table.Comment) |
| 81 |
} |
| 82 |
} |
| 83 |
fmt.Fprintf(writer, "\n") |
| 84 |
} |
| 85 |
|
| 86 |
var tableRegexp = regexp.MustCompile(fmt.Sprintf( |
| 87 |
`(?:{ comment %s ; })?`, commentGroup)) |
| 88 |
|
| 89 |
func (table *Table) parse(line string) error { |
| 90 |
match := tableRegexp.FindStringSubmatch(line) |
| 91 |
if match == nil { |
| 92 |
return fmt.Errorf("failed parsing table add command") |
| 93 |
} |
| 94 |
table.Comment = getComment(match[1]) |
| 95 |
return nil |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
func (chain *Chain) validate(verb verb) error { |
| 100 |
if chain.Hook == nil { |
| 101 |
if chain.Type != nil || chain.Priority != nil { |
| 102 |
return fmt.Errorf("regular chain %q must not specify Type or Priority", chain.Name) |
| 103 |
} |
| 104 |
if chain.Device != nil { |
| 105 |
return fmt.Errorf("regular chain %q must not specify Device", chain.Name) |
| 106 |
} |
| 107 |
} else { |
| 108 |
if chain.Type == nil || chain.Priority == nil { |
| 109 |
return fmt.Errorf("base chain %q must specify Type and Priority", chain.Name) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
switch verb { |
| 114 |
case addVerb, createVerb, flushVerb: |
| 115 |
if chain.Name == "" { |
| 116 |
return fmt.Errorf("no name specified for chain") |
| 117 |
} |
| 118 |
if chain.Handle != nil { |
| 119 |
return fmt.Errorf("cannot specify Handle in %s operation", verb) |
| 120 |
} |
| 121 |
case deleteVerb: |
| 122 |
if chain.Name == "" && chain.Handle == nil { |
| 123 |
return fmt.Errorf("must specify either name or handle") |
| 124 |
} |
| 125 |
default: |
| 126 |
return fmt.Errorf("%s is not implemented for chains", verb) |
| 127 |
} |
| 128 |
|
| 129 |
return nil |
| 130 |
} |
| 131 |
|
| 132 |
func (chain *Chain) writeOperation(verb verb, ctx *nftContext, writer io.Writer) { |
| 133 |
|
| 134 |
if verb == deleteVerb && chain.Handle != nil { |
| 135 |
fmt.Fprintf(writer, "delete chain %s %s handle %d", ctx.family, ctx.table, *chain.Handle) |
| 136 |
return |
| 137 |
} |
| 138 |
|
| 139 |
fmt.Fprintf(writer, "%s chain %s %s %s", verb, ctx.family, ctx.table, chain.Name) |
| 140 |
if verb == addVerb || verb == createVerb { |
| 141 |
if chain.Type != nil || (chain.Comment != nil && !ctx.noObjectComments) { |
| 142 |
fmt.Fprintf(writer, " {") |
| 143 |
|
| 144 |
if chain.Type != nil { |
| 145 |
fmt.Fprintf(writer, " type %s hook %s", *chain.Type, *chain.Hook) |
| 146 |
if chain.Device != nil { |
| 147 |
fmt.Fprintf(writer, " device %q", *chain.Device) |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
if priority, err := ParsePriority(ctx.family, string(*chain.Priority)); err == nil { |
| 155 |
fmt.Fprintf(writer, " priority %d ;", priority) |
| 156 |
} else { |
| 157 |
fmt.Fprintf(writer, " priority %s ;", *chain.Priority) |
| 158 |
} |
| 159 |
} |
| 160 |
if chain.Comment != nil && !ctx.noObjectComments { |
| 161 |
fmt.Fprintf(writer, " comment %q ;", *chain.Comment) |
| 162 |
} |
| 163 |
|
| 164 |
fmt.Fprintf(writer, " }") |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
fmt.Fprintf(writer, "\n") |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
var chainRegexp = regexp.MustCompile(fmt.Sprintf( |
| 173 |
`%s(?: {(?: type %s hook %s(?: device "%s")?(?: priority %s ;))?(?: comment %s ;)? })?`, |
| 174 |
noSpaceGroup, noSpaceGroup, noSpaceGroup, noSpaceGroup, noSpaceGroup, commentGroup)) |
| 175 |
|
| 176 |
func (chain *Chain) parse(line string) error { |
| 177 |
match := chainRegexp.FindStringSubmatch(line) |
| 178 |
if match == nil { |
| 179 |
return fmt.Errorf("failed parsing chain add command") |
| 180 |
} |
| 181 |
chain.Name = match[1] |
| 182 |
chain.Comment = getComment(match[6]) |
| 183 |
if match[2] != "" { |
| 184 |
chain.Type = (*BaseChainType)(&match[2]) |
| 185 |
} |
| 186 |
if match[3] != "" { |
| 187 |
chain.Hook = (*BaseChainHook)(&match[3]) |
| 188 |
} |
| 189 |
if match[4] != "" { |
| 190 |
chain.Device = &match[4] |
| 191 |
} |
| 192 |
if match[5] != "" { |
| 193 |
chain.Priority = (*BaseChainPriority)(&match[5]) |
| 194 |
} |
| 195 |
return nil |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
func (rule *Rule) validate(verb verb) error { |
| 200 |
if rule.Chain == "" { |
| 201 |
return fmt.Errorf("no chain name specified for rule") |
| 202 |
} |
| 203 |
|
| 204 |
if rule.Index != nil && rule.Handle != nil { |
| 205 |
return fmt.Errorf("cannot specify both Index and Handle") |
| 206 |
} |
| 207 |
|
| 208 |
switch verb { |
| 209 |
case addVerb, insertVerb: |
| 210 |
if rule.Rule == "" { |
| 211 |
return fmt.Errorf("no rule specified") |
| 212 |
} |
| 213 |
case replaceVerb: |
| 214 |
if rule.Rule == "" { |
| 215 |
return fmt.Errorf("no rule specified") |
| 216 |
} |
| 217 |
if rule.Handle == nil { |
| 218 |
return fmt.Errorf("must specify Handle with %s", verb) |
| 219 |
} |
| 220 |
case deleteVerb: |
| 221 |
if rule.Handle == nil { |
| 222 |
return fmt.Errorf("must specify Handle with %s", verb) |
| 223 |
} |
| 224 |
default: |
| 225 |
return fmt.Errorf("%s is not implemented for rules", verb) |
| 226 |
} |
| 227 |
|
| 228 |
return nil |
| 229 |
} |
| 230 |
|
| 231 |
func (rule *Rule) writeOperation(verb verb, ctx *nftContext, writer io.Writer) { |
| 232 |
fmt.Fprintf(writer, "%s rule %s %s %s", verb, ctx.family, ctx.table, rule.Chain) |
| 233 |
if rule.Index != nil { |
| 234 |
fmt.Fprintf(writer, " index %d", *rule.Index) |
| 235 |
} else if rule.Handle != nil { |
| 236 |
fmt.Fprintf(writer, " handle %d", *rule.Handle) |
| 237 |
} |
| 238 |
|
| 239 |
switch verb { |
| 240 |
case addVerb, insertVerb, replaceVerb: |
| 241 |
fmt.Fprintf(writer, " %s", rule.Rule) |
| 242 |
|
| 243 |
if rule.Comment != nil { |
| 244 |
fmt.Fprintf(writer, " comment %q", *rule.Comment) |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
fmt.Fprintf(writer, "\n") |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
var ruleRegexp = regexp.MustCompile(fmt.Sprintf( |
| 253 |
`%s(?: index %s)?(?: handle %s)? ([^"]*)(?: comment %s)?$`, |
| 254 |
noSpaceGroup, numberGroup, numberGroup, commentGroup)) |
| 255 |
|
| 256 |
func (rule *Rule) parse(line string) error { |
| 257 |
match := ruleRegexp.FindStringSubmatch(line) |
| 258 |
if match == nil { |
| 259 |
return fmt.Errorf("failed parsing rule add command") |
| 260 |
} |
| 261 |
rule.Chain = match[1] |
| 262 |
rule.Rule = match[4] |
| 263 |
rule.Comment = getComment(match[5]) |
| 264 |
if match[2] != "" { |
| 265 |
rule.Index = parseInt(match[2]) |
| 266 |
} |
| 267 |
if match[3] != "" { |
| 268 |
rule.Handle = parseInt(match[3]) |
| 269 |
} |
| 270 |
return nil |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
func (set *Set) validate(verb verb) error { |
| 275 |
switch verb { |
| 276 |
case addVerb, createVerb: |
| 277 |
if (set.Type == "" && set.TypeOf == "") || (set.Type != "" && set.TypeOf != "") { |
| 278 |
return fmt.Errorf("set must specify either Type or TypeOf") |
| 279 |
} |
| 280 |
if set.Handle != nil { |
| 281 |
return fmt.Errorf("cannot specify Handle in %s operation", verb) |
| 282 |
} |
| 283 |
fallthrough |
| 284 |
case flushVerb: |
| 285 |
if set.Name == "" { |
| 286 |
return fmt.Errorf("no name specified for set") |
| 287 |
} |
| 288 |
case deleteVerb: |
| 289 |
if set.Name == "" && set.Handle == nil { |
| 290 |
return fmt.Errorf("must specify either name or handle") |
| 291 |
} |
| 292 |
default: |
| 293 |
return fmt.Errorf("%s is not implemented for sets", verb) |
| 294 |
} |
| 295 |
|
| 296 |
return nil |
| 297 |
} |
| 298 |
|
| 299 |
func (set *Set) writeOperation(verb verb, ctx *nftContext, writer io.Writer) { |
| 300 |
|
| 301 |
if verb == deleteVerb && set.Handle != nil { |
| 302 |
fmt.Fprintf(writer, "delete set %s %s handle %d", ctx.family, ctx.table, *set.Handle) |
| 303 |
return |
| 304 |
} |
| 305 |
|
| 306 |
fmt.Fprintf(writer, "%s set %s %s %s", verb, ctx.family, ctx.table, set.Name) |
| 307 |
if verb == addVerb || verb == createVerb { |
| 308 |
fmt.Fprintf(writer, " {") |
| 309 |
|
| 310 |
if set.Type != "" { |
| 311 |
fmt.Fprintf(writer, " type %s ;", set.Type) |
| 312 |
} else { |
| 313 |
fmt.Fprintf(writer, " typeof %s ;", set.TypeOf) |
| 314 |
} |
| 315 |
|
| 316 |
if len(set.Flags) != 0 { |
| 317 |
fmt.Fprintf(writer, " flags ") |
| 318 |
for i := range set.Flags { |
| 319 |
if i > 0 { |
| 320 |
fmt.Fprintf(writer, ",") |
| 321 |
} |
| 322 |
fmt.Fprintf(writer, "%s", set.Flags[i]) |
| 323 |
} |
| 324 |
fmt.Fprintf(writer, " ;") |
| 325 |
} |
| 326 |
|
| 327 |
if set.Timeout != nil { |
| 328 |
fmt.Fprintf(writer, " timeout %ds ;", int64(set.Timeout.Seconds())) |
| 329 |
} |
| 330 |
if set.GCInterval != nil { |
| 331 |
fmt.Fprintf(writer, " gc-interval %ds ;", int64(set.GCInterval.Seconds())) |
| 332 |
} |
| 333 |
if set.Size != nil { |
| 334 |
fmt.Fprintf(writer, " size %d ;", *set.Size) |
| 335 |
} |
| 336 |
if set.Policy != nil { |
| 337 |
fmt.Fprintf(writer, " policy %s ;", *set.Policy) |
| 338 |
} |
| 339 |
if set.AutoMerge != nil && *set.AutoMerge { |
| 340 |
fmt.Fprintf(writer, " auto-merge ;") |
| 341 |
} |
| 342 |
|
| 343 |
if set.Comment != nil && !ctx.noObjectComments { |
| 344 |
fmt.Fprintf(writer, " comment %q ;", *set.Comment) |
| 345 |
} |
| 346 |
|
| 347 |
fmt.Fprintf(writer, " }") |
| 348 |
} |
| 349 |
|
| 350 |
fmt.Fprintf(writer, "\n") |
| 351 |
} |
| 352 |
|
| 353 |
func (set *Set) parse(line string) error { |
| 354 |
match := setRegexp.FindStringSubmatch(line) |
| 355 |
if match == nil { |
| 356 |
return fmt.Errorf("failed parsing set add command") |
| 357 |
} |
| 358 |
set.Name, set.Type, set.TypeOf, set.Flags, set.Timeout, set.GCInterval, |
| 359 |
set.Size, set.Policy, set.Comment, set.AutoMerge = parseMapAndSetProps(match) |
| 360 |
return nil |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
func (mapObj *Map) validate(verb verb) error { |
| 365 |
switch verb { |
| 366 |
case addVerb, createVerb: |
| 367 |
if (mapObj.Type == "" && mapObj.TypeOf == "") || (mapObj.Type != "" && mapObj.TypeOf != "") { |
| 368 |
return fmt.Errorf("map must specify either Type or TypeOf") |
| 369 |
} |
| 370 |
if mapObj.Handle != nil { |
| 371 |
return fmt.Errorf("cannot specify Handle in %s operation", verb) |
| 372 |
} |
| 373 |
fallthrough |
| 374 |
case flushVerb: |
| 375 |
if mapObj.Name == "" { |
| 376 |
return fmt.Errorf("no name specified for map") |
| 377 |
} |
| 378 |
case deleteVerb: |
| 379 |
if mapObj.Name == "" && mapObj.Handle == nil { |
| 380 |
return fmt.Errorf("must specify either name or handle") |
| 381 |
} |
| 382 |
default: |
| 383 |
return fmt.Errorf("%s is not implemented for maps", verb) |
| 384 |
} |
| 385 |
|
| 386 |
return nil |
| 387 |
} |
| 388 |
|
| 389 |
func (mapObj *Map) writeOperation(verb verb, ctx *nftContext, writer io.Writer) { |
| 390 |
|
| 391 |
if verb == deleteVerb && mapObj.Handle != nil { |
| 392 |
fmt.Fprintf(writer, "delete map %s %s handle %d", ctx.family, ctx.table, *mapObj.Handle) |
| 393 |
return |
| 394 |
} |
| 395 |
|
| 396 |
fmt.Fprintf(writer, "%s map %s %s %s", verb, ctx.family, ctx.table, mapObj.Name) |
| 397 |
if verb == addVerb || verb == createVerb { |
| 398 |
fmt.Fprintf(writer, " {") |
| 399 |
|
| 400 |
if mapObj.Type != "" { |
| 401 |
fmt.Fprintf(writer, " type %s ;", mapObj.Type) |
| 402 |
} else { |
| 403 |
fmt.Fprintf(writer, " typeof %s ;", mapObj.TypeOf) |
| 404 |
} |
| 405 |
|
| 406 |
if len(mapObj.Flags) != 0 { |
| 407 |
fmt.Fprintf(writer, " flags ") |
| 408 |
for i := range mapObj.Flags { |
| 409 |
if i > 0 { |
| 410 |
fmt.Fprintf(writer, ",") |
| 411 |
} |
| 412 |
fmt.Fprintf(writer, "%s", mapObj.Flags[i]) |
| 413 |
} |
| 414 |
fmt.Fprintf(writer, " ;") |
| 415 |
} |
| 416 |
|
| 417 |
if mapObj.Timeout != nil { |
| 418 |
fmt.Fprintf(writer, " timeout %ds ;", int64(mapObj.Timeout.Seconds())) |
| 419 |
} |
| 420 |
if mapObj.GCInterval != nil { |
| 421 |
fmt.Fprintf(writer, " gc-interval %ds ;", int64(mapObj.GCInterval.Seconds())) |
| 422 |
} |
| 423 |
if mapObj.Size != nil { |
| 424 |
fmt.Fprintf(writer, " size %d ;", *mapObj.Size) |
| 425 |
} |
| 426 |
if mapObj.Policy != nil { |
| 427 |
fmt.Fprintf(writer, " policy %s ;", *mapObj.Policy) |
| 428 |
} |
| 429 |
|
| 430 |
if mapObj.Comment != nil && !ctx.noObjectComments { |
| 431 |
fmt.Fprintf(writer, " comment %q ;", *mapObj.Comment) |
| 432 |
} |
| 433 |
|
| 434 |
fmt.Fprintf(writer, " }") |
| 435 |
} |
| 436 |
|
| 437 |
fmt.Fprintf(writer, "\n") |
| 438 |
} |
| 439 |
|
| 440 |
func (mapObj *Map) parse(line string) error { |
| 441 |
match := mapRegexp.FindStringSubmatch(line) |
| 442 |
if match == nil { |
| 443 |
return fmt.Errorf("failed parsing map add command") |
| 444 |
} |
| 445 |
mapObj.Name, mapObj.Type, mapObj.TypeOf, mapObj.Flags, mapObj.Timeout, mapObj.GCInterval, |
| 446 |
mapObj.Size, mapObj.Policy, mapObj.Comment, _ = parseMapAndSetProps(match) |
| 447 |
return nil |
| 448 |
} |
| 449 |
|
| 450 |
var autoMergeProp = `( auto-merge ;)?` |
| 451 |
|
| 452 |
|
| 453 |
var mapOrSet = `%s {(?: (type|typeof) ([^;]*)) ;(?: flags ([^;]*) ;)?(?: timeout %ss ;)?(?: gc-interval %ss ;)?(?: size %s ;)?(?: policy %s ;)?%s(?: comment %s ;)? }` |
| 454 |
var mapRegexp = regexp.MustCompile(fmt.Sprintf(mapOrSet, noSpaceGroup, numberGroup, numberGroup, noSpaceGroup, noSpaceGroup, "", commentGroup)) |
| 455 |
var setRegexp = regexp.MustCompile(fmt.Sprintf(mapOrSet, noSpaceGroup, numberGroup, numberGroup, noSpaceGroup, noSpaceGroup, autoMergeProp, commentGroup)) |
| 456 |
|
| 457 |
func parseMapAndSetProps(match []string) (name string, typeProp string, typeOf string, flags []SetFlag, |
| 458 |
timeout *time.Duration, gcInterval *time.Duration, size *uint64, policy *SetPolicy, comment *string, autoMerge *bool) { |
| 459 |
name = match[1] |
| 460 |
|
| 461 |
comment = getComment(match[len(match)-1]) |
| 462 |
if match[2] == "type" { |
| 463 |
typeProp = match[3] |
| 464 |
} else { |
| 465 |
typeOf = match[3] |
| 466 |
} |
| 467 |
if match[4] != "" { |
| 468 |
flags = parseSetFlags(match[4]) |
| 469 |
} |
| 470 |
if match[5] != "" { |
| 471 |
timeoutObj, _ := time.ParseDuration(match[5] + "s") |
| 472 |
timeout = &timeoutObj |
| 473 |
} |
| 474 |
if match[6] != "" { |
| 475 |
gcIntervalObj, _ := time.ParseDuration(match[6] + "s") |
| 476 |
gcInterval = &gcIntervalObj |
| 477 |
} |
| 478 |
if match[7] != "" { |
| 479 |
size = parseUint(match[7]) |
| 480 |
} |
| 481 |
if match[8] != "" { |
| 482 |
policy = (*SetPolicy)(&match[8]) |
| 483 |
} |
| 484 |
if len(match) > 10 { |
| 485 |
|
| 486 |
if match[9] != "" { |
| 487 |
autoMergeObj := true |
| 488 |
autoMerge = &autoMergeObj |
| 489 |
} |
| 490 |
} |
| 491 |
return |
| 492 |
} |
| 493 |
|
| 494 |
func parseSetFlags(s string) []SetFlag { |
| 495 |
var res []SetFlag |
| 496 |
for _, flag := range strings.Split(s, ",") { |
| 497 |
res = append(res, SetFlag(flag)) |
| 498 |
} |
| 499 |
return res |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
func (element *Element) validate(verb verb) error { |
| 504 |
if element.Map == "" && element.Set == "" { |
| 505 |
return fmt.Errorf("no set/map name specified for element") |
| 506 |
} else if element.Set != "" && element.Map != "" { |
| 507 |
return fmt.Errorf("element specifies both a set name and a map name") |
| 508 |
} |
| 509 |
|
| 510 |
if len(element.Key) == 0 { |
| 511 |
return fmt.Errorf("no key specified for element") |
| 512 |
} |
| 513 |
if element.Set != "" && len(element.Value) != 0 { |
| 514 |
return fmt.Errorf("map value specified for set element") |
| 515 |
} |
| 516 |
|
| 517 |
switch verb { |
| 518 |
case addVerb, createVerb: |
| 519 |
if element.Map != "" && len(element.Value) == 0 { |
| 520 |
return fmt.Errorf("no map value specified for map element") |
| 521 |
} |
| 522 |
case deleteVerb: |
| 523 |
default: |
| 524 |
return fmt.Errorf("%s is not implemented for elements", verb) |
| 525 |
} |
| 526 |
|
| 527 |
return nil |
| 528 |
} |
| 529 |
|
| 530 |
func (element *Element) writeOperation(verb verb, ctx *nftContext, writer io.Writer) { |
| 531 |
name := element.Set |
| 532 |
if name == "" { |
| 533 |
name = element.Map |
| 534 |
} |
| 535 |
|
| 536 |
fmt.Fprintf(writer, "%s element %s %s %s { %s", verb, ctx.family, ctx.table, name, |
| 537 |
strings.Join(element.Key, " . ")) |
| 538 |
|
| 539 |
if verb == addVerb || verb == createVerb { |
| 540 |
if element.Comment != nil { |
| 541 |
fmt.Fprintf(writer, " comment %q", *element.Comment) |
| 542 |
} |
| 543 |
|
| 544 |
if len(element.Value) != 0 { |
| 545 |
fmt.Fprintf(writer, " : %s", strings.Join(element.Value, " . ")) |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
fmt.Fprintf(writer, " }\n") |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
var mapElementRegexp = regexp.MustCompile(fmt.Sprintf( |
| 554 |
`%s { ([^"]*)(?: comment %s)? : (.*) }`, noSpaceGroup, commentGroup)) |
| 555 |
|
| 556 |
|
| 557 |
var setElementRegexp = regexp.MustCompile(fmt.Sprintf( |
| 558 |
`%s { ([^"]*)(?: comment %s)? }`, noSpaceGroup, commentGroup)) |
| 559 |
|
| 560 |
func (element *Element) parse(line string) error { |
| 561 |
|
| 562 |
|
| 563 |
match := mapElementRegexp.FindStringSubmatch(line) |
| 564 |
if match == nil { |
| 565 |
match = setElementRegexp.FindStringSubmatch(line) |
| 566 |
if match == nil { |
| 567 |
return fmt.Errorf("failed parsing element add command") |
| 568 |
} |
| 569 |
} |
| 570 |
element.Comment = getComment(match[3]) |
| 571 |
mapOrSetName := match[1] |
| 572 |
element.Key = append(element.Key, strings.Split(match[2], " . ")...) |
| 573 |
if len(match) == 5 { |
| 574 |
|
| 575 |
element.Map = mapOrSetName |
| 576 |
element.Value = append(element.Value, strings.Split(match[4], " . ")...) |
| 577 |
} else { |
| 578 |
element.Set = mapOrSetName |
| 579 |
} |
| 580 |
return nil |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
func (flowtable *Flowtable) validate(verb verb) error { |
| 585 |
switch verb { |
| 586 |
case addVerb, createVerb: |
| 587 |
if flowtable.Name == "" { |
| 588 |
return fmt.Errorf("no name specified for flowtable") |
| 589 |
} |
| 590 |
if flowtable.Handle != nil { |
| 591 |
return fmt.Errorf("cannot specify Handle in %s operation", verb) |
| 592 |
} |
| 593 |
case deleteVerb: |
| 594 |
if flowtable.Name == "" && flowtable.Handle == nil { |
| 595 |
return fmt.Errorf("must specify either name or handle") |
| 596 |
} |
| 597 |
default: |
| 598 |
return fmt.Errorf("%s is not implemented for flowtables", verb) |
| 599 |
} |
| 600 |
|
| 601 |
return nil |
| 602 |
} |
| 603 |
|
| 604 |
func (flowtable *Flowtable) writeOperation(verb verb, ctx *nftContext, writer io.Writer) { |
| 605 |
|
| 606 |
if verb == deleteVerb && flowtable.Handle != nil { |
| 607 |
fmt.Fprintf(writer, "delete flowtable %s %s handle %d", ctx.family, ctx.table, *flowtable.Handle) |
| 608 |
return |
| 609 |
} |
| 610 |
|
| 611 |
fmt.Fprintf(writer, "%s flowtable %s %s %s", verb, ctx.family, ctx.table, flowtable.Name) |
| 612 |
if verb == addVerb || verb == createVerb { |
| 613 |
fmt.Fprintf(writer, " {") |
| 614 |
|
| 615 |
if flowtable.Priority != nil { |
| 616 |
|
| 617 |
|
| 618 |
fmt.Fprintf(writer, " hook ingress priority %s ;", *flowtable.Priority) |
| 619 |
} |
| 620 |
|
| 621 |
if len(flowtable.Devices) > 0 { |
| 622 |
fmt.Fprintf(writer, " devices = { %s } ;", strings.Join(flowtable.Devices, ", ")) |
| 623 |
} |
| 624 |
|
| 625 |
fmt.Fprintf(writer, " }") |
| 626 |
} |
| 627 |
|
| 628 |
fmt.Fprintf(writer, "\n") |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
var flowtableRegexp = regexp.MustCompile(fmt.Sprintf( |
| 633 |
`%s(?: {(?: hook ingress priority %s ;)(?: devices = {(.*)} ;) })?`, |
| 634 |
noSpaceGroup, noSpaceGroup)) |
| 635 |
|
| 636 |
func (flowtable *Flowtable) parse(line string) error { |
| 637 |
match := flowtableRegexp.FindStringSubmatch(line) |
| 638 |
if match == nil { |
| 639 |
return fmt.Errorf("failed parsing flowtableRegexp add command") |
| 640 |
} |
| 641 |
flowtable.Name = match[1] |
| 642 |
if match[2] != "" { |
| 643 |
flowtable.Priority = (*FlowtableIngressPriority)(&match[2]) |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
if match[3] != "" { |
| 649 |
devices := strings.Split(strings.TrimSpace(match[3]), ",") |
| 650 |
for i := range devices { |
| 651 |
devices[i] = strings.TrimSpace(devices[i]) |
| 652 |
} |
| 653 |
if len(devices) > 0 { |
| 654 |
flowtable.Devices = devices |
| 655 |
} |
| 656 |
} |
| 657 |
return nil |
| 658 |
} |