Skip to content

Commit 2af948f

Browse files
fboss2 config/delete protocol bgp policy as-path-list commands
Adds `config protocol bgp policy as-path-list <name> [entry <seq-num>] [<attribute> <value> ...]` and `delete protocol bgp policy as-path-list <name>` — the first of the BGP policy object-type command families, under a new `policy` grouping node (config + delete) beneath `protocol bgp` (sibling to global/neighbor/peer-group). - as-path-list dispatcher with a two-level key (list name + entry sequence-number) writing bgp_policy.BgpPolicies.aspath_lists[] through the typed ConfigSession; asn-regexp sets the AsPathType union's inline AsPath arm (.as_path.as_path.asn_regexp), match-logic maps to MatchValueLogicOperator, plus per-list and per-entry description. - shared template attribute-handler factories (BgpCliAttrHandlers.h), reused by the upcoming community-list/prefix-list/routing-policy types. - unit tests (16) + integration (10/10) on an NH-4010-F device. The commit-path integration tests confirm bgpd accepts and adopts the .policies blob end-to-end via the getRunningConfig RPC.
1 parent 7699625 commit 2af948f

17 files changed

Lines changed: 1479 additions & 0 deletions

cmake/CliFboss2.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,11 @@ add_library(fboss2_config_lib
866866
fboss/cli/fboss2/commands/config/protocol/bgp/neighbor/CmdConfigProtocolBgpNeighbor.h
867867
fboss/cli/fboss2/commands/config/protocol/bgp/peer-group/CmdConfigProtocolBgpPeerGroup.cpp
868868
fboss/cli/fboss2/commands/config/protocol/bgp/peer-group/CmdConfigProtocolBgpPeerGroup.h
869+
fboss/cli/fboss2/commands/config/protocol/bgp/BgpCliAttrHandlers.h
870+
fboss/cli/fboss2/commands/config/protocol/bgp/policy/CmdConfigProtocolBgpPolicy.cpp
871+
fboss/cli/fboss2/commands/config/protocol/bgp/policy/CmdConfigProtocolBgpPolicy.h
872+
fboss/cli/fboss2/commands/config/protocol/bgp/policy/as-path-list/CmdConfigProtocolBgpPolicyAsPathList.cpp
873+
fboss/cli/fboss2/commands/config/protocol/bgp/policy/as-path-list/CmdConfigProtocolBgpPolicyAsPathList.h
869874
fboss/cli/fboss2/commands/config/ptp/CmdConfigPtp.cpp
870875
fboss/cli/fboss2/commands/config/ptp/CmdConfigPtp.h
871876
fboss/cli/fboss2/commands/config/ptp/transparent_clock/CmdConfigPtpTransparentClock.cpp
@@ -964,6 +969,10 @@ add_library(fboss2_config_lib
964969
fboss/cli/fboss2/commands/delete/protocol/bgp/neighbor/CmdDeleteProtocolBgpNeighbor.h
965970
fboss/cli/fboss2/commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.cpp
966971
fboss/cli/fboss2/commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.h
972+
fboss/cli/fboss2/commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.cpp
973+
fboss/cli/fboss2/commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.h
974+
fboss/cli/fboss2/commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.cpp
975+
fboss/cli/fboss2/commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.h
967976
fboss/cli/fboss2/commands/delete/protocol/CmdDeleteProtocol.h
968977
fboss/cli/fboss2/commands/delete/protocol/static/CmdDeleteProtocolStatic.cpp
969978
fboss/cli/fboss2/commands/delete/protocol/static/CmdDeleteProtocolStatic.h

fboss/cli/fboss2/BUCK

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,8 @@ cpp_library(
10711071
"commands/config/protocol/bgp/global/CmdConfigProtocolBgpGlobal.cpp",
10721072
"commands/config/protocol/bgp/neighbor/CmdConfigProtocolBgpNeighbor.cpp",
10731073
"commands/config/protocol/bgp/peer-group/CmdConfigProtocolBgpPeerGroup.cpp",
1074+
"commands/config/protocol/bgp/policy/CmdConfigProtocolBgpPolicy.cpp",
1075+
"commands/config/protocol/bgp/policy/as-path-list/CmdConfigProtocolBgpPolicyAsPathList.cpp",
10741076
"commands/config/protocol/static/CmdConfigProtocolStatic.cpp",
10751077
"commands/config/protocol/static/route/add/CmdConfigProtocolStaticRouteAdd.cpp",
10761078
"commands/config/ptp/CmdConfigPtp.cpp",
@@ -1112,6 +1114,8 @@ cpp_library(
11121114
"commands/delete/protocol/bgp/CmdDeleteProtocolBgp.cpp",
11131115
"commands/delete/protocol/bgp/neighbor/CmdDeleteProtocolBgpNeighbor.cpp",
11141116
"commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.cpp",
1117+
"commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.cpp",
1118+
"commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.cpp",
11151119
"commands/delete/protocol/static/CmdDeleteProtocolStatic.cpp",
11161120
"commands/delete/protocol/static/route/CmdDeleteProtocolStaticRoute.cpp",
11171121
"commands/delete/tunnel/CmdDeleteTunnel.cpp",
@@ -1162,6 +1166,9 @@ cpp_library(
11621166
"commands/config/protocol/bgp/global/CmdConfigProtocolBgpGlobal.h",
11631167
"commands/config/protocol/bgp/neighbor/CmdConfigProtocolBgpNeighbor.h",
11641168
"commands/config/protocol/bgp/peer-group/CmdConfigProtocolBgpPeerGroup.h",
1169+
"commands/config/protocol/bgp/BgpCliAttrHandlers.h",
1170+
"commands/config/protocol/bgp/policy/CmdConfigProtocolBgpPolicy.h",
1171+
"commands/config/protocol/bgp/policy/as-path-list/CmdConfigProtocolBgpPolicyAsPathList.h",
11651172
"commands/config/protocol/static/CmdConfigProtocolStatic.h",
11661173
"commands/config/protocol/static/route/StaticRouteUtils.h",
11671174
"commands/config/protocol/static/route/add/CmdConfigProtocolStaticRouteAdd.h",
@@ -1204,6 +1211,8 @@ cpp_library(
12041211
"commands/delete/protocol/bgp/CmdDeleteProtocolBgp.h",
12051212
"commands/delete/protocol/bgp/neighbor/CmdDeleteProtocolBgpNeighbor.h",
12061213
"commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.h",
1214+
"commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.h",
1215+
"commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.h",
12071216
"commands/delete/protocol/static/CmdDeleteProtocolStatic.h",
12081217
"commands/delete/protocol/static/route/CmdDeleteProtocolStaticRoute.h",
12091218
"commands/delete/tunnel/CmdDeleteTunnel.h",

fboss/cli/fboss2/CmdListConfig.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#include "fboss/cli/fboss2/commands/config/protocol/bgp/global/CmdConfigProtocolBgpGlobal.h"
4040
#include "fboss/cli/fboss2/commands/config/protocol/bgp/neighbor/CmdConfigProtocolBgpNeighbor.h"
4141
#include "fboss/cli/fboss2/commands/config/protocol/bgp/peer-group/CmdConfigProtocolBgpPeerGroup.h"
42+
#include "fboss/cli/fboss2/commands/config/protocol/bgp/policy/CmdConfigProtocolBgpPolicy.h"
43+
#include "fboss/cli/fboss2/commands/config/protocol/bgp/policy/as-path-list/CmdConfigProtocolBgpPolicyAsPathList.h"
4244
#include "fboss/cli/fboss2/commands/config/protocol/static/CmdConfigProtocolStatic.h"
4345
#include "fboss/cli/fboss2/commands/config/protocol/static/route/add/CmdConfigProtocolStaticRouteAdd.h"
4446
#include "fboss/cli/fboss2/commands/config/ptp/CmdConfigPtp.h"
@@ -79,6 +81,8 @@
7981
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/CmdDeleteProtocolBgp.h"
8082
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/neighbor/CmdDeleteProtocolBgpNeighbor.h"
8183
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.h"
84+
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.h"
85+
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.h"
8286
#include "fboss/cli/fboss2/commands/delete/protocol/static/CmdDeleteProtocolStatic.h"
8387
#include "fboss/cli/fboss2/commands/delete/protocol/static/route/CmdDeleteProtocolStaticRoute.h"
8488
#include "fboss/cli/fboss2/commands/delete/tunnel/CmdDeleteTunnel.h"
@@ -357,6 +361,23 @@ const CommandTree& kConfigCommandTree() {
357361
commandHandler<CmdConfigProtocolBgpNeighbor>,
358362
argRegistrar<CmdConfigProtocolBgpNeighborTraits>,
359363
},
364+
{
365+
"policy",
366+
"Configure BGP policy objects",
367+
commandHandler<CmdConfigProtocolBgpPolicy>,
368+
argRegistrar<CmdConfigProtocolBgpPolicyTraits>,
369+
{{
370+
"as-path-list",
371+
"Configure BGP AS-path list: <name> "
372+
"[entry <seq-num>] [<attribute> <value> ...] "
373+
"(description; entry <seq-num> "
374+
"asn-regexp|description|match-logic)",
375+
commandHandler<
376+
CmdConfigProtocolBgpPolicyAsPathList>,
377+
argRegistrar<
378+
CmdConfigProtocolBgpPolicyAsPathListTraits>,
379+
}},
380+
},
360381
},
361382
},
362383
{
@@ -615,6 +636,19 @@ const CommandTree& kConfigCommandTree() {
615636
"Delete a BGP peer-group: <name>",
616637
commandHandler<CmdDeleteProtocolBgpPeerGroup>,
617638
argRegistrar<CmdDeleteProtocolBgpPeerGroupTraits>,
639+
},
640+
{
641+
"policy",
642+
"Delete BGP policy objects",
643+
commandHandler<CmdDeleteProtocolBgpPolicy>,
644+
argTypeHandler<CmdDeleteProtocolBgpPolicyTraits>,
645+
{{
646+
"as-path-list",
647+
"Delete a BGP AS-path list: <name>",
648+
commandHandler<CmdDeleteProtocolBgpPolicyAsPathList>,
649+
argRegistrar<
650+
CmdDeleteProtocolBgpPolicyAsPathListTraits>,
651+
}},
618652
}},
619653
},
620654
{
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) 2004-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
*/
10+
11+
#pragma once
12+
13+
#include <fmt/core.h>
14+
#include <cstddef>
15+
#include <functional>
16+
#include <optional>
17+
#include <string>
18+
#include <string_view>
19+
#include <utility>
20+
#include <vector>
21+
#include "fboss/cli/fboss2/commands/config/protocol/bgp/BgpCliValueParsers.h"
22+
#include "fmt/format.h"
23+
24+
/**
25+
* Generic, target-typed per-attribute handler factories shared by the BGP
26+
* `config protocol bgp policy ...` dispatchers.
27+
*
28+
* Unlike the per-command factories in the neighbor/peer-group dispatchers
29+
* (which are hard-wired to a single thrift struct), these are templated on the
30+
* target type T so the same parsing/validation logic can mutate any policy
31+
* object (AsPathList, AsPathListEntry, and — as later object types land —
32+
* CommunityList, PrefixList, BgpPolicyTerm, ...). A handler validates its
33+
* tokens and either mutates T or returns an error the caller surfaces without
34+
* persisting.
35+
*/
36+
namespace facebook::fboss::bgpcli {
37+
38+
using Tokens = std::vector<std::string>;
39+
40+
// A per-attribute handler mutating a typed thrift target T in place.
41+
template <typename T>
42+
using AttrHandler = std::function<Result(T&, const Tokens&)>;
43+
44+
// A single-token string value (names, regex patterns, policy names).
45+
template <typename T>
46+
AttrHandler<T> stringAttr(
47+
std::string_view name,
48+
std::string_view valueName,
49+
std::function<void(T&, const std::string&)> set) {
50+
return [name, valueName, set = std::move(set)](
51+
T& target, const Tokens& values) -> Result {
52+
if (values.size() != 1) {
53+
return err(fmt::format("Error: {} requires <{}>", name, valueName));
54+
}
55+
set(target, values[0]);
56+
return ok(fmt::format("Successfully set {} to: {}", name, values[0]));
57+
};
58+
}
59+
60+
// A free-text string value that may span multiple CLI tokens (descriptions);
61+
// the tokens are re-joined with single spaces.
62+
template <typename T>
63+
AttrHandler<T> joinedStringAttr(
64+
std::string_view name,
65+
std::function<void(T&, const std::string&)> set) {
66+
return
67+
[name, set = std::move(set)](T& target, const Tokens& values) -> Result {
68+
if (values.empty()) {
69+
return err(fmt::format("Error: {} requires <string>", name));
70+
}
71+
std::string joined = values[0];
72+
for (size_t i = 1; i < values.size(); ++i) {
73+
joined += " " + values[i];
74+
}
75+
set(target, joined);
76+
return ok(fmt::format("Successfully set {} to: {}", name, joined));
77+
};
78+
}
79+
80+
// A value drawn from a fixed set of names, mapped to an enum by `lookup`
81+
// (returns std::nullopt for an unrecognized name). `valueDesc` documents the
82+
// accepted names in both the usage and the rejection message.
83+
template <typename T, typename Enum>
84+
AttrHandler<T> enumAttr(
85+
std::string_view name,
86+
std::string_view valueDesc,
87+
std::function<std::optional<Enum>(const std::string&)> lookup,
88+
std::function<void(T&, Enum)> set) {
89+
return [name, valueDesc, lookup = std::move(lookup), set = std::move(set)](
90+
T& target, const Tokens& values) -> Result {
91+
if (values.size() != 1) {
92+
return err(fmt::format("Error: {} requires <{}>", name, valueDesc));
93+
}
94+
auto parsed = lookup(values[0]);
95+
if (!parsed) {
96+
return err(
97+
fmt::format(
98+
"Error: Invalid {} value '{}'; expected {}",
99+
name,
100+
values[0],
101+
valueDesc));
102+
}
103+
set(target, *parsed);
104+
return ok(fmt::format("Successfully set {} to: {}", name, values[0]));
105+
};
106+
}
107+
108+
} // namespace facebook::fboss::bgpcli
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2004-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
*/
10+
11+
#include "fboss/cli/fboss2/commands/config/protocol/bgp/policy/CmdConfigProtocolBgpPolicy.h"
12+
13+
#include "fboss/cli/fboss2/CmdHandler.cpp"
14+
15+
namespace facebook::fboss {
16+
17+
CmdConfigProtocolBgpPolicyTraits::RetType
18+
CmdConfigProtocolBgpPolicy::queryClient(const HostInfo& /* hostInfo */) {
19+
return "BGP policy configuration. Use subcommands: as-path-list";
20+
}
21+
22+
void CmdConfigProtocolBgpPolicy::printOutput(const RetType& output) {
23+
std::cout << output << std::endl;
24+
}
25+
26+
// Explicit template instantiation
27+
template void
28+
CmdHandler<CmdConfigProtocolBgpPolicy, CmdConfigProtocolBgpPolicyTraits>::run();
29+
30+
} // namespace facebook::fboss
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2004-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
*/
10+
11+
#pragma once
12+
13+
#include "fboss/cli/fboss2/CmdHandler.h"
14+
#include "fboss/cli/fboss2/commands/config/protocol/bgp/CmdConfigProtocolBgp.h"
15+
16+
namespace facebook::fboss {
17+
18+
// Grouping node for `config protocol bgp policy <object-type> ...`. Holds no
19+
// state of its own; the per-object-type dispatchers (as-path-list, and later
20+
// community-list/prefix-list/routing-policy) are its subcommands. Mirrors
21+
// CmdConfigProtocolBgp.
22+
struct CmdConfigProtocolBgpPolicyTraits : public WriteCommandTraits {
23+
using ParentCmd = CmdConfigProtocolBgp;
24+
using ObjectArgType = std::monostate;
25+
using RetType = std::string;
26+
};
27+
28+
class CmdConfigProtocolBgpPolicy : public CmdHandler<
29+
CmdConfigProtocolBgpPolicy,
30+
CmdConfigProtocolBgpPolicyTraits> {
31+
public:
32+
using ObjectArgType = CmdConfigProtocolBgpPolicyTraits::ObjectArgType;
33+
using RetType = CmdConfigProtocolBgpPolicyTraits::RetType;
34+
35+
RetType queryClient(const HostInfo& hostInfo);
36+
37+
void printOutput(const RetType& output);
38+
};
39+
40+
} // namespace facebook::fboss

0 commit comments

Comments
 (0)