netns.h revision 2ab698c9
1/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef NETNS_H_
17#define NETNS_H_
18
19#include <vlib/vlib.h>
20
21#include <sys/socket.h>
22#include <linux/rtnetlink.h>
23#include <linux/netlink.h>
24#include <net/if.h>
25
26#include <librtnl/rtnl.h>
27
28/*include it for 'struct mpls_label'*/
29#include <linux/mpls.h>
30/*so far depth is fixed, looking into ways to be dynamic*/
31#define MPLS_STACK_DEPTH 7
32
33typedef struct {
34  struct ifinfomsg ifi;
35  u8 hwaddr[IFHWADDRLEN];
36  u8 broadcast[IFHWADDRLEN];
37  u8 ifname[IFNAMSIZ];
38  u32 mtu;
39  u32 master;
40  u8 qdisc[IFNAMSIZ];
41  struct rtnl_link_stats stats; //This struct is big and only comes as a response to a request
42  f64 last_updated;
43} ns_link_t;
44
45typedef struct {
46  struct rtmsg rtm;
47  u8 dst[16];
48  u8 src[16];
49  u8 via[16];
50  u8 prefsrc[16];
51  u32 iif;
52  u32 oif;
53  u32 table;
54  u8 gateway[16];
55  u32 priority;
56  struct rta_cacheinfo cacheinfo;
57  struct mpls_label encap[MPLS_STACK_DEPTH];
58  f64 last_updated;
59} ns_route_t;
60
61typedef struct {
62  struct ifaddrmsg ifaddr;
63  u8 addr[16];
64  u8 local[16];
65  u8 label[IFNAMSIZ];
66  u8 broadcast[16];
67  u8 anycast[16];
68  struct ifa_cacheinfo cacheinfo;
69  f64 last_updated;
70} ns_addr_t;
71
72typedef struct {
73  struct ndmsg nd;
74  u8 dst[16];
75  u8 lladdr[IFHWADDRLEN];
76  u32 probes;
77  struct nda_cacheinfo cacheinfo;
78  f64 last_updated;
79} ns_neigh_t;
80
81typedef struct {
82  char name[RTNL_NETNS_NAMELEN + 1];
83  ns_link_t  *links;
84  ns_route_t *routes;
85  ns_addr_t  *addresses;
86  ns_neigh_t *neighbors;
87} netns_t;
88
89
90typedef enum {
91  NETNS_TYPE_LINK,
92  NETNS_TYPE_ROUTE,
93  NETNS_TYPE_ADDR,
94  NETNS_TYPE_NEIGH,
95} netns_type_t;
96
97//Flags used in notification functions call
98#define NETNS_F_ADD    0x01
99#define NETNS_F_DEL    0x02
100
101typedef struct {
102  void (*notify)(void *obj, netns_type_t type, u32 flags, uword opaque);
103  uword opaque;
104} netns_sub_t;
105
106/*
107 * Subscribe for events related to the given namespace.
108 * When another subscriber already uses the namespace,
109 * this call will not trigger updates for already
110 * existing routes (This is to protect against
111 * synch. Vs asynch. issues).
112 */
113u32 netns_open(char *name, netns_sub_t *sub);
114
115/*
116 * Retrieves the namespace structure associated with a
117 * given namespace handler.
118 */
119netns_t *netns_getns(u32 handle);
120
121/*
122 * Terminates a subscriber session.
123 */
124void netns_close(u32 handle);
125
126/*
127 * Calls the callback associated with the handle
128 * for all existing objects with the flags
129 * set to (del?NETNS_F_DEL:NETNS_F_ADD).
130 */
131void netns_callme(u32 handle, char del);
132
133/*
134 * netns struct format functions.
135 * Taking the struct as single argument.
136 */
137u8 *format_ns_neigh(u8 *s, va_list *args);
138u8 *format_ns_addr(u8 *s, va_list *args);
139u8 *format_ns_route(u8 *s, va_list *args);
140u8 *format_ns_link(u8 *s, va_list *args);
141
142u8 *format_ns_object(u8 *s, va_list *args);
143u8 *format_ns_flags(u8 *s, va_list *args);
144
145#endif
146