本文整理匯總了Golang中github.com/osrg/gobgp/packet/bgp.AddrPrefixInterface.Serialize方法的典型用法代碼示例。如果您正苦於以下問題:Golang AddrPrefixInterface.Serialize方法的具體用法?Golang AddrPrefixInterface.Serialize怎麽用?Golang AddrPrefixInterface.Serialize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/osrg/gobgp/packet/bgp.AddrPrefixInterface
的用法示例。
在下文中一共展示了AddrPrefixInterface.Serialize方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: createRequestFromIPRouteMessage
func createRequestFromIPRouteMessage(m *zebra.Message) *api.AddPathRequest {
header := m.Header
body := m.Body.(*zebra.IPRouteBody)
family := bgp.RF_IPv6_UC
if header.Command == zebra.IPV4_ROUTE_ADD || header.Command == zebra.IPV4_ROUTE_DELETE {
family = bgp.RF_IPv4_UC
}
var nlri bgp.AddrPrefixInterface
pattr := make([]bgp.PathAttributeInterface, 0)
var mpnlri *bgp.PathAttributeMpReachNLRI
var isWithdraw bool = header.Command == zebra.IPV4_ROUTE_DELETE || header.Command == zebra.IPV6_ROUTE_DELETE
origin := bgp.NewPathAttributeOrigin(bgp.BGP_ORIGIN_ATTR_TYPE_IGP)
pattr = append(pattr, origin)
log.WithFields(log.Fields{
"Topic": "Zebra",
"RouteType": body.Type.String(),
"Flag": body.Flags.String(),
"Message": body.Message,
"Prefix": body.Prefix,
"PrefixLength": body.PrefixLength,
"Nexthop": body.Nexthops,
"IfIndex": body.Ifindexs,
"Metric": body.Metric,
"Distance": body.Distance,
"api": header.Command.String(),
}).Debugf("create path from ip route message.")
switch family {
case bgp.RF_IPv4_UC:
nlri = bgp.NewIPAddrPrefix(body.PrefixLength, body.Prefix.String())
nexthop := bgp.NewPathAttributeNextHop(body.Nexthops[0].String())
pattr = append(pattr, nexthop)
case bgp.RF_IPv6_UC:
nlri = bgp.NewIPv6AddrPrefix(body.PrefixLength, body.Prefix.String())
mpnlri = bgp.NewPathAttributeMpReachNLRI(body.Nexthops[0].String(), []bgp.AddrPrefixInterface{nlri})
pattr = append(pattr, mpnlri)
default:
log.WithFields(log.Fields{
"Topic": "Zebra",
}).Errorf("unsupport address family: %s", family)
return nil
}
med := bgp.NewPathAttributeMultiExitDisc(body.Metric)
pattr = append(pattr, med)
binPattrs := make([][]byte, 0, len(pattr))
for _, a := range pattr {
bin, _ := a.Serialize()
binPattrs = append(binPattrs, bin)
}
binNlri, _ := nlri.Serialize()
path := &api.Path{
Nlri: binNlri,
Pattrs: binPattrs,
IsWithdraw: isWithdraw,
Family: uint32(family),
IsFromExternal: true,
}
return &api.AddPathRequest{
Resource: api.Resource_GLOBAL,
Path: path,
}
}
示例2: parseRouteToGobgp
func parseRouteToGobgp(route ovsdb.RowUpdate, nexthops map[string]ovsdb.Row) (*api.Path, bool, bool, error) {
var nlri bgp.AddrPrefixInterface
path := &api.Path{
IsFromExternal: true,
Pattrs: make([][]byte, 0),
}
isWithdraw := false
isFromGobgp := false
prefix := route.New.Fields["prefix"].(string)
safi := route.New.Fields["sub_address_family"].(string)
afi := route.New.Fields["address_family"].(string)
m := route.New.Fields["metric"].(float64)
attrs := route.New.Fields["path_attributes"].(ovsdb.OvsMap).GoMap
if attrs["IsFromGobgp"] == "true" {
isFromGobgp = true
}
nh := make([]interface{}, 0)
nhId, ok := route.New.Fields["bgp_nexthops"].(ovsdb.UUID)
if ok {
for id, n := range nexthops {
if id == nhId.GoUUID {
nh = append(nh, n.Fields["ip_address"])
}
}
}
nexthop := "0.0.0.0"
if afi == "ipv6" {
nexthop = "::"
}
if len(nh) == 0 {
log.Debug("nexthop addres does not exist")
} else if len(nh) == 1 {
if net.ParseIP(nh[0].(string)) == nil {
return nil, isWithdraw, isFromGobgp, fmt.Errorf("invalid nexthop address")
} else {
nexthop = nh[0].(string)
}
} else {
return nil, isWithdraw, isFromGobgp, fmt.Errorf("route has multiple nexthop address")
}
med, _ := bgp.NewPathAttributeMultiExitDisc(uint32(m)).Serialize()
path.Pattrs = append(path.Pattrs, med)
lpref, err := strconv.Atoi(attrs["BGP_loc_pref"].(string))
if err != nil {
return nil, isWithdraw, isFromGobgp, err
}
localPref, _ := bgp.NewPathAttributeLocalPref(uint32(lpref)).Serialize()
path.Pattrs = append(path.Pattrs, localPref)
var origin_t int
switch attrs["BGP_origin"].(string) {
case "i":
origin_t = bgp.BGP_ORIGIN_ATTR_TYPE_IGP
case "e":
origin_t = bgp.BGP_ORIGIN_ATTR_TYPE_EGP
case "?":
origin_t = bgp.BGP_ORIGIN_ATTR_TYPE_INCOMPLETE
default:
return nil, isWithdraw, isFromGobgp, fmt.Errorf("invalid origin")
}
origin, _ := bgp.NewPathAttributeOrigin(uint8(origin_t)).Serialize()
path.Pattrs = append(path.Pattrs, origin)
switch afi {
case "ipv4", "ipv6":
ip, net, err := net.ParseCIDR(prefix)
if err != nil {
return nil, isWithdraw, isFromGobgp, err
}
ones, _ := net.Mask.Size()
if afi == "ipv4" {
if ip.To4() == nil {
return nil, isWithdraw, isFromGobgp, fmt.Errorf("invalid ipv4 prefix")
}
nlri = bgp.NewIPAddrPrefix(uint8(ones), ip.String())
} else {
if ip.To16() == nil {
return nil, isWithdraw, isFromGobgp, fmt.Errorf("invalid ipv6 prefix")
}
nlri = bgp.NewIPv6AddrPrefix(uint8(ones), ip.String())
}
default:
return nil, isWithdraw, isFromGobgp, fmt.Errorf("unsupported address family: %s", afi)
}
if afi == "ipv4" && safi == "unicast" {
path.Nlri, _ = nlri.Serialize()
n, _ := bgp.NewPathAttributeNextHop(nexthop).Serialize()
path.Pattrs = append(path.Pattrs, n)
} else {
mpreach, _ := bgp.NewPathAttributeMpReachNLRI(nexthop, []bgp.AddrPrefixInterface{nlri}).Serialize()
path.Pattrs = append(path.Pattrs, mpreach)
}
if attrs["BGP_flags"].(string) == "512" {
isWithdraw = true
//.........這裏部分代碼省略.........
示例3: ParsePath
//.........這裏部分代碼省略.........
case bgp.RF_IPv4_VPN, bgp.RF_IPv6_VPN:
if len(args) < 3 || args[1] != "rd" {
return nil, fmt.Errorf("invalid format")
}
ip, net, _ := net.ParseCIDR(args[0])
ones, _ := net.Mask.Size()
rd, err := bgp.ParseRouteDistinguisher(args[2])
if err != nil {
return nil, err
}
extcomms = args[3:]
mpls := bgp.NewMPLSLabelStack()
if rf == bgp.RF_IPv4_VPN {
if ip.To4() == nil {
return nil, fmt.Errorf("invalid ipv4 prefix")
}
nlri = bgp.NewLabeledVPNIPAddrPrefix(uint8(ones), ip.String(), *mpls, rd)
} else {
if ip.To16() == nil {
return nil, fmt.Errorf("invalid ipv6 prefix")
}
nlri = bgp.NewLabeledVPNIPv6AddrPrefix(uint8(ones), ip.String(), *mpls, rd)
}
case bgp.RF_IPv4_MPLS, bgp.RF_IPv6_MPLS:
if len(args) < 2 {
return nil, fmt.Errorf("invalid format")
}
ip, net, _ := net.ParseCIDR(args[0])
ones, _ := net.Mask.Size()
mpls, err := bgp.ParseMPLSLabelStack(args[1])
if err != nil {
return nil, err
}
extcomms = args[2:]
if rf == bgp.RF_IPv4_MPLS {
if ip.To4() == nil {
return nil, fmt.Errorf("invalid ipv4 prefix")
}
nlri = bgp.NewLabeledIPAddrPrefix(uint8(ones), ip.String(), *mpls)
} else {
if ip.To4() != nil {
return nil, fmt.Errorf("invalid ipv6 prefix")
}
nlri = bgp.NewLabeledIPv6AddrPrefix(uint8(ones), ip.String(), *mpls)
}
case bgp.RF_EVPN:
nlri, extcomms, err = ParseEvpnArgs(args)
case bgp.RF_FS_IPv4_UC, bgp.RF_FS_IPv6_UC, bgp.RF_FS_L2_VPN:
nlri, extcomms, err = ParseFlowSpecArgs(rf, args)
case bgp.RF_OPAQUE:
m := extractReserved(args, []string{"key", "value"})
if len(m["key"]) != 1 || len(m["value"]) != 1 {
return nil, fmt.Errorf("invalid key-value format")
}
nlri = bgp.NewOpaqueNLRI([]byte(m["key"][0]))
attrs = append(attrs, bgp.NewPathAttributeOpaqueValue([]byte(m["value"][0])))
default:
return nil, fmt.Errorf("Unsupported route family: %s", rf)
}
if err != nil {
return nil, err
}
if rf == bgp.RF_IPv4_UC {
path.Nlri, _ = nlri.Serialize()
attrs = append(attrs, bgp.NewPathAttributeNextHop(nexthop))
} else {
mpreach := bgp.NewPathAttributeMpReachNLRI(nexthop, []bgp.AddrPrefixInterface{nlri})
attrs = append(attrs, mpreach)
}
if extcomms != nil && len(extcomms) > 0 {
extcomms, err := ParseExtendedCommunities(strings.Join(extcomms, " "))
if err != nil {
return nil, err
}
p := bgp.NewPathAttributeExtendedCommunities(extcomms)
attrs = append(attrs, p)
}
sort.Sort(attrs)
for _, attr := range attrs {
buf, err := attr.Serialize()
if err != nil {
return nil, err
}
path.Pattrs = append(path.Pattrs, buf)
}
return path, nil
}