netns.h revision 4c047107
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
28typedef struct {
29  struct ifinfomsg ifi;
30  u8 hwaddr[IFHWADDRLEN];
31  u8 broadcast[IFHWADDRLEN];
32  u8 ifname[IFNAMSIZ];
33  u32 mtu;
34  u32 master;
35  u8 qdisc[IFNAMSIZ];
36  struct rtnl_link_stats stats; //This struct is big and only comes as a response to a request
37  f64 last_updated;
38} ns_link_t;
39
40typedef struct {
41  struct rtmsg rtm;
42  u8 dst[16];
43  u8 src[16];
44  u8 prefsrc[16];
45  u32 iif;
46  u32 oif;
47  u32 table;
48  u8 gateway[16];
49  u32 priority;
50  struct rta_cacheinfo cacheinfo;
51  f64 last_updated;
52} ns_route_t;
53
54typedef struct {
55  struct ifaddrmsg ifaddr;
56  u8 addr[16];
57  u8 local[16];
58  u8 label[IFNAMSIZ];
59  u8 broadcast[16];
60  u8 anycast[16];
61  struct ifa_cacheinfo cacheinfo;
62  f64 last_updated;
63} ns_addr_t;
64
65typedef struct {
66  struct ndmsg nd;
67  u8 dst[16];
68  u8 lladdr[IFHWADDRLEN];
69  u32 probes;
70  struct nda_cacheinfo cacheinfo;
71  f64 last_updated;
72} ns_neigh_t;
73
74typedef struct {
75  char name[RTNL_NETNS_NAMELEN + 1];
76  ns_link_t  *links;
77  ns_route_t *routes;
78  ns_addr_t  *addresses;
79  ns_neigh_t *neighbors;
80} netns_t;
81
82
83typedef enum {
84  NETNS_TYPE_LINK,
85  NETNS_TYPE_ROUTE,
86  NETNS_TYPE_ADDR,
87  NETNS_TYPE_NEIGH,
88} netns_type_t;
89
90//Flags used in notification functions call
91#define NETNS_F_ADD    0x01
92#define NETNS_F_DEL    0x02
93
94typedef struct {
95  void (*notify)(void *obj, netns_type_t type, u32 flags, uword opaque);
96  uword opaque;
97} netns_sub_t;
98
99/*
100 * Subscribe for events related to the given namespace.
101 * When another subscriber already uses the namespace,
102 * this call will not trigger updates for already
103 * existing routes (This is to protect against
104 * synch. Vs asynch. issues).
105 */
106u32 netns_open(char *name, netns_sub_t *sub);
107
108/*
109 * Retrieves the namespace structure associated with a
110 * given namespace handler.
111 */
112netns_t *netns_getns(u32 handle);
113
114/*
115 * Terminates a subscriber session.
116 */
117void netns_close(u32 handle);
118
119/*
120 * Calls the callback associated with the handle
121 * for all existing objects with the flags
122 * set to (del?NETNS_F_DEL:NETNS_F_ADD).
123 */
124void netns_callme(u32 handle, char del);
125
126/*
127 * netns struct format functions.
128 * Taking the struct as single argument.
129 */
130u8 *format_ns_neigh(u8 *s, va_list *args);
131u8 *format_ns_addr(u8 *s, va_list *args);
132u8 *format_ns_route(u8 *s, va_list *args);
133u8 *format_ns_link(u8 *s, va_list *args);
134
135u8 *format_ns_object(u8 *s, va_list *args);
136u8 *format_ns_flags(u8 *s, va_list *args);
137
138#endif
139