]> git.puffer.fish Git - matthieu/frr.git/commitdiff
zebra: collapse some duplicate LSP nhlfe apis
authorMark Stapp <mjs@voltanet.io>
Tue, 2 Jun 2020 20:16:21 +0000 (16:16 -0400)
committerMark Stapp <mjs@voltanet.io>
Tue, 7 Jul 2020 17:25:23 +0000 (13:25 -0400)
Collapse some apis where primary and backup nhlfe code
was very similar, generally using a single common api
and using a bool to distinguish between primary and
backup.

Signed-off-by: Mark Stapp <mjs@voltanet.io>
zebra/zapi_msg.c
zebra/zebra_mpls.c
zebra/zebra_mpls.h

index a40aa8b64345cc5e921b0ffa629d1d8edef6b7c7..dc7c595d26133c7db9031753ae9bd6418b768f71 100644 (file)
@@ -2485,7 +2485,7 @@ static void zread_vrf_label(ZAPI_HANDLER_ARGS)
                if (really_remove)
                        mpls_lsp_uninstall(def_zvrf, ltype, zvrf->label[afi],
                                           NEXTHOP_TYPE_IFINDEX, NULL,
-                                          ifp->ifindex);
+                                          ifp->ifindex, false /*backup*/);
        }
 
        if (nlabel != MPLS_LABEL_NONE) {
index 7903b97c494644aa9b2f76669d32af242497af41..f203405cc9c192dda23fba81bf087e9ba3539d38 100644 (file)
@@ -110,17 +110,14 @@ static zebra_nhlfe_t *nhlfe_find(struct nhlfe_list_head *list,
 static zebra_nhlfe_t *nhlfe_add(zebra_lsp_t *lsp, enum lsp_types_t lsp_type,
                                enum nexthop_types_t gtype,
                                const union g_addr *gate, ifindex_t ifindex,
-                               uint8_t num_labels, const mpls_label_t *labels);
+                               uint8_t num_labels, const mpls_label_t *labels,
+                               bool is_backup);
 static int nhlfe_del(zebra_nhlfe_t *nhlfe);
 static void nhlfe_free(zebra_nhlfe_t *nhlfe);
 static void nhlfe_out_label_update(zebra_nhlfe_t *nhlfe,
                                   struct mpls_label_stack *nh_label);
 static int mpls_lsp_uninstall_all(struct hash *lsp_table, zebra_lsp_t *lsp,
                                  enum lsp_types_t type);
-static int lsp_backup_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
-                               mpls_label_t in_label,
-                               enum nexthop_types_t gtype,
-                               const union g_addr *gate, ifindex_t ifindex);
 static int mpls_static_lsp_uninstall_all(struct zebra_vrf *zvrf,
                                         mpls_label_t in_label);
 static void nhlfe_print(zebra_nhlfe_t *nhlfe, struct vty *vty);
@@ -249,7 +246,8 @@ static int lsp_install(struct zebra_vrf *zvrf, mpls_label_t label,
                        nhlfe = nhlfe_add(lsp, lsp_type, nexthop->type,
                                          &nexthop->gate, nexthop->ifindex,
                                          nexthop->nh_label->num_labels,
-                                         nexthop->nh_label->label);
+                                         nexthop->nh_label->label,
+                                         false /*backup*/);
                        if (!nhlfe)
                                return -1;
 
@@ -1319,39 +1317,14 @@ static zebra_nhlfe_t *nhlfe_alloc(zebra_lsp_t *lsp, enum lsp_types_t lsp_type,
 }
 
 /*
- * Add NHLFE. Base entry must have been created and duplicate
- * check done.
+ * Add primary or backup NHLFE. Base entry must have been created and
+ * duplicate check done.
  */
 static zebra_nhlfe_t *nhlfe_add(zebra_lsp_t *lsp, enum lsp_types_t lsp_type,
                                enum nexthop_types_t gtype,
                                const union g_addr *gate, ifindex_t ifindex,
-                               uint8_t num_labels, const mpls_label_t *labels)
-{
-       zebra_nhlfe_t *nhlfe;
-
-       if (!lsp)
-               return NULL;
-
-       /* Allocate new object */
-       nhlfe = nhlfe_alloc(lsp, lsp_type, gtype, gate, ifindex, num_labels,
-                           labels);
-
-       /* Enqueue to LSP, at head of list. */
-       nhlfe_list_add_head(&lsp->nhlfe_list, nhlfe);
-
-       return nhlfe;
-}
-
-/*
- * Add backup NHLFE. Base entry must have been created and duplicate
- * check done.
- */
-static zebra_nhlfe_t *nhlfe_backup_add(zebra_lsp_t *lsp,
-                                      enum lsp_types_t lsp_type,
-                                      enum nexthop_types_t gtype,
-                                      const union g_addr *gate,
-                                      ifindex_t ifindex, uint8_t num_labels,
-                                      const mpls_label_t *labels)
+                               uint8_t num_labels, const mpls_label_t *labels,
+                               bool is_backup)
 {
        zebra_nhlfe_t *nhlfe;
 
@@ -1362,10 +1335,12 @@ static zebra_nhlfe_t *nhlfe_backup_add(zebra_lsp_t *lsp,
        nhlfe = nhlfe_alloc(lsp, lsp_type, gtype, gate, ifindex, num_labels,
                            labels);
 
-       SET_FLAG(nhlfe->flags, NHLFE_FLAG_IS_BACKUP);
-
-       /* Enqueue to LSP, at tail of list. */
-       nhlfe_list_add_tail(&lsp->backup_nhlfe_list, nhlfe);
+       /* Enqueue to LSP: primaries at head of list, backups at tail */
+       if (is_backup) {
+               SET_FLAG(nhlfe->flags, NHLFE_FLAG_IS_BACKUP);
+               nhlfe_list_add_tail(&lsp->backup_nhlfe_list, nhlfe);
+       } else
+               nhlfe_list_add_head(&lsp->nhlfe_list, nhlfe);
 
        return nhlfe;
 }
@@ -2313,7 +2288,7 @@ zebra_nhlfe_t *zebra_mpls_lsp_add_nhlfe(zebra_lsp_t *lsp,
 {
        /* Just a public pass-through to the internal implementation */
        return nhlfe_add(lsp, lsp_type, gtype, gate, ifindex, num_labels,
-                        out_labels);
+                        out_labels, false /*backup*/);
 }
 
 /*
@@ -2330,8 +2305,8 @@ zebra_nhlfe_t *zebra_mpls_lsp_add_backup_nhlfe(zebra_lsp_t *lsp,
                                               const mpls_label_t *out_labels)
 {
        /* Just a public pass-through to the internal implementation */
-       return nhlfe_backup_add(lsp, lsp_type, gtype, gate, ifindex, num_labels,
-                               out_labels);
+       return nhlfe_add(lsp, lsp_type, gtype, gate, ifindex, num_labels,
+                        out_labels, true);
 }
 
 /*
@@ -2347,7 +2322,8 @@ zebra_nhlfe_t *zebra_mpls_lsp_add_nh(zebra_lsp_t *lsp,
                return NULL;
 
        nhlfe = nhlfe_add(lsp, lsp_type, nh->type, &nh->gate, nh->ifindex,
-                         nh->nh_label->num_labels, nh->nh_label->label);
+                         nh->nh_label->num_labels, nh->nh_label->label,
+                         false /*backup*/);
 
        return nhlfe;
 }
@@ -2365,9 +2341,9 @@ zebra_nhlfe_t *zebra_mpls_lsp_add_backup_nh(zebra_lsp_t *lsp,
        if (nh->nh_label == NULL || nh->nh_label->num_labels == 0)
                return NULL;
 
-       nhlfe = nhlfe_backup_add(lsp, lsp_type, nh->type, &nh->gate,
+       nhlfe = nhlfe_add(lsp, lsp_type, nh->type, &nh->gate,
                                 nh->ifindex, nh->nh_label->num_labels,
-                                nh->nh_label->label);
+                                nh->nh_label->label, true);
 
        return nhlfe;
 }
@@ -3088,7 +3064,8 @@ int mpls_zapi_labels_process(bool add_p, struct zebra_vrf *zvrf,
                else
                        ret = mpls_lsp_uninstall(zvrf, zl->type,
                                                 zl->local_label, znh->type,
-                                                &znh->gate, znh->ifindex);
+                                                &znh->gate, znh->ifindex,
+                                                false);
                if (ret < 0) {
                        if (IS_ZEBRA_DEBUG_RECV || IS_ZEBRA_DEBUG_MPLS) {
                                zapi_nexthop2str(znh, buf, sizeof(buf));
@@ -3130,10 +3107,10 @@ int mpls_zapi_labels_process(bool add_p, struct zebra_vrf *zvrf,
                if (add_p)
                        ret = lsp_backup_znh_install(lsp, zl->type, znh);
                else
-                       ret = lsp_backup_uninstall(zvrf, zl->type,
-                                                  zl->local_label,
-                                                  znh->type, &znh->gate,
-                                                  znh->ifindex);
+                       ret = mpls_lsp_uninstall(zvrf, zl->type,
+                                                zl->local_label,
+                                                znh->type, &znh->gate,
+                                                znh->ifindex, true);
 
                if (ret < 0) {
                        if (IS_ZEBRA_DEBUG_RECV ||
@@ -3198,92 +3175,22 @@ static zebra_nhlfe_t *
 lsp_add_nhlfe(zebra_lsp_t *lsp, enum lsp_types_t type,
              uint8_t num_out_labels, const mpls_label_t *out_labels,
              enum nexthop_types_t gtype, const union g_addr *gate,
-             ifindex_t ifindex)
+             ifindex_t ifindex, bool is_backup)
 {
        zebra_nhlfe_t *nhlfe;
        char buf[MPLS_LABEL_STRLEN];
+       const char *backup_str;
 
-       nhlfe = nhlfe_find(&lsp->nhlfe_list, type, gtype, gate, ifindex);
-       if (nhlfe) {
-               struct nexthop *nh = nhlfe->nexthop;
-
-               assert(nh);
-               assert(nh->nh_label);
-
-               /* Clear deleted flag (in case it was set) */
-               UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
-               if (nh->nh_label->num_labels == num_out_labels
-                   && !memcmp(nh->nh_label->label, out_labels,
-                              sizeof(mpls_label_t) * num_out_labels))
-                       /* No change */
-                       return nhlfe;
-
-               if (IS_ZEBRA_DEBUG_MPLS) {
-                       char buf2[MPLS_LABEL_STRLEN];
-                       char buf3[MPLS_LABEL_STRLEN];
-
-                       nhlfe2str(nhlfe, buf, sizeof(buf));
-                       mpls_label2str(num_out_labels, out_labels, buf2,
-                                      sizeof(buf2), 0);
-                       mpls_label2str(nh->nh_label->num_labels,
-                                      nh->nh_label->label, buf3, sizeof(buf3),
-                                      0);
-
-                       zlog_debug("LSP in-label %u type %d nexthop %s out-label(s) changed to %s (old %s)",
-                                  lsp->ile.in_label, type, buf, buf2, buf3);
-               }
-
-               /* Update out label(s), trigger processing. */
-               if (nh->nh_label->num_labels == num_out_labels)
-                       memcpy(nh->nh_label->label, out_labels,
-                              sizeof(mpls_label_t) * num_out_labels);
-               else {
-                       nexthop_del_labels(nh);
-                       nexthop_add_labels(nh, type, num_out_labels,
-                                          out_labels);
-               }
+       if (is_backup) {
+               nhlfe = nhlfe_find(&lsp->backup_nhlfe_list, type, gtype,
+                                  gate, ifindex);
+               backup_str = "backup ";
        } else {
-               /* Add LSP entry to this nexthop */
-               nhlfe = nhlfe_add(lsp, type, gtype, gate, ifindex,
-                                 num_out_labels, out_labels);
-               if (!nhlfe)
-                       return NULL;
-
-               if (IS_ZEBRA_DEBUG_MPLS) {
-                       char buf2[MPLS_LABEL_STRLEN];
-
-                       nhlfe2str(nhlfe, buf, sizeof(buf));
-                       mpls_label2str(num_out_labels, out_labels, buf2,
-                                      sizeof(buf2), 0);
-
-                       zlog_debug("Add LSP in-label %u type %d nexthop %s out-label(s) %s",
-                                  lsp->ile.in_label, type, buf, buf2);
-               }
-
-               lsp->addr_family = NHLFE_FAMILY(nhlfe);
+               nhlfe = nhlfe_find(&lsp->nhlfe_list, type, gtype, gate,
+                                  ifindex);
+               backup_str = "";
        }
 
-       /* Mark NHLFE, queue LSP for processing. */
-       SET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
-
-       return nhlfe;
-}
-
-/*
- * Install/update a NHLFE for an LSP in the forwarding table. This may be
- * a new LSP entry or a new NHLFE for an existing in-label or an update of
- * the out-label for an existing NHLFE (update case).
- */
-static zebra_nhlfe_t *
-lsp_add_backup_nhlfe(zebra_lsp_t *lsp, enum lsp_types_t type,
-                    uint8_t num_out_labels, const mpls_label_t *out_labels,
-                    enum nexthop_types_t gtype, const union g_addr *gate,
-                    ifindex_t ifindex)
-{
-       zebra_nhlfe_t *nhlfe;
-       char buf[MPLS_LABEL_STRLEN];
-
-       nhlfe = nhlfe_find(&lsp->backup_nhlfe_list, type, gtype, gate, ifindex);
        if (nhlfe) {
                struct nexthop *nh = nhlfe->nexthop;
 
@@ -3309,8 +3216,9 @@ lsp_add_backup_nhlfe(zebra_lsp_t *lsp, enum lsp_types_t type,
                                       nh->nh_label->label, buf3, sizeof(buf3),
                                       0);
 
-                       zlog_debug("LSP in-label %u type %d backup nexthop %s out-label(s) changed to %s (old %s)",
-                                  lsp->ile.in_label, type, buf, buf2, buf3);
+                       zlog_debug("LSP in-label %u type %d %snexthop %s out-label(s) changed to %s (old %s)",
+                                  lsp->ile.in_label, type, backup_str, buf,
+                                  buf2, buf3);
                }
 
                /* Update out label(s), trigger processing. */
@@ -3324,8 +3232,8 @@ lsp_add_backup_nhlfe(zebra_lsp_t *lsp, enum lsp_types_t type,
                }
        } else {
                /* Add LSP entry to this nexthop */
-               nhlfe = nhlfe_backup_add(lsp, type, gtype, gate, ifindex,
-                                        num_out_labels, out_labels);
+               nhlfe = nhlfe_add(lsp, type, gtype, gate, ifindex,
+                                 num_out_labels, out_labels, is_backup);
                if (!nhlfe)
                        return NULL;
 
@@ -3336,8 +3244,9 @@ lsp_add_backup_nhlfe(zebra_lsp_t *lsp, enum lsp_types_t type,
                        mpls_label2str(num_out_labels, out_labels, buf2,
                                       sizeof(buf2), 0);
 
-                       zlog_debug("Add LSP in-label %u type %d backup nexthop %s out-label(s) %s",
-                                  lsp->ile.in_label, type, buf, buf2);
+                       zlog_debug("Add LSP in-label %u type %d %snexthop %s out-label(s) %s",
+                                  lsp->ile.in_label, type, backup_str, buf,
+                                  buf2);
                }
 
                lsp->addr_family = NHLFE_FAMILY(nhlfe);
@@ -3375,7 +3284,7 @@ int mpls_lsp_install(struct zebra_vrf *zvrf, enum lsp_types_t type,
                return -1;
 
        nhlfe = lsp_add_nhlfe(lsp, type, num_out_labels, out_labels, gtype,
-                             gate, ifindex);
+                             gate, ifindex, false /*backup*/);
        if (nhlfe == NULL)
                return -1;
 
@@ -3395,7 +3304,8 @@ static int lsp_znh_install(zebra_lsp_t *lsp, enum lsp_types_t type,
        zebra_nhlfe_t *nhlfe;
 
        nhlfe = lsp_add_nhlfe(lsp, type, znh->label_num, znh->labels,
-                             znh->type, &znh->gate, znh->ifindex);
+                             znh->type, &znh->gate, znh->ifindex,
+                             false /*backup*/);
        if (nhlfe == NULL)
                return -1;
 
@@ -3420,9 +3330,9 @@ static int lsp_backup_znh_install(zebra_lsp_t *lsp, enum lsp_types_t type,
 {
        zebra_nhlfe_t *nhlfe;
 
-       nhlfe = lsp_add_backup_nhlfe(lsp, type, znh->label_num,
-                                    znh->labels, znh->type, &znh->gate,
-                                    znh->ifindex);
+       nhlfe = lsp_add_nhlfe(lsp, type, znh->label_num,
+                             znh->labels, znh->type, &znh->gate,
+                             znh->ifindex, true /*backup*/);
        if (nhlfe == NULL) {
                if (IS_ZEBRA_DEBUG_MPLS)
                        zlog_debug("%s: unable to add backup nhlfe, label: %u",
@@ -3443,13 +3353,14 @@ static int lsp_backup_znh_install(zebra_lsp_t *lsp, enum lsp_types_t type,
  */
 int mpls_lsp_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
                       mpls_label_t in_label, enum nexthop_types_t gtype,
-                      const union g_addr *gate, ifindex_t ifindex)
+                      const union g_addr *gate, ifindex_t ifindex,
+                      bool backup_p)
 {
        struct hash *lsp_table;
        zebra_ile_t tmp_ile;
        zebra_lsp_t *lsp;
        zebra_nhlfe_t *nhlfe;
-       char buf[BUFSIZ];
+       char buf[NEXTHOP_STRLEN];
        bool schedule_lsp = false;
 
        /* Lookup table. */
@@ -3462,7 +3373,13 @@ int mpls_lsp_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
        lsp = hash_lookup(lsp_table, &tmp_ile);
        if (!lsp)
                return 0;
-       nhlfe = nhlfe_find(&lsp->nhlfe_list, type, gtype, gate, ifindex);
+
+       if (backup_p)
+               nhlfe = nhlfe_find(&lsp->backup_nhlfe_list, type, gtype,
+                                  gate, ifindex);
+       else
+               nhlfe = nhlfe_find(&lsp->nhlfe_list, type, gtype, gate,
+                                  ifindex);
        if (!nhlfe)
                return 0;
 
@@ -3495,56 +3412,6 @@ int mpls_lsp_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
        return 0;
 }
 
-/*
- * Uninstall a particular NHLFE in the forwarding table. If this is
- * the only NHLFE, the entire LSP forwarding entry has to be deleted.
- */
-static int lsp_backup_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
-                               mpls_label_t in_label,
-                               enum nexthop_types_t gtype,
-                               const union g_addr *gate, ifindex_t ifindex)
-{
-       struct hash *lsp_table;
-       zebra_ile_t tmp_ile;
-       zebra_lsp_t *lsp;
-       zebra_nhlfe_t *nhlfe;
-       char buf[BUFSIZ];
-
-       /* Lookup table. */
-       lsp_table = zvrf->lsp_table;
-       if (!lsp_table)
-               return -1;
-
-       /* If entry is not present, exit. */
-       tmp_ile.in_label = in_label;
-       lsp = hash_lookup(lsp_table, &tmp_ile);
-       if (!lsp)
-               return 0;
-       nhlfe = nhlfe_find(&lsp->backup_nhlfe_list, type, gtype, gate, ifindex);
-       if (!nhlfe)
-               return 0;
-
-       if (IS_ZEBRA_DEBUG_MPLS) {
-               nhlfe2str(nhlfe, buf, BUFSIZ);
-               zlog_debug("Del backup LSP in-label %u type %d nexthop %s flags 0x%x",
-                          in_label, type, buf, nhlfe->flags);
-       }
-
-       /* Mark NHLFE for delete or directly delete, as appropriate. */
-       if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED)) {
-               UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
-               SET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
-               if (lsp_processq_add(lsp))
-                       return -1;
-       } else {
-               nhlfe_del(nhlfe);
-
-               /* Free LSP entry if no other NHLFEs and not scheduled. */
-               lsp_check_free(lsp_table, &lsp);
-       }
-       return 0;
-}
-
 int mpls_lsp_uninstall_all_vrf(struct zebra_vrf *zvrf, enum lsp_types_t type,
                               mpls_label_t in_label)
 {
@@ -3829,7 +3696,7 @@ int zebra_mpls_static_lsp_del(struct zebra_vrf *zvrf, mpls_label_t in_label,
 
                /* Uninstall LSP from the main table. */
                mpls_lsp_uninstall(zvrf, ZEBRA_LSP_STATIC, in_label, gtype,
-                                  gate, ifindex);
+                                  gate, ifindex, false);
 
                /* Delete static LSP NHLFE */
                snhlfe_del(snhlfe);
index 53429ee99c380171d64d959a71341e4f23764829..07a8efeb8b2890f84621247e6d31cb00d5b7f049 100644 (file)
@@ -331,7 +331,8 @@ int mpls_lsp_install(struct zebra_vrf *zvrf, enum lsp_types_t type,
  */
 int mpls_lsp_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
                       mpls_label_t in_label, enum nexthop_types_t gtype,
-                      const union g_addr *gate, ifindex_t ifindex);
+                      const union g_addr *gate, ifindex_t ifindex,
+                      bool backup_p);
 
 /*
  * Uninstall all NHLFEs for a particular LSP forwarding entry.
@@ -339,12 +340,6 @@ int mpls_lsp_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
 int mpls_lsp_uninstall_all_vrf(struct zebra_vrf *zvrf, enum lsp_types_t type,
                               mpls_label_t in_label);
 
-/*
- * Uninstall all Segment Routing NHLFEs for a particular LSP forwarding entry.
- * If no other NHLFEs exist, the entry would be deleted.
- */
-void mpls_sr_lsp_uninstall_all(struct hash_bucket *bucket, void *ctxt);
-
 #if defined(HAVE_CUMULUS)
 /*
  * Check that the label values used in LSP creation are consistent. The