-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (120 loc) · 3.47 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
// Copyright 2020 Jörg Kost All rights reserved.
// jk@ip-clear.de
// Use of this source code is governed by Apache 2.0
// license that can be found in the LICENSE.MD file.
import (
"flag"
"log"
"os"
"sort"
)
type bgpAS struct {
name string
value int
routesNumber int
asNumber int
country string
picked bool
nospace bool
zero bool
}
type node struct {
asnumbers []int
value int
routesNumber int
}
/* matrix */
var matrix [][]node
var bgpASlist []bgpAS
/* helper maps, poor globals, need refactor */
var asNumber = make(map[int]string)
var asNumberRoutes map[int]int
var countries = make(map[string]int)
var countrycounter = make(map[string]int)
var personalValue = make(map[int]int)
var routesSeen uint32
/* some global defaults */
var fmtAsPathDefault = "ip as-path access-list %s permit %s$\n"
var fmtASPathName = "savethefib"
var fmtASPathNameFmt *string
var fmtAsPathFmt *string
var defaultValue = 10
var bgpDefaultValue *int
func main() {
aslist := flag.String("aslist", "data_asnums", "as number list")
bestRoutes := flag.String("routes", "bestroutes.slx", "router output, e.g. show ipv6 bgp routes best")
countryList := flag.String("country", "config_country", "list with country default weight values")
personalList := flag.String("personal", "", "list with preferred personal as config")
bgpDefaultValue = flag.Int("value", defaultValue, "Default order value for a BGP as")
camSize := flag.Int("camsize", 512000, "size of the routers cam")
sorttype := flag.Int("sorttype", 1, "type of sort, 0=value,then routesnumber bigger, 1=value,then routesnumber smaller")
fmtAsPathFmt = flag.String("aspathfmt", fmtAsPathDefault, "default for printing the as-path list")
fmtASPathNameFmt = flag.String("aspathname", fmtASPathName, "default name for as-path list")
debug := flag.Bool("debug", false, "if memory or other infos is being printed out")
flag.Parse()
readConfigsToMemory(*personalList, *countryList, *bestRoutes, *aslist)
/* sortswitch */
if *sorttype == 0 {
sort.Slice(bgpASlist, func(i, j int) bool {
switch {
case bgpASlist[i].value != bgpASlist[j].value:
return bgpASlist[i].value >= bgpASlist[j].value
default:
return bgpASlist[i].routesNumber >= bgpASlist[j].routesNumber
}
})
} else {
sort.Slice(bgpASlist, func(i, j int) bool {
switch {
case bgpASlist[i].value != bgpASlist[j].value:
return bgpASlist[i].value >= bgpASlist[j].value
default:
return bgpASlist[i].routesNumber <= bgpASlist[j].routesNumber
}
})
}
if *debug {
log.Printf("Starting optimizer, target cam size %d\n"+
"input: %d routes in %d bgp autonomous systems\n",
*camSize, routesSeen, len(bgpASlist))
}
optimizeGreedy(*camSize, *debug)
if *debug {
PrintMemUsage()
}
}
func readConfigsToMemory(personalList, countryList, bestRoutes, aslist string) {
/* read personal file if any */
if personalList != "" {
file, err := os.Open(personalList)
if err != nil {
log.Fatal(err)
}
readPersonalPreference(file)
file.Close()
}
/* read country file if any */
if countryList != "" {
file, err := os.Open(countryList)
if err != nil {
log.Fatal(err)
}
readCountryList(file)
file.Close()
}
/* read routes file */
file, err := os.Open(bestRoutes)
if err != nil {
log.Fatal(err)
}
routesSeen, asNumberRoutes = readRoutes(file)
file.Close()
/* read aslist file */
file, err = os.Open(aslist)
if err != nil {
log.Fatal(err)
}
readAsList(file)
file.Close()
}