summaryrefslogtreecommitdiffstats
path: root/zebra/zebra_mpls.h
blob: f9d58a46e80eab4a720b5d8d3499d05cc2c72c35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
 * Zebra MPLS Data structures and definitions
 * Copyright (C) 2015 Cumulus Networks, Inc.
 *
 * 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.
 */

#ifndef _ZEBRA_MPLS_H
#define _ZEBRA_MPLS_H

#include "prefix.h"
#include "table.h"
#include "queue.h"
#include "hash.h"
#include "jhash.h"
#include "nexthop.h"
#include "vty.h"
#include "memory.h"
#include "mpls.h"
#include "zebra/zserv.h"
#include "zebra/zebra_vrf.h"


/* Definitions and macros. */

#define MPLS_MAX_LABELS 2  /* Maximum # labels that can be pushed. */

#define NHLFE_FAMILY(nhlfe)                        \
  (((nhlfe)->nexthop->type == NEXTHOP_TYPE_IPV6 || \
    (nhlfe)->nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) ? AF_INET6 : AF_INET)


/* Typedefs */

typedef struct zebra_ile_t_ zebra_ile_t;
typedef struct zebra_snhlfe_t_ zebra_snhlfe_t;
typedef struct zebra_slsp_t_ zebra_slsp_t;
typedef struct zebra_nhlfe_t_ zebra_nhlfe_t;
typedef struct zebra_lsp_t_ zebra_lsp_t;
typedef struct zebra_fec_t_ zebra_fec_t;

/*
 * (Outgoing) nexthop label forwarding entry configuration
 */
struct zebra_snhlfe_t_
{
  /* Nexthop information */
  enum nexthop_types_t gtype;
  union g_addr gate;
  char *ifname;
  ifindex_t ifindex;

  /* Out label. */
  mpls_label_t out_label;

  /* Backpointer to base entry. */
  zebra_slsp_t *slsp;

  /* Pointers to more outgoing information for same in-label */
  zebra_snhlfe_t *next;
  zebra_snhlfe_t *prev;
};

/*
 * (Outgoing) nexthop label forwarding entry
 */
struct zebra_nhlfe_t_
{
  /* Type of entry - static etc. */
  enum lsp_types_t type;

  /* Nexthop information (with outgoing label) */
  struct nexthop *nexthop;

  /* Backpointer to base entry. */
  zebra_lsp_t *lsp;

  /* Runtime info - flags, pointers etc. */
  u_int32_t flags;
#define NHLFE_FLAG_CHANGED     (1 << 0)
#define NHLFE_FLAG_SELECTED    (1 << 1)
#define NHLFE_FLAG_MULTIPATH   (1 << 2)
#define NHLFE_FLAG_DELETED     (1 << 3)
#define NHLFE_FLAG_INSTALLED   (1 << 4)

  zebra_nhlfe_t *next;
  zebra_nhlfe_t *prev;
  u_char distance;
};

/*
 * Incoming label entry
 */
struct zebra_ile_t_
{
  mpls_label_t in_label;
};

/*
 * Label swap entry static configuration.
 */
struct zebra_slsp_t_
{
  /* Incoming label */
  zebra_ile_t ile;

  /* List of outgoing nexthop static configuration */
  zebra_snhlfe_t *snhlfe_list;

};

/*
 * Label swap entry (ile -> list of nhlfes)
 */
struct zebra_lsp_t_
{
  /* Incoming label */
  zebra_ile_t ile;

  /* List of NHLFE, pointer to best and num equal-cost. */
  zebra_nhlfe_t *nhlfe_list;
  zebra_nhlfe_t *best_nhlfe;
  u_int32_t num_ecmp;

  /* Flags */
  u_int32_t flags;
#define LSP_FLAG_SCHEDULED        (1 << 0)
#define LSP_FLAG_INSTALLED        (1 << 1)
#define LSP_FLAG_CHANGED          (1 << 2)

  /* Address-family of NHLFE - saved here for delete. All NHLFEs */
  /* have to be of the same AF */
  u_char addr_family;
};

/*
 * FEC to label binding.
 */
struct zebra_fec_t_
{
  /* FEC (prefix) */
  struct route_node *rn;

  /* In-label - either statically bound or derived from label block. */
  mpls_label_t label;

  /* Flags. */
  u_int32_t flags;
#define FEC_FLAG_CONFIGURED       (1 << 0)

  /* Clients interested in this FEC. */
  struct list *client_list;
};

/* Function declarations. */

/*
 * String to label conversion, labels separated by '/'.
 */
int
mpls_str2label (const char *label_str, u_int8_t *num_labels,
                mpls_label_t *labels);

/*
 * Label to string conversion, labels in string separated by '/'.
 */
char *
mpls_label2str (u_int8_t num_labels, mpls_label_t *labels,
                char *buf, int len);

/*
 * Install dynamic LSP entry.
 */
int
zebra_mpls_lsp_install (struct zebra_vrf *zvrf, struct route_node *rn, struct rib *rib);

/*
 * Uninstall dynamic LSP entry, if any. 
 */
int
zebra_mpls_lsp_uninstall (struct zebra_vrf *zvrf, struct route_node *rn, struct rib *rib);

/*
 * Registration from a client for the label binding for a FEC. If a binding
 * already exists, it is informed to the client.
 */
int
zebra_mpls_fec_register (struct zebra_vrf *zvrf, struct prefix *p,
                         struct zserv *client);

/*
 * Deregistration from a client for the label binding for a FEC. The FEC
 * itself is deleted if no other registered clients exist and there is no
 * label bound to the FEC.
 */
int
zebra_mpls_fec_unregister (struct zebra_vrf *zvrf, struct prefix *p,
                           struct zserv *client);

/*
 * Cleanup any FECs registered by this client.
 */
int
zebra_mpls_cleanup_fecs_for_client (struct zebra_vrf *zvrf, struct zserv *client);

/*
 * Return FEC (if any) to which this label is bound.
 * Note: Only works for per-prefix binding and when the label is not
 * implicit-null.
 * TODO: Currently walks entire table, can optimize later with another
 * hash..
 */
zebra_fec_t *
zebra_mpls_fec_for_label (struct zebra_vrf *zvrf, mpls_label_t label);

/*
 * Inform if specified label is currently bound to a FEC or not.
 */
int
zebra_mpls_label_already_bound (struct zebra_vrf *zvrf, mpls_label_t label);

/*
 * Add static FEC to label binding. If there are clients registered for this
 * FEC, notify them. If there are labeled routes for this FEC, install the
 * label forwarding entry.
 */
int
zebra_mpls_static_fec_add (struct zebra_vrf *zvrf, struct prefix *p,
                           mpls_label_t in_label);

/*
 * Remove static FEC to label binding. If there are no clients registered
 * for this FEC, delete the FEC; else notify clients.
 */
int
zebra_mpls_static_fec_del (struct zebra_vrf *zvrf, struct prefix *p);

/*
 * Display MPLS FEC to label binding configuration (VTY command handler).
 */
int
zebra_mpls_write_fec_config (struct vty *vty, struct zebra_vrf *zvrf);

/*
 * Display MPLS FEC to label binding (VTY command handler).
 */
void
zebra_mpls_print_fec_table (struct vty *vty, struct zebra_vrf *zvrf);

/*
 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
 */
void
zebra_mpls_print_fec (struct vty *vty, struct zebra_vrf *zvrf, struct prefix *p);

/*
 * Install/uninstall a FEC-To-NHLFE (FTN) binding.
 */
int
mpls_ftn_update (int add, struct zebra_vrf *zvrf, enum lsp_types_t type,
		 struct prefix *prefix, enum nexthop_types_t gtype,
		 union g_addr *gate, ifindex_t ifindex, u_int8_t distance,
		 mpls_label_t out_label);

/*
 * 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).
 */
int
mpls_lsp_install (struct zebra_vrf *zvrf, enum lsp_types_t type,
		  mpls_label_t in_label, mpls_label_t out_label,
		  enum nexthop_types_t gtype, union g_addr *gate,
		  ifindex_t ifindex);

/*
 * Uninstall a particular NHLFE in the forwarding table. If this is
 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
 */
int
mpls_lsp_uninstall (struct zebra_vrf *zvrf, enum lsp_types_t type,
		    mpls_label_t in_label, enum nexthop_types_t gtype,
		    union g_addr *gate, ifindex_t ifindex);

/*
 * Uninstall all LDP NHLFEs for a particular LSP forwarding entry.
 * If no other NHLFEs exist, the entry would be deleted.
 */
void
mpls_ldp_lsp_uninstall_all (struct hash_backet *backet, void *ctxt);

/*
 * Uninstall all LDP FEC-To-NHLFE (FTN) bindings of the given address-family.
 */
void
mpls_ldp_ftn_uninstall_all (struct zebra_vrf *zvrf, int afi);

#if defined(HAVE_CUMULUS)
/*
 * Check that the label values used in LSP creation are consistent. The
 * main criteria is that if there is ECMP, the label operation must still
 * be consistent - i.e., all paths either do a swap or do PHP. This is due
 * to current HW restrictions.
 */
int
zebra_mpls_lsp_label_consistent (struct zebra_vrf *zvrf, mpls_label_t in_label,
                     mpls_label_t out_label, enum nexthop_types_t gtype,
                     union g_addr *gate, ifindex_t ifindex);
#endif /* HAVE_CUMULUS */

/*
 * Add static LSP entry. This may be the first entry for this incoming label
 * or an additional nexthop; an existing entry may also have outgoing label
 * changed.
 * Note: The label operation (swap or PHP) is common for the LSP entry (all
 * NHLFEs).
 */
int
zebra_mpls_static_lsp_add (struct zebra_vrf *zvrf, mpls_label_t in_label,
                     mpls_label_t out_label, enum nexthop_types_t gtype,
                     union g_addr *gate, ifindex_t ifindex);

/*
 * Delete static LSP entry. This may be the delete of one particular
 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
 * all NHLFEs).
 * NOTE: Delete of the only NHLFE will also end up deleting the entire
 * LSP configuration.
 */
int
zebra_mpls_static_lsp_del (struct zebra_vrf *zvrf, mpls_label_t in_label,
                           enum nexthop_types_t gtype, union g_addr *gate,
                           ifindex_t ifindex);

/*
 * Schedule all MPLS label forwarding entries for processing.
 * Called upon changes that may affect one or more of them such as
 * interface or nexthop state changes.
 */
void
zebra_mpls_lsp_schedule (struct zebra_vrf *zvrf);

/*
 * Display MPLS label forwarding table for a specific LSP
 * (VTY command handler).
 */
void
zebra_mpls_print_lsp (struct vty *vty, struct zebra_vrf *zvrf, mpls_label_t label,
                      u_char use_json);

/*
 * Display MPLS label forwarding table (VTY command handler).
 */
void
zebra_mpls_print_lsp_table (struct vty *vty, struct zebra_vrf *zvrf,
                            u_char use_json);

/*
 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
 */
int
zebra_mpls_write_lsp_config (struct vty *vty, struct zebra_vrf *zvrf);

/*
 * Called upon process exiting, need to delete LSP forwarding
 * entries from the kernel.
 * NOTE: Currently supported only for default VRF.
 */
void
zebra_mpls_close_tables (struct zebra_vrf *zvrf);

/*
 * Allocate MPLS tables for this VRF.
 * NOTE: Currently supported only for default VRF.
 */
void
zebra_mpls_init_tables (struct zebra_vrf *zvrf);

/*
 * Global MPLS initialization.
 */
void
zebra_mpls_init (void);

/*
 * MPLS VTY.
 */
void
zebra_mpls_vty_init (void);

/* Inline functions. */

/*
 * Distance (priority) definition for LSP NHLFE.
 */
static inline u_char
lsp_distance (enum lsp_types_t type)
{
  if (type == ZEBRA_LSP_STATIC)
    return (route_distance (ZEBRA_ROUTE_STATIC));

  return 150;
}

/*
 * Map RIB type to LSP type. Used when labeled-routes from BGP
 * are converted into LSPs.
 */
static inline enum lsp_types_t
lsp_type_from_rib_type (int rib_type)
{
  switch (rib_type)
    {
      case ZEBRA_ROUTE_STATIC:
        return ZEBRA_LSP_STATIC;
      case ZEBRA_ROUTE_BGP:
        return ZEBRA_LSP_BGP;
      default:
        return ZEBRA_LSP_NONE;
    }
}

/* NHLFE type as printable string. */
static inline const char *
nhlfe_type2str(enum lsp_types_t lsp_type)
{
  switch (lsp_type)
    {
      case ZEBRA_LSP_STATIC:
        return "Static";
      case ZEBRA_LSP_LDP:
        return "LDP";
      case ZEBRA_LSP_BGP:
        return "BGP";
      default:
        return "Unknown";
    }
}

static inline void
mpls_mark_lsps_for_processing(struct zebra_vrf *zvrf)
{
  if (!zvrf)
    return;

  zvrf->mpls_flags |= MPLS_FLAG_SCHEDULE_LSPS;
}

static inline void
mpls_unmark_lsps_for_processing(struct zebra_vrf *zvrf)
{
  if (!zvrf)
    return;

  zvrf->mpls_flags &= ~MPLS_FLAG_SCHEDULE_LSPS;
}

static inline int
mpls_should_lsps_be_processed(struct zebra_vrf *zvrf)
{
  if (!zvrf)
    return 0;

  return ((zvrf->mpls_flags & MPLS_FLAG_SCHEDULE_LSPS) ? 1 : 0);
}

/* Global variables. */
extern int mpls_enabled;

#endif /*_ZEBRA_MPLS_H */