github.com/osrg/gobgp/v3@v3.30.0/docs/sources/lib-ls.md (about) 1 # Using BGP-LS in GoBGP library mode 2 3 This page explains how to use GoBGP for getting BGP-LS prefixes. 4 5 ## Contents 6 7 - [Basic BGP-LS Example](#basic-bgp-ls-example) 8 9 ## Basic BGP-LS Example 10 11 ```go 12 package main 13 14 import ( 15 "context" 16 17 "github.com/sirupsen/logrus" 18 "google.golang.org/protobuf/encoding/protojson" 19 20 api "github.com/osrg/gobgp/v3/api" 21 "github.com/osrg/gobgp/v3/pkg/server" 22 "github.com/osrg/gobgp/v3/pkg/log" 23 ) 24 25 func main() { 26 log := logrus.New() 27 log.SetLevel(logrus.DebugLevel) 28 29 s := server.NewBgpServer(server.LoggerOption(&myLogger{logger: log})) 30 go s.Serve() 31 32 if err := s.StartBgp(context.Background(), &api.StartBgpRequest{ 33 Global: &api.Global{ 34 Asn: 64512, 35 RouterId: "10.0.255.254", 36 ListenPort: -1, // gobgp won't listen on tcp:179 37 }, 38 }); err != nil { 39 log.Fatal(err) 40 } 41 42 marshaller := protojson.MarshalOptions{ 43 Indent: " ", 44 UseProtoNames: true, 45 } 46 47 // the change of the peer state and path 48 if err := s.WatchEvent(context.Background(), &api.WatchEventRequest{ 49 Peer: &api.WatchEventRequest_Peer{}, 50 Table: &api.WatchEventRequest_Table{ 51 Filters: []*api.WatchEventRequest_Table_Filter{ 52 { 53 Type: api.WatchEventRequest_Table_Filter_BEST, 54 }, 55 }, 56 },}, func(r *api.WatchEventResponse) { 57 if p := r.GetPeer(); p != nil && p.Type == api.WatchEventResponse_PeerEvent_STATE { 58 log.Info(p) 59 } else if t := r.GetTable(); t != nil { 60 // Your application should do something useful with the BGP-LS path here. 61 for _, p := range t.Paths { 62 marshaller.Marshal(p) 63 } 64 } 65 }); err != nil { 66 log.Fatal(err) 67 } 68 69 // neighbor configuration 70 n := &api.Peer{ 71 Conf: &api.PeerConf{ 72 NeighborAddress: "172.17.0.2", 73 PeerAsn: 65002, 74 }, 75 ApplyPolicy: &api.ApplyPolicy{ 76 ImportPolicy: &api.PolicyAssignment{ 77 DefaultAction: api.RouteAction_ACCEPT, 78 }, 79 ExportPolicy: &api.PolicyAssignment{ 80 DefaultAction: api.RouteAction_REJECT, 81 }, 82 }, 83 AfiSafis: []*api.AfiSafi{ 84 { 85 Config: &api.AfiSafiConfig{ 86 Family: &api.Family{ 87 Afi: api.Family_AFI_LS, 88 Safi: api.Family_SAFI_LS, 89 }, 90 Enabled: true, 91 }, 92 }, 93 }, 94 } 95 96 if err := s.AddPeer(context.Background(), &api.AddPeerRequest{ 97 Peer: n, 98 }); err != nil { 99 log.Fatal(err) 100 } 101 102 select {} 103 } 104 105 // implement github.com/osrg/gobgp/v3/pkg/log/Logger interface 106 type myLogger struct { 107 logger *logrus.Logger 108 } 109 110 func (l *myLogger) Panic(msg string, fields log.Fields) { 111 l.logger.WithFields(logrus.Fields(fields)).Panic(msg) 112 } 113 114 func (l *myLogger) Fatal(msg string, fields log.Fields) { 115 l.logger.WithFields(logrus.Fields(fields)).Fatal(msg) 116 } 117 118 func (l *myLogger) Error(msg string, fields log.Fields) { 119 l.logger.WithFields(logrus.Fields(fields)).Error(msg) 120 } 121 122 func (l *myLogger) Warn(msg string, fields log.Fields) { 123 l.logger.WithFields(logrus.Fields(fields)).Warn(msg) 124 } 125 126 func (l *myLogger) Info(msg string, fields log.Fields) { 127 l.logger.WithFields(logrus.Fields(fields)).Info(msg) 128 } 129 130 func (l *myLogger) Debug(msg string, fields log.Fields) { 131 l.logger.WithFields(logrus.Fields(fields)).Debug(msg) 132 } 133 134 func (l *myLogger) SetLevel(level log.LogLevel) { 135 l.logger.SetLevel(logrus.Level(level)) 136 } 137 138 func (l *myLogger) GetLevel() log.LogLevel { 139 return log.LogLevel(l.logger.GetLevel()) 140 } 141 142 ```