Exemple #1
0
int main(int argc, char **argv)
{
	int retval = 0;
	struct switch_dev *dev;
	struct switch_attr *a;
	struct switch_val val;
	int err;
	int i;

	int cmd = CMD_NONE;
	char *cdev = NULL;
	int cport = -1;
	int cvlan = -1;
	char *ckey = NULL;
	char *cvalue = NULL;

	if(argc < 4)
		print_usage();

	if(strcmp(argv[1], "dev"))
		print_usage();

	cdev = argv[2];

	for(i = 3; i < argc; i++)
	{
		char *arg = argv[i];
		if (cmd != CMD_NONE) {
			print_usage();
		} else if (!strcmp(arg, "port") && i+1 < argc) {
			cport = atoi(argv[++i]);
		} else if (!strcmp(arg, "vlan") && i+1 < argc) {
			cvlan = atoi(argv[++i]);
		} else if (!strcmp(arg, "help")) {
			cmd = CMD_HELP;
		} else if (!strcmp(arg, "set") && i+1 < argc) {
			cmd = CMD_SET;
			ckey = argv[++i];
			if (i+1 < argc)
				cvalue = argv[++i];
		} else if (!strcmp(arg, "get") && i+1 < argc) {
			cmd = CMD_GET;
			ckey = argv[++i];
		} else if (!strcmp(arg, "load") && i+1 < argc) {
			if ((cport >= 0) || (cvlan >= 0))
				print_usage();
			cmd = CMD_LOAD;
			ckey = argv[++i];
		} else if (!strcmp(arg, "show")) {
			cmd = CMD_SHOW;
		} else {
			print_usage();
		}
	}

	if (cmd == CMD_NONE)
		print_usage();
	if (cport > -1 && cvlan > -1)
		print_usage();

	dev = swlib_connect(cdev);
	if (!dev) {
		fprintf(stderr, "Failed to connect to the switch\n");
		return 1;
	}

	swlib_scan(dev);

	if (cmd == CMD_GET || cmd == CMD_SET) {
		if(cport > -1)
			a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_PORT, ckey);
		else if(cvlan > -1)
			a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, ckey);
		else
			a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_GLOBAL, ckey);

		if(!a)
		{
			fprintf(stderr, "Unknown attribute \"%s\"\n", ckey);
			goto out;
		}
	}

	switch(cmd)
	{
	case CMD_SET:
		if ((a->type != SWITCH_TYPE_NOVAL) &&
				(cvalue == NULL))
			print_usage();

		if(cvlan > -1)
			cport = cvlan;

		if(swlib_set_attr_string(dev, a, cport, cvalue) < 0)
		{
			fprintf(stderr, "failed\n");
			retval = -1;
			goto out;
		}
		break;
	case CMD_GET:
		if(cvlan > -1)
			val.port_vlan = cvlan;
		if(cport > -1)
			val.port_vlan = cport;
		if(swlib_get_attr(dev, a, &val) < 0)
		{
			fprintf(stderr, "failed\n");
			retval = -1;
			goto out;
		}
		print_attr_val(a, &val);
		putchar('\n');
		break;
	case CMD_LOAD:
		swconfig_load_uci(dev, ckey);
		break;
	case CMD_HELP:
		list_attributes(dev);
		break;
	case CMD_SHOW:
		if (cport >= 0 || cvlan >= 0) {
			if (cport >= 0)
				show_port(dev, cport);
			else
				show_vlan(dev, cvlan, false);
		} else {
			show_global(dev);
			for (i=0; i < dev->ports; i++)
				show_port(dev, i);
			for (i=0; i < dev->vlans; i++)
				show_vlan(dev, i, true);
		}
		break;
	}

out:
	swlib_free_all(dev);
	return 0;
}
int
main( int argc, char *argv[] ) {
  command_options options;
  bool ret = parse_arguments( argc, argv, &options );
  if ( !ret ) {
    usage();
    exit( EXIT_FAILURE );
  }

  init_log( basename( argv[ 0 ] ), LOG_OUTPUT_STDOUT );
  init_vxlan_ctrl_client();

  uint8_t status = SUCCEEDED;
  ret = false;
  switch ( options.type ) {
    case ADD_INSTANCE_REQUEST:
    {
      ret = add_instance( options.vni, options.ip_addr, options.port, options.aging_time, &status );
    }
    break;

    case SET_INSTANCE_REQUEST:
    {
      ret = set_instance( options.vni, options.set_bitmap, options.ip_addr, options.port,
                          options.aging_time, &status );
    }
    break;

    case INACTIVATE_INSTANCE_REQUEST:
    {
      ret = inactivate_instance( options.vni, &status );
    }
    break;

    case ACTIVATE_INSTANCE_REQUEST:
    {
      ret = activate_instance( options.vni, &status );
    }
    break;

    case DEL_INSTANCE_REQUEST:
    {
      ret = delete_instance( options.vni, &status );
    }
    break;

    case SHOW_GLOBAL_REQUEST:
    {
      ret = show_global( options.set_bitmap, &status );
    }
    break;

    case LIST_INSTANCES_REQUEST:
    {
      ret = list_instances( options.vni, options.set_bitmap, &status );
    }
    break;

    case SHOW_FDB_REQUEST:
    {
      ret = show_fdb( options.vni, &status );
    }
    break;

    case ADD_FDB_ENTRY_REQUEST:
    {
      ret = add_fdb_entry( options.vni, options.eth_addr, options.ip_addr, options.aging_time, &status );
    }
    break;

    case DEL_FDB_ENTRY_REQUEST:
    {
      ret = delete_fdb_entry( options.vni, options.eth_addr, &status );
    }
    break;

    default:
    {
      printf( "Undefined command ( %#x ).\n", options.type );
      status = OTHER_ERROR;
      ret = false;
    }
    break;
  }

  if ( !ret ) {
    printf( "Failed to execute a command ( %u ).\n", status );
  }

  finalize_vxlan_ctrl_client();
  finalize_log();

  return status;
}