]> git.proxmox.com Git - mirror_frr.git/commitdiff
bgpd: Convert bgp_packet_mpattr_prefix to use an enum for safi's
authorDonald Sharp <sharpd@nvidia.com>
Fri, 16 Dec 2022 13:43:16 +0000 (08:43 -0500)
committerDonald Sharp <sharpd@nvidia.com>
Fri, 16 Dec 2022 13:43:16 +0000 (08:43 -0500)
The function bgp_packet_mpattr_prefix was using an if statement
to encode packets to the peer.  Change it to a switch and make
it handle all the cases and fail appropriately when something
has gone wrong.  Hopefully in the future when a new afi/safi
is added we can catch it by compilation breaking instead of
weird runtime errors

Signed-off-by: Donald Sharp <sharpd@nvidia.com>
bgpd/bgp_attr.c

index 1c8dfea33ce187d3b7f61b3202f3021e8c791c5c..72905a6acb6f21da00728018ac0789e6bb17a116 100644 (file)
@@ -3961,7 +3961,12 @@ void bgp_packet_mpattr_prefix(struct stream *s, afi_t afi, safi_t safi,
                              uint32_t num_labels, bool addpath_capable,
                              uint32_t addpath_tx_id, struct attr *attr)
 {
-       if (safi == SAFI_MPLS_VPN) {
+       switch (safi) {
+       case SAFI_UNSPEC:
+       case SAFI_MAX:
+               assert(!"Dev escape usage of SAFI_UNSPEC or MAX");
+               break;
+       case SAFI_MPLS_VPN:
                if (addpath_capable)
                        stream_putl(s, addpath_tx_id);
                /* Label, RD, Prefix write. */
@@ -3969,20 +3974,35 @@ void bgp_packet_mpattr_prefix(struct stream *s, afi_t afi, safi_t safi,
                stream_put(s, label, BGP_LABEL_BYTES);
                stream_put(s, prd->val, 8);
                stream_put(s, &p->u.prefix, PSIZE(p->prefixlen));
-       } else if (afi == AFI_L2VPN && safi == SAFI_EVPN) {
-               /* EVPN prefix - contents depend on type */
-               bgp_evpn_encode_prefix(s, p, prd, label, num_labels, attr,
-                                      addpath_capable, addpath_tx_id);
-       } else if (safi == SAFI_LABELED_UNICAST) {
+               break;
+       case SAFI_EVPN:
+               if (afi == AFI_L2VPN)
+                       /* EVPN prefix - contents depend on type */
+                       bgp_evpn_encode_prefix(s, p, prd, label, num_labels,
+                                              attr, addpath_capable,
+                                              addpath_tx_id);
+               else
+                       assert(!"Add encoding bits here for other AFI's");
+               break;
+       case SAFI_LABELED_UNICAST:
                /* Prefix write with label. */
                stream_put_labeled_prefix(s, p, label, addpath_capable,
                                          addpath_tx_id);
-       } else if (safi == SAFI_FLOWSPEC) {
+               break;
+       case SAFI_FLOWSPEC:
                stream_putc(s, p->u.prefix_flowspec.prefixlen);
                stream_put(s, (const void *)p->u.prefix_flowspec.ptr,
                           p->u.prefix_flowspec.prefixlen);
-       } else
+               break;
+
+       case SAFI_UNICAST:
+       case SAFI_MULTICAST:
                stream_put_prefix_addpath(s, p, addpath_capable, addpath_tx_id);
+               break;
+       case SAFI_ENCAP:
+               assert(!"Please add proper encoding of SAFI_ENCAP");
+               break;
+       }
 }
 
 size_t bgp_packet_mpattr_prefix_size(afi_t afi, safi_t safi,