1/*	$NetBSD: pvctxctl.c,v 1.6 2009/04/19 08:53:37 lukem Exp $	*/
2
3/*
4 * Copyright (C) 1998
5 *	Sony Computer Science Laboratory Inc.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * ALTQ Id: pvctxctl.c,v 0.4 1999/05/19 11:31:11 kjc Exp
29 */
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <unistd.h>
34#include <string.h>
35#include <ctype.h>
36#include <err.h>
37
38#include <sys/socket.h>
39#include <sys/ioctl.h>
40#include <net/if.h>
41
42#include <net/if_atm.h>
43
44static int str2vc(char *str, int *vpi, int *vci);
45__dead static void usage(void);
46
47static void
48usage(void)
49{
50	fprintf(stderr, "usage: pvctxctl interface [vpi:]vci\n");
51	fprintf(stderr, "          [-p pcr] [-b pcr_in_bps] [-j [vpi:]vci\n");
52	fprintf(stderr, "          [-n] \n");
53	exit(1);
54}
55
56int
57main(int argc, char **argv)
58{
59	struct pvctxreq pvcreq;
60	int s, ch;
61	long bandwidth;
62	char *if_name, *cp;
63	int vpi = 0;
64	int vci = 0;
65	int joint_vpi = 0;
66	int joint_vci = 0;
67	int pcr = 0;
68	int llcsnap = ATM_PH_LLCSNAP;
69	int getinfo = 1;
70	int subinterface = 0;
71	int verbose = 1;
72
73	if (argc < 2)
74		usage();
75
76	if_name = argv[1];
77	if (argc > 2 && isdigit((unsigned char)argv[2][0]))
78		str2vc(argv[2], &vpi, &vci);
79
80	optind = 3;
81	while ((ch = getopt(argc, argv, "p:b:j:snv")) != -1) {
82		switch (ch) {
83		case 'p':
84			pcr = strtol(optarg, NULL, 0);
85			getinfo = 0;
86			break;
87		case 'b':
88			cp = NULL;
89			bandwidth = strtol(optarg, &cp, 0);
90			if (cp != NULL) {
91				if (*cp == 'K' || *cp == 'k')
92					bandwidth *= 1000;
93				if (*cp == 'M' || *cp == 'm')
94					bandwidth *= 1000000;
95			}
96			pcr = bandwidth / 8 / 48;
97			getinfo = 0;
98			break;
99		case 'j':
100			str2vc(optarg, &joint_vpi, &joint_vci);
101			break;
102		case 'n':
103			llcsnap = 0;
104			break;
105		case 'v':
106			verbose = 1;
107			break;
108		default:
109			usage();
110		}
111	}
112
113	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
114		err(1, "can't open socket");
115
116	pvcreq.pvc_ifname[IFNAMSIZ-1] = '\0';
117	strncpy(pvcreq.pvc_ifname, if_name, IFNAMSIZ-1);
118
119	if (strncmp(if_name, "pvc", 3) == 0)
120		/* pvc subinterface */
121		subinterface = 1;
122
123	ATM_PH_FLAGS(&pvcreq.pvc_aph) = ATM_PH_AAL5 | llcsnap;
124	ATM_PH_VPI(&pvcreq.pvc_aph) = vpi;
125	ATM_PH_SETVCI(&pvcreq.pvc_aph, vci);
126
127	ATM_PH_FLAGS(&pvcreq.pvc_joint) = 0;
128	ATM_PH_VPI(&pvcreq.pvc_joint) = joint_vpi;
129	ATM_PH_SETVCI(&pvcreq.pvc_joint, joint_vci);
130
131	pvcreq.pvc_pcr = pcr;
132
133	if (getinfo) {
134		if (ioctl(s, SIOCGPVCTX, &pvcreq) < 0)
135			err(1, "SIOCSPVCTX");
136	}
137	else {
138		if (verbose) {
139		    printf("setting pvc tx: interface:%s vc:%d:%d ph=0x%x\n",
140			   if_name, vpi, vci, ATM_PH_FLAGS(&pvcreq.pvc_aph));
141		    printf("  joint:%d:%d, setting pcr:%d\n",
142			   joint_vpi, joint_vci, pcr);
143	        }
144
145		if (ioctl(s, SIOCSPVCTX, &pvcreq) < 0)
146				err(1, "SIOCSPVCTX");
147	}
148
149	pcr = pvcreq.pvc_pcr;
150
151	/*
152	 * print info
153	 */
154	printf("  %s", if_name);
155	if (subinterface)
156		printf(" (bound to %s)", pvcreq.pvc_ifname);
157	printf(": vci:[%d:%d] (",
158	       ATM_PH_VPI(&pvcreq.pvc_aph), ATM_PH_VCI(&pvcreq.pvc_aph));
159	if (ATM_PH_FLAGS(&pvcreq.pvc_aph) & ATM_PH_AAL5)
160		printf("AAL5");
161	if (ATM_PH_FLAGS(&pvcreq.pvc_aph) & ATM_PH_LLCSNAP)
162		printf("/LLCSNAP");
163	printf(") ");
164	if (pcr < 0)
165		printf("(invalid)\n");
166	else if (pcr == 0)
167		printf("pcr:%d(full speed)\n", pcr);
168	else if (pcr < 1000)
169		printf("pcr:%d(%dbps)\n", pcr, pcr * 48 * 8);
170	else if (pcr < 1000000)
171		printf("pcr:%d(%dKbps)\n", pcr, pcr * 48 * 8 / 1000);
172	else
173		printf("pcr:%d(%dMbps)\n", pcr, pcr * 48 * 8 / 1000000);
174
175	close(s);
176
177	if (getinfo && pcr < 0) {
178		fprintf(stderr, "can't get pvc info for vci:%d\n", vci);
179		fprintf(stderr, "to setup a vci, use -p or -b option\n");
180	}
181
182	return (0);
183}
184
185static int
186str2vc(char *str, int *vpip, int *vcip)
187{
188	char *c;
189
190	if ((c = strchr(str, ':')) != NULL) {
191		*c = '\0';
192		*vpip = strtol(str, NULL, 0);
193		str = c + 1;
194	}
195	else
196		*vpip = 0;
197
198	*vcip = strtol(str, NULL, 0);
199	return (0);
200}
201
202