1 /*
2 * Copyright 2009-2017 Citrix Ltd and other contributors
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; version 2.1 only. with the special
7 * exception on linking described in file LICENSE.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 */
14
15 #include <stdlib.h>
16
17 #include <libxl.h>
18 #include <libxl_utils.h>
19 #include <libxlutil.h>
20
21 #include "xl.h"
22 #include "xl_utils.h"
23 #include "xl_parse.h"
24
25 void set_default_nic_values(libxl_device_nic *nic);
set_default_nic_values(libxl_device_nic * nic)26 void set_default_nic_values(libxl_device_nic *nic)
27 {
28
29 if (default_vifscript) {
30 free(nic->script);
31 nic->script = strdup(default_vifscript);
32 }
33
34 if (default_bridge) {
35 free(nic->bridge);
36 nic->bridge = strdup(default_bridge);
37 }
38
39 if (default_gatewaydev) {
40 free(nic->gatewaydev);
41 nic->gatewaydev = strdup(default_gatewaydev);
42 }
43
44 if (default_vifbackend) {
45 free(nic->backend_domname);
46 nic->backend_domname = strdup(default_vifbackend);
47 }
48 }
49
main_networkattach(int argc,char ** argv)50 int main_networkattach(int argc, char **argv)
51 {
52 uint32_t domid;
53 int opt;
54 libxl_device_nic nic;
55 XLU_Config *config = 0;
56
57 SWITCH_FOREACH_OPT(opt, "", NULL, "network-attach", 1) {
58 /* No options */
59 }
60
61 domid = find_domain(argv[optind]);
62
63 config= xlu_cfg_init(stderr, "command line");
64 if (!config) {
65 fprintf(stderr, "Failed to allocate for configuration\n");
66 return 1;
67 }
68
69 libxl_device_nic_init(&nic);
70 set_default_nic_values(&nic);
71
72 for (argv += optind+1, argc -= optind+1; argc > 0; ++argv, --argc) {
73 if (parse_nic_config(&nic, &config, *argv))
74 return 1;
75 }
76
77 if (dryrun_only) {
78 char *json = libxl_device_nic_to_json(ctx, &nic);
79 printf("vif: %s\n", json);
80 free(json);
81 libxl_device_nic_dispose(&nic);
82 if (ferror(stdout) || fflush(stdout)) { perror("stdout"); exit(-1); }
83 return 0;
84 }
85
86 if (libxl_device_nic_add(ctx, domid, &nic, 0)) {
87 fprintf(stderr, "libxl_device_nic_add failed.\n");
88 return 1;
89 }
90 libxl_device_nic_dispose(&nic);
91 xlu_cfg_destroy(config);
92 return 0;
93 }
94
main_networklist(int argc,char ** argv)95 int main_networklist(int argc, char **argv)
96 {
97 int opt;
98 libxl_device_nic *nics;
99 libxl_nicinfo nicinfo;
100 int nb, i;
101
102 SWITCH_FOREACH_OPT(opt, "", NULL, "network-list", 1) {
103 /* No options */
104 }
105
106 /* Idx BE MAC Hdl Sta evch txr/rxr BE-path */
107 printf("%-3s %-2s %-17s %-6s %-5s %-6s %5s/%-5s %-30s\n",
108 "Idx", "BE", "Mac Addr.", "handle", "state", "evt-ch", "tx-", "rx-ring-ref", "BE-path");
109 for (argv += optind, argc -= optind; argc > 0; --argc, ++argv) {
110 uint32_t domid = find_domain(*argv);
111 nics = libxl_device_nic_list(ctx, domid, &nb);
112 if (!nics) {
113 continue;
114 }
115 for (i = 0; i < nb; ++i) {
116 if (!libxl_device_nic_getinfo(ctx, domid, &nics[i], &nicinfo)) {
117 /* Idx BE */
118 printf("%-3d %-2d ", nicinfo.devid, nicinfo.backend_id);
119 /* MAC */
120 printf(LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nics[i].mac));
121 /* Hdl Sta evch txr/rxr BE-path */
122 printf("%6d %5d %6d %5d/%-11d %-30s\n",
123 nicinfo.devid, nicinfo.state, nicinfo.evtch,
124 nicinfo.rref_tx, nicinfo.rref_rx, nicinfo.backend);
125 libxl_nicinfo_dispose(&nicinfo);
126 }
127 }
128 libxl_device_nic_list_free(nics, nb);
129 }
130 return 0;
131 }
132
main_networkdetach(int argc,char ** argv)133 int main_networkdetach(int argc, char **argv)
134 {
135 uint32_t domid;
136 int opt;
137 libxl_device_nic nic;
138
139 SWITCH_FOREACH_OPT(opt, "", NULL, "network-detach", 2) {
140 /* No options */
141 }
142
143 domid = find_domain(argv[optind]);
144
145 if (!strchr(argv[optind+1], ':')) {
146 if (libxl_devid_to_device_nic(ctx, domid, atoi(argv[optind+1]), &nic)) {
147 fprintf(stderr, "Unknown device %s.\n", argv[optind+1]);
148 return 1;
149 }
150 } else {
151 if (libxl_mac_to_device_nic(ctx, domid, argv[optind+1], &nic)) {
152 fprintf(stderr, "Unknown device %s.\n", argv[optind+1]);
153 return 1;
154 }
155 }
156 if (libxl_device_nic_remove(ctx, domid, &nic, 0)) {
157 fprintf(stderr, "libxl_device_nic_del failed.\n");
158 return 1;
159 }
160 libxl_device_nic_dispose(&nic);
161 return 0;
162 }
163
164
165 /*
166 * Local variables:
167 * mode: C
168 * c-basic-offset: 4
169 * indent-tabs-mode: nil
170 * End:
171 */
172