vnic_vic.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright 2010 Cisco Systems, Inc. All rights reserved.
  3. *
  4. * This program is free software; you may redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; version 2 of the License.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  9. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  10. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  11. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  12. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  13. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  14. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  15. * SOFTWARE.
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/types.h>
  21. #include <linux/slab.h>
  22. #include "vnic_vic.h"
  23. struct vic_provinfo *vic_provinfo_alloc(gfp_t flags, u8 *oui, u8 type)
  24. {
  25. struct vic_provinfo *vp;
  26. if (!oui)
  27. return NULL;
  28. vp = kzalloc(VIC_PROVINFO_MAX_DATA, flags);
  29. if (!vp)
  30. return NULL;
  31. memcpy(vp->oui, oui, sizeof(vp->oui));
  32. vp->type = type;
  33. vp->length = htonl(sizeof(vp->num_tlvs));
  34. return vp;
  35. }
  36. void vic_provinfo_free(struct vic_provinfo *vp)
  37. {
  38. kfree(vp);
  39. }
  40. int vic_provinfo_add_tlv(struct vic_provinfo *vp, u16 type, u16 length,
  41. void *value)
  42. {
  43. struct vic_provinfo_tlv *tlv;
  44. if (!vp || !value)
  45. return -EINVAL;
  46. if (ntohl(vp->length) + offsetof(struct vic_provinfo_tlv, value) +
  47. length > VIC_PROVINFO_MAX_TLV_DATA)
  48. return -ENOMEM;
  49. tlv = (struct vic_provinfo_tlv *)((u8 *)vp->tlv +
  50. ntohl(vp->length) - sizeof(vp->num_tlvs));
  51. tlv->type = htons(type);
  52. tlv->length = htons(length);
  53. memcpy(tlv->value, value, length);
  54. vp->num_tlvs = htonl(ntohl(vp->num_tlvs) + 1);
  55. vp->length = htonl(ntohl(vp->length) +
  56. offsetof(struct vic_provinfo_tlv, value) + length);
  57. return 0;
  58. }
  59. size_t vic_provinfo_size(struct vic_provinfo *vp)
  60. {
  61. return vp ? ntohl(vp->length) + sizeof(*vp) - sizeof(vp->num_tlvs) : 0;
  62. }