github.com/osrg/gobgp/v3@v3.30.0/docs/sources/lib.md (about)

     1  # GoBGP as a Go Native BGP library
     2  
     3  This page explains how to use GoBGP as a Go Native BGP library.
     4  
     5  ## Contents
     6  
     7  - [Basic Example](#basic-example)
     8  
     9  ## Basic Example
    10  
    11  ```go
    12  package main
    13  
    14  import (
    15  	"context"
    16  	"time"
    17  
    18  	"github.com/sirupsen/logrus"
    19  	apb "google.golang.org/protobuf/types/known/anypb"
    20  
    21  	api "github.com/osrg/gobgp/v3/api"
    22  	"github.com/osrg/gobgp/v3/pkg/log"
    23  	"github.com/osrg/gobgp/v3/pkg/server"
    24  )
    25  
    26  func main() {
    27  	log	:= logrus.New()
    28  
    29  	s := server.NewBgpServer(server.LoggerOption(&myLogger{logger: log}))
    30  	go s.Serve()
    31  
    32  	// global configuration
    33  	if err := s.StartBgp(context.Background(), &api.StartBgpRequest{
    34  		Global: &api.Global{
    35  			Asn:         65003,
    36  			RouterId:   "10.0.255.254",
    37  			ListenPort: -1, // gobgp won't listen on tcp:179
    38  		},
    39  	}); err != nil {
    40  		log.Fatal(err)
    41  	}
    42  
    43  	// monitor the change of the peer state
    44  	if err := s.WatchEvent(context.Background(), &api.WatchEventRequest{Peer: &api.WatchEventRequest_Peer{},}, func(r *api.WatchEventResponse) {
    45  			if p := r.GetPeer(); p != nil && p.Type == api.WatchEventResponse_PeerEvent_STATE {
    46  				log.Info(p)
    47  			}
    48  		}); err != nil {
    49  		log.Fatal(err)
    50  	}
    51  
    52  	// neighbor configuration
    53  	n := &api.Peer{
    54  		Conf: &api.PeerConf{
    55  			NeighborAddress: "172.17.0.2",
    56  			PeerAsn:          65002,
    57  		},
    58  	}
    59  
    60  	if err := s.AddPeer(context.Background(), &api.AddPeerRequest{
    61  		Peer: n,
    62  	}); err != nil {
    63  		log.Fatal(err)
    64  	}
    65  
    66  	// add routes
    67  	nlri, _ := apb.New(&api.IPAddressPrefix{
    68  		Prefix:    "10.0.0.0",
    69  		PrefixLen: 24,
    70  	})
    71  
    72  	a1, _ := apb.New(&api.OriginAttribute{
    73  		Origin: 0,
    74  	})
    75  	a2, _ := apb.New(&api.NextHopAttribute{
    76  		NextHop: "10.0.0.1",
    77  	})
    78  	a3, _ := apb.New(&api.AsPathAttribute{
    79  		Segments: []*api.AsSegment{
    80  			{
    81  				Type:    2,
    82  				Numbers: []uint32{6762, 39919, 65000, 35753, 65000},
    83  			},
    84  		},
    85  	})
    86  	attrs := []*apb.Any{a1, a2, a3}
    87  
    88  	_, err := s.AddPath(context.Background(), &api.AddPathRequest{
    89  		Path: &api.Path{
    90  			Family: &api.Family{Afi: api.Family_AFI_IP, Safi: api.Family_SAFI_UNICAST},
    91  			Nlri:   nlri,
    92  			Pattrs: attrs,
    93  		},
    94  	})
    95  	if err != nil {
    96  		log.Fatal(err)
    97  	}
    98  
    99  	v6Family := &api.Family{
   100  		Afi:  api.Family_AFI_IP6,
   101  		Safi: api.Family_SAFI_UNICAST,
   102  	}
   103  
   104  	// add v6 route
   105  	nlri, _ = apb.New(&api.IPAddressPrefix{
   106  		PrefixLen: 64,
   107  		Prefix:    "2001:db8:1::",
   108  	})
   109  	v6Attrs, _ := apb.New(&api.MpReachNLRIAttribute{
   110  		Family:   v6Family,
   111  		NextHops: []string{"2001:db8::1"},
   112  		Nlris:    []*apb.Any{nlri},
   113  	})
   114  
   115  	c, _ := apb.New(&api.CommunitiesAttribute{
   116  		Communities: []uint32{100, 200},
   117  	})
   118  
   119  	_, err = s.AddPath(context.Background(), &api.AddPathRequest{
   120  		Path: &api.Path{
   121  			Family: v6Family,
   122  			Nlri:   nlri,
   123  			Pattrs: []*apb.Any{a1, v6Attrs, c},
   124  		},
   125  	})
   126  	if err != nil {
   127  		log.Fatal(err)
   128  	}
   129  
   130  	s.ListPath(context.Background(), &api.ListPathRequest{Family: v6Family}, func(p *api.Destination) {
   131  		log.Info(p)
   132  	})
   133  
   134  	// do something useful here instead of exiting
   135  	time.Sleep(time.Minute * 3)
   136  }
   137  
   138  // implement github.com/osrg/gobgp/v3/pkg/log/Logger interface
   139  type myLogger struct {
   140  	logger *logrus.Logger
   141  }
   142  
   143  func (l *myLogger) Panic(msg string, fields log.Fields) {
   144  	l.logger.WithFields(logrus.Fields(fields)).Panic(msg)
   145  }
   146  
   147  func (l *myLogger) Fatal(msg string, fields log.Fields) {
   148  	l.logger.WithFields(logrus.Fields(fields)).Fatal(msg)
   149  }
   150  
   151  func (l *myLogger) Error(msg string, fields log.Fields) {
   152  	l.logger.WithFields(logrus.Fields(fields)).Error(msg)
   153  }
   154  
   155  func (l *myLogger) Warn(msg string, fields log.Fields) {
   156  	l.logger.WithFields(logrus.Fields(fields)).Warn(msg)
   157  }
   158  
   159  func (l *myLogger) Info(msg string, fields log.Fields) {
   160  	l.logger.WithFields(logrus.Fields(fields)).Info(msg)
   161  }
   162  
   163  func (l *myLogger) Debug(msg string, fields log.Fields) {
   164  	l.logger.WithFields(logrus.Fields(fields)).Debug(msg)
   165  }
   166  
   167  func (l *myLogger) SetLevel(level log.LogLevel) {
   168  	l.logger.SetLevel(logrus.Level(level))
   169  }
   170  
   171  func (l *myLogger) GetLevel() log.LogLevel {
   172  	return log.LogLevel(l.logger.GetLevel())
   173  }
   174  ```