]> git.proxmox.com Git - mirror_frr.git/blobdiff - bgpd/bgp_mplsvpn.c
*: ditch vty_outln(), part 1 of 2
[mirror_frr.git] / bgpd / bgp_mplsvpn.c
index 4a8bfed8bbacbfa0bb8b2967de5fb83cf59ba981..3efbeb8b49e55d4e1984b845e60c75882fb042ba 100644 (file)
@@ -1,22 +1,22 @@
 /* MPLS-VPN
  Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
-
-This file is part of GNU Zebra.
-
-GNU Zebra is free software; you can redistribute it and/or modify it
-under the terms of the GNU General Public License as published by the
-Free Software Foundation; either version 2, or (at your option) any
-later version.
-
-GNU Zebra is distributed in the hope that it will be useful, but
-WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with GNU Zebra; see the file COPYING.  If not, write to the Free
-Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA.  */
* Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * GNU Zebra is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; see the file COPYING; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
 
 #include <zebra.h>
 
@@ -27,12 +27,13 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 #include "stream.h"
 #include "queue.h"
 #include "filter.h"
-
 #include "lib/json.h"
+
 #include "bgpd/bgpd.h"
 #include "bgpd/bgp_table.h"
 #include "bgpd/bgp_route.h"
 #include "bgpd/bgp_attr.h"
+#include "bgpd/bgp_label.h"
 #include "bgpd/bgp_mplsvpn.h"
 #include "bgpd/bgp_packet.h"
 #include "bgpd/bgp_vty.h"
@@ -87,9 +88,10 @@ encode_rd_type (u_int16_t v, u_char *pnt)
 }
 
 u_int32_t
-decode_label (u_char *pnt)
+decode_label (mpls_label_t *label_pnt)
 {
   u_int32_t l;
+  u_char *pnt = (u_char *) label_pnt;
 
   l = ((u_int32_t) *pnt++ << 12);
   l |= (u_int32_t) *pnt++ << 4;
@@ -98,9 +100,10 @@ decode_label (u_char *pnt)
 }
 
 void
-encode_label(u_int32_t label,
-             u_char *pnt)
+encode_label(mpls_label_t label,
+             mpls_label_t *label_pnt)
 {
+    u_char *pnt = (u_char *) label_pnt;
     if (pnt == NULL)
         return;
     *pnt++ = (label>>12) & 0xff;
@@ -169,7 +172,7 @@ bgp_nlri_parse_vpn (struct peer *peer, struct attr *attr,
   struct rd_as rd_as;
   struct rd_ip rd_ip;
   struct prefix_rd prd;
-  u_char *tagpnt;
+  mpls_label_t label;
   afi_t afi;
   safi_t safi;
   int addpath_encoded;
@@ -249,14 +252,15 @@ bgp_nlri_parse_vpn (struct peer *peer, struct attr *attr,
           return -1;
         }
       
-      /* Copyr label to prefix. */
-      tagpnt = pnt;
+      /* Copy label to prefix. */
+      memcpy(&label, pnt, BGP_LABEL_BYTES);
+      bgp_set_valid_label(&label);
 
       /* Copy routing distinguisher to rd. */
-      memcpy (&prd.val, pnt + 3, 8);
+      memcpy (&prd.val, pnt + BGP_LABEL_BYTES, 8);
 
       /* Decode RD type. */
-      type = decode_rd_type (pnt + 3);
+      type = decode_rd_type (pnt + BGP_LABEL_BYTES);
 
       switch (type)
         {
@@ -289,12 +293,12 @@ bgp_nlri_parse_vpn (struct peer *peer, struct attr *attr,
       if (attr)
         {
           bgp_update (peer, &p, addpath_id, attr, packet->afi, SAFI_MPLS_VPN,
-                      ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, &prd, tagpnt, 0, NULL);
+                      ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, &prd, &label, 0, NULL);
         }
       else
         {
           bgp_withdraw (peer, &p, addpath_id, attr, packet->afi, SAFI_MPLS_VPN,
-                        ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, &prd, tagpnt, NULL);
+                        ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, &prd, &label, NULL);
         }
     }
   /* Packet length consistency check. */
@@ -381,31 +385,6 @@ out:
   return lret;
 }
 
-int
-str2tag (const char *str, u_char *tag)
-{
-  unsigned long l;
-  char *endptr;
-  u_int32_t t;
-
-  if (*str == '-')
-    return 0;
-  
-  errno = 0;
-  l = strtoul (str, &endptr, 10);
-
-  if (*endptr != '\0' || errno || l > UINT32_MAX)
-    return 0;
-
-  t = (u_int32_t) l;
-  
-  tag[0] = (u_char)(t >> 12);
-  tag[1] = (u_char)(t >> 4);
-  tag[2] = (u_char)(t << 4);
-
-  return 1;
-}
-
 char *
 prefix_rd2str (struct prefix_rd *prd, char *buf, size_t size)
 {
@@ -571,8 +550,6 @@ bgp_show_mpls_vpn (struct vty *vty, afi_t afi, struct prefix_rd *prd,
   struct bgp_info *ri;
   int rd_header;
   int header = 1;
-  char v4_header[] = "   Network          Next Hop            Metric LocPrf Weight Path%s";
-  char v4_header_tag[] = "   Network          Next Hop      In tag/Out tag%s";
   unsigned long output_count = 0;
   unsigned long total_count  = 0;
   json_object *json = NULL;
@@ -586,7 +563,7 @@ bgp_show_mpls_vpn (struct vty *vty, afi_t afi, struct prefix_rd *prd,
   if (bgp == NULL)
     {
       if (!use_json)
-        vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE);
+        vty_out (vty, "No BGP process is configured\n");
       return CMD_WARNING;
     }
 
@@ -612,7 +589,7 @@ bgp_show_mpls_vpn (struct vty *vty, afi_t afi, struct prefix_rd *prd,
 
   if ((afi != AFI_IP) && (afi != AFI_IP6))
     {
-      vty_out (vty, "Afi %d not supported%s", afi, VTY_NEWLINE);
+      vty_out (vty, "Afi %d not supported\n", afi);
       return CMD_WARNING;
     }
 
@@ -657,16 +634,16 @@ bgp_show_mpls_vpn (struct vty *vty, afi_t afi, struct prefix_rd *prd,
                       else
                         {
                          if (tags)
-                           vty_out (vty, v4_header_tag, VTY_NEWLINE);
+                           vty_outln (vty, V4_HEADER_TAG);
                          else
                            {
-                             vty_out (vty, "BGP table version is 0, local router ID is %s%s",
-                                      inet_ntoa (bgp->router_id), VTY_NEWLINE);
-                             vty_out (vty, "Status codes: s suppressed, d damped, h history, * valid, > best, i - internal%s",
-                                      VTY_NEWLINE);
-                             vty_out (vty, "Origin codes: i - IGP, e - EGP, ? - incomplete%s%s",
-                                      VTY_NEWLINE, VTY_NEWLINE);
-                             vty_out (vty, v4_header, VTY_NEWLINE);
+                             vty_out (vty, "BGP table version is 0, local router ID is %s\n",
+                                      inet_ntoa(bgp->router_id));
+                             vty_out (vty,
+                                         "Status codes: s suppressed, d damped, h history, * valid, > best, i - internal\n");
+                             vty_out (vty, "Origin codes: i - IGP, e - EGP, ? - incomplete%s\n",
+                                      VTYNL);
+                             vty_outln (vty, V4_HEADER);
                            }
                         }
                      header = 0;
@@ -726,7 +703,7 @@ bgp_show_mpls_vpn (struct vty *vty, afi_t afi, struct prefix_rd *prd,
                                      rd_vnc_eth.macaddr.octet[4],
                                      rd_vnc_eth.macaddr.octet[5]);
 #endif
-                         vty_out (vty, "%s", VTY_NEWLINE);
+                         vty_out (vty, VTYNL);
                         }
                      rd_header = 0;
                    }
@@ -763,16 +740,17 @@ bgp_show_mpls_vpn (struct vty *vty, afi_t afi, struct prefix_rd *prd,
   if (use_json)
     {
       json_object_object_add(json, "routes", json_nroute);
-      vty_out (vty, "%s%s", json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY), VTY_NEWLINE);
+      vty_out (vty, "%s\n",
+                 json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY));
       json_object_free(json);
     }
   else
     {
       if (output_count == 0)
-       vty_out (vty, "No prefixes displayed, %ld exist%s", total_count, VTY_NEWLINE);
+       vty_out (vty, "No prefixes displayed, %ld exist\n", total_count);
       else
-       vty_out (vty, "%sDisplayed %ld routes and %ld total paths%s",
-                VTY_NEWLINE, output_count, total_count, VTY_NEWLINE);
+       vty_out (vty, "%sDisplayed %ld routes and %ld total paths\n",
+                VTYNL, output_count, total_count);
     }
 
   return CMD_SUCCESS;
@@ -803,7 +781,7 @@ DEFUN (show_bgp_ip_vpn_all_rd,
           ret = str2prefix_rd (argv[idx_rd]->arg, &prd);
           if (! ret)
             {
-              vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
+              vty_out (vty, "%% Malformed Route Distinguisher\n");
               return CMD_WARNING;
             }
           return bgp_show_mpls_vpn (vty, afi, &prd, bgp_show_type_normal, NULL, 0, use_json (argc, argv));
@@ -838,7 +816,7 @@ DEFUN (show_ip_bgp_vpn_rd,
       ret = str2prefix_rd (argv[idx_ext_community]->arg, &prd);
       if (! ret)
         {
-          vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
+          vty_out (vty, "%% Malformed Route Distinguisher\n");
           return CMD_WARNING;
         }
       return bgp_show_mpls_vpn (vty, afi, &prd, bgp_show_type_normal, NULL, 0, 0);
@@ -903,7 +881,7 @@ DEFUN (show_ip_bgp_vpn_rd_tags,
       ret = str2prefix_rd (argv[idx_ext_community]->arg, &prd);
       if (! ret)
         {
-          vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
+          vty_out (vty, "%% Malformed Route Distinguisher\n");
           return CMD_WARNING;
         }
       return bgp_show_mpls_vpn (vty, afi, &prd, bgp_show_type_normal, NULL, 1, 0);
@@ -942,11 +920,11 @@ DEFUN (show_ip_bgp_vpn_all_neighbor_routes,
               json_object *json_no = NULL;
               json_no = json_object_new_object();
               json_object_string_add(json_no, "warning", "Malformed address");
-              vty_out (vty, "%s%s", json_object_to_json_string(json_no), VTY_NEWLINE);
+              vty_out (vty, "%s\n", json_object_to_json_string(json_no));
               json_object_free(json_no);
             }
           else
-            vty_out (vty, "Malformed address: %s%s", argv[idx_ipv4]->arg, VTY_NEWLINE);
+            vty_out (vty, "Malformed address: %s\n", argv[idx_ipv4]->arg);
           return CMD_WARNING;
         }
 
@@ -958,11 +936,11 @@ DEFUN (show_ip_bgp_vpn_all_neighbor_routes,
               json_object *json_no = NULL;
               json_no = json_object_new_object();
               json_object_string_add(json_no, "warning", "No such neighbor or address family");
-              vty_out (vty, "%s%s", json_object_to_json_string(json_no), VTY_NEWLINE);
+              vty_out (vty, "%s\n", json_object_to_json_string(json_no));
               json_object_free(json_no);
             }
           else
-            vty_out (vty, "%% No such neighbor or address family%s", VTY_NEWLINE);
+            vty_out (vty, "%% No such neighbor or address family\n");
           return CMD_WARNING;
         }
 
@@ -1005,11 +983,11 @@ DEFUN (show_ip_bgp_vpn_rd_neighbor_routes,
               json_object *json_no = NULL;
               json_no = json_object_new_object();
               json_object_string_add(json_no, "warning", "Malformed Route Distinguisher");
-              vty_out (vty, "%s%s", json_object_to_json_string(json_no), VTY_NEWLINE);
+              vty_out (vty, "%s\n", json_object_to_json_string(json_no));
               json_object_free(json_no);
             }
           else
-            vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
+            vty_out (vty, "%% Malformed Route Distinguisher\n");
           return CMD_WARNING;
         }
 
@@ -1021,11 +999,12 @@ DEFUN (show_ip_bgp_vpn_rd_neighbor_routes,
               json_object *json_no = NULL;
               json_no = json_object_new_object();
               json_object_string_add(json_no, "warning", "Malformed address");
-              vty_out (vty, "%s%s", json_object_to_json_string(json_no), VTY_NEWLINE);
+              vty_out (vty, "%s\n", json_object_to_json_string(json_no));
               json_object_free(json_no);
             }
           else
-            vty_out (vty, "Malformed address: %s%s", argv[idx_ext_community]->arg, VTY_NEWLINE);
+            vty_out (vty, "Malformed address: %s\n",
+                       argv[idx_ext_community]->arg);
           return CMD_WARNING;
         }
 
@@ -1037,11 +1016,11 @@ DEFUN (show_ip_bgp_vpn_rd_neighbor_routes,
               json_object *json_no = NULL;
               json_no = json_object_new_object();
               json_object_string_add(json_no, "warning", "No such neighbor or address family");
-              vty_out (vty, "%s%s", json_object_to_json_string(json_no), VTY_NEWLINE);
+              vty_out (vty, "%s\n", json_object_to_json_string(json_no));
               json_object_free(json_no);
             }
           else
-            vty_out (vty, "%% No such neighbor or address family%s", VTY_NEWLINE);
+            vty_out (vty, "%% No such neighbor or address family\n");
           return CMD_WARNING;
         }
 
@@ -1081,11 +1060,11 @@ DEFUN (show_ip_bgp_vpn_all_neighbor_advertised_routes,
               json_object *json_no = NULL;
               json_no = json_object_new_object();
               json_object_string_add(json_no, "warning", "Malformed address");
-              vty_out (vty, "%s%s", json_object_to_json_string(json_no), VTY_NEWLINE);
+              vty_out (vty, "%s\n", json_object_to_json_string(json_no));
               json_object_free(json_no);
             }
           else
-            vty_out (vty, "Malformed address: %s%s", argv[idx_ipv4]->arg, VTY_NEWLINE);
+            vty_out (vty, "Malformed address: %s\n", argv[idx_ipv4]->arg);
           return CMD_WARNING;
         }
       peer = peer_lookup (NULL, &su);
@@ -1096,11 +1075,11 @@ DEFUN (show_ip_bgp_vpn_all_neighbor_advertised_routes,
               json_object *json_no = NULL;
               json_no = json_object_new_object();
               json_object_string_add(json_no, "warning", "No such neighbor or address family");
-              vty_out (vty, "%s%s", json_object_to_json_string(json_no), VTY_NEWLINE);
+              vty_out (vty, "%s\n", json_object_to_json_string(json_no));
               json_object_free(json_no);
             }
           else
-            vty_out (vty, "%% No such neighbor or address family%s", VTY_NEWLINE);
+            vty_out (vty, "%% No such neighbor or address family\n");
           return CMD_WARNING;
         }
       return show_adj_route_vpn (vty, peer, NULL, AFI_IP, SAFI_MPLS_VPN, uj);
@@ -1142,11 +1121,12 @@ DEFUN (show_ip_bgp_vpn_rd_neighbor_advertised_routes,
               json_object *json_no = NULL;
               json_no = json_object_new_object();
               json_object_string_add(json_no, "warning", "Malformed address");
-              vty_out (vty, "%s%s", json_object_to_json_string(json_no), VTY_NEWLINE);
+              vty_out (vty, "%s\n", json_object_to_json_string(json_no));
               json_object_free(json_no);
             }
           else
-            vty_out (vty, "Malformed address: %s%s", argv[idx_ext_community]->arg, VTY_NEWLINE);
+            vty_out (vty, "Malformed address: %s\n",
+                       argv[idx_ext_community]->arg);
           return CMD_WARNING;
         }
       peer = peer_lookup (NULL, &su);
@@ -1157,11 +1137,11 @@ DEFUN (show_ip_bgp_vpn_rd_neighbor_advertised_routes,
               json_object *json_no = NULL;
               json_no = json_object_new_object();
               json_object_string_add(json_no, "warning", "No such neighbor or address family");
-              vty_out (vty, "%s%s", json_object_to_json_string(json_no), VTY_NEWLINE);
+              vty_out (vty, "%s\n", json_object_to_json_string(json_no));
               json_object_free(json_no);
             }
           else
-            vty_out (vty, "%% No such neighbor or address family%s", VTY_NEWLINE);
+            vty_out (vty, "%% No such neighbor or address family\n");
           return CMD_WARNING;
         }
 
@@ -1173,11 +1153,11 @@ DEFUN (show_ip_bgp_vpn_rd_neighbor_advertised_routes,
               json_object *json_no = NULL;
               json_no = json_object_new_object();
               json_object_string_add(json_no, "warning", "Malformed Route Distinguisher");
-              vty_out (vty, "%s%s", json_object_to_json_string(json_no), VTY_NEWLINE);
+              vty_out (vty, "%s\n", json_object_to_json_string(json_no));
               json_object_free(json_no);
             }
           else
-            vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
+            vty_out (vty, "%% Malformed Route Distinguisher\n");
           return CMD_WARNING;
         }